a0f1a75908d98edd8b6d92e95e3faa79a2d32dce
[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.commissioning.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.junit.jupiter.api.Assertions.assertTrue;
29 import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_SERVICE_TEMPLATE_YAML;
30 import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_ST_TEMPLATE_YAML;
31
32 import java.util.List;
33 import java.util.Map;
34 import javax.ws.rs.client.Entity;
35 import javax.ws.rs.client.Invocation;
36 import javax.ws.rs.core.Response;
37 import org.junit.jupiter.api.AfterEach;
38 import org.junit.jupiter.api.BeforeAll;
39 import org.junit.jupiter.api.BeforeEach;
40 import org.junit.jupiter.api.Test;
41 import org.junit.jupiter.api.extension.ExtendWith;
42 import org.junit.jupiter.api.parallel.Execution;
43 import org.junit.jupiter.api.parallel.ExecutionMode;
44 import org.onap.policy.clamp.acm.runtime.instantiation.InstantiationUtils;
45 import org.onap.policy.clamp.acm.runtime.util.rest.CommonRestController;
46 import org.onap.policy.clamp.models.acm.messages.rest.commissioning.CommissioningResponse;
47 import org.onap.policy.clamp.models.acm.persistence.provider.ServiceTemplateProvider;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.boot.test.context.SpringBootTest;
52 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
53 import org.springframework.boot.web.server.LocalServerPort;
54 import org.springframework.test.context.ActiveProfiles;
55 import org.springframework.test.context.junit.jupiter.SpringExtension;
56
57 @ExtendWith(SpringExtension.class)
58 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
59 @ActiveProfiles("test")
60 @Execution(ExecutionMode.SAME_THREAD)
61 class CommissioningControllerTest extends CommonRestController {
62
63     private static final String COMMISSIONING_ENDPOINT = "commission";
64     private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
65     private static ToscaServiceTemplate commonPropertiesServiceTemplate = new ToscaServiceTemplate();
66
67     @Autowired
68     private ServiceTemplateProvider serviceTemplateProvider;
69
70     @LocalServerPort
71     private int randomServerPort;
72
73     /**
74      * starts Main and inserts a commissioning template.
75      */
76     @BeforeAll
77     public static void setUpBeforeClass() {
78         serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
79         commonPropertiesServiceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_ST_TEMPLATE_YAML);
80     }
81
82     @BeforeEach
83     public void setUpPort() {
84         super.setHttpPrefix(randomServerPort);
85     }
86
87     @AfterEach
88     public void cleanDatabase() throws Exception {
89         deleteEntryInDB();
90     }
91
92     @Test
93     void testSwagger() throws Exception {
94         super.testSwagger(COMMISSIONING_ENDPOINT);
95     }
96
97     @Test
98     void testUnauthorizedCreate() throws Exception {
99         assertUnauthorizedPost(COMMISSIONING_ENDPOINT, Entity.json(serviceTemplate));
100     }
101
102     @Test
103     void testUnauthorizedQuery() throws Exception {
104         assertUnauthorizedGet(COMMISSIONING_ENDPOINT);
105     }
106
107     @Test
108     void testUnauthorizedQueryElements() throws Exception {
109         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/elements");
110     }
111
112     @Test
113     void testUnauthorizedDelete() throws Exception {
114         assertUnauthorizedDelete(COMMISSIONING_ENDPOINT);
115     }
116
117     @Test
118     void testUnauthorizedQueryToscaServiceTemplate() throws Exception {
119         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
120     }
121
122     @Test
123     void testUnauthorizedQueryToscaServiceTemplateSchema() throws Exception {
124         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
125     }
126
127     @Test
128     void testUnauthorizedQueryToscaServiceCommonOrInstanceProperties() throws Exception {
129         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/getCommonOrInstanceProperties");
130     }
131
132     @Test
133     void testQueryToscaServiceTemplate() throws Exception {
134         createFullEntryInDbWithCommonProps();
135
136         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
137         Response rawresp = invocationBuilder.buildGet().invoke();
138         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
139         ToscaServiceTemplate template = rawresp.readEntity(ToscaServiceTemplate.class);
140         assertNotNull(template);
141         assertThat(template.getNodeTypes()).hasSize(7);
142     }
143
144     @Test
145     void testQueryToscaServiceTemplateSchema() throws Exception {
146         createFullEntryInDbWithCommonProps();
147
148         Invocation.Builder invocationBuilder =
149                 super.sendRequest(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
150         Response rawresp = invocationBuilder.buildGet().invoke();
151         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
152         String schema = rawresp.readEntity(String.class);
153         assertNotNull(schema);
154     }
155
156     @Test
157     void testQueryCommonOrInstanceProperties() throws Exception {
158         createFullEntryInDbWithCommonProps();
159
160         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT
161                 + "/getCommonOrInstanceProperties?common=true");
162         Response rawresp = invocationBuilder.buildGet().invoke();
163         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
164
165         @SuppressWarnings("unchecked")
166         Map<String, ToscaNodeTemplate> commonProperties = rawresp.readEntity(Map.class);
167
168         assertNotNull(commonProperties);
169         assertThat(commonProperties).hasSize(5);
170     }
171
172     @Test
173     void testCreateBadRequest() throws Exception {
174         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
175         Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte"));
176
177         assertThat(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).isEqualTo(resp.getStatus());
178         CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
179         assertThat(commissioningResponse.getErrorDetails()).isNotNull();
180         assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).isNull();
181     }
182
183     @Test
184     void testCreate() throws Exception {
185         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
186         Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
187         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
188         CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
189
190         assertNotNull(commissioningResponse);
191         assertNull(commissioningResponse.getErrorDetails());
192         // Response should return the number of node templates present in the service template
193         assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).hasSize(13);
194         for (String nodeTemplateName : serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().keySet()) {
195             assertTrue(commissioningResponse.getAffectedAutomationCompositionDefinitions().stream()
196                     .anyMatch(ac -> ac.getName().equals(nodeTemplateName)));
197         }
198     }
199
200     @Test
201     void testQuery_NoResultWithThisName() throws Exception {
202         createEntryInDB();
203
204         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name=noResultWithThisName");
205         Response rawresp = invocationBuilder.buildGet().invoke();
206         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
207         List<?> entityList = rawresp.readEntity(List.class);
208         assertThat(entityList).isEmpty();
209     }
210
211     @Test
212     void testQuery() throws Exception {
213         createEntryInDB();
214
215         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
216         Response rawresp = invocationBuilder.buildGet().invoke();
217         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
218         List<?> entityList = rawresp.readEntity(List.class);
219         assertNotNull(entityList);
220         assertThat(entityList).hasSize(2);
221     }
222
223     @Test
224     void testQueryElementsBadRequest() throws Exception {
225         createEntryInDB();
226
227         // Call get elements with no info
228         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/elements");
229         Response resp = invocationBuilder.buildGet().invoke();
230         assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), resp.getStatus());
231     }
232
233     @Test
234     void testQueryElements() throws Exception {
235         createEntryInDB();
236
237         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/elements"
238                 + "?name=org.onap.domain.pmsh.PMSHAutomationCompositionDefinition");
239         Response rawresp = invocationBuilder.buildGet().invoke();
240         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
241         List<?> entityList = rawresp.readEntity(List.class);
242         assertNotNull(entityList);
243         assertThat(entityList).hasSize(4);
244     }
245
246     @Test
247     void testDeleteBadRequest() throws Exception {
248         createEntryInDB();
249
250         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
251         // Call delete with no info
252         Response resp = invocationBuilder.delete();
253         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
254     }
255
256     @Test
257     void testDelete() throws Exception {
258         var serviceTemplateCreated = createEntryInDB();
259
260         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name="
261                 + serviceTemplateCreated.getName() + "&version=" + serviceTemplateCreated.getVersion());
262         // Call delete with no info
263         Response resp = invocationBuilder.delete();
264         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
265
266         List<ToscaServiceTemplate> templatesInDB = serviceTemplateProvider.getAllServiceTemplates();
267         assertThat(templatesInDB).isEmpty();
268     }
269
270     private synchronized ToscaServiceTemplate createEntryInDB() throws Exception {
271         deleteEntryInDB();
272         return serviceTemplateProvider.createServiceTemplate(serviceTemplate);
273     }
274
275     // Delete entries from the DB after relevant tests
276     private synchronized void deleteEntryInDB() throws Exception {
277         var list = serviceTemplateProvider.getAllServiceTemplates();
278         if (!list.isEmpty()) {
279             serviceTemplateProvider.deleteServiceTemplate(list.get(0).getName(), list.get(0).getVersion());
280         }
281     }
282
283     private synchronized void createFullEntryInDbWithCommonProps() throws Exception {
284         deleteEntryInDB();
285         serviceTemplateProvider.createServiceTemplate(commonPropertiesServiceTemplate);
286     }
287 }