diff options
author | 2025-02-19 15:15:47 +0000 | |
---|---|---|
committer | 2025-02-19 15:25:08 +0000 | |
commit | 6a67b7355c2c39a72cc0cce0c995620f75a81c62 (patch) | |
tree | a135df858b3fb88e26dd50f29643d0eff5f8e2a8 | |
parent | 5b42ebe43ce4201ebea05a157e8ca71d0c5ece51 (diff) |
Remove org.glassfish from Actuator test in all ACM participants
Issue-ID: POLICY-5290
Change-Id: I7c4640d6d3fe18c5ed64eaaf3e4ab4be44e7190f
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
15 files changed, 196 insertions, 1095 deletions
diff --git a/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/rest/ActuatorControllerTest.java b/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/rest/ActuatorControllerTest.java index b8f160057..89ce8c078 100644 --- a/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/rest/ActuatorControllerTest.java +++ b/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/rest/ActuatorControllerTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2022-2023 Nordix Foundation. + * Copyright (C) 2022-2023,2025 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,72 +20,56 @@ package org.onap.policy.clamp.acm.participant.a1pms.rest; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.http.MediaType.TEXT_PLAIN; -import jakarta.ws.rs.core.Response; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.onap.policy.clamp.acm.participant.a1pms.utils.CommonActuatorController; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.autoconfigure.actuate.observability.AutoConfigureObservability; +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.reactive.function.client.ExchangeFilterFunctions; -@AutoConfigureObservability(tracing = false) -@ExtendWith(SpringExtension.class) +@AutoConfigureObservability +@AutoConfigureWebTestClient @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @ActiveProfiles("test") -class ActuatorControllerTest extends CommonActuatorController { +class ActuatorControllerTest { - private static final String HEALTH_ENDPOINT = "health"; - private static final String METRICS_ENDPOINT = "metrics"; - private static final String PROMETHEUS_ENDPOINT = "prometheus"; + @Autowired + private WebTestClient webClient; - @LocalServerPort - private int randomServerPort; + @Value("${spring.security.user.name}") + private String username; + @Value("${spring.security.user.password}") + private String password; @BeforeEach - public void setUpPort() { - super.setHttpPrefix(randomServerPort); - } - - @Test - void testGetHealth_Unauthorized() { - assertUnauthorizedActGet(HEALTH_ENDPOINT); - } - - @Test - void testGetMetrics_Unauthorized() { - assertUnauthorizedActGet(METRICS_ENDPOINT); - } - - @Test - void testGetPrometheus_Unauthorized() { - assertUnauthorizedActGet(PROMETHEUS_ENDPOINT); + void beforeEach() { + var filter = ExchangeFilterFunctions.basicAuthentication(username, password); + webClient = webClient.mutate().filter(filter).build(); } @Test void testGetHealth() { - var invocationBuilder = super.sendActRequest(HEALTH_ENDPOINT); - var rawresp = invocationBuilder.buildGet().invoke(); - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + webClient.get().uri("/health").accept(APPLICATION_JSON) + .exchange().expectStatus().isOk(); } @Test void testGetMetrics() { - var invocationBuilder = super.sendActRequest(METRICS_ENDPOINT); - var rawresp = invocationBuilder.buildGet().invoke(); - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + webClient.get().uri("/metrics").accept(APPLICATION_JSON) + .exchange().expectStatus().isOk(); } @Test - void testGePrometheus() { - var invocationBuilder = super.sendActRequest(PROMETHEUS_ENDPOINT); - var rawresp = invocationBuilder.buildGet().invoke(); - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + void testGetPrometheus() { + webClient.get().uri("/prometheus").accept(TEXT_PLAIN) + .exchange().expectStatus().isOk(); } - } diff --git a/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/CommonActuatorController.java b/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/CommonActuatorController.java deleted file mode 100644 index c4a93472f..000000000 --- a/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/CommonActuatorController.java +++ /dev/null @@ -1,107 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2022-2023 Nordix Foundation. - * ================================================================================ - * 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 - * - * http://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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.clamp.acm.participant.a1pms.utils; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import jakarta.ws.rs.client.Client; -import jakarta.ws.rs.client.ClientBuilder; -import jakarta.ws.rs.client.Invocation; -import jakarta.ws.rs.client.WebTarget; -import jakarta.ws.rs.core.MediaType; -import jakarta.ws.rs.core.Response; -import org.glassfish.jersey.client.ClientProperties; -import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; -import org.onap.policy.common.gson.GsonMessageBodyHandler; -import org.onap.policy.common.utils.network.NetworkUtil; - -/** - * Class to perform Rest unit tests. - */ -public class CommonActuatorController { - - public static final String SELF = NetworkUtil.getHostname(); - public static final String CONTEXT_PATH = "onap/policy/clamp/acm/a1pmsparticipant/"; - - private static String httpPrefix; - - /** - * Sends a request to an actuator endpoint. - * - * @param endpoint the target endpoint - * @return a request builder - */ - protected Invocation.Builder sendActRequest(final String endpoint) { - return sendFqeRequest(httpPrefix + CONTEXT_PATH + endpoint, true); - } - - /** - * Sends a request to an actuator endpoint, without any authorization header. - * - * @param endpoint the target endpoint - * @return a request builder - */ - protected Invocation.Builder sendNoAuthActRequest(final String endpoint) { - return sendFqeRequest(httpPrefix + CONTEXT_PATH + endpoint, false); - } - - /** - * Sends a request to a fully qualified endpoint. - * - * @param fullyQualifiedEndpoint the fully qualified target endpoint - * @param includeAuth if authorization header should be included - * @return a request builder - */ - protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth) { - final Client client = ClientBuilder.newBuilder().build(); - - client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); - client.register(GsonMessageBodyHandler.class); - - if (includeAuth) { - client.register(HttpAuthenticationFeature.basic("participantUser", "zb!XztG34")); - } - - final WebTarget webTarget = client.target(fullyQualifiedEndpoint); - - return webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN); - } - - /** - * Assert that GET call to actuator endpoint is Unauthorized. - * - * @param endPoint the endpoint - */ - protected void assertUnauthorizedActGet(final String endPoint) { - Response rawresp = sendNoAuthActRequest(endPoint).buildGet().invoke(); - assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus()); - } - - /** - * Set Up httpPrefix. - * - * @param port the port - */ - protected void setHttpPrefix(int port) { - httpPrefix = "http://" + SELF + ":" + port + "/"; - } - -} diff --git a/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/rest/ActuatorControllerTest.java b/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/rest/ActuatorControllerTest.java index 453ccc916..e3c665ace 100644 --- a/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/rest/ActuatorControllerTest.java +++ b/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/rest/ActuatorControllerTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2022-2024 Nordix Foundation. + * Copyright (C) 2022-2025 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,98 +20,62 @@ package org.onap.policy.clamp.acm.element.rest; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.http.MediaType.TEXT_PLAIN; -import jakarta.ws.rs.core.Response; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.onap.policy.clamp.acm.element.utils.CommonActuatorController; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.autoconfigure.actuate.observability.AutoConfigureObservability; +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.reactive.function.client.ExchangeFilterFunctions; -@AutoConfigureObservability(tracing = false) -@ExtendWith(SpringExtension.class) +@AutoConfigureObservability +@AutoConfigureWebTestClient @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @ActiveProfiles({ "test", "default" }) -class ActuatorControllerTest extends CommonActuatorController { +class ActuatorControllerTest { - private static final String HEALTH_ENDPOINT = "onap/policy/clamp/acelement/v2/health"; - private static final String METRICS_ENDPOINT = "onap/policy/clamp/acelement/v2/metrics"; - private static final String PROMETHEUS_ENDPOINT = "onap/policy/clamp/acelement/v2/prometheus"; - private static final String SWAGGER_ENDPOINT = "onap/policy/clamp/acelement/v2/v3/api-docs"; + @Autowired + private WebTestClient webClient; - private static final String WRONG_ENDPOINT = "onap/policy/clamp/acelement/v2/wrong"; - - @LocalServerPort - private int randomServerPort; + @Value("${spring.security.user.name}") + private String username; + @Value("${spring.security.user.password}") + private String password; @BeforeEach - public void setUpPort() { - super.setHttpPrefix(randomServerPort); - } - - @Test - void testGetHealth_Unauthorized() { - assertUnauthorizedActGet(HEALTH_ENDPOINT); - } - - @Test - void testGetMetrics_Unauthorized() { - assertUnauthorizedActGet(METRICS_ENDPOINT); - } - - @Test - void testGetPrometheus_Unauthorized() { - assertUnauthorizedActGet(PROMETHEUS_ENDPOINT); - } - - @Test - void testGetSwagger_Unauthorized() { - assertUnauthorizedActGet(SWAGGER_ENDPOINT); + void beforeEach() { + var filter = ExchangeFilterFunctions.basicAuthentication(username, password); + webClient = webClient.mutate().filter(filter).build(); } @Test void testGetHealth() { - var invocationBuilder = super.sendActRequest(HEALTH_ENDPOINT); - try (var rawresp = invocationBuilder.buildGet().invoke()) { - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - } + webClient.get().uri("/health").accept(APPLICATION_JSON) + .exchange().expectStatus().isOk(); } @Test void testGetMetrics() { - var invocationBuilder = super.sendActRequest(METRICS_ENDPOINT); - try (var rawresp = invocationBuilder.buildGet().invoke()) { - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - } + webClient.get().uri("/metrics").accept(APPLICATION_JSON) + .exchange().expectStatus().isOk(); } @Test void testGetPrometheus() { - var invocationBuilder = super.sendActRequest(PROMETHEUS_ENDPOINT); - try (var rawresp = invocationBuilder.buildGet().invoke()) { - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - } + webClient.get().uri("/prometheus").accept(TEXT_PLAIN) + .exchange().expectStatus().isOk(); } @Test void testGetSwagger() { - var invocationBuilder = super.sendActRequest(SWAGGER_ENDPOINT); - try (var rawresp = invocationBuilder.buildGet().invoke()) { - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - } - } - - @Test - void testWrongEndPoint() { - var invocationBuilder = super.sendActRequest(WRONG_ENDPOINT); - try (var rawresp = invocationBuilder.buildGet().invoke()) { - assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus()); - } + webClient.get().uri("/v3/api-docs").accept(APPLICATION_JSON) + .exchange().expectStatus().isOk(); } } diff --git a/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/utils/CommonActuatorController.java b/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/utils/CommonActuatorController.java deleted file mode 100644 index a1d22038e..000000000 --- a/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/utils/CommonActuatorController.java +++ /dev/null @@ -1,107 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2022-2023 Nordix Foundation. - * ================================================================================ - * 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 - * - * http://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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.clamp.acm.element.utils; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import jakarta.ws.rs.client.Client; -import jakarta.ws.rs.client.ClientBuilder; -import jakarta.ws.rs.client.Invocation; -import jakarta.ws.rs.client.WebTarget; -import jakarta.ws.rs.core.MediaType; -import jakarta.ws.rs.core.Response; -import org.glassfish.jersey.client.ClientProperties; -import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; -import org.onap.policy.common.gson.GsonMessageBodyHandler; -import org.onap.policy.common.utils.network.NetworkUtil; - -/** - * Class to perform Rest unit tests. - * - */ -public class CommonActuatorController { - - public static final String SELF = NetworkUtil.getHostname(); - - private static String httpPrefix; - - /** - * Sends a request to an actuator endpoint. - * - * @param endpoint the target endpoint - * @return a request builder - */ - protected Invocation.Builder sendActRequest(final String endpoint) { - return sendFqeRequest(httpPrefix + endpoint, true); - } - - /** - * Sends a request to an actuator endpoint, without any authorization header. - * - * @param endpoint the target endpoint - * @return a request builder - */ - protected Invocation.Builder sendNoAuthActRequest(final String endpoint) { - return sendFqeRequest(httpPrefix + endpoint, false); - } - - /** - * Sends a request to a fully qualified endpoint. - * - * @param fullyQualifiedEndpoint the fully qualified target endpoint - * @param includeAuth if authorization header should be included - * @return a request builder - */ - protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth) { - final Client client = ClientBuilder.newBuilder().build(); - - client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); - client.register(GsonMessageBodyHandler.class); - - if (includeAuth) { - client.register(HttpAuthenticationFeature.basic("acmUser", "zb!XztG34")); - } - - final WebTarget webTarget = client.target(fullyQualifiedEndpoint); - - return webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN); - } - - /** - * Assert that GET call to actuator endpoint is Unauthorized. - * - * @param endPoint the endpoint - */ - protected void assertUnauthorizedActGet(final String endPoint) { - Response rawresp = sendNoAuthActRequest(endPoint).buildGet().invoke(); - assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus()); - } - - /** - * Set Up httpPrefix. - * - * @param port the port - */ - protected void setHttpPrefix(int port) { - httpPrefix = "http://" + SELF + ":" + port + "/"; - } - -} diff --git a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/rest/ActuatorControllerTest.java b/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/rest/ActuatorControllerTest.java index 149d8d910..130f65694 100644 --- a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/rest/ActuatorControllerTest.java +++ b/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/rest/ActuatorControllerTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021-2024 Nordix Foundation. + * Copyright (C) 2021-2025 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,75 +20,56 @@ package org.onap.policy.clamp.acm.participant.http.rest; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.http.MediaType.TEXT_PLAIN; -import jakarta.ws.rs.core.Response; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.onap.policy.clamp.acm.participant.http.utils.CommonActuatorController; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.autoconfigure.actuate.observability.AutoConfigureObservability; +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.reactive.function.client.ExchangeFilterFunctions; -@AutoConfigureObservability(tracing = false) -@ExtendWith(SpringExtension.class) +@AutoConfigureObservability +@AutoConfigureWebTestClient @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @ActiveProfiles("test") -class ActuatorControllerTest extends CommonActuatorController { +class ActuatorControllerTest { - private static final String HEALTH_ENDPOINT = "health"; - private static final String METRICS_ENDPOINT = "metrics"; - private static final String PROMETHEUS_ENDPOINT = "prometheus"; + @Autowired + private WebTestClient webClient; - @LocalServerPort - private int randomServerPort; + @Value("${spring.security.user.name}") + private String username; + @Value("${spring.security.user.password}") + private String password; @BeforeEach - public void setUpPort() { - super.setHttpPrefix(randomServerPort); - } - - @Test - void testGetHealth_Unauthorized() { - assertUnauthorizedActGet(HEALTH_ENDPOINT); - } - - @Test - void testGetMetrics_Unauthorized() { - assertUnauthorizedActGet(METRICS_ENDPOINT); - } - - @Test - void testGetPrometheus_Unauthorized() { - assertUnauthorizedActGet(PROMETHEUS_ENDPOINT); + void beforeEach() { + var filter = ExchangeFilterFunctions.basicAuthentication(username, password); + webClient = webClient.mutate().filter(filter).build(); } @Test void testGetHealth() { - var invocationBuilder = super.sendActRequest(HEALTH_ENDPOINT); - try (var rawresp = invocationBuilder.buildGet().invoke()) { - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - } + webClient.get().uri("/health").accept(APPLICATION_JSON) + .exchange().expectStatus().isOk(); } @Test void testGetMetrics() { - var invocationBuilder = super.sendActRequest(METRICS_ENDPOINT); - try (var rawresp = invocationBuilder.buildGet().invoke()) { - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - } + webClient.get().uri("/metrics").accept(APPLICATION_JSON) + .exchange().expectStatus().isOk(); } @Test - void testGePrometheus() { - var invocationBuilder = super.sendActRequest(PROMETHEUS_ENDPOINT); - try (var rawresp = invocationBuilder.buildGet().invoke()) { - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - } + void testGetPrometheus() { + webClient.get().uri("/prometheus").accept(TEXT_PLAIN) + .exchange().expectStatus().isOk(); } - } diff --git a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/utils/CommonActuatorController.java b/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/utils/CommonActuatorController.java deleted file mode 100644 index fa9068a21..000000000 --- a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/utils/CommonActuatorController.java +++ /dev/null @@ -1,108 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021-2023 Nordix Foundation. - * ================================================================================ - * 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 - * - * http://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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.clamp.acm.participant.http.utils; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import jakarta.ws.rs.client.Client; -import jakarta.ws.rs.client.ClientBuilder; -import jakarta.ws.rs.client.Invocation; -import jakarta.ws.rs.client.WebTarget; -import jakarta.ws.rs.core.MediaType; -import jakarta.ws.rs.core.Response; -import org.glassfish.jersey.client.ClientProperties; -import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; -import org.onap.policy.common.gson.GsonMessageBodyHandler; -import org.onap.policy.common.utils.network.NetworkUtil; - -/** - * Class to perform Rest unit tests. - * - */ -public class CommonActuatorController { - - public static final String SELF = NetworkUtil.getHostname(); - public static final String CONTEXT_PATH = "onap/policy/clamp/acm/httpparticipant/"; - - private static String httpPrefix; - - /** - * Sends a request to an actuator endpoint. - * - * @param endpoint the target endpoint - * @return a request builder - */ - protected Invocation.Builder sendActRequest(final String endpoint) { - return sendFqeRequest(httpPrefix + CONTEXT_PATH + endpoint, true); - } - - /** - * Sends a request to an actuator endpoint, without any authorization header. - * - * @param endpoint the target endpoint - * @return a request builder - */ - protected Invocation.Builder sendNoAuthActRequest(final String endpoint) { - return sendFqeRequest(httpPrefix + CONTEXT_PATH + endpoint, false); - } - - /** - * Sends a request to a fully qualified endpoint. - * - * @param fullyQualifiedEndpoint the fully qualified target endpoint - * @param includeAuth if authorization header should be included - * @return a request builder - */ - protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth) { - final Client client = ClientBuilder.newBuilder().build(); - - client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); - client.register(GsonMessageBodyHandler.class); - - if (includeAuth) { - client.register(HttpAuthenticationFeature.basic("participantUser", "zb!XztG34")); - } - - final WebTarget webTarget = client.target(fullyQualifiedEndpoint); - - return webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN); - } - - /** - * Assert that GET call to actuator endpoint is Unauthorized. - * - * @param endPoint the endpoint - */ - protected void assertUnauthorizedActGet(final String endPoint) { - Response rawresp = sendNoAuthActRequest(endPoint).buildGet().invoke(); - assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus()); - } - - /** - * Set Up httpPrefix. - * - * @param port the port - */ - protected void setHttpPrefix(int port) { - httpPrefix = "http://" + SELF + ":" + port + "/"; - } - -} diff --git a/participant/participant-impl/participant-impl-kserve/src/test/java/org/onap/policy/clamp/acm/participant/kserve/rest/ActuatorControllerTest.java b/participant/participant-impl/participant-impl-kserve/src/test/java/org/onap/policy/clamp/acm/participant/kserve/rest/ActuatorControllerTest.java index dbdefd25a..732dd7bbd 100644 --- a/participant/participant-impl/participant-impl-kserve/src/test/java/org/onap/policy/clamp/acm/participant/kserve/rest/ActuatorControllerTest.java +++ b/participant/participant-impl/participant-impl-kserve/src/test/java/org/onap/policy/clamp/acm/participant/kserve/rest/ActuatorControllerTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021-2024 Nordix Foundation. + * Copyright (C) 2021-2025 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,79 +20,61 @@ package org.onap.policy.clamp.acm.participant.kserve.rest; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.http.MediaType.TEXT_PLAIN; import io.kubernetes.client.openapi.ApiClient; -import jakarta.ws.rs.core.Response; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.onap.policy.clamp.acm.participant.kserve.utils.CommonActuatorController; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.autoconfigure.actuate.observability.AutoConfigureObservability; +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.reactive.function.client.ExchangeFilterFunctions; -@AutoConfigureObservability(tracing = false) -@ExtendWith(SpringExtension.class) +@AutoConfigureObservability +@AutoConfigureWebTestClient @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @ActiveProfiles("test") -class ActuatorControllerTest extends CommonActuatorController { +class ActuatorControllerTest { - private static final String HEALTH_ENDPOINT = "health"; - private static final String METRICS_ENDPOINT = "metrics"; - private static final String PROMETHEUS_ENDPOINT = "prometheus"; + @Autowired + private WebTestClient webClient; @MockBean - ApiClient apiClient; + private ApiClient apiClient; - @LocalServerPort - private int randomServerPort; + @Value("${spring.security.user.name}") + private String username; + @Value("${spring.security.user.password}") + private String password; @BeforeEach - public void setUpPort() { - super.setHttpPrefix(randomServerPort); - } - - @Test - void testGetHealth_Unauthorized() { - assertUnauthorizedActGet(HEALTH_ENDPOINT); - } - - @Test - void testGetMetrics_Unauthorized() { - assertUnauthorizedActGet(METRICS_ENDPOINT); - } - - @Test - void testGetPrometheus_Unauthorized() { - assertUnauthorizedActGet(PROMETHEUS_ENDPOINT); + void beforeEach() { + var filter = ExchangeFilterFunctions.basicAuthentication(username, password); + webClient = webClient.mutate().filter(filter).build(); } @Test void testGetHealth() { - var invocationBuilder = super.sendActRequest(HEALTH_ENDPOINT); - try (var rawresp = invocationBuilder.buildGet().invoke()) { - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - } + webClient.get().uri("/health").accept(APPLICATION_JSON) + .exchange().expectStatus().isOk(); } @Test void testGetMetrics() { - var invocationBuilder = super.sendActRequest(METRICS_ENDPOINT); - try (var rawresp = invocationBuilder.buildGet().invoke()) { - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - } + webClient.get().uri("/metrics").accept(APPLICATION_JSON) + .exchange().expectStatus().isOk(); } @Test - void testGePrometheus() { - var invocationBuilder = super.sendActRequest(PROMETHEUS_ENDPOINT); - try (var rawresp = invocationBuilder.buildGet().invoke()) { - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - } + void testGetPrometheus() { + webClient.get().uri("/prometheus").accept(TEXT_PLAIN) + .exchange().expectStatus().isOk(); } } diff --git a/participant/participant-impl/participant-impl-kserve/src/test/java/org/onap/policy/clamp/acm/participant/kserve/utils/CommonActuatorController.java b/participant/participant-impl/participant-impl-kserve/src/test/java/org/onap/policy/clamp/acm/participant/kserve/utils/CommonActuatorController.java deleted file mode 100644 index ab2d28ae1..000000000 --- a/participant/participant-impl/participant-impl-kserve/src/test/java/org/onap/policy/clamp/acm/participant/kserve/utils/CommonActuatorController.java +++ /dev/null @@ -1,107 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2023 Nordix Foundation. - * ================================================================================ - * 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 - * - * http://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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.clamp.acm.participant.kserve.utils; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import jakarta.ws.rs.client.Client; -import jakarta.ws.rs.client.ClientBuilder; -import jakarta.ws.rs.client.Invocation; -import jakarta.ws.rs.client.WebTarget; -import jakarta.ws.rs.core.MediaType; -import jakarta.ws.rs.core.Response; -import org.glassfish.jersey.client.ClientProperties; -import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; -import org.onap.policy.common.gson.GsonMessageBodyHandler; -import org.onap.policy.common.utils.network.NetworkUtil; - -/** - * Class to perform Rest unit tests. - */ -public class CommonActuatorController { - - public static final String SELF = NetworkUtil.getHostname(); - public static final String CONTEXT_PATH = "onap/policy/clamp/acm/kserveparticipant/"; - - private static String httpPrefix; - - /** - * Sends a request to an actuator endpoint. - * - * @param endpoint the target endpoint - * @return a request builder - */ - protected Invocation.Builder sendActRequest(final String endpoint) { - return sendFqeRequest(httpPrefix + CONTEXT_PATH + endpoint, true); - } - - /** - * Sends a request to an actuator endpoint, without any authorization header. - * - * @param endpoint the target endpoint - * @return a request builder - */ - protected Invocation.Builder sendNoAuthActRequest(final String endpoint) { - return sendFqeRequest(httpPrefix + CONTEXT_PATH + endpoint, false); - } - - /** - * Sends a request to a fully qualified endpoint. - * - * @param fullyQualifiedEndpoint the fully qualified target endpoint - * @param includeAuth if authorization header should be included - * @return a request builder - */ - protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth) { - final Client client = ClientBuilder.newBuilder().build(); - - client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); - client.register(GsonMessageBodyHandler.class); - - if (includeAuth) { - client.register(HttpAuthenticationFeature.basic("participantUser", "zb!XztG34")); - } - - final WebTarget webTarget = client.target(fullyQualifiedEndpoint); - - return webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN); - } - - /** - * Assert that GET call to actuator endpoint is Unauthorized. - * - * @param endPoint the endpoint - */ - protected void assertUnauthorizedActGet(final String endPoint) { - Response rawresp = sendNoAuthActRequest(endPoint).buildGet().invoke(); - assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus()); - } - - /** - * Set Up httpPrefix. - * - * @param port the port - */ - protected void setHttpPrefix(int port) { - httpPrefix = "http://" + SELF + ":" + port + "/"; - } - -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/rest/ActuatorControllerTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/rest/ActuatorControllerTest.java index b5b18c5cb..0dce19e22 100644 --- a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/rest/ActuatorControllerTest.java +++ b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/rest/ActuatorControllerTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021-2023 Nordix Foundation. + * Copyright (C) 2021-2023,2025 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,84 +20,62 @@ package org.onap.policy.clamp.acm.participant.kubernetes.rest; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.http.MediaType.TEXT_PLAIN; -import jakarta.ws.rs.core.Response; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.onap.policy.clamp.acm.participant.kubernetes.utils.CommonActuatorController; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.autoconfigure.actuate.observability.AutoConfigureObservability; +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.reactive.function.client.ExchangeFilterFunctions; -@AutoConfigureObservability(tracing = false) -@ExtendWith(SpringExtension.class) +@AutoConfigureObservability +@AutoConfigureWebTestClient @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @ActiveProfiles("test") -class ActuatorControllerTest extends CommonActuatorController { +class ActuatorControllerTest { - private static final String HEALTH_ENDPOINT = "health"; - private static final String METRICS_ENDPOINT = "metrics"; - private static final String PROMETHEUS_ENDPOINT = "prometheus"; - private static final String SWAGGER_ENDPOINT = "v3/api-docs"; + @Autowired + private WebTestClient webClient; - @LocalServerPort - private int randomServerPort; + @Value("${spring.security.user.name}") + private String username; + @Value("${spring.security.user.password}") + private String password; @BeforeEach - public void setUpPort() { - super.setHttpPrefix(randomServerPort); - } - - @Test - void testGetHealth_Unauthorized() { - assertUnauthorizedActGet(HEALTH_ENDPOINT); - } - - @Test - void testGetMetrics_Unauthorized() { - assertUnauthorizedActGet(METRICS_ENDPOINT); - } - - @Test - void testGetPrometheus_Unauthorized() { - assertUnauthorizedActGet(PROMETHEUS_ENDPOINT); - } - - @Test - void testGetSwagger_Unauthorized() { - assertUnauthorizedActGet(SWAGGER_ENDPOINT); + void beforeEach() { + var filter = ExchangeFilterFunctions.basicAuthentication(username, password); + webClient = webClient.mutate().filter(filter).build(); } @Test void testGetHealth() { - var invocationBuilder = super.sendActRequest(HEALTH_ENDPOINT); - var rawresp = invocationBuilder.buildGet().invoke(); - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + webClient.get().uri("/health").accept(APPLICATION_JSON) + .exchange().expectStatus().isOk(); } @Test void testGetMetrics() { - var invocationBuilder = super.sendActRequest(METRICS_ENDPOINT); - var rawresp = invocationBuilder.buildGet().invoke(); - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + webClient.get().uri("/metrics").accept(APPLICATION_JSON) + .exchange().expectStatus().isOk(); } @Test void testGetPrometheus() { - var invocationBuilder = super.sendActRequest(PROMETHEUS_ENDPOINT); - var rawresp = invocationBuilder.buildGet().invoke(); - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + webClient.get().uri("/prometheus").accept(TEXT_PLAIN) + .exchange().expectStatus().isOk(); } @Test void testGetSwagger() { - var invocationBuilder = super.sendActRequest(SWAGGER_ENDPOINT); - var rawresp = invocationBuilder.buildGet().invoke(); - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + webClient.get().uri("/v3/api-docs").accept(APPLICATION_JSON) + .exchange().expectStatus().isOk(); } } diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/utils/CommonActuatorController.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/utils/CommonActuatorController.java deleted file mode 100644 index 4487dbe07..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/utils/CommonActuatorController.java +++ /dev/null @@ -1,106 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021-2023 Nordix Foundation. - * ================================================================================ - * 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 - * - * http://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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.clamp.acm.participant.kubernetes.utils; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import jakarta.ws.rs.client.ClientBuilder; -import jakarta.ws.rs.client.Invocation; -import jakarta.ws.rs.core.MediaType; -import jakarta.ws.rs.core.Response; -import org.glassfish.jersey.client.ClientProperties; -import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; -import org.onap.policy.common.gson.GsonMessageBodyHandler; -import org.onap.policy.common.utils.network.NetworkUtil; - -/** - * Class to perform Rest unit tests. - * - */ -public class CommonActuatorController { - - public static final String SELF = NetworkUtil.getHostname(); - public static final String CONTEXT_PATH = "onap/policy/clamp/acm/k8sparticipant/"; - - private static String httpPrefix; - - /** - * Sends a request to an actuator endpoint. - * - * @param endpoint the target endpoint - * @return a request builder - */ - protected Invocation.Builder sendActRequest(final String endpoint) { - return sendFqeRequest(httpPrefix + CONTEXT_PATH + endpoint, true); - } - - /** - * Sends a request to an actuator endpoint, without any authorization header. - * - * @param endpoint the target endpoint - * @return a request builder - */ - protected Invocation.Builder sendNoAuthActRequest(final String endpoint) { - return sendFqeRequest(httpPrefix + CONTEXT_PATH + endpoint, false); - } - - /** - * Sends a request to a fully qualified endpoint. - * - * @param fullyQualifiedEndpoint the fully qualified target endpoint - * @param includeAuth if authorization header should be included - * @return a request builder - */ - protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth) { - final var client = ClientBuilder.newBuilder().build(); - - client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); - client.register(GsonMessageBodyHandler.class); - - if (includeAuth) { - client.register(HttpAuthenticationFeature.basic("participantUser", "zb!XztG34")); - } - - final var webTarget = client.target(fullyQualifiedEndpoint); - - return webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN); - } - - /** - * Assert that GET call to actuator endpoint is Unauthorized. - * - * @param endPoint the endpoint - */ - protected void assertUnauthorizedActGet(final String endPoint) { - var rawresp = sendNoAuthActRequest(endPoint).buildGet().invoke(); - assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus()); - } - - /** - * Set Up httpPrefix. - * - * @param port the port - */ - protected void setHttpPrefix(int port) { - httpPrefix = "http://" + SELF + ":" + port + "/"; - } - -} diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/rest/ActuatorControllerTest.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/rest/ActuatorControllerTest.java index 3672e5903..91627ed28 100644 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/rest/ActuatorControllerTest.java +++ b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/rest/ActuatorControllerTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021-2024 Nordix Foundation. + * Copyright (C) 2021-2025 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,74 +20,56 @@ package org.onap.policy.clamp.acm.participant.policy.main.rest; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.http.MediaType.TEXT_PLAIN; -import jakarta.ws.rs.core.Response; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.onap.policy.clamp.acm.participant.policy.main.utils.CommonActuatorController; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.autoconfigure.actuate.observability.AutoConfigureObservability; +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.reactive.function.client.ExchangeFilterFunctions; -@AutoConfigureObservability(tracing = false) -@ExtendWith(SpringExtension.class) +@AutoConfigureObservability +@AutoConfigureWebTestClient @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @ActiveProfiles("test") -class ActuatorControllerTest extends CommonActuatorController { +class ActuatorControllerTest { - private static final String HEALTH_ENDPOINT = "health"; - private static final String METRICS_ENDPOINT = "metrics"; - private static final String PROMETHEUS_ENDPOINT = "prometheus"; + @Autowired + private WebTestClient webClient; - @LocalServerPort - private int randomServerPort; + @Value("${spring.security.user.name}") + private String username; + @Value("${spring.security.user.password}") + private String password; @BeforeEach - public void setUpPort() { - super.setHttpPrefix(randomServerPort); - } - - @Test - void testGetHealth_Unauthorized() { - assertUnauthorizedActGet(HEALTH_ENDPOINT); - } - - @Test - void testGetMetrics_Unauthorized() { - assertUnauthorizedActGet(METRICS_ENDPOINT); - } - - @Test - void testGetPrometheus_Unauthorized() { - assertUnauthorizedActGet(PROMETHEUS_ENDPOINT); + void beforeEach() { + var filter = ExchangeFilterFunctions.basicAuthentication(username, password); + webClient = webClient.mutate().filter(filter).build(); } @Test void testGetHealth() { - var invocationBuilder = super.sendActRequest(HEALTH_ENDPOINT); - try (var rawresp = invocationBuilder.buildGet().invoke()) { - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - } + webClient.get().uri("/health").accept(APPLICATION_JSON) + .exchange().expectStatus().isOk(); } @Test void testGetMetrics() { - var invocationBuilder = super.sendActRequest(METRICS_ENDPOINT); - try (var rawresp = invocationBuilder.buildGet().invoke()) { - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - } + webClient.get().uri("/metrics").accept(APPLICATION_JSON) + .exchange().expectStatus().isOk(); } @Test - void testGePrometheus() { - var invocationBuilder = super.sendActRequest(PROMETHEUS_ENDPOINT); - try (var rawresp = invocationBuilder.buildGet().invoke()) { - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - } + void testGetPrometheus() { + webClient.get().uri("/prometheus").accept(TEXT_PLAIN) + .exchange().expectStatus().isOk(); } } diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/utils/CommonActuatorController.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/utils/CommonActuatorController.java deleted file mode 100644 index 2b77452a5..000000000 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/utils/CommonActuatorController.java +++ /dev/null @@ -1,108 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021-2023 Nordix Foundation. - * ================================================================================ - * 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 - * - * http://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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.clamp.acm.participant.policy.main.utils; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import jakarta.ws.rs.client.Client; -import jakarta.ws.rs.client.ClientBuilder; -import jakarta.ws.rs.client.Invocation; -import jakarta.ws.rs.client.WebTarget; -import jakarta.ws.rs.core.MediaType; -import jakarta.ws.rs.core.Response; -import org.glassfish.jersey.client.ClientProperties; -import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; -import org.onap.policy.common.gson.GsonMessageBodyHandler; -import org.onap.policy.common.utils.network.NetworkUtil; - -/** - * Class to perform Rest unit tests. - * - */ -public class CommonActuatorController { - - public static final String SELF = NetworkUtil.getHostname(); - public static final String CONTEXT_PATH = "onap/policy/clamp/acm/policyparticipant/"; - - private static String httpPrefix; - - /** - * Sends a request to an actuator endpoint. - * - * @param endpoint the target endpoint - * @return a request builder - */ - protected Invocation.Builder sendActRequest(final String endpoint) { - return sendFqeRequest(httpPrefix + CONTEXT_PATH + endpoint, true); - } - - /** - * Sends a request to an actuator endpoint, without any authorization header. - * - * @param endpoint the target endpoint - * @return a request builder - */ - protected Invocation.Builder sendNoAuthActRequest(final String endpoint) { - return sendFqeRequest(httpPrefix + CONTEXT_PATH + endpoint, false); - } - - /** - * Sends a request to a fully qualified endpoint. - * - * @param fullyQualifiedEndpoint the fully qualified target endpoint - * @param includeAuth if authorization header should be included - * @return a request builder - */ - protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth) { - final Client client = ClientBuilder.newBuilder().build(); - - client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); - client.register(GsonMessageBodyHandler.class); - - if (includeAuth) { - client.register(HttpAuthenticationFeature.basic("participantUser", "zb!XztG34")); - } - - final WebTarget webTarget = client.target(fullyQualifiedEndpoint); - - return webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN); - } - - /** - * Assert that GET call to actuator endpoint is Unauthorized. - * - * @param endPoint the endpoint - */ - protected void assertUnauthorizedActGet(final String endPoint) { - Response rawresp = sendNoAuthActRequest(endPoint).buildGet().invoke(); - assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus()); - } - - /** - * Set Up httpPrefix. - * - * @param port the port - */ - protected void setHttpPrefix(int port) { - httpPrefix = "http://" + SELF + ":" + port + "/"; - } - -} diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/rest/ActuatorControllerTest.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/rest/ActuatorControllerTest.java index ac24dea72..a3bf68230 100644 --- a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/rest/ActuatorControllerTest.java +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/rest/ActuatorControllerTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2023-2024 Nordix Foundation. + * Copyright (C) 2023-2025 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,75 +20,56 @@ package org.onap.policy.clamp.acm.participant.sim.rest; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.http.MediaType.TEXT_PLAIN; -import jakarta.ws.rs.core.Response; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.onap.policy.clamp.acm.participant.sim.utils.CommonActuatorController; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.autoconfigure.actuate.observability.AutoConfigureObservability; +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.reactive.function.client.ExchangeFilterFunctions; -@AutoConfigureObservability(tracing = false) -@ExtendWith(SpringExtension.class) +@AutoConfigureObservability +@AutoConfigureWebTestClient @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @ActiveProfiles("test") -class ActuatorControllerTest extends CommonActuatorController { +class ActuatorControllerTest { - private static final String HEALTH_ENDPOINT = "health"; - private static final String METRICS_ENDPOINT = "metrics"; - private static final String PROMETHEUS_ENDPOINT = "prometheus"; + @Autowired + private WebTestClient webClient; - @LocalServerPort - private int randomServerPort; + @Value("${spring.security.user.name}") + private String username; + @Value("${spring.security.user.password}") + private String password; @BeforeEach - public void setUpPort() { - super.setHttpPrefix(randomServerPort); - } - - @Test - void testGetHealth_Unauthorized() { - assertUnauthorizedActGet(HEALTH_ENDPOINT); - } - - @Test - void testGetMetrics_Unauthorized() { - assertUnauthorizedActGet(METRICS_ENDPOINT); - } - - @Test - void testGetPrometheus_Unauthorized() { - assertUnauthorizedActGet(PROMETHEUS_ENDPOINT); + void beforeEach() { + var filter = ExchangeFilterFunctions.basicAuthentication(username, password); + webClient = webClient.mutate().filter(filter).build(); } @Test void testGetHealth() { - var invocationBuilder = super.sendActRequest(HEALTH_ENDPOINT); - try (var rawresp = invocationBuilder.buildGet().invoke()) { - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - } + webClient.get().uri("/health").accept(APPLICATION_JSON) + .exchange().expectStatus().isOk(); } @Test void testGetMetrics() { - var invocationBuilder = super.sendActRequest(METRICS_ENDPOINT); - try (var rawresp = invocationBuilder.buildGet().invoke()) { - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - } + webClient.get().uri("/metrics").accept(APPLICATION_JSON) + .exchange().expectStatus().isOk(); } @Test - void testGePrometheus() { - var invocationBuilder = super.sendActRequest(PROMETHEUS_ENDPOINT); - try (var rawresp = invocationBuilder.buildGet().invoke()) { - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - } + void testGetPrometheus() { + webClient.get().uri("/prometheus").accept(TEXT_PLAIN) + .exchange().expectStatus().isOk(); } - } diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/utils/CommonActuatorController.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/utils/CommonActuatorController.java deleted file mode 100644 index 2cb013442..000000000 --- a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/utils/CommonActuatorController.java +++ /dev/null @@ -1,108 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2023 Nordix Foundation. - * ================================================================================ - * 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 - * - * http://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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.clamp.acm.participant.sim.utils; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import jakarta.ws.rs.client.Client; -import jakarta.ws.rs.client.ClientBuilder; -import jakarta.ws.rs.client.Invocation; -import jakarta.ws.rs.client.WebTarget; -import jakarta.ws.rs.core.MediaType; -import jakarta.ws.rs.core.Response; -import org.glassfish.jersey.client.ClientProperties; -import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; -import org.onap.policy.common.gson.GsonMessageBodyHandler; -import org.onap.policy.common.utils.network.NetworkUtil; - -/** - * Class to perform Rest unit tests. - * - */ -public class CommonActuatorController { - - public static final String SELF = NetworkUtil.getHostname(); - public static final String CONTEXT_PATH = "onap/policy/clamp/acm/simparticipant/"; - - private static String httpPrefix; - - /** - * Sends a request to an actuator endpoint. - * - * @param endpoint the target endpoint - * @return a request builder - */ - protected Invocation.Builder sendActRequest(final String endpoint) { - return sendFqeRequest(httpPrefix + CONTEXT_PATH + endpoint, true); - } - - /** - * Sends a request to an actuator endpoint, without any authorization header. - * - * @param endpoint the target endpoint - * @return a request builder - */ - protected Invocation.Builder sendNoAuthActRequest(final String endpoint) { - return sendFqeRequest(httpPrefix + CONTEXT_PATH + endpoint, false); - } - - /** - * Sends a request to a fully qualified endpoint. - * - * @param fullyQualifiedEndpoint the fully qualified target endpoint - * @param includeAuth if authorization header should be included - * @return a request builder - */ - protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth) { - final Client client = ClientBuilder.newBuilder().build(); - - client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); - client.register(GsonMessageBodyHandler.class); - - if (includeAuth) { - client.register(HttpAuthenticationFeature.basic("participantUser", "zb!XztG34")); - } - - final WebTarget webTarget = client.target(fullyQualifiedEndpoint); - - return webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN); - } - - /** - * Assert that GET call to actuator endpoint is Unauthorized. - * - * @param endPoint the endpoint - */ - protected void assertUnauthorizedActGet(final String endPoint) { - Response rawresp = sendNoAuthActRequest(endPoint).buildGet().invoke(); - assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus()); - } - - /** - * Set Up httpPrefix. - * - * @param port the port - */ - protected void setHttpPrefix(int port) { - httpPrefix = "http://" + SELF + ":" + port + "/"; - } - -} diff --git a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/main/rest/ActuatorControllerTest.java b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/main/rest/ActuatorControllerTest.java index 18911f461..4b3e8ed7a 100644 --- a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/main/rest/ActuatorControllerTest.java +++ b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/main/rest/ActuatorControllerTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021-2024 Nordix Foundation. + * Copyright (C) 2021-2025 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,12 +42,12 @@ import org.springframework.web.reactive.function.client.ExchangeFilterFunctions; class ActuatorControllerTest { @Autowired - WebTestClient webClient; + private WebTestClient webClient; @Value("${spring.security.user.name}") - String username; + private String username; @Value("${spring.security.user.password}") - String password; + private String password; @BeforeEach void beforeEach() { |