diff options
author | FrancescoFioraEst <francesco.fiora@est.tech> | 2023-06-13 09:19:08 +0100 |
---|---|---|
committer | FrancescoFioraEst <francesco.fiora@est.tech> | 2023-06-13 10:43:14 +0100 |
commit | 73a48d9c7e599baf145820d0bc0968040142dac8 (patch) | |
tree | 6c43242a65decb54306ecd29bbe6a6591721f280 /participant/participant-impl/participant-impl-simulator/src/test/java | |
parent | 33f508d48de474c752c2c06d396b7b946f998447 (diff) |
Add Java Implementation for mock participant in ACM
Add Java Code Implementation for mock participant
to test different ACM scenarios.
Issue-ID: POLICY-4722
Change-Id: I8475c05c2469e190ca81d4caff0babc22c5d6db9
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
Diffstat (limited to 'participant/participant-impl/participant-impl-simulator/src/test/java')
6 files changed, 660 insertions, 0 deletions
diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/comm/CommonTestData.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/comm/CommonTestData.java index c2338d9fe..ee86b8f88 100644 --- a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/comm/CommonTestData.java +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/comm/CommonTestData.java @@ -26,6 +26,8 @@ import java.util.Map; import java.util.TreeMap; import java.util.UUID; import org.onap.policy.clamp.acm.participant.sim.parameters.ParticipantSimParameters; +import org.onap.policy.clamp.models.acm.concepts.AutomationComposition; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement; import org.onap.policy.common.endpoints.parameters.TopicParameters; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; @@ -112,4 +114,32 @@ public class CommonTestData { public static UUID getParticipantId() { return UUID.fromString("101c62b3-8918-41b9-a747-d21eb79c6c01"); } + + /** + * Returns a Map of ToscaConceptIdentifier and AutomationComposition for test cases. + * + * @return automationCompositionMap + * + * @throws CoderException if there is an error with .json file. + */ + public static Map<UUID, AutomationComposition> getTestAutomationCompositionMap() { + var automationComposition = getTestAutomationComposition(); + return Map.of(automationComposition.getInstanceId(), automationComposition); + } + + /** + * Returns List of AutomationComposition for test cases. + * + * @return AutomationCompositions + * + * @throws CoderException if there is an error with .json file. + */ + public static AutomationComposition getTestAutomationComposition() { + var automationComposition = new AutomationComposition(); + automationComposition.setInstanceId(UUID.randomUUID()); + var element = new AutomationCompositionElement(); + element.setId(UUID.randomUUID()); + automationComposition.setElements(Map.of(element.getId(), element)); + return automationComposition; + } } diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerTest.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerTest.java new file mode 100644 index 000000000..977b9d8e4 --- /dev/null +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerTest.java @@ -0,0 +1,236 @@ +/*- + * ============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.main.handler; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.clamp.acm.participant.sim.comm.CommonTestData; +import org.onap.policy.clamp.acm.participant.sim.model.SimConfig; +import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy; +import org.onap.policy.clamp.models.acm.concepts.AcTypeState; +import org.onap.policy.clamp.models.acm.concepts.DeployState; +import org.onap.policy.clamp.models.acm.concepts.LockState; +import org.onap.policy.clamp.models.acm.concepts.StateChangeResult; +import org.onap.policy.models.base.PfModelException; + +class AutomationCompositionElementHandlerTest { + + @Test + void testDeploy() throws PfModelException { + var config = new SimConfig(); + config.setDeployTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); + acElementHandler.setConfig(config); + var instanceId = UUID.randomUUID(); + var element = new AcElementDeploy(); + element.setId(UUID.randomUUID()); + acElementHandler.deploy(instanceId, element, Map.of()); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed"); + + config.setDeploySuccess(false); + acElementHandler.deploy(instanceId, element, Map.of()); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Deploy failed!"); + } + + @Test + void testUndeploy() throws PfModelException { + var config = new SimConfig(); + config.setUndeployTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); + acElementHandler.setConfig(config); + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + acElementHandler.undeploy(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED, + null, StateChangeResult.NO_ERROR, "Undeployed"); + + config.setUndeploySuccess(false); + acElementHandler.undeploy(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED, + null, StateChangeResult.FAILED, "Undeploy failed!"); + } + + @Test + void testLock() throws PfModelException { + var config = new SimConfig(); + config.setLockTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); + acElementHandler.setConfig(config); + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + acElementHandler.lock(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED, + StateChangeResult.NO_ERROR, "Locked"); + + config.setLockSuccess(false); + acElementHandler.lock(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED, + StateChangeResult.FAILED, "Lock failed!"); + } + + @Test + void testUnlock() throws PfModelException { + var config = new SimConfig(); + config.setUnlockTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); + acElementHandler.setConfig(config); + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + acElementHandler.unlock(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED, + StateChangeResult.NO_ERROR, "Unlocked"); + + config.setUnlockSuccess(false); + acElementHandler.unlock(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED, + StateChangeResult.FAILED, "Unlock failed!"); + } + + @Test + void testUpdate() throws PfModelException { + var config = new SimConfig(); + config.setUpdateTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); + acElementHandler.setConfig(config); + var instanceId = UUID.randomUUID(); + var element = new AcElementDeploy(); + element.setId(UUID.randomUUID()); + acElementHandler.update(instanceId, element, Map.of()); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated"); + + config.setUpdateSuccess(false); + acElementHandler.update(instanceId, element, Map.of()); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!"); + } + + @Test + void testDelete() throws PfModelException { + var config = new SimConfig(); + config.setDeleteTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); + acElementHandler.setConfig(config); + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + acElementHandler.delete(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED, + null, StateChangeResult.NO_ERROR, "Deleted"); + + config.setDeleteSuccess(false); + acElementHandler.delete(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED, + null, StateChangeResult.FAILED, "Delete failed!"); + } + + @Test + void testgetAutomationComposition() throws PfModelException { + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); + + var map = CommonTestData.getTestAutomationCompositionMap(); + when(intermediaryApi.getAutomationCompositions()).thenReturn(map); + var result = acElementHandler.getAutomationCompositions(); + assertEquals(map.values().iterator().next(), result.getAutomationCompositionList().get(0)); + } + + @Test + void testsetOutProperties() { + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); + + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + var useState = "useState"; + var operationalState = "operationalState"; + Map<String, Object> map = Map.of("id", "1234"); + + acElementHandler.setOutProperties(instanceId, elementId, useState, operationalState, map); + verify(intermediaryApi).sendAcElementInfo(instanceId, elementId, useState, operationalState, map); + } + + @Test + void testgetDataList() { + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); + + var map = CommonTestData.getTestAutomationCompositionMap(); + when(intermediaryApi.getAutomationCompositions()).thenReturn(map); + var result = acElementHandler.getDataList(); + var data = result.getList().get(0); + var autocomposition = map.values().iterator().next(); + assertEquals(autocomposition.getInstanceId(), data.getAutomationCompositionId()); + var element = autocomposition.getElements().values().iterator().next(); + assertEquals(element.getId(), data.getAutomationCompositionElementId()); + } + + @Test + void testPrime() throws PfModelException { + var config = new SimConfig(); + config.setPrimeTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); + acElementHandler.setConfig(config); + var compositionId = UUID.randomUUID(); + acElementHandler.prime(compositionId, List.of()); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, + "Primed"); + + config.setPrimeSuccess(false); + acElementHandler.prime(compositionId, List.of()); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED, + StateChangeResult.FAILED, "Prime failed!"); + } + + @Test + void testDeprime() throws PfModelException { + var config = new SimConfig(); + config.setDeprimeTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); + acElementHandler.setConfig(config); + var compositionId = UUID.randomUUID(); + acElementHandler.deprime(compositionId); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED, + StateChangeResult.NO_ERROR, "Deprimed"); + + config.setDeprimeSuccess(false); + acElementHandler.deprime(compositionId); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED, + "Deprime failed!"); + } +} diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/rest/AcSimRestTest.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/rest/AcSimRestTest.java new file mode 100644 index 000000000..bcdabc86d --- /dev/null +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/rest/AcSimRestTest.java @@ -0,0 +1,143 @@ +/*- + * ============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.rest; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.doReturn; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.util.UUID; +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.comm.CommonTestData; +import org.onap.policy.clamp.acm.participant.sim.main.handler.AutomationCompositionElementHandler; +import org.onap.policy.clamp.acm.participant.sim.model.InternalData; +import org.onap.policy.clamp.acm.participant.sim.model.InternalDatas; +import org.onap.policy.clamp.acm.participant.sim.model.SimConfig; +import org.onap.policy.clamp.acm.participant.sim.parameters.ParticipantSimParameters; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration; +import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +@ExtendWith(SpringExtension.class) +@WebMvcTest(value = SimulatorController.class) +@Import({MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class}) +@EnableConfigurationProperties(value = ParticipantSimParameters.class) +class AcSimRestTest { + + private static final Coder CODER = new StandardCoder(); + private static final String CONFIG_URL = "/v2/parameters"; + private static final String INSTANCE_URL = "/v2/instances"; + private static final String DATAS_URL = "/v2/datas"; + + @Autowired + private MockMvc mockMvc; + + @MockBean + private AutomationCompositionElementHandler automationCompositionElementHandler; + + @Autowired + private WebApplicationContext context; + + @BeforeEach + void mockServiceClass() { + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build(); + } + + @Test + void testgetConfig() throws Exception { + var requestBuilder = MockMvcRequestBuilders.get(CONFIG_URL).accept(MediaType.APPLICATION_JSON_VALUE); + + doReturn(new SimConfig()).when(automationCompositionElementHandler).getConfig(); + + mockMvc.perform(requestBuilder).andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.primeTimerMs", is(100))); + } + + @Test + void testsetConfig() throws Exception { + var requestBuilder = MockMvcRequestBuilders.put(CONFIG_URL).accept(MediaType.APPLICATION_JSON_VALUE) + .content(CODER.encode(new SimConfig())).contentType(MediaType.APPLICATION_JSON_VALUE); + + mockMvc.perform(requestBuilder).andExpect(status().isOk()); + } + + @Test + void testgetAutomationCompositions() throws Exception { + var requestBuilder = MockMvcRequestBuilders.get(INSTANCE_URL).accept(MediaType.APPLICATION_JSON_VALUE); + + var automationCompositions = new AutomationCompositions(); + automationCompositions.getAutomationCompositionList().add(CommonTestData.getTestAutomationComposition()); + + doReturn(automationCompositions).when(automationCompositionElementHandler).getAutomationCompositions(); + + var result = mockMvc.perform(requestBuilder).andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andReturn(); + var body = result.getResponse().getContentAsString(); + var acsResult = CODER.decode(body, AutomationCompositions.class); + assertEquals(automationCompositions.getAutomationCompositionList().get(0).getInstanceId(), + acsResult.getAutomationCompositionList().get(0).getInstanceId()); + } + + @Test + void testgetDatas() throws Exception { + var internalDatas = new InternalDatas(); + var internalData = new InternalData(); + internalData.setAutomationCompositionId(UUID.randomUUID()); + internalDatas.getList().add(internalData); + + doReturn(internalDatas).when(automationCompositionElementHandler).getDataList(); + + var requestBuilder = MockMvcRequestBuilders.get(DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE); + var result = mockMvc.perform(requestBuilder).andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andReturn(); + var body = result.getResponse().getContentAsString(); + var acsResult = CODER.decode(body, InternalDatas.class); + assertEquals(internalData.getAutomationCompositionId(), + acsResult.getList().get(0).getAutomationCompositionId()); + } + + @Test + void testsetDatas() throws Exception { + var requestBuilder = MockMvcRequestBuilders.put(DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE) + .content(CODER.encode(new InternalData())).contentType(MediaType.APPLICATION_JSON_VALUE); + + mockMvc.perform(requestBuilder).andExpect(status().isOk()); + } +} 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 new file mode 100644 index 000000000..474d45cc5 --- /dev/null +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/rest/ActuatorControllerTest.java @@ -0,0 +1,92 @@ +/*- + * ============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.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.sim.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.test.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() throws Exception { + assertUnauthorizedActGet(HEALTH_ENDPOINT); + } + + @Test + void testGetMetrics_Unauthorized() throws Exception { + assertUnauthorizedActGet(METRICS_ENDPOINT); + } + + @Test + void testGetPrometheus_Unauthorized() throws Exception { + assertUnauthorizedActGet(PROMETHEUS_ENDPOINT); + } + + @Test + void testGetHealth() throws Exception { + Invocation.Builder invocationBuilder = super.sendActRequest(HEALTH_ENDPOINT); + Response rawresp = invocationBuilder.buildGet().invoke(); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + } + + @Test + void testGetMetrics() throws Exception { + Invocation.Builder invocationBuilder = super.sendActRequest(METRICS_ENDPOINT); + Response rawresp = invocationBuilder.buildGet().invoke(); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + } + + @Test + void testGePrometheus() throws Exception { + 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-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 new file mode 100644 index 000000000..846e76349 --- /dev/null +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/utils/CommonActuatorController.java @@ -0,0 +1,108 @@ +/*- + * ============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.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/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/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/utils/ToscaUtils.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/utils/ToscaUtils.java new file mode 100644 index 000000000..768e18d1a --- /dev/null +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/utils/ToscaUtils.java @@ -0,0 +1,51 @@ +/*- + * ============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 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-http.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); + } +} |