0cc2036e72a52203e2cb3f6e9f9f7f14d41530a0
[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() {
94         super.testSwagger(COMMISSIONING_ENDPOINT);
95     }
96
97     @Test
98     void testUnauthorizedCreate() {
99         assertUnauthorizedPost(COMMISSIONING_ENDPOINT, Entity.json(serviceTemplate));
100     }
101
102     @Test
103     void testUnauthorizedQuery() {
104         assertUnauthorizedGet(COMMISSIONING_ENDPOINT);
105     }
106
107     @Test
108     void testUnauthorizedQueryElements() {
109         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/elements");
110     }
111
112     @Test
113     void testUnauthorizedDelete() {
114         assertUnauthorizedDelete(COMMISSIONING_ENDPOINT);
115     }
116
117     @Test
118     void testUnauthorizedQueryToscaServiceTemplate() {
119         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
120     }
121
122     @Test
123     void testUnauthorizedQueryToscaServiceTemplateSchema() {
124         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
125     }
126
127     @Test
128     void testUnauthorizedQueryToscaServiceCommonOrInstanceProperties() {
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 testQueryCommonOrInstanceProperties() throws Exception {
146         createFullEntryInDbWithCommonProps();
147
148         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT
149                 + "/getCommonOrInstanceProperties?common=true");
150         Response rawresp = invocationBuilder.buildGet().invoke();
151         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
152
153         @SuppressWarnings("unchecked")
154         Map<String, ToscaNodeTemplate> commonProperties = rawresp.readEntity(Map.class);
155
156         assertNotNull(commonProperties);
157         assertThat(commonProperties).hasSize(4);
158     }
159
160     @Test
161     void testCreateBadRequest() {
162         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
163         Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte"));
164
165         assertThat(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).isEqualTo(resp.getStatus());
166         CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
167         assertThat(commissioningResponse.getErrorDetails()).isNotNull();
168         assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).isNull();
169     }
170
171     @Test
172     void testCreate() {
173         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
174         Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
175         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
176         CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
177
178         assertNotNull(commissioningResponse);
179         assertNull(commissioningResponse.getErrorDetails());
180         // Response should return the number of node templates present in the service template
181         assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).hasSize(13);
182         for (String nodeTemplateName : serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().keySet()) {
183             assertTrue(commissioningResponse.getAffectedAutomationCompositionDefinitions().stream()
184                     .anyMatch(ac -> ac.getName().equals(nodeTemplateName)));
185         }
186     }
187
188     @Test
189     void testQuery_NoResultWithThisName() throws Exception {
190         createEntryInDB();
191
192         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name=noResultWithThisName");
193         Response rawresp = invocationBuilder.buildGet().invoke();
194         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
195         List<?> entityList = rawresp.readEntity(List.class);
196         assertThat(entityList).isEmpty();
197     }
198
199     @Test
200     void testQuery() throws Exception {
201         createEntryInDB();
202
203         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
204         Response rawresp = invocationBuilder.buildGet().invoke();
205         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
206         List<?> entityList = rawresp.readEntity(List.class);
207         assertNotNull(entityList);
208         assertThat(entityList).hasSize(2);
209     }
210
211     @Test
212     void testQueryElementsBadRequest() throws Exception {
213         createEntryInDB();
214
215         // Call get elements with no info
216         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/elements");
217         Response resp = invocationBuilder.buildGet().invoke();
218         assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), resp.getStatus());
219     }
220
221     @Test
222     void testQueryElements() throws Exception {
223         createEntryInDB();
224
225         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/elements"
226                 + "?name=org.onap.domain.pmsh.PMSHAutomationCompositionDefinition");
227         Response rawresp = invocationBuilder.buildGet().invoke();
228         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
229         List<?> entityList = rawresp.readEntity(List.class);
230         assertNotNull(entityList);
231         assertThat(entityList).hasSize(4);
232     }
233
234     @Test
235     void testDeleteBadRequest() throws Exception {
236         createEntryInDB();
237
238         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
239         // Call delete with no info
240         Response resp = invocationBuilder.delete();
241         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
242     }
243
244     @Test
245     void testDelete() throws Exception {
246         var serviceTemplateCreated = createEntryInDB();
247
248         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name="
249                 + serviceTemplateCreated.getName() + "&version=" + serviceTemplateCreated.getVersion());
250         // Call delete with no info
251         Response resp = invocationBuilder.delete();
252         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
253
254         List<ToscaServiceTemplate> templatesInDB = serviceTemplateProvider.getAllServiceTemplates();
255         assertThat(templatesInDB).isEmpty();
256     }
257
258     private synchronized ToscaServiceTemplate createEntryInDB() throws Exception {
259         deleteEntryInDB();
260         return serviceTemplateProvider.createServiceTemplate(serviceTemplate);
261     }
262
263     // Delete entries from the DB after relevant tests
264     private synchronized void deleteEntryInDB() throws Exception {
265         var list = serviceTemplateProvider.getAllServiceTemplates();
266         if (!list.isEmpty()) {
267             serviceTemplateProvider.deleteServiceTemplate(list.get(0).getName(), list.get(0).getVersion());
268         }
269     }
270
271     private synchronized void createFullEntryInDbWithCommonProps() throws Exception {
272         deleteEntryInDB();
273         serviceTemplateProvider.createServiceTemplate(commonPropertiesServiceTemplate);
274     }
275 }