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