2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.simulator.endtoend;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import java.util.List;
30 import java.util.UUID;
31 import javax.ws.rs.client.Client;
32 import javax.ws.rs.client.ClientBuilder;
33 import javax.ws.rs.client.Entity;
34 import javax.ws.rs.client.Invocation;
35 import javax.ws.rs.client.WebTarget;
36 import javax.ws.rs.core.GenericType;
37 import javax.ws.rs.core.MediaType;
38 import javax.ws.rs.core.MultivaluedMap;
39 import javax.ws.rs.core.Response;
40 import org.glassfish.jersey.client.ClientProperties;
41 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.api.extension.ExtendWith;
44 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
45 import org.onap.policy.clamp.acm.participant.intermediary.comm.AutomationCompositionUpdateListener;
46 import org.onap.policy.clamp.acm.participant.intermediary.handler.ParticipantHandler;
47 import org.onap.policy.clamp.acm.participant.simulator.main.parameters.CommonTestData;
48 import org.onap.policy.clamp.acm.participant.simulator.main.rest.AbstractRestController;
49 import org.onap.policy.clamp.acm.participant.simulator.main.rest.TestListenerUtils;
50 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
51 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
52 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
53 import org.onap.policy.clamp.models.acm.concepts.Participant;
54 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
55 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionUpdate;
56 import org.onap.policy.clamp.models.acm.messages.rest.TypedSimpleResponse;
57 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
58 import org.onap.policy.common.gson.GsonMessageBodyHandler;
59 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
60 import org.springframework.beans.factory.annotation.Autowired;
61 import org.springframework.beans.factory.annotation.Value;
62 import org.springframework.boot.test.context.SpringBootTest;
63 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
64 import org.springframework.boot.web.server.LocalServerPort;
65 import org.springframework.test.context.TestPropertySource;
66 import org.springframework.test.context.junit.jupiter.SpringExtension;
68 @ExtendWith(SpringExtension.class)
69 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
70 @TestPropertySource(locations = {"classpath:application_test.properties"})
71 class ParticipantSimulatorTest {
73 private static final String PARTICIPANTS_ENDPOINT = "participants";
74 private static final String ELEMENTS_ENDPOINT = "elements";
75 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
76 private static final String TOPIC = "my-topic";
78 @Value("${spring.security.user.name}")
81 @Value("${spring.security.user.password}")
82 private String password;
85 private int randomServerPort;
88 private ParticipantIntermediaryApi participantIntermediaryApi;
91 private ParticipantHandler participantHandler;
93 private static final Object lockit = new Object();
94 private boolean check = false;
96 private void setUp() throws Exception {
97 synchronized (lockit) {
100 AutomationCompositionUpdateListener acUpdateListener =
101 new AutomationCompositionUpdateListener(participantHandler);
103 AutomationCompositionUpdate automationCompositionUpdateMsg =
104 TestListenerUtils.createAutomationCompositionUpdateMsg();
105 acUpdateListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionUpdateMsg);
111 private String getPath(String path) {
112 return "http://localhost:" + randomServerPort + "/onap/participantsim/v2/" + path;
115 void testSwagger(String endPoint) {
116 final Client client = ClientBuilder.newBuilder().build();
118 client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
119 client.register(GsonMessageBodyHandler.class);
120 client.register(HttpAuthenticationFeature.basic(user, password));
122 final WebTarget webTarget = client.target(getPath("api-docs"));
124 Response response = webTarget.request(MediaType.APPLICATION_JSON).get();
126 assertThat(response.getStatus()).isEqualTo(200);
127 assertTrue(response.readEntity(String.class).contains("/onap/participantsim/v2/" + endPoint));
131 void testEndParticipatsSwagger() {
132 testSwagger(PARTICIPANTS_ENDPOINT);
136 void testElementsSwagger() {
137 testSwagger(ELEMENTS_ENDPOINT);
141 void testProducerYaml() {
142 final Client client = ClientBuilder.newBuilder().build();
144 client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
145 client.register(GsonMessageBodyHandler.class);
146 client.register(HttpAuthenticationFeature.basic(user, password));
148 String path = getPath(PARTICIPANTS_ENDPOINT + "/org.onap.PM_CDS_Blueprint/1");
149 final WebTarget webTarget = client.target(path);
151 Response response = webTarget.request("application/yaml").get();
153 assertThat(response.getStatus()).isEqualTo(200);
157 void testQuery_Unauthorized() throws Exception {
158 String path = PARTICIPANTS_ENDPOINT + "/org.onap.PM_CDS_Blueprint/1";
160 Response response = performRequest(path, true, null).get();
161 assertThat(response.getStatus()).isEqualTo(200);
164 response = performRequest(path, false, null).get();
165 assertThat(response.getStatus()).isEqualTo(401);
168 private Invocation.Builder performRequest(String endpoint, boolean includeAuth, UUID uuid) {
169 final Client client = ClientBuilder.newBuilder().build();
171 client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
172 client.register(GsonMessageBodyHandler.class);
174 client.register(HttpAuthenticationFeature.basic(user, password));
176 Invocation.Builder builder = client.target(getPath(endpoint)).request(MediaType.APPLICATION_JSON);
178 builder = builder.header(AbstractRestController.REQUEST_ID_NAME, uuid.toString());
183 private Response performGet(String endpoint, UUID uuid) throws Exception {
184 return performRequest(endpoint, true, uuid).get();
188 void testQueryParticipants() throws Exception {
189 Participant participant = new Participant();
190 ToscaConceptIdentifier participantId = CommonTestData.getParticipantId();
191 participant.setDefinition(participantId);
192 participant.setName(participantId.getName());
193 participant.setVersion(participantId.getVersion());
194 UUID uuid = UUID.randomUUID();
196 // GET REST call for querying the participants
197 Response response = performGet(
198 PARTICIPANTS_ENDPOINT + "/" + participant.getKey().getName() + "/" + participant.getKey().getVersion(),
200 checkResponseEntity(response, 200, uuid);
202 Participant[] returnValue = response.readEntity(Participant[].class);
203 assertThat(returnValue).hasSize(1);
204 // Verify the result of GET participants with what is stored
205 assertEquals(participant.getDefinition(), returnValue[0].getDefinition());
208 private <T> void checkResponseEntity(Response response, int status, UUID uuid) {
209 assertThat(response.getStatus()).isEqualTo(status);
210 assertThat(getHeader(response.getHeaders(), AbstractRestController.VERSION_MINOR_NAME)).isEqualTo("0");
211 assertThat(getHeader(response.getHeaders(), AbstractRestController.VERSION_PATCH_NAME)).isEqualTo("0");
212 assertThat(getHeader(response.getHeaders(), AbstractRestController.VERSION_LATEST_NAME)).isEqualTo("1.0.0");
213 assertThat(getHeader(response.getHeaders(), AbstractRestController.REQUEST_ID_NAME)).isEqualTo(uuid.toString());
216 private String getHeader(MultivaluedMap<String, Object> httpHeaders, String param) {
217 List<Object> list = httpHeaders.get(param);
218 assertThat(list).hasSize(1);
219 assertThat(list.get(0)).isNotNull();
220 return (String) list.get(0);
224 void testQueryAutomationCompositionElements() throws Exception {
226 UUID uuid = UUID.randomUUID();
227 ToscaConceptIdentifier participantId = CommonTestData.getParticipantId();
229 // GET REST call for querying the automationComposition elements
231 performGet(ELEMENTS_ENDPOINT + "/" + participantId.getName() + "/" + participantId.getVersion(), uuid);
232 checkResponseEntity(response, 200, uuid);
234 Map<?, ?> returnValue = response.readEntity(Map.class);
235 // Verify the result of GET automation composition elements with what is stored
236 assertThat(returnValue).isEmpty();
239 private Response performPut(String endpoint, final Entity<?> entity, UUID uuid) throws Exception {
240 return performRequest(endpoint, true, uuid).put(entity);
244 void testUpdateParticipant() throws Exception {
246 List<Participant> participants = participantIntermediaryApi.getParticipants(
247 CommonTestData.getParticipantId().getName(), CommonTestData.getParticipantId().getVersion());
248 assertEquals(ParticipantState.UNKNOWN, participants.get(0).getParticipantState());
249 // Change the state of the participant to PASSIVE from UNKNOWN
250 participants.get(0).setParticipantState(ParticipantState.PASSIVE);
251 UUID uuid = UUID.randomUUID();
253 // PUT REST call for updating Participant
254 Response response = performPut(PARTICIPANTS_ENDPOINT, Entity.json(participants.get(0)), uuid);
255 checkResponseEntity(response, 200, uuid);
257 TypedSimpleResponse<Participant> resp =
258 response.readEntity(new GenericType<TypedSimpleResponse<Participant>>() {});
259 assertNotNull(resp.getResponse());
260 // Verify the response and state returned by PUT REST call for updating participants
261 assertEquals(participants.get(0).getDefinition(), resp.getResponse().getDefinition());
262 assertEquals(ParticipantState.PASSIVE, resp.getResponse().getParticipantState());
266 void testUpdateAutomationCompositionElement() throws Exception {
268 AutomationComposition automationComposition = TestListenerUtils.createAutomationComposition();
269 Map<UUID, AutomationCompositionElement> automationCompositionElements =
270 participantIntermediaryApi.getAutomationCompositionElements(automationComposition.getDefinition().getName(),
271 automationComposition.getDefinition().getVersion());
273 UUID uuid = automationCompositionElements.keySet().iterator().next();
274 AutomationCompositionElement automationCompositionElement = automationCompositionElements.get(uuid);
276 automationCompositionElement.setOrderedState(AutomationCompositionOrderedState.PASSIVE);
277 // PUT REST call for updating AutomationCompositionElement
278 Response response = performPut(ELEMENTS_ENDPOINT, Entity.json(automationCompositionElement), uuid);
279 checkResponseEntity(response, 200, uuid);
281 TypedSimpleResponse<AutomationCompositionElement> resp =
282 response.readEntity(new GenericType<TypedSimpleResponse<AutomationCompositionElement>>() {});
283 assertNotNull(resp.getResponse());
284 // Verify the response and state returned by PUT REST call for updating participants
285 assertEquals(automationCompositionElement.getDefinition(), resp.getResponse().getDefinition());
286 assertEquals(AutomationCompositionOrderedState.PASSIVE, resp.getResponse().getOrderedState());