diff options
author | FrancescoFioraEst <francesco.fiora@est.tech> | 2022-09-26 09:42:40 +0100 |
---|---|---|
committer | Francesco Fiora <francesco.fiora@est.tech> | 2022-09-26 13:46:21 +0000 |
commit | 15898295b842e097d640996543cf5de4fe3ea522 (patch) | |
tree | 7a546c68e546dc5371fe8f991e61690224e4ec4f /participant/participant-impl/participant-impl-http/src/test/java | |
parent | 9a005431305990f6accd743b80dcf6a9550af26a (diff) |
Refactor Mock Server in httpParticipant Unit Tests
Replace MockServerRest, that failing and causing all tests ignored.
Issue-ID: POLICY-4366
Change-Id: Icc86cf26f4cd329b0d24b3dd05b85143e6621517
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
Diffstat (limited to 'participant/participant-impl/participant-impl-http/src/test/java')
3 files changed, 154 insertions, 47 deletions
diff --git a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/utils/MockRestEndpoint.java b/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/utils/MockRestEndpoint.java new file mode 100644 index 000000000..037e62986 --- /dev/null +++ b/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/utils/MockRestEndpoint.java @@ -0,0 +1,66 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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 java.util.List; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Response; + +/** + * The Class MockRestEndpoint creates rest server endpoints for simulating Rest calls. + */ +@Path("/") +@Produces("application/json") +public class MockRestEndpoint { + + /** + * Get dummy endpoint. + * + * @param name the name + * @param version the version + * @return the response + */ + @Path("get") + @GET + public Response getMessages(@QueryParam("name") String name, @QueryParam("version") String version) { + String createRequest = "dummy body"; + return Response.status(200).entity(List.of(createRequest)).build(); + } + + /** + * Post dummy endpoint. + * + * @param name the name + * @param version the version + * @param jsonString the message + * @return the response + */ + @Path("post") + @POST + public Response policyMessage(@QueryParam("name") String name, @QueryParam("version") String version, + final String jsonString) { + return Response.status(200).build(); + } +} diff --git a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/utils/MockServerRest.java b/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/utils/MockServerRest.java new file mode 100644 index 000000000..1ac52124a --- /dev/null +++ b/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/utils/MockServerRest.java @@ -0,0 +1,65 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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 org.onap.policy.common.endpoints.http.server.HttpServletServer; +import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance; +import org.onap.policy.common.gson.GsonMessageBodyHandler; +import org.onap.policy.common.utils.network.NetworkUtil; + +/** + * The Class MockServerRest that manages test servers for REST requests for the test. + */ +public class MockServerRest implements AutoCloseable { + private static final String HOST = "localhost"; + private HttpServletServer restServer; + private int restServerPort = 0; + + /** + * Instantiates a new REST simulator. + */ + public MockServerRest(int restServerPort) { + this.restServerPort = restServerPort; + restServer = HttpServletServerFactoryInstance.getServerFactory().build("MockRestEndpoint", false, HOST, + restServerPort, "/", false, false); + restServer.addServletClass(null, MockRestEndpoint.class.getName()); + restServer.setSerializationProvider(GsonMessageBodyHandler.class.getName()); + restServer.start(); + } + + /** + * Validate the Rest server. + * @throws InterruptedException if is not alive + */ + public void validate() throws InterruptedException { + if (!NetworkUtil.isTcpPortOpen(HOST, restServerPort, 50, 200L)) { + throw new IllegalStateException("port " + restServerPort + " is still not in use"); + } + } + + @Override + public void close() throws Exception { + if (restServer != null) { + restServer.stop(); + restServer = null; + } + } +} diff --git a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/webclient/AcHttpClientTest.java b/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/webclient/AcHttpClientTest.java index e0a04a12a..3ddd7b17c 100644 --- a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/webclient/AcHttpClientTest.java +++ b/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/webclient/AcHttpClientTest.java @@ -22,26 +22,20 @@ package org.onap.policy.clamp.acm.participant.http.webclient; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.mockserver.model.HttpRequest.request; -import static org.mockserver.model.HttpResponse.response; import java.io.IOException; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import javax.ws.rs.core.MediaType; import org.apache.commons.lang3.tuple.Pair; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockserver.integration.ClientAndServer; -import org.mockserver.model.Parameter; import org.onap.policy.clamp.acm.participant.http.main.models.ConfigRequest; -import org.onap.policy.clamp.acm.participant.http.main.models.ConfigurationEntity; import org.onap.policy.clamp.acm.participant.http.main.webclient.AcHttpClient; import org.onap.policy.clamp.acm.participant.http.utils.CommonTestData; +import org.onap.policy.clamp.acm.participant.http.utils.MockServerRest; import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.springframework.test.context.junit.jupiter.SpringExtension; @@ -55,75 +49,57 @@ class AcHttpClientTest { private final String testMockUrl = "http://localhost"; - private static ClientAndServer mockServer; + private static MockServerRest mockServer; /** * Set up Mock server. */ @BeforeAll - static void setUpMockServer() throws IOException { + static void setUpMockServer() throws IOException, InterruptedException { mockServerPort = NetworkUtil.allocPort(); - mockServer = ClientAndServer.startClientAndServer(mockServerPort); + mockServer = new MockServerRest(mockServerPort); + mockServer.validate(); commonTestData = new CommonTestData(); - List<Parameter> queryParams = new ArrayList<>(); - commonTestData.getQueryParams().forEach((k, v) -> queryParams.add(new Parameter(k, v))); - - mockServer.when(request().withMethod("GET").withPath("/get") - .withHeader("Content-type", MediaType.APPLICATION_JSON) - .withHeader("Accept", MediaType.APPLICATION_JSON).withQueryStringParameters(queryParams)) - .respond(response().withBody("dummy body").withStatusCode(200) - .withHeader("Content-Type", MediaType.APPLICATION_JSON)); - - mockServer.when(request().withMethod("POST").withPath("/post") - .withHeader("Content-type", MediaType.APPLICATION_JSON) - .withHeader("Accept", MediaType.APPLICATION_JSON).withQueryStringParameters(queryParams) - .withBody("Test body")) - .respond(response().withStatusCode(200)); } @AfterAll - public static void stopServer() { - mockServer.stop(); + public static void stopServer() throws Exception { + mockServer.close(); mockServer = null; } - @Test void test_validRequest() { - //Add valid rest requests POST, GET - ConfigurationEntity configurationEntity = commonTestData.getConfigurationEntity(); + // Add valid rest requests POST, GET + var configurationEntity = commonTestData.getConfigurationEntity(); Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap = new HashMap<>(); - Map<String, String> headers = commonTestData.getHeaders(); - ConfigRequest configRequest = new ConfigRequest(testMockUrl + ":" + mockServerPort, headers, - List.of(configurationEntity), 10); + var headers = commonTestData.getHeaders(); + var configRequest = + new ConfigRequest(testMockUrl + ":" + mockServerPort, headers, List.of(configurationEntity), 10); - AcHttpClient client = new AcHttpClient(configRequest, responseMap); + var client = new AcHttpClient(configRequest, responseMap); assertDoesNotThrow(client::run); - assertThat(responseMap).hasSize(2).containsKey(commonTestData - .restParamsWithGet().getRestRequestId()); + assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId()); - Pair<Integer, String> restResponseMap = responseMap.get(commonTestData - .restParamsWithGet().getRestRequestId()); + var restResponseMap = responseMap.get(commonTestData.restParamsWithGet().getRestRequestId()); assertThat(restResponseMap.getKey()).isEqualTo(200); } @Test void test_invalidRequest() { - //Add rest requests Invalid POST, Valid GET - ConfigurationEntity configurationEntity = commonTestData.getInvalidConfigurationEntity(); + // Add rest requests Invalid POST, Valid GET + var configurationEntity = commonTestData.getInvalidConfigurationEntity(); Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap = new HashMap<>(); - Map<String, String> headers = commonTestData.getHeaders(); - ConfigRequest configRequest = new ConfigRequest(testMockUrl + ":" + mockServerPort, headers, - List.of(configurationEntity), 10); + var headers = commonTestData.getHeaders(); + var configRequest = + new ConfigRequest(testMockUrl + ":" + mockServerPort, headers, List.of(configurationEntity), 10); - AcHttpClient client = new AcHttpClient(configRequest, responseMap); + var client = new AcHttpClient(configRequest, responseMap); assertDoesNotThrow(client::run); - assertThat(responseMap).hasSize(2).containsKey(commonTestData - .restParamsWithGet().getRestRequestId()); - Pair<Integer, String> response = responseMap - .get(commonTestData.restParamsWithInvalidPost().getRestRequestId()); + assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId()); + var response = responseMap.get(commonTestData.restParamsWithInvalidPost().getRestRequestId()); assertThat(response.getKey()).isEqualTo(404); } } |