7cdc8a9751e7dfbed41551fa9be771b2a0bb901f
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 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.acm.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 import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_SERVICE_TEMPLATE_YAML;
29
30 import javax.ws.rs.client.Entity;
31 import javax.ws.rs.client.Invocation;
32 import javax.ws.rs.core.Response;
33 import org.junit.jupiter.api.AfterEach;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.onap.policy.clamp.acm.runtime.instantiation.AutomationCompositionInstantiationProvider;
39 import org.onap.policy.clamp.acm.runtime.instantiation.InstantiationUtils;
40 import org.onap.policy.clamp.acm.runtime.main.rest.InstantiationController;
41 import org.onap.policy.clamp.acm.runtime.util.CommonTestData;
42 import org.onap.policy.clamp.acm.runtime.util.rest.CommonRestController;
43 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
44 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
45 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationCommand;
46 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationResponse;
47 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
48 import org.onap.policy.clamp.models.acm.persistence.provider.ServiceTemplateProvider;
49 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionRepository;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import org.springframework.boot.test.context.SpringBootTest;
54 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
55 import org.springframework.boot.web.server.LocalServerPort;
56 import org.springframework.test.context.ActiveProfiles;
57 import org.springframework.test.context.junit.jupiter.SpringExtension;
58
59 /**
60  * Class to perform unit test of {@link InstantiationController}}.
61  *
62  */
63 @ExtendWith(SpringExtension.class)
64 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
65 @ActiveProfiles("test")
66 class InstantiationControllerTest extends CommonRestController {
67
68     private static final String ID_NAME = "PMSH_Test_Instance";
69     private static final String ID_VERSION = "1.2.3";
70
71     private static final String AC_INSTANTIATION_CREATE_JSON =
72         "src/test/resources/rest/acm/AutomationCompositions.json";
73
74     private static final String AC_INSTANTIATION_UPDATE_JSON =
75         "src/test/resources/rest/acm/AutomationCompositionsUpdate.json";
76
77     private static final String AC_INSTANTIATION_CHANGE_STATE_JSON = "src/test/resources/rest/acm/PassiveCommand.json";
78
79     private static final String INSTANTIATION_ENDPOINT = "instantiation";
80     private static final String INSTANTIATION_COMMAND_ENDPOINT = "instantiation/command";
81
82     private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
83
84     @Autowired
85     private AutomationCompositionRepository automationCompositionRepository;
86
87     @Autowired
88     private ServiceTemplateProvider serviceTemplateProvider;
89
90     @Autowired
91     private AutomationCompositionInstantiationProvider instantiationProvider;
92
93     @Autowired
94     private ParticipantProvider participantProvider;
95
96     @LocalServerPort
97     private int randomServerPort;
98
99     @BeforeAll
100     public static void setUpBeforeClass() {
101         serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
102     }
103
104     @BeforeEach
105     public void populateDb() throws Exception {
106         createEntryInDB();
107     }
108
109     @BeforeEach
110     public void setUpPort() {
111         super.setHttpPrefix(randomServerPort);
112     }
113
114     @AfterEach
115     public void cleanDatabase() throws Exception {
116         deleteEntryInDB();
117     }
118
119     @Test
120     void testSwagger() {
121         super.testSwagger(INSTANTIATION_ENDPOINT);
122     }
123
124     @Test
125     void testCreate_Unauthorized() throws Exception {
126         AutomationCompositions automationCompositions =
127             InstantiationUtils.getAutomationCompositionsFromResource(AC_INSTANTIATION_CREATE_JSON, "Unauthorized");
128
129         assertUnauthorizedPost(INSTANTIATION_ENDPOINT, Entity.json(automationCompositions));
130     }
131
132     @Test
133     void testQuery_Unauthorized() {
134         assertUnauthorizedGet(INSTANTIATION_ENDPOINT);
135     }
136
137     @Test
138     void testUpdate_Unauthorized() throws Exception {
139         AutomationCompositions automationCompositions =
140             InstantiationUtils.getAutomationCompositionsFromResource(AC_INSTANTIATION_UPDATE_JSON, "Unauthorized");
141
142         assertUnauthorizedPut(INSTANTIATION_ENDPOINT, Entity.json(automationCompositions));
143     }
144
145     @Test
146     void testDelete_Unauthorized() {
147         assertUnauthorizedDelete(INSTANTIATION_ENDPOINT);
148     }
149
150     @Test
151     void testCommand_Unauthorized() throws Exception {
152         InstantiationCommand instantiationCommand =
153             InstantiationUtils.getInstantiationCommandFromResource(AC_INSTANTIATION_CHANGE_STATE_JSON, "Unauthorized");
154
155         assertUnauthorizedPut(INSTANTIATION_COMMAND_ENDPOINT, Entity.json(instantiationCommand));
156     }
157
158     @Test
159     void testCreate() throws Exception {
160
161         AutomationCompositions automationCompositionsFromRsc =
162             InstantiationUtils.getAutomationCompositionsFromResource(AC_INSTANTIATION_CREATE_JSON, "Create");
163
164         Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT);
165         Response resp = invocationBuilder.post(Entity.json(automationCompositionsFromRsc));
166         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
167         InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
168         InstantiationUtils.assertInstantiationResponse(instResponse, automationCompositionsFromRsc);
169
170         for (AutomationComposition automationCompositionFromRsc : automationCompositionsFromRsc
171             .getAutomationCompositionList()) {
172             AutomationCompositions automationCompositionsFromDb = instantiationProvider.getAutomationCompositions(
173                 automationCompositionFromRsc.getKey().getName(), automationCompositionFromRsc.getKey().getVersion());
174
175             assertNotNull(automationCompositionsFromDb);
176             assertThat(automationCompositionsFromDb.getAutomationCompositionList()).hasSize(1);
177             assertEquals(automationCompositionFromRsc,
178                 automationCompositionsFromDb.getAutomationCompositionList().get(0));
179         }
180     }
181
182     @Test
183     void testCreateBadRequest() throws Exception {
184
185         AutomationCompositions automationCompositionsFromRsc =
186             InstantiationUtils.getAutomationCompositionsFromResource(AC_INSTANTIATION_CREATE_JSON, "CreateBadRequest");
187
188         Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT);
189         Response resp = invocationBuilder.post(Entity.json(automationCompositionsFromRsc));
190         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
191
192         // testing Bad Request: AC already defined
193         resp = invocationBuilder.post(Entity.json(automationCompositionsFromRsc));
194         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
195         InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
196         assertNotNull(instResponse.getErrorDetails());
197         assertNull(instResponse.getAffectedAutomationCompositions());
198     }
199
200     @Test
201     void testQuery_NoResultWithThisName() {
202         Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT + "?name=noResultWithThisName");
203         Response rawresp = invocationBuilder.buildGet().invoke();
204         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
205         AutomationCompositions resp = rawresp.readEntity(AutomationCompositions.class);
206         assertThat(resp.getAutomationCompositionList()).isEmpty();
207     }
208
209     @Test
210     void testQuery() throws Exception {
211
212         var automationCompositions =
213             InstantiationUtils.getAutomationCompositionsFromResource(AC_INSTANTIATION_CREATE_JSON, "Query");
214         instantiationProvider.createAutomationCompositions(automationCompositions);
215
216         for (AutomationComposition automationCompositionFromRsc : automationCompositions
217             .getAutomationCompositionList()) {
218             Invocation.Builder invocationBuilder =
219                 super.sendRequest(INSTANTIATION_ENDPOINT + "?name=" + automationCompositionFromRsc.getKey().getName());
220             Response rawresp = invocationBuilder.buildGet().invoke();
221             assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
222             AutomationCompositions automationCompositionsQuery = rawresp.readEntity(AutomationCompositions.class);
223             assertNotNull(automationCompositionsQuery);
224             assertThat(automationCompositionsQuery.getAutomationCompositionList()).hasSize(1);
225             assertEquals(automationCompositionFromRsc,
226                 automationCompositionsQuery.getAutomationCompositionList().get(0));
227         }
228     }
229
230     @Test
231     void testUpdate() throws Exception {
232
233         AutomationCompositions automationCompositionsCreate =
234             InstantiationUtils.getAutomationCompositionsFromResource(AC_INSTANTIATION_CREATE_JSON, "Update");
235
236         var automationCompositions =
237             InstantiationUtils.getAutomationCompositionsFromResource(AC_INSTANTIATION_UPDATE_JSON, "Update");
238         instantiationProvider.createAutomationCompositions(automationCompositionsCreate);
239
240         Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT);
241         Response resp = invocationBuilder.put(Entity.json(automationCompositions));
242         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
243
244         InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
245         InstantiationUtils.assertInstantiationResponse(instResponse, automationCompositions);
246
247         for (AutomationComposition automationCompositionUpdate : automationCompositions
248             .getAutomationCompositionList()) {
249             AutomationCompositions automationCompositionsFromDb = instantiationProvider.getAutomationCompositions(
250                 automationCompositionUpdate.getKey().getName(), automationCompositionUpdate.getKey().getVersion());
251
252             assertNotNull(automationCompositionsFromDb);
253             assertThat(automationCompositionsFromDb.getAutomationCompositionList()).hasSize(1);
254             assertEquals(automationCompositionUpdate,
255                 automationCompositionsFromDb.getAutomationCompositionList().get(0));
256         }
257     }
258
259     @Test
260     void testDelete() throws Exception {
261
262         AutomationCompositions automationCompositionsFromRsc =
263             InstantiationUtils.getAutomationCompositionsFromResource(AC_INSTANTIATION_CREATE_JSON, "Delete");
264
265         instantiationProvider.createAutomationCompositions(automationCompositionsFromRsc);
266
267         for (AutomationComposition automationCompositionFromRsc : automationCompositionsFromRsc
268             .getAutomationCompositionList()) {
269             Invocation.Builder invocationBuilder =
270                 super.sendRequest(INSTANTIATION_ENDPOINT + "?name=" + automationCompositionFromRsc.getKey().getName()
271                     + "&version=" + automationCompositionFromRsc.getKey().getVersion());
272             Response resp = invocationBuilder.delete();
273             assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
274             InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
275             InstantiationUtils.assertInstantiationResponse(instResponse, automationCompositionFromRsc);
276
277             AutomationCompositions automationCompositionsFromDb = instantiationProvider.getAutomationCompositions(
278                 automationCompositionFromRsc.getKey().getName(), automationCompositionFromRsc.getKey().getVersion());
279             assertThat(automationCompositionsFromDb.getAutomationCompositionList()).isEmpty();
280         }
281     }
282
283     @Test
284     void testDeleteBadRequest() throws Exception {
285
286         AutomationCompositions automationCompositionsFromRsc =
287             InstantiationUtils.getAutomationCompositionsFromResource(AC_INSTANTIATION_CREATE_JSON, "DelBadRequest");
288
289         instantiationProvider.createAutomationCompositions(automationCompositionsFromRsc);
290
291         for (AutomationComposition automationCompositionFromRsc : automationCompositionsFromRsc
292             .getAutomationCompositionList()) {
293             Invocation.Builder invocationBuilder =
294                 super.sendRequest(INSTANTIATION_ENDPOINT + "?name=" + automationCompositionFromRsc.getKey().getName());
295             Response resp = invocationBuilder.delete();
296             assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
297         }
298     }
299
300     @Test
301     void testCommand_NotFound1() {
302         Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_COMMAND_ENDPOINT);
303         Response resp = invocationBuilder.put(Entity.json(new InstantiationCommand()));
304         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
305     }
306
307     @Test
308     void testCommand_NotFound2() throws Exception {
309         InstantiationCommand command =
310             InstantiationUtils.getInstantiationCommandFromResource(AC_INSTANTIATION_CHANGE_STATE_JSON, "Command");
311         command.setOrderedState(null);
312
313         Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_COMMAND_ENDPOINT);
314         Response resp = invocationBuilder.put(Entity.json(command));
315         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
316     }
317
318     @Test
319     void testCommand() throws Exception {
320         var automationCompositions =
321             InstantiationUtils.getAutomationCompositionsFromResource(AC_INSTANTIATION_CREATE_JSON, "Command");
322         instantiationProvider.createAutomationCompositions(automationCompositions);
323
324         var participants = CommonTestData.createParticipants();
325         for (var participant : participants) {
326             participantProvider.saveParticipant(participant);
327         }
328
329         InstantiationCommand command =
330             InstantiationUtils.getInstantiationCommandFromResource(AC_INSTANTIATION_CHANGE_STATE_JSON, "Command");
331
332         Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_COMMAND_ENDPOINT);
333         Response resp = invocationBuilder.put(Entity.json(command));
334         assertEquals(Response.Status.ACCEPTED.getStatusCode(), resp.getStatus());
335         InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
336         InstantiationUtils.assertInstantiationResponse(instResponse, command);
337
338         // check passive state on DB
339         for (ToscaConceptIdentifier toscaConceptIdentifier : command.getAutomationCompositionIdentifierList()) {
340             AutomationCompositions automationCompositionsGet = instantiationProvider
341                 .getAutomationCompositions(toscaConceptIdentifier.getName(), toscaConceptIdentifier.getVersion());
342             assertThat(automationCompositionsGet.getAutomationCompositionList()).hasSize(1);
343             assertEquals(command.getOrderedState(),
344                 automationCompositionsGet.getAutomationCompositionList().get(0).getOrderedState());
345         }
346     }
347
348     private synchronized void deleteEntryInDB() throws Exception {
349         automationCompositionRepository.deleteAll();
350         var list = serviceTemplateProvider.getAllServiceTemplates();
351         if (!list.isEmpty()) {
352             serviceTemplateProvider.deleteServiceTemplate(list.get(0).getName(), list.get(0).getVersion());
353         }
354     }
355
356     private synchronized void createEntryInDB() throws Exception {
357         deleteEntryInDB();
358         serviceTemplateProvider.createServiceTemplate(serviceTemplate);
359     }
360 }