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.runtime.instantiation.rest;
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.assertNull;
28 import javax.ws.rs.client.Entity;
29 import javax.ws.rs.client.Invocation;
30 import javax.ws.rs.core.Response;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.extension.ExtendWith;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
36 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand;
37 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse;
38 import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider;
39 import org.onap.policy.clamp.controlloop.runtime.instantiation.ControlLoopInstantiationProvider;
40 import org.onap.policy.clamp.controlloop.runtime.instantiation.InstantiationUtils;
41 import org.onap.policy.clamp.controlloop.runtime.main.rest.InstantiationController;
42 import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.boot.test.context.SpringBootTest;
46 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
47 import org.springframework.boot.web.server.LocalServerPort;
48 import org.springframework.test.context.TestPropertySource;
49 import org.springframework.test.context.junit.jupiter.SpringExtension;
52 * Class to perform unit test of {@link InstantiationController}}.
55 @ExtendWith(SpringExtension.class)
56 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
57 @TestPropertySource(locations = {"classpath:application_test.properties"})
58 class InstantiationControllerTest extends CommonRestController {
60 private static final String CL_INSTANTIATION_CREATE_JSON = "src/test/resources/rest/controlloops/ControlLoops.json";
62 private static final String CL_INSTANTIATION_UPDATE_JSON =
63 "src/test/resources/rest/controlloops/ControlLoopsUpdate.json";
65 private static final String CL_INSTANTIATION_CHANGE_STATE_JSON =
66 "src/test/resources/rest/controlloops/PassiveCommand.json";
68 private static final String TOSCA_TEMPLATE_YAML =
69 "src/test/resources/rest/servicetemplates/pmsh_multiple_cl_tosca.yaml";
71 private static final String INSTANTIATION_ENDPOINT = "instantiation";
73 private static final String INSTANTIATION_COMMAND_ENDPOINT = "instantiation/command";
76 private ControlLoopInstantiationProvider instantiationProvider;
79 private CommissioningProvider commissioningProvider;
82 private int randomServerPort;
85 * starts Main and inserts a commissioning template.
87 * @throws Exception if an error occurs
90 public void setUpBeforeClass() throws Exception {
91 // to validate control Loop, it needs to define ToscaServiceTemplate
92 InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, commissioningProvider);
96 public void setUpPort() {
97 super.setHttpPrefix(randomServerPort);
101 void testSwagger() throws Exception {
102 super.testSwagger(INSTANTIATION_ENDPOINT);
106 void testCreate_Unauthorized() throws Exception {
107 ControlLoops controlLoops =
108 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Unauthorized");
110 assertUnauthorizedPost(INSTANTIATION_ENDPOINT, Entity.json(controlLoops));
114 void testQuery_Unauthorized() throws Exception {
115 assertUnauthorizedGet(INSTANTIATION_ENDPOINT);
119 void testUpdate_Unauthorized() throws Exception {
120 ControlLoops controlLoops =
121 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_UPDATE_JSON, "Unauthorized");
123 assertUnauthorizedPut(INSTANTIATION_ENDPOINT, Entity.json(controlLoops));
127 void testDelete_Unauthorized() throws Exception {
128 assertUnauthorizedDelete(INSTANTIATION_ENDPOINT);
132 void testCommand_Unauthorized() throws Exception {
133 InstantiationCommand instantiationCommand = InstantiationUtils
134 .getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON, "Unauthorized");
136 assertUnauthorizedPut(INSTANTIATION_COMMAND_ENDPOINT, Entity.json(instantiationCommand));
140 void testCreate() throws Exception {
141 ControlLoops controlLoopsFromRsc =
142 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Create");
144 Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT);
145 Response resp = invocationBuilder.post(Entity.json(controlLoopsFromRsc));
146 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
147 InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
148 InstantiationUtils.assertInstantiationResponse(instResponse, controlLoopsFromRsc);
150 for (ControlLoop controlLoopFromRsc : controlLoopsFromRsc.getControlLoopList()) {
151 ControlLoops controlLoopsFromDb = instantiationProvider
152 .getControlLoops(controlLoopFromRsc.getKey().getName(), controlLoopFromRsc.getKey().getVersion());
154 assertNotNull(controlLoopsFromDb);
155 assertThat(controlLoopsFromDb.getControlLoopList()).hasSize(1);
156 assertEquals(controlLoopFromRsc, controlLoopsFromDb.getControlLoopList().get(0));
161 void testCreateBadRequest() throws Exception {
162 ControlLoops controlLoopsFromRsc =
163 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "CreateBadRequest");
165 Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT);
166 Response resp = invocationBuilder.post(Entity.json(controlLoopsFromRsc));
167 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
169 // testing Bad Request: CL already defined
170 resp = invocationBuilder.post(Entity.json(controlLoopsFromRsc));
171 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
172 InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
173 assertNotNull(instResponse.getErrorDetails());
174 assertNull(instResponse.getAffectedControlLoops());
178 void testQuery_NoResultWithThisName() throws Exception {
179 Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT + "?name=noResultWithThisName");
180 Response rawresp = invocationBuilder.buildGet().invoke();
181 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
182 ControlLoops resp = rawresp.readEntity(ControlLoops.class);
183 assertThat(resp.getControlLoopList()).isEmpty();
187 void testQuery() throws Exception {
188 // inserts a ControlLoops to DB
189 var controlLoops = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Query");
190 instantiationProvider.createControlLoops(controlLoops);
192 for (ControlLoop controlLoopFromRsc : controlLoops.getControlLoopList()) {
193 Invocation.Builder invocationBuilder =
194 super.sendRequest(INSTANTIATION_ENDPOINT + "?name=" + controlLoopFromRsc.getKey().getName());
195 Response rawresp = invocationBuilder.buildGet().invoke();
196 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
197 ControlLoops controlLoopsQuery = rawresp.readEntity(ControlLoops.class);
198 assertNotNull(controlLoopsQuery);
199 assertThat(controlLoopsQuery.getControlLoopList()).hasSize(1);
200 assertEquals(controlLoopFromRsc, controlLoopsQuery.getControlLoopList().get(0));
205 void testUpdate() throws Exception {
206 ControlLoops controlLoopsCreate =
207 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Update");
209 var controlLoops = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_UPDATE_JSON, "Update");
210 instantiationProvider.createControlLoops(controlLoopsCreate);
212 Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT);
213 Response resp = invocationBuilder.put(Entity.json(controlLoops));
214 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
216 InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
217 InstantiationUtils.assertInstantiationResponse(instResponse, controlLoops);
219 for (ControlLoop controlLoopUpdate : controlLoops.getControlLoopList()) {
220 ControlLoops controlLoopsFromDb = instantiationProvider
221 .getControlLoops(controlLoopUpdate.getKey().getName(), controlLoopUpdate.getKey().getVersion());
223 assertNotNull(controlLoopsFromDb);
224 assertThat(controlLoopsFromDb.getControlLoopList()).hasSize(1);
225 assertEquals(controlLoopUpdate, controlLoopsFromDb.getControlLoopList().get(0));
230 void testDelete_NoResultWithThisName() throws Exception {
231 Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT + "?name=noResultWithThisName");
232 Response resp = invocationBuilder.delete();
233 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), resp.getStatus());
234 InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
235 assertNotNull(instResponse.getErrorDetails());
236 assertNull(instResponse.getAffectedControlLoops());
240 void testDelete() throws Exception {
241 ControlLoops controlLoopsFromRsc =
242 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Delete");
244 instantiationProvider.createControlLoops(controlLoopsFromRsc);
246 for (ControlLoop controlLoopFromRsc : controlLoopsFromRsc.getControlLoopList()) {
247 Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT + "?name="
248 + controlLoopFromRsc.getKey().getName() + "&version=" + controlLoopFromRsc.getKey().getVersion());
249 Response resp = invocationBuilder.delete();
250 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
251 InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
252 InstantiationUtils.assertInstantiationResponse(instResponse, controlLoopFromRsc);
254 ControlLoops controlLoopsFromDb = instantiationProvider
255 .getControlLoops(controlLoopFromRsc.getKey().getName(), controlLoopFromRsc.getKey().getVersion());
256 assertThat(controlLoopsFromDb.getControlLoopList()).isEmpty();
261 void testDeleteBadRequest() throws Exception {
262 ControlLoops controlLoopsFromRsc =
263 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "DelBadRequest");
265 instantiationProvider.createControlLoops(controlLoopsFromRsc);
267 for (ControlLoop controlLoopFromRsc : controlLoopsFromRsc.getControlLoopList()) {
268 Invocation.Builder invocationBuilder =
269 super.sendRequest(INSTANTIATION_ENDPOINT + "?name=" + controlLoopFromRsc.getKey().getName());
270 Response resp = invocationBuilder.delete();
271 // should be BAD_REQUEST
272 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), resp.getStatus());
277 void testCommand_NotFound1() throws Exception {
278 Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_COMMAND_ENDPOINT);
279 Response resp = invocationBuilder.put(Entity.json(new InstantiationCommand()));
280 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
284 void testCommand_NotFound2() throws Exception {
285 InstantiationCommand command =
286 InstantiationUtils.getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON, "Command");
287 command.setOrderedState(null);
289 Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_COMMAND_ENDPOINT);
290 Response resp = invocationBuilder.put(Entity.json(command));
291 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
295 void testCommand() throws Exception {
296 var controlLoops = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Command");
297 instantiationProvider.createControlLoops(controlLoops);
299 InstantiationCommand command =
300 InstantiationUtils.getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON, "Command");
302 Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_COMMAND_ENDPOINT);
303 Response resp = invocationBuilder.put(Entity.json(command));
304 assertEquals(Response.Status.ACCEPTED.getStatusCode(), resp.getStatus());
305 InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
306 InstantiationUtils.assertInstantiationResponse(instResponse, command);
308 // check passive state on DB
309 for (ToscaConceptIdentifier toscaConceptIdentifier : command.getControlLoopIdentifierList()) {
310 ControlLoops controlLoopsGet = instantiationProvider.getControlLoops(toscaConceptIdentifier.getName(),
311 toscaConceptIdentifier.getVersion());
312 assertThat(controlLoopsGet.getControlLoopList()).hasSize(1);
313 assertEquals(command.getOrderedState(), controlLoopsGet.getControlLoopList().get(0).getOrderedState());