diff options
Diffstat (limited to 'participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/simulator/endtoend/ParticipantSimulatorTest.java')
-rw-r--r-- | participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/simulator/endtoend/ParticipantSimulatorTest.java | 153 |
1 files changed, 76 insertions, 77 deletions
diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/simulator/endtoend/ParticipantSimulatorTest.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/simulator/endtoend/ParticipantSimulatorTest.java index de6855d03..4cf9f1e3c 100644 --- a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/simulator/endtoend/ParticipantSimulatorTest.java +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/simulator/endtoend/ParticipantSimulatorTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. + * Copyright (C) 2021-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. @@ -46,7 +46,7 @@ import org.onap.policy.clamp.acm.participant.intermediary.comm.AutomationComposi import org.onap.policy.clamp.acm.participant.intermediary.handler.ParticipantHandler; import org.onap.policy.clamp.acm.participant.simulator.main.parameters.CommonTestData; import org.onap.policy.clamp.acm.participant.simulator.main.rest.AbstractRestController; -import org.onap.policy.clamp.acm.participant.simulator.main.rest.TestListenerUtils; +import org.onap.policy.clamp.acm.participant.simulator.utils.TestListenerUtils; import org.onap.policy.clamp.models.acm.concepts.AutomationComposition; import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement; import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState; @@ -62,12 +62,12 @@ import org.springframework.beans.factory.annotation.Value; 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.TestPropertySource; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit.jupiter.SpringExtension; @ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -@TestPropertySource(locations = {"classpath:application_test.properties"}) +@ActiveProfiles("test") class ParticipantSimulatorTest { private static final String PARTICIPANTS_ENDPOINT = "participants"; @@ -93,7 +93,7 @@ class ParticipantSimulatorTest { private static final Object lockit = new Object(); private boolean check = false; - private void setUp() throws Exception { + private void setUp() { synchronized (lockit) { if (!check) { check = true; @@ -103,32 +103,12 @@ class ParticipantSimulatorTest { AutomationCompositionUpdate automationCompositionUpdateMsg = TestListenerUtils.createAutomationCompositionUpdateMsg(); acUpdateListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionUpdateMsg); - } } } - private String getPath(String path) { - return "http://localhost:" + randomServerPort + "/onap/participantsim/v2/" + path; - } - - void testSwagger(String endPoint) { - final Client client = ClientBuilder.newBuilder().build(); - - client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); - client.register(GsonMessageBodyHandler.class); - client.register(HttpAuthenticationFeature.basic(user, password)); - - final WebTarget webTarget = client.target(getPath("api-docs")); - - Response response = webTarget.request(MediaType.APPLICATION_JSON).get(); - - assertThat(response.getStatus()).isEqualTo(200); - assertTrue(response.readEntity(String.class).contains("/onap/participantsim/v2/" + endPoint)); - } - @Test - void testEndParticipatsSwagger() { + void testEndParticipantsSwagger() { testSwagger(PARTICIPANTS_ENDPOINT); } @@ -154,7 +134,7 @@ class ParticipantSimulatorTest { } @Test - void testQuery_Unauthorized() throws Exception { + void testQuery_Unauthorized() { String path = PARTICIPANTS_ENDPOINT + "/org.onap.PM_CDS_Blueprint/1"; Response response = performRequest(path, true, null).get(); @@ -165,27 +145,8 @@ class ParticipantSimulatorTest { assertThat(response.getStatus()).isEqualTo(401); } - private Invocation.Builder performRequest(String endpoint, boolean includeAuth, UUID uuid) { - final Client client = ClientBuilder.newBuilder().build(); - - client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); - client.register(GsonMessageBodyHandler.class); - if (includeAuth) { - client.register(HttpAuthenticationFeature.basic(user, password)); - } - Invocation.Builder builder = client.target(getPath(endpoint)).request(MediaType.APPLICATION_JSON); - if (uuid != null) { - builder = builder.header(AbstractRestController.REQUEST_ID_NAME, uuid.toString()); - } - return builder; - } - - private Response performGet(String endpoint, UUID uuid) throws Exception { - return performRequest(endpoint, true, uuid).get(); - } - @Test - void testQueryParticipants() throws Exception { + void testQueryParticipants() { Participant participant = new Participant(); ToscaConceptIdentifier participantId = CommonTestData.getParticipantId(); participant.setDefinition(participantId); @@ -197,7 +158,7 @@ class ParticipantSimulatorTest { Response response = performGet( PARTICIPANTS_ENDPOINT + "/" + participant.getKey().getName() + "/" + participant.getKey().getVersion(), uuid); - checkResponseEntity(response, 200, uuid); + checkResponseEntity(response, uuid); Participant[] returnValue = response.readEntity(Participant[].class); assertThat(returnValue).hasSize(1); @@ -205,23 +166,8 @@ class ParticipantSimulatorTest { assertEquals(participant.getDefinition(), returnValue[0].getDefinition()); } - private <T> void checkResponseEntity(Response response, int status, UUID uuid) { - assertThat(response.getStatus()).isEqualTo(status); - assertThat(getHeader(response.getHeaders(), AbstractRestController.VERSION_MINOR_NAME)).isEqualTo("0"); - assertThat(getHeader(response.getHeaders(), AbstractRestController.VERSION_PATCH_NAME)).isEqualTo("0"); - assertThat(getHeader(response.getHeaders(), AbstractRestController.VERSION_LATEST_NAME)).isEqualTo("1.0.0"); - assertThat(getHeader(response.getHeaders(), AbstractRestController.REQUEST_ID_NAME)).isEqualTo(uuid.toString()); - } - - private String getHeader(MultivaluedMap<String, Object> httpHeaders, String param) { - List<Object> list = httpHeaders.get(param); - assertThat(list).hasSize(1); - assertThat(list.get(0)).isNotNull(); - return (String) list.get(0); - } - @Test - void testQueryAutomationCompositionElements() throws Exception { + void testQueryAutomationCompositionElements() { setUp(); UUID uuid = UUID.randomUUID(); ToscaConceptIdentifier participantId = CommonTestData.getParticipantId(); @@ -229,19 +175,15 @@ class ParticipantSimulatorTest { // GET REST call for querying the automationComposition elements Response response = performGet(ELEMENTS_ENDPOINT + "/" + participantId.getName() + "/" + participantId.getVersion(), uuid); - checkResponseEntity(response, 200, uuid); + checkResponseEntity(response, uuid); Map<?, ?> returnValue = response.readEntity(Map.class); // Verify the result of GET automation composition elements with what is stored assertThat(returnValue).isEmpty(); } - private Response performPut(String endpoint, final Entity<?> entity, UUID uuid) throws Exception { - return performRequest(endpoint, true, uuid).put(entity); - } - @Test - void testUpdateParticipant() throws Exception { + void testUpdateParticipant() { setUp(); List<Participant> participants = participantIntermediaryApi.getParticipants( CommonTestData.getParticipantId().getName(), CommonTestData.getParticipantId().getVersion()); @@ -252,10 +194,10 @@ class ParticipantSimulatorTest { // PUT REST call for updating Participant Response response = performPut(PARTICIPANTS_ENDPOINT, Entity.json(participants.get(0)), uuid); - checkResponseEntity(response, 200, uuid); + checkResponseEntity(response, uuid); - TypedSimpleResponse<Participant> resp = - response.readEntity(new GenericType<TypedSimpleResponse<Participant>>() {}); + TypedSimpleResponse<Participant> resp = response.readEntity(new GenericType<>() { + }); assertNotNull(resp.getResponse()); // Verify the response and state returned by PUT REST call for updating participants assertEquals(participants.get(0).getDefinition(), resp.getResponse().getDefinition()); @@ -263,7 +205,7 @@ class ParticipantSimulatorTest { } @Test - void testUpdateAutomationCompositionElement() throws Exception { + void testUpdateAutomationCompositionElement() { setUp(); AutomationComposition automationComposition = TestListenerUtils.createAutomationComposition(); Map<UUID, AutomationCompositionElement> automationCompositionElements = @@ -276,13 +218,70 @@ class ParticipantSimulatorTest { automationCompositionElement.setOrderedState(AutomationCompositionOrderedState.PASSIVE); // PUT REST call for updating AutomationCompositionElement Response response = performPut(ELEMENTS_ENDPOINT, Entity.json(automationCompositionElement), uuid); - checkResponseEntity(response, 200, uuid); + checkResponseEntity(response, uuid); - TypedSimpleResponse<AutomationCompositionElement> resp = - response.readEntity(new GenericType<TypedSimpleResponse<AutomationCompositionElement>>() {}); + TypedSimpleResponse<AutomationCompositionElement> resp = response.readEntity(new GenericType<>() { + }); assertNotNull(resp.getResponse()); // Verify the response and state returned by PUT REST call for updating participants assertEquals(automationCompositionElement.getDefinition(), resp.getResponse().getDefinition()); assertEquals(AutomationCompositionOrderedState.PASSIVE, resp.getResponse().getOrderedState()); } + + private String getPath(String path) { + return "http://localhost:" + randomServerPort + "/onap/participantsim/v2/" + path; + } + + void testSwagger(String endPoint) { + final Client client = ClientBuilder.newBuilder().build(); + + client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); + client.register(GsonMessageBodyHandler.class); + client.register(HttpAuthenticationFeature.basic(user, password)); + + final WebTarget webTarget = client.target(getPath("api-docs")); + + Response response = webTarget.request(MediaType.APPLICATION_JSON).get(); + + assertThat(response.getStatus()).isEqualTo(200); + assertTrue(response.readEntity(String.class).contains("/onap/participantsim/v2/" + endPoint)); + } + + private Invocation.Builder performRequest(String endpoint, boolean includeAuth, UUID uuid) { + final Client client = ClientBuilder.newBuilder().build(); + + client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); + client.register(GsonMessageBodyHandler.class); + if (includeAuth) { + client.register(HttpAuthenticationFeature.basic(user, password)); + } + Invocation.Builder builder = client.target(getPath(endpoint)).request(MediaType.APPLICATION_JSON); + if (uuid != null) { + builder = builder.header(AbstractRestController.REQUEST_ID_NAME, uuid.toString()); + } + return builder; + } + + private Response performGet(String endpoint, UUID uuid) { + return performRequest(endpoint, true, uuid).get(); + } + + private void checkResponseEntity(Response response, UUID uuid) { + assertThat(response.getStatus()).isEqualTo(200); + assertThat(getHeader(response.getHeaders(), AbstractRestController.VERSION_MINOR_NAME)).isEqualTo("0"); + assertThat(getHeader(response.getHeaders(), AbstractRestController.VERSION_PATCH_NAME)).isEqualTo("0"); + assertThat(getHeader(response.getHeaders(), AbstractRestController.VERSION_LATEST_NAME)).isEqualTo("1.0.0"); + assertThat(getHeader(response.getHeaders(), AbstractRestController.REQUEST_ID_NAME)).isEqualTo(uuid.toString()); + } + + private String getHeader(MultivaluedMap<String, Object> httpHeaders, String param) { + List<Object> list = httpHeaders.get(param); + assertThat(list).hasSize(1); + assertThat(list.get(0)).isNotNull(); + return (String) list.get(0); + } + + private Response performPut(String endpoint, final Entity<?> entity, UUID uuid) { + return performRequest(endpoint, true, uuid).put(entity); + } } |