Tolerate actuator endpoints with the same id

Closes gh-39249
This commit is contained in:
Moritz Halbritter
2024-01-19 15:31:27 +01:00
parent c3b710a1f0
commit ca799f7b21
5 changed files with 142 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,16 +27,20 @@ import org.springframework.boot.configurationsample.endpoint.CustomPropertiesEnd
import org.springframework.boot.configurationsample.endpoint.DisabledEndpoint;
import org.springframework.boot.configurationsample.endpoint.EnabledEndpoint;
import org.springframework.boot.configurationsample.endpoint.SimpleEndpoint;
import org.springframework.boot.configurationsample.endpoint.SimpleEndpoint2;
import org.springframework.boot.configurationsample.endpoint.SimpleEndpoint3;
import org.springframework.boot.configurationsample.endpoint.SpecificEndpoint;
import org.springframework.boot.configurationsample.endpoint.incremental.IncrementalEndpoint;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* Metadata generation tests for Actuator endpoints.
*
* @author Stephane Nicoll
* @author Scott Frederick
* @author Moritz Halbritter
*/
class EndpointMetadataGenerationTests extends AbstractMetadataGenerationTests {
@@ -148,6 +152,24 @@ class EndpointMetadataGenerationTests extends AbstractMetadataGenerationTests {
assertThat(metadata.getItems()).hasSize(3);
}
@Test
void shouldTolerateEndpointWithSameId() {
ConfigurationMetadata metadata = compile(SimpleEndpoint.class, SimpleEndpoint2.class);
assertThat(metadata).has(Metadata.withGroup("management.endpoint.simple").fromSource(SimpleEndpoint.class));
assertThat(metadata).has(enabledFlag("simple", "simple", true));
assertThat(metadata).has(cacheTtl("simple"));
assertThat(metadata.getItems()).hasSize(3);
}
@Test
void shouldFailIfEndpointWithSameIdButWithConflictingEnabledByDefaultSetting() {
assertThatRuntimeException().isThrownBy(() -> compile(SimpleEndpoint.class, SimpleEndpoint3.class))
.havingRootCause()
.isInstanceOf(IllegalStateException.class)
.withMessage(
"Existing property 'management.endpoint.simple.enabled' from type org.springframework.boot.configurationsample.endpoint.SimpleEndpoint has a conflicting value. Existing value: true, new value from type org.springframework.boot.configurationsample.endpoint.SimpleEndpoint3: false");
}
private Metadata.MetadataItemCondition enabledFlag(String endpointId, String endpointSuffix, Boolean defaultValue) {
return Metadata.withEnabledFlag("management.endpoint." + endpointSuffix + ".enabled")
.withDefaultValue(defaultValue)

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.endpoint;
import org.springframework.boot.configurationsample.Endpoint;
import org.springframework.boot.configurationsample.ReadOperation;
/**
* A simple endpoint with no default override, with the same id as {@link SimpleEndpoint}.
*
* @author Moritz Halbritter
*/
@Endpoint(id = "simple")
public class SimpleEndpoint2 {
@ReadOperation
public String invoke() {
return "test";
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.endpoint;
import org.springframework.boot.configurationsample.Endpoint;
import org.springframework.boot.configurationsample.ReadOperation;
/**
* A simple endpoint with no default override, with the same id as {@link SimpleEndpoint},
* but not enabled by default.
*
* @author Moritz Halbritter
*/
@Endpoint(id = "simple", enableByDefault = false)
public class SimpleEndpoint3 {
@ReadOperation
public String invoke() {
return "test";
}
}