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.controlloop.participant.simulator.main.rest;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
27 import java.util.Collection;
28 import java.util.List;
30 import java.util.UUID;
31 import javax.ws.rs.client.Entity;
32 import javax.ws.rs.client.Invocation;
33 import javax.ws.rs.core.GenericType;
34 import javax.ws.rs.core.MediaType;
35 import javax.ws.rs.core.Response;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
41 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
42 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
43 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
44 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
45 import org.onap.policy.clamp.controlloop.models.messages.rest.TypedSimpleResponse;
46 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopUpdateListener;
47 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.CommonTestData;
48 import org.onap.policy.clamp.controlloop.participant.simulator.simulation.SimulationHandler;
49 import org.onap.policy.clamp.controlloop.participant.simulator.simulation.SimulationProvider;
50 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
55 * Class to perform unit test of {@link TestSimulationRestController}.
57 public class TestSimulationRestController extends CommonParticipantRestServer {
59 private static ControlLoopUpdateListener clUpdateListener;
60 private static final String PARTICIPANTS_ENDPOINT = "participants";
61 private static final String ELEMENTS_ENDPOINT = "elements";
62 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
63 private static final String TOPIC = "my-topic";
64 static CommonTestData commonTestData = new CommonTestData();
71 public static void setUpBeforeClass() throws Exception {
72 CommonParticipantRestServer.setUpBeforeClass();
73 clUpdateListener = new ControlLoopUpdateListener(
74 SimulationHandler.getInstance()
75 .getSimulationProvider()
77 .getParticipantHandler());
78 ParticipantControlLoopUpdate participantControlLoopUpdateMsg =
79 TestListenerUtils.createControlLoopUpdateMsg();
80 participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
81 clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
85 public static void teardownAfterClass() {
86 CommonParticipantRestServer.teardownAfterClass();
90 public void testSwagger() throws Exception {
91 super.testSwagger(ELEMENTS_ENDPOINT);
95 public void testQuery_Unauthorized() throws Exception {
96 Invocation.Builder invocationBuilder = super.sendNoAuthRequest(ELEMENTS_ENDPOINT);
97 Response rawresp = invocationBuilder.buildGet().invoke();
98 assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
102 public void testQueryParticipants() throws Exception {
103 Participant participant = new Participant();
104 ToscaConceptIdentifier participantId = CommonTestData.getParticipantId();
105 participant.setDefinition(participantId);
106 participant.setName(participantId.getName());
107 participant.setVersion(participantId.getVersion());
109 // GET REST call for querying the participants
110 Invocation.Builder invocationBuilder =
111 super.sendRequest(PARTICIPANTS_ENDPOINT + "/" + participant.getKey().getName()
112 + "/" + participant.getVersion());
114 Response rawresp = invocationBuilder.buildGet().invoke();
115 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
116 List<Participant> returnValue = rawresp.readEntity(new GenericType<List<Participant>>() {});
117 assertNotNull(returnValue);
118 assertThat(returnValue).hasSize(1);
119 // Verify the result of GET participants with what is stored
120 assertEquals(participant.getDefinition(), returnValue.get(0).getDefinition());
124 public void testQueryControlLoopElements() throws Exception {
125 // GET REST call for querying the controlLoop elements
126 Invocation.Builder invocationBuilder = super.sendRequest(ELEMENTS_ENDPOINT + "/"
127 + "PMSHInstance0" + "/" + "1.0.0");
129 Response rawresp = invocationBuilder.buildGet().invoke();
130 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
131 Map<UUID, ControlLoopElement> returnValue =
132 rawresp.readEntity(new GenericType<Map<UUID, ControlLoopElement>>() {});
133 assertNotNull(returnValue);
134 // Verify the result of GET controlloop elements with what is stored
135 assertThat(returnValue).hasSize(1);
136 returnValue.values().forEach(element -> assertEquals("org.onap.PM_CDS_Blueprint",
137 element.getDefinition().getName()));
141 public void testUpdateParticipant() throws Exception {
142 SimulationProvider provider = SimulationHandler.getInstance().getSimulationProvider();
143 List<Participant> participants = provider.getParticipants(CommonTestData.getParticipantId().getName(),
144 CommonTestData.getParticipantId().getVersion());
145 assertEquals(ParticipantState.UNKNOWN, participants.get(0).getParticipantState());
146 // Change the state of the participant to PASSIVE from UNKNOWN
147 participants.get(0).setParticipantState(ParticipantState.PASSIVE);
148 Entity<Participant> entParticipant = Entity.entity(participants.get(0), MediaType.APPLICATION_JSON);
150 // PUT REST call for updating Participant
151 Invocation.Builder invocationBuilder = sendRequest(PARTICIPANTS_ENDPOINT);
152 Response rawresp = invocationBuilder.put(entParticipant);
153 TypedSimpleResponse<Participant> resp = rawresp.readEntity(TypedSimpleResponse.class);
154 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
155 assertNotNull(resp.getResponse());
156 // Verify the response and state returned by PUT REST call for updating participants
157 assertThat(resp.toString()).contains("definition={name=org.onap.PM_CDS_Blueprint, version=1.0.0}");
158 assertThat(resp.toString()).contains("participantState=PASSIVE");
162 public void testUpdateControlLoopElement() throws Exception {
163 ControlLoop controlLoop = TestListenerUtils.createControlLoop();
164 SimulationProvider provider = SimulationHandler.getInstance().getSimulationProvider();
165 Map<UUID, ControlLoopElement> controlLoopElements = provider.getControlLoopElements(
166 controlLoop.getDefinition().getName(), controlLoop.getDefinition().getVersion());
168 for (Map.Entry<UUID, ControlLoopElement> clElement : controlLoopElements.entrySet()) {
169 // Check the initial state on the ControlLoopElement, which is UNINITIALISED
170 assertEquals(ControlLoopOrderedState.UNINITIALISED, clElement.getValue().getOrderedState());
171 // Change the state of the ControlLoopElement to PASSIVE from UNINITIALISED
172 clElement.getValue().setOrderedState(ControlLoopOrderedState.PASSIVE);
173 Entity<ControlLoopElement> entClElement = Entity.entity(clElement.getValue(), MediaType.APPLICATION_JSON);
175 // PUT REST call for updating ControlLoopElement
176 Invocation.Builder invocationBuilder = sendRequest(ELEMENTS_ENDPOINT);
177 Response rawresp = invocationBuilder.put(entClElement);
178 TypedSimpleResponse<ControlLoopElement> resp = rawresp.readEntity(TypedSimpleResponse.class);
179 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
180 assertNotNull(resp.getResponse());
181 // Verify the response and state returned by PUT REST call for updating participants
182 assertThat(resp.toString()).contains("definition={name=org.onap.PM_CDS_Blueprint, version=1.0.0}");
183 assertThat(resp.toString()).contains("orderedState=PASSIVE");