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.List;
28 import javax.ws.rs.client.Entity;
29 import javax.ws.rs.client.Invocation;
30 import javax.ws.rs.core.GenericType;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.Response;
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
42 import org.onap.policy.clamp.controlloop.models.messages.rest.TypedSimpleResponse;
43 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopUpdateListener;
44 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.CommonTestData;
45 import org.onap.policy.clamp.controlloop.participant.simulator.simulation.SimulationHandler;
46 import org.onap.policy.clamp.controlloop.participant.simulator.simulation.SimulationProvider;
47 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
51 * Class to perform unit test of {@link TestSimulationRestController}.
53 public class TestSimulationRestController extends CommonParticipantRestServer {
55 private static ControlLoopUpdateListener clUpdateListener;
56 private static final String PARTICIPANTS_ENDPOINT = "participants";
57 private static final String ELEMENTS_ENDPOINT = "elements";
58 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
59 private static final String TOPIC = "my-topic";
60 static CommonTestData commonTestData = new CommonTestData();
67 public static void setUpBeforeClass() throws Exception {
68 CommonParticipantRestServer.setUpBeforeClass();
69 clUpdateListener = new ControlLoopUpdateListener(
70 SimulationHandler.getInstance()
71 .getSimulationProvider()
73 .getParticipantHandler());
74 ParticipantControlLoopUpdate participantControlLoopUpdateMsg =
75 TestListenerUtils.createControlLoopUpdateMsg();
76 participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
77 clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
81 public static void teardownAfterClass() {
82 CommonParticipantRestServer.teardownAfterClass();
86 public void testSwagger() throws Exception {
87 super.testSwagger(ELEMENTS_ENDPOINT);
91 public void testQuery_Unauthorized() throws Exception {
92 Invocation.Builder invocationBuilder = super.sendNoAuthRequest(ELEMENTS_ENDPOINT);
93 Response rawresp = invocationBuilder.buildGet().invoke();
94 assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
98 public void testQueryParticipants() throws Exception {
99 Participant participant = new Participant();
100 ToscaConceptIdentifier participantId = CommonTestData.getParticipantId();
101 participant.setDefinition(participantId);
102 participant.setName(participantId.getName());
103 participant.setVersion(participantId.getVersion());
105 // GET REST call for querying the participants
106 Invocation.Builder invocationBuilder =
107 super.sendRequest(PARTICIPANTS_ENDPOINT + "/" + participant.getKey().getName()
108 + "/" + participant.getVersion());
110 Response rawresp = invocationBuilder.buildGet().invoke();
111 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
112 List<Participant> returnValue = rawresp.readEntity(new GenericType<List<Participant>>() {});
113 assertNotNull(returnValue);
114 assertThat(returnValue).hasSize(1);
115 // Verify the result of GET participants with what is stored
116 assertEquals(participant.getDefinition(), returnValue.get(0).getDefinition());
120 public void testQueryControlLoopElements() throws Exception {
121 // GET REST call for querying the controlLoop elements
122 Invocation.Builder invocationBuilder = super.sendRequest(ELEMENTS_ENDPOINT + "/"
123 + "PMSHInstance0" + "/" + "1.0.0");
125 Response rawresp = invocationBuilder.buildGet().invoke();
126 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
127 List<ControlLoopElement> returnValue = rawresp.readEntity(new GenericType<List<ControlLoopElement>>() {});
128 assertNotNull(returnValue);
129 // Verify the result of GET controlloop elements with what is stored
130 assertThat(returnValue).hasSize(4);
131 assertEquals("org.onap.PM_Subscription_Handler", returnValue.get(0).getDefinition().getName());
135 public void testUpdateParticipant() throws Exception {
136 SimulationProvider provider = SimulationHandler.getInstance().getSimulationProvider();
137 List<Participant> participants = provider.getParticipants(CommonTestData.getParticipantId().getName(),
138 CommonTestData.getParticipantId().getVersion());
139 assertEquals(ParticipantState.UNKNOWN, participants.get(0).getParticipantState());
140 // Change the state of the participant to PASSIVE from UNKNOWN
141 participants.get(0).setParticipantState(ParticipantState.PASSIVE);
142 Entity<Participant> entParticipant = Entity.entity(participants.get(0), MediaType.APPLICATION_JSON);
144 // PUT REST call for updating Participant
145 Invocation.Builder invocationBuilder = sendRequest(PARTICIPANTS_ENDPOINT);
146 Response rawresp = invocationBuilder.put(entParticipant);
147 TypedSimpleResponse<Participant> resp = rawresp.readEntity(TypedSimpleResponse.class);
148 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
149 assertNotNull(resp.getResponse());
150 // Verify the response and state returned by PUT REST call for updating participants
151 assertThat(resp.toString()).contains("definition={name=org.onap.PM_CDS_Blueprint, version=1.0.0}");
152 assertThat(resp.toString()).contains("participantState=PASSIVE");
156 public void testUpdateControlLoopElement() throws Exception {
157 ControlLoop controlLoop = TestListenerUtils.createControlLoop();
158 SimulationProvider provider = SimulationHandler.getInstance().getSimulationProvider();
159 List<ControlLoopElement> controlLoopElements = provider.getControlLoopElements(
160 controlLoop.getDefinition().getName(), controlLoop.getDefinition().getVersion());
162 // Check the initial state on the ControlLoopElement, which is UNINITIALISED
163 assertEquals(ControlLoopOrderedState.UNINITIALISED, controlLoopElements.get(0).getOrderedState());
165 // Change the state of the ControlLoopElement to PASSIVE from UNINITIALISED
166 controlLoopElements.get(0).setOrderedState(ControlLoopOrderedState.PASSIVE);
167 Entity<ControlLoopElement> entClElement = Entity.entity(controlLoopElements.get(0), MediaType.APPLICATION_JSON);
169 // PUT REST call for updating ControlLoopElement
170 Invocation.Builder invocationBuilder = sendRequest(ELEMENTS_ENDPOINT);
171 Response rawresp = invocationBuilder.put(entClElement);
172 TypedSimpleResponse<ControlLoopElement> resp = rawresp.readEntity(TypedSimpleResponse.class);
173 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
174 assertNotNull(resp.getResponse());
175 // Verify the response and state returned by PUT REST call for updating participants
176 assertThat(resp.toString()).contains("definition={name=org.onap.PM_Subscription_Handler, version=0.0.0}");
177 assertThat(resp.toString()).contains("orderedState=PASSIVE");