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