+++ /dev/null
-/*-
- * ============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.a1pms.utils;
-
-import jakarta.ws.rs.DELETE;
-import jakarta.ws.rs.GET;
-import jakarta.ws.rs.PUT;
-import jakarta.ws.rs.Path;
-import jakarta.ws.rs.PathParam;
-import jakarta.ws.rs.Produces;
-import jakarta.ws.rs.core.Response;
-
-/**
- * The Class MockRestEndpoint creates rest server endpoints for simulating Rest calls.
- */
-@Path("/")
-@Produces("application/json")
-public class MockRestEndpoint {
-
- /**
- * Get dummy health endpoint.
- *
- * @return the response
- */
- @Path("/healthy")
- @GET
- public Response getApplicationHealthy() {
- return Response.status(200).entity("{}").build();
- }
-
- /**
- * Get dummy health endpoint.
- *
- * @return the response
- */
- @Path("/unhealthy")
- @GET
- public Response getApplicationUnHealthy() {
- return Response.status(500).entity("{}").build();
- }
-
- @Path("/services/success")
- @PUT
- public Response createServiceSuccess() {
- return Response.status(200).entity("{}").build();
- }
-
- @Path("/services/failure")
- @PUT
- public Response createServiceFailure() {
- return Response.status(500).entity("{}").build();
- }
-
- @Path("/service/success/{clientId}")
- @DELETE
- public Response deleteServiceSuccess(@PathParam("clientId") String clientId) {
- return Response.status(204).entity("{}").build();
- }
-
- @Path("/service/failure/{clientId}")
- @DELETE
- public Response deleteServiceFailure(@PathParam("clientId") String clientId) {
- return Response.status(500).entity("{}").build();
- }
-}
+++ /dev/null
-/*-
- * ============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 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, 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;
- }
- }
-}
import static org.mockito.Mockito.when;
import java.io.IOException;
+import okhttp3.mockwebserver.Dispatcher;
+import okhttp3.mockwebserver.MockResponse;
+import okhttp3.mockwebserver.MockWebServer;
+import okhttp3.mockwebserver.RecordedRequest;
+import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.onap.policy.clamp.acm.participant.a1pms.parameters.A1PmsParameters;
import org.onap.policy.clamp.acm.participant.a1pms.utils.CommonTestData;
-import org.onap.policy.clamp.acm.participant.a1pms.utils.MockServerRest;
-import org.onap.policy.common.utils.network.NetworkUtil;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
private static int mockServerPort;
- private static MockServerRest mockServer;
+ private static MockWebServer mockServer;
private static CommonTestData commonTestData;
* Set up Mock server.
*/
@BeforeAll
- static void setUpMockServer() throws IOException, InterruptedException {
- mockServerPort = NetworkUtil.allocPort();
- mockServer = new MockServerRest(mockServerPort);
- mockServer.validate();
+ static void setUpMockServer() throws IOException {
+ mockServerPort = 42545;
+ mockServer = new MockWebServer();
+ mockServer.start(mockServerPort);
+ mockServer.setDispatcher(new Dispatcher() {
+ @NotNull
+ @Override
+ public MockResponse dispatch(@NotNull RecordedRequest request) {
+ String path = request.getPath();
+ assert path != null;
+ if (path.equals("/healthy") && "GET".equals(request.getMethod())) {
+ return new MockResponse()
+ .setResponseCode(200);
+ }
+ if (path.equals("/unhealthy") && "GET".equals(request.getMethod())) {
+ return new MockResponse()
+ .setResponseCode(500);
+ }
+ if (path.equals("/services/success") && "PUT".equals(request.getMethod())) {
+ return new MockResponse()
+ .setResponseCode(200)
+ .addHeader("Content-Type", "application/json");
+ }
+ if (path.equals("/services/failure") && "PUT".equals(request.getMethod())) {
+ return new MockResponse()
+ .setResponseCode(500)
+ .addHeader("Content-Type", "application/json");
+ }
+ if (path.startsWith("/service/success/") && "DELETE".equals(request.getMethod())) {
+ return new MockResponse()
+ .setResponseCode(204)
+ .addHeader("Content-Type", "application/json");
+ }
+
+ if (path.startsWith("/service/failure/") && "DELETE".equals(request.getMethod())) {
+ return new MockResponse()
+ .setResponseCode(500);
+ }
+ return new MockResponse().setResponseCode(404);
+ }
+ });
commonTestData = new CommonTestData();
}
+++ /dev/null
-/*-
- * ============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 jakarta.ws.rs.GET;
-import jakarta.ws.rs.POST;
-import jakarta.ws.rs.Path;
-import jakarta.ws.rs.Produces;
-import jakarta.ws.rs.QueryParam;
-import jakarta.ws.rs.core.Response;
-import java.util.List;
-
-/**
- * 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();
- }
-}
+++ /dev/null
-/*-
- * ============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.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, 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;
- }
- }
-}
import java.io.IOException;
import java.util.List;
+import okhttp3.mockwebserver.Dispatcher;
+import okhttp3.mockwebserver.MockResponse;
+import okhttp3.mockwebserver.MockWebServer;
+import okhttp3.mockwebserver.RecordedRequest;
+import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.onap.policy.clamp.acm.participant.http.main.models.ConfigRequest;
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.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
private static final String MOCK_URL = "http://localhost";
private static final String WRONG_URL = "http://wrong-url";
- private static MockServerRest mockServer;
+ private static MockWebServer mockServer;
/**
* Set up Mock server.
*/
@BeforeAll
- static void setUpMockServer() throws IOException, InterruptedException {
- mockServerPort = NetworkUtil.allocPort();
- mockServer = new MockServerRest(mockServerPort);
- mockServer.validate();
+ static void setUpMockServer() throws IOException {
+ mockServerPort = 42545;
+ mockServer = new MockWebServer();
+ mockServer.start(mockServerPort);
+ mockServer.setDispatcher(new Dispatcher() {
+ @NotNull
+ @Override
+ public MockResponse dispatch(@NotNull RecordedRequest request) {
+ String path = request.getPath();
+ assert path != null;
+ if (path.startsWith("/get") && "GET".equals(request.getMethod())) {
+ return new MockResponse()
+ .setResponseCode(200);
+ }
+ if (path.equals("/post") && "POST".equals(request.getMethod())) {
+ return new MockResponse()
+ .setResponseCode(200)
+ .addHeader("Content-Type", "application/json");
+ }
+ return new MockResponse().setResponseCode(404);
+ }
+ });
commonTestData = new CommonTestData();
}
@AfterAll
static void stopServer() throws Exception {
- mockServer.close();
- mockServer = null;
+ mockServer.shutdown();
}
@Test
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.io.IOException;
+import okhttp3.mockwebserver.Dispatcher;
+import okhttp3.mockwebserver.MockResponse;
+import okhttp3.mockwebserver.MockWebServer;
+import okhttp3.mockwebserver.RecordedRequest;
+import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.onap.policy.clamp.acm.participant.policy.concepts.DeploymentSubGroup;
import org.onap.policy.clamp.acm.participant.policy.main.parameters.ParticipantPolicyParameters;
-import org.onap.policy.clamp.acm.participant.policy.main.utils.MockServer;
import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException;
import org.onap.policy.common.parameters.rest.RestClientParameters;
-import org.onap.policy.common.utils.network.NetworkUtil;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
/**
*/
class HttpClientTest {
- private static int mockServerPort;
-
private static PolicyApiHttpClient apiHttpClient;
private static PolicyPapHttpClient papHttpClient;
- private static MockServer mockServer;
-
/**
* Set up Mock server.
*/
@BeforeAll
- static void setUpMockServer() throws IOException, InterruptedException {
- mockServerPort = NetworkUtil.allocPort();
- mockServer = new MockServer(mockServerPort);
- mockServer.validate();
+ static void setUpMockServer() throws IOException {
+ // Setup mock web server
+ int mockServerPort = 42545;
+ MockWebServer mockServer = new MockWebServer();
+ mockServer.start(mockServerPort);
+ mockServer.setDispatcher(new Dispatcher() {
+ @NotNull
+ @Override
+ public MockResponse dispatch(@NotNull RecordedRequest request) {
+ String path = request.getPath();
+ assert path != null;
+ if (path.equals("/policy/api/v1/policytypes") && "POST".equals(request.getMethod())) {
+ return new MockResponse()
+ .setResponseCode(200)
+ .addHeader("Content-Type", "application/json");
+ }
+ if (path.equals("/policy/api/v1/policies") && "POST".equals(request.getMethod())) {
+ return new MockResponse()
+ .setResponseCode(200)
+ .addHeader("Content-Type", "application/json");
+ }
+ if (path.matches("^/policy/api/v1/policytypes/[^/]+/versions/[^/]+$")
+ && "DELETE".equals(request.getMethod())) {
+ return new MockResponse().setResponseCode(200);
+ }
+ if (path.matches("^/policy/api/v1/policies/[^/]+/versions/[^/]+$")
+ && "DELETE".equals(request.getMethod())) {
+ return new MockResponse().setResponseCode(200);
+ }
+ if (path.equals("/policy/pap/v1/pdps/deployments/batch")
+ && "POST".equals(request.getMethod())) {
+ return new MockResponse()
+ .setResponseCode(200)
+ .addHeader("Content-Type", "application/json");
+ }
+ return new MockResponse().setResponseCode(404);
+ }
+ });
+
// Setup mock api and pap client
ParticipantPolicyParameters params = new ParticipantPolicyParameters();
RestClientParameters restClientParameters = getMockClientParameters(mockServerPort);
+++ /dev/null
-/*-
- * ============LICENSE_START=======================================================
- * Copyright (C) 2022-2025 OpenInfra Foundation Europe. All rights reserved.
- * ================================================================================
- * 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 jakarta.ws.rs.DELETE;
-import jakarta.ws.rs.POST;
-import jakarta.ws.rs.Path;
-import jakarta.ws.rs.Produces;
-import jakarta.ws.rs.core.Response;
-import org.onap.policy.clamp.acm.participant.policy.concepts.DeploymentGroups;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
-import org.springframework.web.bind.annotation.RequestBody;
-
-/**
- * Mock rest endpoints for api and pap servers.
- */
-@Path("/")
-@Produces("application/json")
-public class MockRestEndpoint {
-
- /**
- * Dummy endpoint for create policy types.
- *
- * @param body tosca service template
- * @return the response
- */
- @Path("policy/api/v1/policytypes")
- @POST
- public Response createPolicyType(@RequestBody ToscaServiceTemplate body) {
- return Response.status(200).build();
- }
-
- /**
- * Dummy endpoint for create policies.
- *
- * @param body tosca service template
- * @return the response
- */
- @Path("policy/api/v1/policies")
- @POST
- public Response createPolicy(@RequestBody ToscaServiceTemplate body) {
- return Response.status(200).build();
- }
-
- /**
- * Dummy endpoint for delete policy types.
- *
- * @return the response
- */
- @Path("policy/api/v1/policytypes/{policyTypeId}/versions/{versionId}")
- @DELETE
- public Response deletePolicyType() {
- return Response.status(200).build();
- }
-
- /**
- * Dummy endpoint for delete policy.
- *
- * @return the response
- */
- @Path("policy/api/v1/policies/{policyId}/versions/{versionId}")
- @DELETE
- public Response deletePolicy() {
- return Response.status(200).build();
- }
-
- /**
- * Dummy endpoint for deploy/undeploy policy in pap.
- *
- * @param body pdp deployment group
- * @return the response
- */
- @Path("policy/pap/v1/pdps/deployments/batch")
- @POST
- public Response handlePolicyDeployOrUndeploy(@RequestBody DeploymentGroups body) {
- return Response.status(200).build();
- }
-
-}
+++ /dev/null
-/*-
- * ============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.policy.main.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;
-
-/**
- * Mock REST server for pap and api tests.
- */
-public class MockServer implements AutoCloseable {
- private static final String HOST = "localhost";
- private HttpServletServer restServer;
- private int restServerPort = 0;
-
- /**
- * Instantiates a new REST simulator.
- */
- public MockServer(int restServerPort) {
- this.restServerPort = restServerPort;
- restServer = HttpServletServerFactoryInstance.getServerFactory().build("MockRestEndpoint", false, HOST,
- restServerPort, false, "/", 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;
- }
- }
-
-
-}
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>com.squareup.okhttp3</groupId>
+ <artifactId>mockwebserver</artifactId>
+ <version>5.3.2</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>