6c9674dec15ae3e1bf825c353fbae08b53ec65db
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.runtime.instantiation.rest;
22
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;
27
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;
50
51 /**
52  * Class to perform unit test of {@link InstantiationController}}.
53  *
54  */
55 @ExtendWith(SpringExtension.class)
56 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
57 @TestPropertySource(locations = {"classpath:application_test.properties"})
58 class InstantiationControllerTest extends CommonRestController {
59
60     private static final String CL_INSTANTIATION_CREATE_JSON = "src/test/resources/rest/controlloops/ControlLoops.json";
61
62     private static final String CL_INSTANTIATION_UPDATE_JSON =
63             "src/test/resources/rest/controlloops/ControlLoopsUpdate.json";
64
65     private static final String CL_INSTANTIATION_CHANGE_STATE_JSON =
66             "src/test/resources/rest/controlloops/PassiveCommand.json";
67
68     private static final String TOSCA_TEMPLATE_YAML =
69             "src/test/resources/rest/servicetemplates/pmsh_multiple_cl_tosca.yaml";
70
71     private static final String INSTANTIATION_ENDPOINT = "instantiation";
72
73     private static final String INSTANTIATION_COMMAND_ENDPOINT = "instantiation/command";
74
75     @Autowired
76     private ControlLoopInstantiationProvider instantiationProvider;
77
78     @Autowired
79     private CommissioningProvider commissioningProvider;
80
81     @LocalServerPort
82     private int randomServerPort;
83
84     /**
85      * starts Main and inserts a commissioning template.
86      *
87      * @throws Exception if an error occurs
88      */
89     @BeforeEach
90     public void setUpBeforeClass() throws Exception {
91         // to validate control Loop, it needs to define ToscaServiceTemplate
92         InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, commissioningProvider);
93     }
94
95     @BeforeEach
96     public void setUpPort() {
97         super.setHttpPrefix(randomServerPort);
98     }
99
100     @Test
101     void testSwagger() throws Exception {
102         super.testSwagger(INSTANTIATION_ENDPOINT);
103     }
104
105     @Test
106     void testCreate_Unauthorized() throws Exception {
107         ControlLoops controlLoops =
108                 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Unauthorized");
109
110         assertUnauthorizedPost(INSTANTIATION_ENDPOINT, Entity.json(controlLoops));
111     }
112
113     @Test
114     void testQuery_Unauthorized() throws Exception {
115         assertUnauthorizedGet(INSTANTIATION_ENDPOINT);
116     }
117
118     @Test
119     void testUpdate_Unauthorized() throws Exception {
120         ControlLoops controlLoops =
121                 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_UPDATE_JSON, "Unauthorized");
122
123         assertUnauthorizedPut(INSTANTIATION_ENDPOINT, Entity.json(controlLoops));
124     }
125
126     @Test
127     void testDelete_Unauthorized() throws Exception {
128         assertUnauthorizedDelete(INSTANTIATION_ENDPOINT);
129     }
130
131     @Test
132     void testCommand_Unauthorized() throws Exception {
133         InstantiationCommand instantiationCommand = InstantiationUtils
134                 .getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON, "Unauthorized");
135
136         assertUnauthorizedPut(INSTANTIATION_COMMAND_ENDPOINT, Entity.json(instantiationCommand));
137     }
138
139     @Test
140     void testCreate() throws Exception {
141         ControlLoops controlLoopsFromRsc =
142                 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Create");
143
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);
149
150         for (ControlLoop controlLoopFromRsc : controlLoopsFromRsc.getControlLoopList()) {
151             ControlLoops controlLoopsFromDb = instantiationProvider
152                     .getControlLoops(controlLoopFromRsc.getKey().getName(), controlLoopFromRsc.getKey().getVersion());
153
154             assertNotNull(controlLoopsFromDb);
155             assertThat(controlLoopsFromDb.getControlLoopList()).hasSize(1);
156             assertEquals(controlLoopFromRsc, controlLoopsFromDb.getControlLoopList().get(0));
157         }
158     }
159
160     @Test
161     void testCreateBadRequest() throws Exception {
162         ControlLoops controlLoopsFromRsc =
163                 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "CreateBadRequest");
164
165         Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT);
166         Response resp = invocationBuilder.post(Entity.json(controlLoopsFromRsc));
167         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
168
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());
175     }
176
177     @Test
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();
184     }
185
186     @Test
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);
191
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));
201         }
202     }
203
204     @Test
205     void testUpdate() throws Exception {
206         ControlLoops controlLoopsCreate =
207                 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Update");
208
209         var controlLoops = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_UPDATE_JSON, "Update");
210         instantiationProvider.createControlLoops(controlLoopsCreate);
211
212         Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT);
213         Response resp = invocationBuilder.put(Entity.json(controlLoops));
214         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
215
216         InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
217         InstantiationUtils.assertInstantiationResponse(instResponse, controlLoops);
218
219         for (ControlLoop controlLoopUpdate : controlLoops.getControlLoopList()) {
220             ControlLoops controlLoopsFromDb = instantiationProvider
221                     .getControlLoops(controlLoopUpdate.getKey().getName(), controlLoopUpdate.getKey().getVersion());
222
223             assertNotNull(controlLoopsFromDb);
224             assertThat(controlLoopsFromDb.getControlLoopList()).hasSize(1);
225             assertEquals(controlLoopUpdate, controlLoopsFromDb.getControlLoopList().get(0));
226         }
227     }
228
229     @Test
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());
237     }
238
239     @Test
240     void testDelete() throws Exception {
241         ControlLoops controlLoopsFromRsc =
242                 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Delete");
243
244         instantiationProvider.createControlLoops(controlLoopsFromRsc);
245
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);
253
254             ControlLoops controlLoopsFromDb = instantiationProvider
255                     .getControlLoops(controlLoopFromRsc.getKey().getName(), controlLoopFromRsc.getKey().getVersion());
256             assertThat(controlLoopsFromDb.getControlLoopList()).isEmpty();
257         }
258     }
259
260     @Test
261     void testDeleteBadRequest() throws Exception {
262         ControlLoops controlLoopsFromRsc =
263                 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "DelBadRequest");
264
265         instantiationProvider.createControlLoops(controlLoopsFromRsc);
266
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());
273         }
274     }
275
276     @Test
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());
281     }
282
283     @Test
284     void testCommand_NotFound2() throws Exception {
285         InstantiationCommand command =
286                 InstantiationUtils.getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON, "Command");
287         command.setOrderedState(null);
288
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());
292     }
293
294     @Test
295     void testCommand() throws Exception {
296         var controlLoops = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Command");
297         instantiationProvider.createControlLoops(controlLoops);
298
299         InstantiationCommand command =
300                 InstantiationUtils.getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON, "Command");
301
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);
307
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());
314         }
315     }
316 }