2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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.junit.Assert.assertEquals;
25 import java.util.ArrayList;
26 import java.util.List;
27 import javax.ws.rs.client.Entity;
28 import javax.ws.rs.client.Invocation;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
39 import org.onap.policy.clamp.controlloop.models.messages.rest.TypedSimpleResponse;
40 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.CommonTestData;
41 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
45 * Class to perform unit test of {@link TestSimulationRestController}.
47 public class TestSimulationRestController extends CommonParticipantRestServer {
48 private static final String PARTICIPANTS_ENDPOINT = "participants";
49 private static final String ELEMENTS_ENDPOINT = "elements";
50 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
51 private static final String TOPIC = "my-topic";
52 static CommonTestData commonTestData = new CommonTestData();
53 private static RestController restController;
56 * Setup before class, instantiate SimulationProvider.
60 public static void setUpBeforeClass() throws Exception {
61 CommonParticipantRestServer.setUpBeforeClass();
65 public static void teardownAfterClass() {
66 CommonParticipantRestServer.teardownAfterClass();
70 public void testSwagger() throws Exception {
71 super.testSwagger(ELEMENTS_ENDPOINT);
75 public void testQuery_Unauthorized() throws Exception {
76 Invocation.Builder invocationBuilder = super.sendNoAuthRequest(ELEMENTS_ENDPOINT);
77 Response rawresp = invocationBuilder.buildGet().invoke();
78 assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
82 public void testQueryParticipants() throws Exception {
83 Participant participant = new Participant();
84 ToscaConceptIdentifier participantId = CommonTestData.getParticipantId();
85 participant.setDefinition(participantId);
86 participant.setName(participantId.getName());
87 participant.setVersion(participantId.getVersion());
89 // GET REST call for querying the participants
90 Invocation.Builder invocationBuilder =
91 super.sendRequest(PARTICIPANTS_ENDPOINT + "/" + participant.getKey().getName()
92 + "/" + participant.getVersion());
94 Response rawresp = invocationBuilder.buildGet().invoke();
95 // Response is not OK, as handling is not done
96 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rawresp.getStatus());
100 public void testQueryControlLoopElements() throws Exception {
101 // GET REST call for querying the controlLoop elements
102 Invocation.Builder invocationBuilder =
103 super.sendRequest(ELEMENTS_ENDPOINT + "/" + "PMSHInstance0" + "/" + "1.0.0");
105 Response rawresp = invocationBuilder.buildGet().invoke();
106 // Response is not OK, as handling is not done
107 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rawresp.getStatus());
111 public void testUpdateParticipant() throws Exception {
112 Participant participant = new Participant();
113 ToscaConceptIdentifier participantId = CommonTestData.getParticipantId();
114 participant.setDefinition(participantId);
115 participant.setName(participantId.getName());
116 participant.setVersion(participantId.getVersion());
118 List<Participant> participants = new ArrayList<>();
119 participants.add(participant);
120 assertEquals(ParticipantState.UNKNOWN, participants.get(0).getParticipantState());
121 // Change the state of the participant to PASSIVE from UNKNOWN
122 participants.get(0).setParticipantState(ParticipantState.PASSIVE);
123 Entity<Participant> entParticipant = Entity.entity(participants.get(0), MediaType.APPLICATION_JSON);
125 // PUT REST call for updating Participant
126 Invocation.Builder invocationBuilder = sendRequest(PARTICIPANTS_ENDPOINT);
127 Response rawresp = invocationBuilder.put(entParticipant);
128 TypedSimpleResponse<Participant> resp = rawresp.readEntity(TypedSimpleResponse.class);
129 // Response is not OK, as handling is not done
130 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rawresp.getStatus());
134 public void testUpdateControlLoopElement() throws Exception {
135 ControlLoop controlLoop = TestListenerUtils.createControlLoop();
136 List<ControlLoopElement> controlLoopElements = controlLoop.getElements();
138 // Check the initial state on the ControlLoopElement, which is UNINITIALISED
139 assertEquals(ControlLoopOrderedState.UNINITIALISED, controlLoopElements.get(0).getOrderedState());
141 // Change the state of the ControlLoopElement to PASSIVE from UNINITIALISED
142 controlLoopElements.get(0).setOrderedState(ControlLoopOrderedState.PASSIVE);
143 Entity<ControlLoopElement> entClElement = Entity.entity(controlLoopElements.get(0), MediaType.APPLICATION_JSON);
145 // PUT REST call for updating ControlLoopElement
146 Invocation.Builder invocationBuilder = sendRequest(ELEMENTS_ENDPOINT);
147 Response rawresp = invocationBuilder.put(entClElement);
148 TypedSimpleResponse<ControlLoopElement> resp = rawresp.readEntity(TypedSimpleResponse.class);
149 // Response is not OK, as handling is not done
150 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rawresp.getStatus());