e361ff469b4f8918ba4c04df0a23cf36b40954ca
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.controlloop.runtime.instantiation.rest;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertNull;
28
29 import javax.ws.rs.client.Entity;
30 import javax.ws.rs.client.Invocation;
31 import javax.ws.rs.core.Response;
32 import org.junit.jupiter.api.AfterEach;
33 import org.junit.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.extension.ExtendWith;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
39 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider;
40 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ServiceTemplateProvider;
41 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.ControlLoopPrimedResponse;
42 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand;
43 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse;
44 import org.onap.policy.clamp.controlloop.runtime.instantiation.ControlLoopInstantiationProvider;
45 import org.onap.policy.clamp.controlloop.runtime.instantiation.InstantiationUtils;
46 import org.onap.policy.clamp.controlloop.runtime.main.rest.InstantiationController;
47 import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData;
48 import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.boot.test.context.SpringBootTest;
53 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
54 import org.springframework.boot.web.server.LocalServerPort;
55 import org.springframework.test.context.TestPropertySource;
56 import org.springframework.test.context.junit.jupiter.SpringExtension;
57
58 /**
59  * Class to perform unit test of {@link InstantiationController}}.
60  *
61  */
62 @ExtendWith(SpringExtension.class)
63 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
64 @TestPropertySource(locations = {"classpath:application_test.properties"})
65 class InstantiationControllerTest extends CommonRestController {
66
67     private static final String CL_INSTANTIATION_CREATE_JSON = "src/test/resources/rest/controlloops/ControlLoops.json";
68
69     private static final String CL_INSTANTIATION_UPDATE_JSON =
70             "src/test/resources/rest/controlloops/ControlLoopsUpdate.json";
71
72     private static final String CL_INSTANTIATION_CHANGE_STATE_JSON =
73             "src/test/resources/rest/controlloops/PassiveCommand.json";
74
75     private static final String TOSCA_TEMPLATE_YAML =
76             "src/test/resources/rest/servicetemplates/pmsh_multiple_cl_tosca.yaml";
77
78     private static final String INSTANTIATION_ENDPOINT = "instantiation";
79
80     private static final String INSTANTIATION_COMMAND_ENDPOINT = "instantiation/command";
81
82     private static final String PRIMING_ENDPOINT = "controlLoopPriming";
83
84     private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
85
86     @Autowired
87     private ServiceTemplateProvider serviceTemplateProvider;
88
89     @Autowired
90     private ControlLoopInstantiationProvider instantiationProvider;
91
92     @Autowired
93     private ParticipantProvider participantProvider;
94
95     @LocalServerPort
96     private int randomServerPort;
97
98     @BeforeAll
99     public static void setUpBeforeClass() throws Exception {
100         serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_TEMPLATE_YAML);
101     }
102
103     @BeforeEach
104     public void populateDb() throws Exception {
105         createEntryInDB();
106     }
107
108     @BeforeEach
109     public void setUpPort() {
110         super.setHttpPrefix(randomServerPort);
111     }
112
113     @AfterEach
114     public void cleanDatabase() throws Exception {
115         deleteEntryInDB(serviceTemplate.getName(), serviceTemplate.getVersion());
116     }
117
118     @Test
119     void testSwagger() throws Exception {
120         super.testSwagger(INSTANTIATION_ENDPOINT);
121     }
122
123     @Test
124     void testCreate_Unauthorized() throws Exception {
125         ControlLoops controlLoops =
126                 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Unauthorized");
127
128         assertUnauthorizedPost(INSTANTIATION_ENDPOINT, Entity.json(controlLoops));
129     }
130
131     @Test
132     void testQuery_Unauthorized() throws Exception {
133         assertUnauthorizedGet(INSTANTIATION_ENDPOINT);
134     }
135
136     @Test
137     void testUpdate_Unauthorized() throws Exception {
138         ControlLoops controlLoops =
139                 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_UPDATE_JSON, "Unauthorized");
140
141         assertUnauthorizedPut(INSTANTIATION_ENDPOINT, Entity.json(controlLoops));
142     }
143
144     @Test
145     void testDelete_Unauthorized() throws Exception {
146         assertUnauthorizedDelete(INSTANTIATION_ENDPOINT);
147     }
148
149     @Test
150     void testCommand_Unauthorized() throws Exception {
151         InstantiationCommand instantiationCommand = InstantiationUtils
152                 .getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON, "Unauthorized");
153
154         assertUnauthorizedPut(INSTANTIATION_COMMAND_ENDPOINT, Entity.json(instantiationCommand));
155     }
156
157     @Test
158     void testCreate() throws Exception {
159
160         ControlLoops controlLoopsFromRsc =
161                 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Create");
162
163         Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT);
164         Response resp = invocationBuilder.post(Entity.json(controlLoopsFromRsc));
165         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
166         InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
167         InstantiationUtils.assertInstantiationResponse(instResponse, controlLoopsFromRsc);
168
169         for (ControlLoop controlLoopFromRsc : controlLoopsFromRsc.getControlLoopList()) {
170             ControlLoops controlLoopsFromDb = instantiationProvider
171                     .getControlLoops(controlLoopFromRsc.getKey().getName(), controlLoopFromRsc.getKey().getVersion());
172
173             assertNotNull(controlLoopsFromDb);
174             assertThat(controlLoopsFromDb.getControlLoopList()).hasSize(1);
175             assertEquals(controlLoopFromRsc, controlLoopsFromDb.getControlLoopList().get(0));
176         }
177
178         invocationBuilder =
179                 super.sendRequest(PRIMING_ENDPOINT + "?name=" + "PMSHInstance0Create" + "&version=" + "1.0.1");
180         Response rawresp = invocationBuilder.buildGet().invoke();
181         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
182         ControlLoopPrimedResponse primResponse = rawresp.readEntity(ControlLoopPrimedResponse.class);
183         assertEquals(false, primResponse.getPrimedControlLoopsList().get(0).isPrimed());
184     }
185
186     @Test
187     void testCreateBadRequest() throws Exception {
188
189         ControlLoops controlLoopsFromRsc =
190                 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "CreateBadRequest");
191
192         Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT);
193         Response resp = invocationBuilder.post(Entity.json(controlLoopsFromRsc));
194         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
195
196         // testing Bad Request: CL already defined
197         resp = invocationBuilder.post(Entity.json(controlLoopsFromRsc));
198         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
199         InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
200         assertNotNull(instResponse.getErrorDetails());
201         assertNull(instResponse.getAffectedControlLoops());
202     }
203
204     @Test
205     void testQuery_NoResultWithThisName() throws Exception {
206         Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT + "?name=noResultWithThisName");
207         Response rawresp = invocationBuilder.buildGet().invoke();
208         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
209         ControlLoops resp = rawresp.readEntity(ControlLoops.class);
210         assertThat(resp.getControlLoopList()).isEmpty();
211     }
212
213     @Test
214     void testQuery() throws Exception {
215
216         var controlLoops = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Query");
217         instantiationProvider.createControlLoops(controlLoops);
218
219         for (ControlLoop controlLoopFromRsc : controlLoops.getControlLoopList()) {
220             Invocation.Builder invocationBuilder =
221                     super.sendRequest(INSTANTIATION_ENDPOINT + "?name=" + controlLoopFromRsc.getKey().getName());
222             Response rawresp = invocationBuilder.buildGet().invoke();
223             assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
224             ControlLoops controlLoopsQuery = rawresp.readEntity(ControlLoops.class);
225             assertNotNull(controlLoopsQuery);
226             assertThat(controlLoopsQuery.getControlLoopList()).hasSize(1);
227             assertEquals(controlLoopFromRsc, controlLoopsQuery.getControlLoopList().get(0));
228         }
229     }
230
231     @Test
232     void testUpdate() throws Exception {
233
234         ControlLoops controlLoopsCreate =
235                 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Update");
236
237         var controlLoops = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_UPDATE_JSON, "Update");
238         instantiationProvider.createControlLoops(controlLoopsCreate);
239
240         Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT);
241         Response resp = invocationBuilder.put(Entity.json(controlLoops));
242         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
243
244         InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
245         InstantiationUtils.assertInstantiationResponse(instResponse, controlLoops);
246
247         for (ControlLoop controlLoopUpdate : controlLoops.getControlLoopList()) {
248             ControlLoops controlLoopsFromDb = instantiationProvider
249                     .getControlLoops(controlLoopUpdate.getKey().getName(), controlLoopUpdate.getKey().getVersion());
250
251             assertNotNull(controlLoopsFromDb);
252             assertThat(controlLoopsFromDb.getControlLoopList()).hasSize(1);
253             assertEquals(controlLoopUpdate, controlLoopsFromDb.getControlLoopList().get(0));
254         }
255     }
256
257     @Test
258     void testDelete_NoResultWithThisName() throws Exception {
259         Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT + "?name=noResultWithThisName");
260         Response resp = invocationBuilder.delete();
261         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), resp.getStatus());
262         InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
263         assertNotNull(instResponse.getErrorDetails());
264         assertNull(instResponse.getAffectedControlLoops());
265     }
266
267     @Test
268     void testDelete() throws Exception {
269
270         ControlLoops controlLoopsFromRsc =
271                 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Delete");
272
273         instantiationProvider.createControlLoops(controlLoopsFromRsc);
274
275         for (ControlLoop controlLoopFromRsc : controlLoopsFromRsc.getControlLoopList()) {
276             Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT + "?name="
277                     + controlLoopFromRsc.getKey().getName() + "&version=" + controlLoopFromRsc.getKey().getVersion());
278             Response resp = invocationBuilder.delete();
279             assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
280             InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
281             InstantiationUtils.assertInstantiationResponse(instResponse, controlLoopFromRsc);
282
283             ControlLoops controlLoopsFromDb = instantiationProvider
284                     .getControlLoops(controlLoopFromRsc.getKey().getName(), controlLoopFromRsc.getKey().getVersion());
285             assertThat(controlLoopsFromDb.getControlLoopList()).isEmpty();
286         }
287     }
288
289     @Test
290     void testDeleteBadRequest() throws Exception {
291
292         ControlLoops controlLoopsFromRsc =
293                 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "DelBadRequest");
294
295         instantiationProvider.createControlLoops(controlLoopsFromRsc);
296
297         for (ControlLoop controlLoopFromRsc : controlLoopsFromRsc.getControlLoopList()) {
298             Invocation.Builder invocationBuilder =
299                     super.sendRequest(INSTANTIATION_ENDPOINT + "?name=" + controlLoopFromRsc.getKey().getName());
300             Response resp = invocationBuilder.delete();
301             // should be BAD_REQUEST
302             assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), resp.getStatus());
303         }
304     }
305
306     @Test
307     void testCommand_NotFound1() throws Exception {
308         Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_COMMAND_ENDPOINT);
309         Response resp = invocationBuilder.put(Entity.json(new InstantiationCommand()));
310         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
311     }
312
313     @Test
314     void testCommand_NotFound2() throws Exception {
315         InstantiationCommand command =
316                 InstantiationUtils.getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON, "Command");
317         command.setOrderedState(null);
318
319         Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_COMMAND_ENDPOINT);
320         Response resp = invocationBuilder.put(Entity.json(command));
321         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
322     }
323
324     @Test
325     void testCommand() throws Exception {
326         var controlLoops = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Command");
327         instantiationProvider.createControlLoops(controlLoops);
328
329         participantProvider.createParticipants(CommonTestData.createParticipants());
330
331         InstantiationCommand command =
332                 InstantiationUtils.getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON, "Command");
333
334         Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_COMMAND_ENDPOINT);
335         Response resp = invocationBuilder.put(Entity.json(command));
336         assertEquals(Response.Status.ACCEPTED.getStatusCode(), resp.getStatus());
337         InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
338         InstantiationUtils.assertInstantiationResponse(instResponse, command);
339
340         // check passive state on DB
341         for (ToscaConceptIdentifier toscaConceptIdentifier : command.getControlLoopIdentifierList()) {
342             ControlLoops controlLoopsGet = instantiationProvider.getControlLoops(toscaConceptIdentifier.getName(),
343                     toscaConceptIdentifier.getVersion());
344             assertThat(controlLoopsGet.getControlLoopList()).hasSize(1);
345             assertEquals(command.getOrderedState(), controlLoopsGet.getControlLoopList().get(0).getOrderedState());
346         }
347     }
348
349     private synchronized void deleteEntryInDB(String name, String version) throws Exception {
350         if (!serviceTemplateProvider.getServiceTemplateList(null, null).isEmpty()) {
351             serviceTemplateProvider.deleteServiceTemplate(name, version);
352         }
353     }
354
355     private synchronized void createEntryInDB() throws Exception {
356         deleteEntryInDB(serviceTemplate.getName(), serviceTemplate.getVersion());
357         serviceTemplateProvider.createServiceTemplate(serviceTemplate);
358     }
359 }