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