From bf6cc68f7f6c58f57ca8a384bbe71f4061a9285c Mon Sep 17 00:00:00 2001 From: "aravind.est" Date: Mon, 21 Nov 2022 15:26:58 +0000 Subject: Add A1 PMS participant Issue-ID: CCSDK-3816 Signed-off-by: aravind.est Change-Id: I00c04542f5ec4672b1c3a29ca32477bf5d2ca336 --- .../a1pms/handler/AcElementHandlerTest.java | 110 +++++++++++++++ .../a1pms/rest/ActuatorControllerTest.java | 92 +++++++++++++ .../a1pms/utils/CommonActuatorController.java | 107 +++++++++++++++ .../participant/a1pms/utils/CommonTestData.java | 78 +++++++++++ .../participant/a1pms/utils/MockRestEndpoint.java | 83 ++++++++++++ .../participant/a1pms/utils/MockServerRest.java | 65 +++++++++ .../acm/participant/a1pms/utils/ToscaUtils.java | 51 +++++++ .../a1pms/webclient/AcA1PmsClientTest.java | 148 +++++++++++++++++++++ .../src/test/resources/application-test.yaml | 24 ++++ 9 files changed, 758 insertions(+) create mode 100755 participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/handler/AcElementHandlerTest.java create mode 100755 participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/rest/ActuatorControllerTest.java create mode 100755 participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/CommonActuatorController.java create mode 100755 participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/CommonTestData.java create mode 100755 participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/MockRestEndpoint.java create mode 100755 participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/MockServerRest.java create mode 100755 participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/ToscaUtils.java create mode 100755 participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/webclient/AcA1PmsClientTest.java create mode 100755 participant/participant-impl/participant-impl-a1pms/src/test/resources/application-test.yaml (limited to 'participant/participant-impl/participant-impl-a1pms/src/test') diff --git a/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/handler/AcElementHandlerTest.java b/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/handler/AcElementHandlerTest.java new file mode 100755 index 000000000..690f5f2f4 --- /dev/null +++ b/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/handler/AcElementHandlerTest.java @@ -0,0 +1,110 @@ +/*- + * ============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.handler; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Map; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Spy; +import org.onap.policy.clamp.acm.participant.a1pms.exception.A1PolicyServiceException; +import org.onap.policy.clamp.acm.participant.a1pms.utils.CommonTestData; +import org.onap.policy.clamp.acm.participant.a1pms.utils.ToscaUtils; +import org.onap.policy.clamp.acm.participant.a1pms.webclient.AcA1PmsClient; +import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +class AcElementHandlerTest { + + private final AcA1PmsClient acA1PmsClient = mock(AcA1PmsClient.class); + + @InjectMocks + @Spy + private AutomationCompositionElementHandler automationCompositionElementHandler = + new AutomationCompositionElementHandler(acA1PmsClient); + + + private final CommonTestData commonTestData = new CommonTestData(); + + private static ToscaServiceTemplate serviceTemplate; + private static final String A1_AUTOMATION_COMPOSITION_ELEMENT = + "org.onap.domain.database.A1PMSAutomationCompositionElement"; + + @BeforeAll + static void init() { + serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca(); + } + + @BeforeEach + void startMocks() throws A1PolicyServiceException { + automationCompositionElementHandler.setIntermediaryApi(mock(ParticipantIntermediaryApi.class)); + when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.TRUE); + doNothing().when(acA1PmsClient).createService(any()); + } + + @Test + void test_automationCompositionElementStateChange() throws A1PolicyServiceException { + var automationCompositionId = commonTestData.getAutomationCompositionId(); + var element = commonTestData.getAutomationCompositionElement(); + var automationCompositionElementId = element.getId(); + + Map nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates(); + automationCompositionElementHandler.automationCompositionElementUpdate( + commonTestData.getAutomationCompositionId(), element, + nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT)); + + assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange( + automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE, + AutomationCompositionOrderedState.PASSIVE)); + + assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange( + automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE, + AutomationCompositionOrderedState.UNINITIALISED)); + + assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange( + automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE, + AutomationCompositionOrderedState.RUNNING)); + } + + @Test + void test_AutomationCompositionElementUpdate() { + AutomationCompositionElement element = commonTestData.getAutomationCompositionElement(); + + Map nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates(); + assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementUpdate( + commonTestData.getAutomationCompositionId(), element, + nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT))); + } +} 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 new file mode 100755 index 000000000..831dc5e77 --- /dev/null +++ b/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/rest/ActuatorControllerTest.java @@ -0,0 +1,92 @@ +/*- + * ============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.rest; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import javax.ws.rs.client.Invocation; +import javax.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.boot.test.autoconfigure.actuate.metrics.AutoConfigureMetrics; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@AutoConfigureMetrics +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@ActiveProfiles("test") +class ActuatorControllerTest extends CommonActuatorController { + + private static final String HEALTH_ENDPOINT = "health"; + private static final String METRICS_ENDPOINT = "metrics"; + private static final String PROMETHEUS_ENDPOINT = "prometheus"; + + @LocalServerPort + private int randomServerPort; + + @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 testGetHealth() { + Invocation.Builder invocationBuilder = super.sendActRequest(HEALTH_ENDPOINT); + Response rawresp = invocationBuilder.buildGet().invoke(); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + } + + @Test + void testGetMetrics() { + Invocation.Builder invocationBuilder = super.sendActRequest(METRICS_ENDPOINT); + Response rawresp = invocationBuilder.buildGet().invoke(); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + } + + @Test + void testGePrometheus() { + Invocation.Builder invocationBuilder = super.sendActRequest(PROMETHEUS_ENDPOINT); + Response rawresp = invocationBuilder.buildGet().invoke(); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + } + +} 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 new file mode 100755 index 000000000..fc00f66b8 --- /dev/null +++ b/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/CommonActuatorController.java @@ -0,0 +1,107 @@ +/*- + * ============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 static org.junit.Assert.assertEquals; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; +import javax.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-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/CommonTestData.java b/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/CommonTestData.java new file mode 100755 index 000000000..be573cb37 --- /dev/null +++ b/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/CommonTestData.java @@ -0,0 +1,78 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation. + * Modifications Copyright (C) 2022 AT&T Intellectual Property. 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.a1pms.utils; + +import java.util.List; +import java.util.UUID; +import org.onap.policy.clamp.acm.participant.a1pms.models.A1PolicyServiceEntity; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; + +public class CommonTestData { + + private static final String TEST_KEY_NAME = "org.onap.domain.database.A1PMSAutomationCompositionElement"; + + /** + * Get a automationComposition Element. + * + * @return automationCompositionElement object + */ + public AutomationCompositionElement getAutomationCompositionElement() { + AutomationCompositionElement element = new AutomationCompositionElement(); + element.setId(UUID.randomUUID()); + element.setDefinition(new ToscaConceptIdentifier(TEST_KEY_NAME, "1.0.1")); + element.setOrderedState(AutomationCompositionOrderedState.PASSIVE); + return element; + } + + /** + * Get automation composition id. + * + * @return ToscaConceptIdentifier automationCompositionId + */ + public ToscaConceptIdentifier getAutomationCompositionId() { + return getAutomationCompositionId(0); + } + + /** + * Get automation composition id. + * @param instanceNo Identifier instance no + * @return ToscaConceptIdentifier automationCompositionId + */ + public ToscaConceptIdentifier getAutomationCompositionId(int instanceNo) { + return new ToscaConceptIdentifier("A1PMSInstance" + instanceNo, "1.0.0"); + } + + + /** + * Get valid policy entities. + * @return List of policy entities + */ + public List getValidPolicyEntities() { + A1PolicyServiceEntity a1PolicyServiceEntity1 = new A1PolicyServiceEntity(getAutomationCompositionId(0), + "testService1", "http://localhost", 0); + A1PolicyServiceEntity a1PolicyServiceEntity2 = new A1PolicyServiceEntity(getAutomationCompositionId(1), + "testService2", "http://127.0.0.1", 0); + return List.of(a1PolicyServiceEntity1, a1PolicyServiceEntity2); + } +} diff --git a/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/MockRestEndpoint.java b/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/MockRestEndpoint.java new file mode 100755 index 000000000..9f8a28193 --- /dev/null +++ b/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/MockRestEndpoint.java @@ -0,0 +1,83 @@ +/*- + * ============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 javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +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 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(); + } +} diff --git a/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/MockServerRest.java b/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/MockServerRest.java new file mode 100755 index 000000000..dc0ed4a01 --- /dev/null +++ b/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/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.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); + 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-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/ToscaUtils.java b/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/ToscaUtils.java new file mode 100755 index 000000000..19471c016 --- /dev/null +++ b/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/utils/ToscaUtils.java @@ -0,0 +1,51 @@ +/*- + * ============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 lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.onap.policy.common.utils.coder.YamlJsonTranslator; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; + +/** + * Util class for Test scope. + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class ToscaUtils { + + private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator(); + private static final String TOSCA_TEMPLATE_YAML = "clamp/acm/test/participant-a1pms.yaml"; + + + /** + * Read a service template yaml. + * @return ToscaServiceTemplate + */ + public static ToscaServiceTemplate readAutomationCompositionFromTosca() { + return serializeAutomationCompositionYaml(); + } + + private static ToscaServiceTemplate serializeAutomationCompositionYaml() { + String automationCompositionString = ResourceUtils.getResourceAsString(ToscaUtils.TOSCA_TEMPLATE_YAML); + return yamlTranslator.fromYaml(automationCompositionString, ToscaServiceTemplate.class); + } +} diff --git a/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/webclient/AcA1PmsClientTest.java b/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/webclient/AcA1PmsClientTest.java new file mode 100755 index 000000000..0c4eabc21 --- /dev/null +++ b/participant/participant-impl/participant-impl-a1pms/src/test/java/org/onap/policy/clamp/acm/participant/a1pms/webclient/AcA1PmsClientTest.java @@ -0,0 +1,148 @@ +/*- + * ============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.webclient; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.when; + +import java.io.IOException; +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.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) +class AcA1PmsClientTest { + + private static int mockServerPort; + + private static MockServerRest mockServer; + + private static CommonTestData commonTestData; + + @Mock + private A1PmsParameters a1PmsParameters; + + + private final String healthyUrl = "/healthy"; + + private final String servicesUrl = "/services/success"; + + private final String serviceUrl = "/service/success/{serviceId}"; + + /** + * Set up Mock server. + */ + @BeforeAll + static void setUpMockServer() throws IOException, InterruptedException { + mockServerPort = NetworkUtil.allocPort(); + mockServer = new MockServerRest(mockServerPort); + mockServer.validate(); + commonTestData = new CommonTestData(); + } + + @AfterAll + static void stopServer() throws Exception { + mockServer.close(); + mockServer = null; + } + + void initializePmsHealthParameters(String healthUrl) { + String testMockUrl = "http://localhost"; + when(a1PmsParameters.getBaseUrl()).thenReturn(testMockUrl + ":" + mockServerPort); + var endpoints = new A1PmsParameters.Endpoints(); + endpoints.setHealth(healthUrl); + when(a1PmsParameters.getEndpoints()).thenReturn(endpoints); + } + + void initializePmsServicesParameters(String servicesUrl) { + initializePmsHealthParameters(healthyUrl); + var endpoints = a1PmsParameters.getEndpoints(); + endpoints.setServices(servicesUrl); + when(a1PmsParameters.getEndpoints()).thenReturn(endpoints); + } + + void initializePmsServiceParameters(String serviceUrl) { + initializePmsServicesParameters(servicesUrl); + var endpoints = a1PmsParameters.getEndpoints(); + endpoints.setService(serviceUrl); + when(a1PmsParameters.getEndpoints()).thenReturn(endpoints); + } + + + @Test + void test_healthyPms() { + initializePmsHealthParameters(healthyUrl); + var client = new AcA1PmsClient(a1PmsParameters); + assertTrue(client.isPmsHealthy()); + } + + @Test + void test_unhealthyPms() { + String unhealthyUrl = "/unhealthy"; + initializePmsHealthParameters(unhealthyUrl); + var client = new AcA1PmsClient(a1PmsParameters); + assertFalse(client.isPmsHealthy()); + } + + @Test + void test_createServicesSuccess() { + initializePmsServicesParameters(servicesUrl); + var client = new AcA1PmsClient(a1PmsParameters); + assertDoesNotThrow(() -> client.createService(commonTestData.getValidPolicyEntities())); + } + + @Test + void test_createServicesFailure() { + String createServiceFailureUrl = "services/failure"; + initializePmsServicesParameters(createServiceFailureUrl); + var client = new AcA1PmsClient(a1PmsParameters); + String expectedMessage = "Error in creating policy service"; + assertThrows(Exception.class, + () -> client.createService(commonTestData.getValidPolicyEntities()), expectedMessage); + } + + @Test + void test_deleteServicesSuccess() { + initializePmsServiceParameters(serviceUrl); + var client = new AcA1PmsClient(a1PmsParameters); + assertDoesNotThrow(() -> client.deleteService(commonTestData.getValidPolicyEntities())); + } + + @Test + void test_deleteServicesFailure() { + String deleteServiceFailureUrl = "services/failure/{serviceId}"; + initializePmsServiceParameters(deleteServiceFailureUrl); + var client = new AcA1PmsClient(a1PmsParameters); + String expectedMessage = "Error in deleting policy service"; + assertThrows(Exception.class, + () -> client.deleteService(commonTestData.getValidPolicyEntities()), expectedMessage); + } +} diff --git a/participant/participant-impl/participant-impl-a1pms/src/test/resources/application-test.yaml b/participant/participant-impl/participant-impl-a1pms/src/test/resources/application-test.yaml new file mode 100755 index 000000000..be33b5249 --- /dev/null +++ b/participant/participant-impl/participant-impl-a1pms/src/test/resources/application-test.yaml @@ -0,0 +1,24 @@ +participant: + intermediaryParameters: + reportingTimeInterval: 120000 + description: Participant Description + participantId: + name: A1PMSParticipant0 + version: 1.0.0 + participantType: + name: org.onap.policy.clamp.acm.A1PMSParticipant + version: 2.3.4 + clampAutomationCompositionTopics: + topicSources: + - + topic: POLICY-ACRUNTIME-PARTICIPANT + servers: + - localhost + topicCommInfrastructure: dmaap + fetchTimeout: 15000 + topicSinks: + - + topicCommInfrastructure: dmaap + servers: + - localhost + topic: POLICY-ACRUNTIME-PARTICIPANT \ No newline at end of file -- cgit 1.2.3-korg