e4a12143996107b4079cd83a06b53e0e7d599db7
[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();
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         ToscaServiceTemplate template = rawresp.readEntity(ToscaServiceTemplate.class);
147         assertNotNull(template);
148         assertThat(template.getNodeTypes()).hasSize(8);
149
150     }
151
152     @Test
153     void testQueryToscaServiceTemplateSchema() throws Exception {
154         createFullEntryInDbWithCommonProps();
155
156         Invocation.Builder invocationBuilder =
157                 super.sendRequest(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
158         Response rawresp = invocationBuilder.buildGet().invoke();
159         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
160         String schema = rawresp.readEntity(String.class);
161         assertNotNull(schema);
162
163     }
164
165     @Test
166     void testQueryCommonOrInstanceProperties() throws Exception {
167         createFullEntryInDbWithCommonProps();
168
169         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT
170                 + "/getCommonOrInstanceProperties" + "?common=true&name=ToscaServiceTemplateSimple&version=1.0.0");
171         Response rawresp = invocationBuilder.buildGet().invoke();
172         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
173
174         @SuppressWarnings("unchecked")
175         Map<String, ToscaNodeTemplate> commonProperties = rawresp.readEntity(Map.class);
176
177         assertNotNull(commonProperties);
178         assertThat(commonProperties).hasSize(6);
179
180     }
181
182     @Test
183     void testCreateBadRequest() throws Exception {
184         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
185         Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte"));
186
187         assertThat(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).isEqualTo(resp.getStatus());
188         CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
189         assertThat(commissioningResponse.getErrorDetails()).isNotNull();
190         assertThat(commissioningResponse.getAffectedControlLoopDefinitions()).isNull();
191     }
192
193     @Test
194     void testCreate() throws Exception {
195         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
196         Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
197         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
198         CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
199
200         assertNotNull(commissioningResponse);
201         assertNull(commissioningResponse.getErrorDetails());
202         // Response should return the number of node templates present in the service template
203         assertThat(commissioningResponse.getAffectedControlLoopDefinitions()).hasSize(13);
204         for (String nodeTemplateName : serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().keySet()) {
205             assertTrue(commissioningResponse.getAffectedControlLoopDefinitions().stream()
206                     .anyMatch(ac -> ac.getName().equals(nodeTemplateName)));
207         }
208
209     }
210
211     @Test
212     void testQuery_NoResultWithThisName() throws Exception {
213         createEntryInDB();
214
215         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name=noResultWithThisName");
216         Response rawresp = invocationBuilder.buildGet().invoke();
217         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
218         List<?> entityList = rawresp.readEntity(List.class);
219         assertThat(entityList).isEmpty();
220
221     }
222
223     @Test
224     void testQuery() throws Exception {
225         createEntryInDB();
226
227         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
228         Response rawresp = invocationBuilder.buildGet().invoke();
229         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
230         List<?> entityList = rawresp.readEntity(List.class);
231         assertNotNull(entityList);
232         assertThat(entityList).hasSize(2);
233
234     }
235
236     @Test
237     void testQueryElementsBadRequest() throws Exception {
238         createEntryInDB();
239
240         // Call get elements with no info
241         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/elements");
242         Response resp = invocationBuilder.buildGet().invoke();
243         assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), resp.getStatus());
244
245     }
246
247     @Test
248     void testQueryElements() throws Exception {
249         createEntryInDB();
250
251         Invocation.Builder invocationBuilder = super.sendRequest(
252                 COMMISSIONING_ENDPOINT + "/elements" + "?name=org.onap.domain.pmsh.PMSHControlLoopDefinition");
253         Response rawresp = invocationBuilder.buildGet().invoke();
254         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
255         List<?> entityList = rawresp.readEntity(List.class);
256         assertNotNull(entityList);
257         assertThat(entityList).hasSize(4);
258
259     }
260
261     @Test
262     void testDeleteBadRequest() throws Exception {
263         createEntryInDB();
264
265         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
266         // Call delete with no info
267         Response resp = invocationBuilder.delete();
268         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
269
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 }