4a5a6e2a3c7cbf5bf5e0ccf1a8b11c30eff165a7
[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.messages.rest.commissioning.CommissioningResponse;
43 import org.onap.policy.clamp.controlloop.runtime.instantiation.InstantiationUtils;
44 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
45 import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController;
46 import org.onap.policy.models.provider.PolicyModelsProvider;
47 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
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.TestPropertySource;
55 import org.springframework.test.context.junit.jupiter.SpringExtension;
56
57 @ExtendWith(SpringExtension.class)
58 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
59 @TestPropertySource(locations = {"classpath:application_test.properties"})
60 @Execution(ExecutionMode.SAME_THREAD)
61 class CommissioningControllerTest extends CommonRestController {
62
63     private static final String TOSCA_SERVICE_TEMPLATE_YAML =
64             "src/test/resources/rest/servicetemplates/pmsh_multiple_cl_tosca.yaml";
65     private static final String COMMON_TOSCA_SERVICE_TEMPLATE_YAML =
66             "src/test/resources/rest/servicetemplates/full-tosca-with-common-properties.yaml";
67
68     private static final String COMMISSIONING_ENDPOINT = "commission";
69     private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
70     private static ToscaServiceTemplate commonPropertiesServiceTemplate = new ToscaServiceTemplate();
71
72     @Autowired
73     private ClRuntimeParameterGroup clRuntimeParameterGroup;
74
75     @LocalServerPort
76     private int randomServerPort;
77
78     /**
79      * starts Main and inserts a commissioning template.
80      *
81      * @throws Exception if an error occurs
82      */
83     @BeforeAll
84     public static void setUpBeforeClass() throws Exception {
85
86         serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
87         commonPropertiesServiceTemplate =
88                 InstantiationUtils.getToscaServiceTemplate(COMMON_TOSCA_SERVICE_TEMPLATE_YAML);
89     }
90
91     @BeforeEach
92     public void setUpPort() {
93         super.setHttpPrefix(randomServerPort);
94     }
95
96     @AfterEach
97     public void cleanDatabase() throws Exception {
98         deleteEntryInDB(serviceTemplate.getName(), serviceTemplate.getVersion());
99         deleteEntryInDB(commonPropertiesServiceTemplate.getName(), commonPropertiesServiceTemplate.getVersion());
100     }
101
102     @Test
103     void testSwagger() throws Exception {
104         super.testSwagger(COMMISSIONING_ENDPOINT);
105     }
106
107     @Test
108     void testUnauthorizedCreate() throws Exception {
109         assertUnauthorizedPost(COMMISSIONING_ENDPOINT, Entity.json(serviceTemplate));
110     }
111
112     @Test
113     void testUnauthorizedQuery() throws Exception {
114         assertUnauthorizedGet(COMMISSIONING_ENDPOINT);
115     }
116
117     @Test
118     void testUnauthorizedQueryElements() throws Exception {
119         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/elements");
120     }
121
122     @Test
123     void testUnauthorizedDelete() throws Exception {
124         assertUnauthorizedDelete(COMMISSIONING_ENDPOINT);
125     }
126
127     @Test
128     void testUnauthorizedQueryToscaServiceTemplate() throws Exception {
129         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
130     }
131
132     @Test
133     void testUnauthorizedQueryToscaServiceTemplateSchema() throws Exception {
134         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
135     }
136
137     @Test
138     void testUnauthorizedQueryToscaServiceCommonOrInstanceProperties() throws Exception {
139         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/getCommonOrInstanceProperties");
140     }
141
142     @Test
143     void testQueryToscaServiceTemplate() throws Exception {
144         createFullEntryInDbWithCommonProps();
145
146         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
147         Response rawresp = invocationBuilder.buildGet().invoke();
148         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
149         ToscaServiceTemplate template = rawresp.readEntity(ToscaServiceTemplate.class);
150         assertNotNull(template);
151         assertThat(template.getNodeTypes()).hasSize(8);
152
153     }
154
155     @Test
156     void testQueryToscaServiceTemplateSchema() throws Exception {
157         createFullEntryInDbWithCommonProps();
158
159         Invocation.Builder invocationBuilder =
160                 super.sendRequest(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
161         Response rawresp = invocationBuilder.buildGet().invoke();
162         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
163         String schema = rawresp.readEntity(String.class);
164         assertNotNull(schema);
165
166     }
167
168     @Test
169     void testQueryCommonOrInstanceProperties() throws Exception {
170         createFullEntryInDbWithCommonProps();
171
172         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT
173                 + "/getCommonOrInstanceProperties" + "?common=true&name=ToscaServiceTemplateSimple&version=1.0.0");
174         Response rawresp = invocationBuilder.buildGet().invoke();
175         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
176
177         @SuppressWarnings("unchecked")
178         Map<String, ToscaNodeTemplate> commonProperties = rawresp.readEntity(Map.class);
179
180         assertNotNull(commonProperties);
181         assertThat(commonProperties).hasSize(6);
182
183     }
184
185     @Test
186     void testCreateBadRequest() throws Exception {
187         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
188         Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte"));
189
190         assertThat(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).isEqualTo(resp.getStatus());
191         CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
192         assertThat(commissioningResponse.getErrorDetails()).isNotNull();
193         assertThat(commissioningResponse.getAffectedControlLoopDefinitions()).isNull();
194     }
195
196     @Test
197     void testCreate() throws Exception {
198         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
199         Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
200         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
201         CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
202
203         assertNotNull(commissioningResponse);
204         assertNull(commissioningResponse.getErrorDetails());
205         // Response should return the number of node templates present in the service template
206         assertThat(commissioningResponse.getAffectedControlLoopDefinitions()).hasSize(13);
207         for (String nodeTemplateName : serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().keySet()) {
208             assertTrue(commissioningResponse.getAffectedControlLoopDefinitions().stream()
209                     .anyMatch(ac -> ac.getName().equals(nodeTemplateName)));
210         }
211
212     }
213
214     @Test
215     void testQuery_NoResultWithThisName() throws Exception {
216         createEntryInDB();
217
218         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name=noResultWithThisName");
219         Response rawresp = invocationBuilder.buildGet().invoke();
220         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
221         List<?> entityList = rawresp.readEntity(List.class);
222         assertThat(entityList).isEmpty();
223
224     }
225
226     @Test
227     void testQuery() throws Exception {
228         createEntryInDB();
229
230         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
231         Response rawresp = invocationBuilder.buildGet().invoke();
232         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
233         List<?> entityList = rawresp.readEntity(List.class);
234         assertNotNull(entityList);
235         assertThat(entityList).hasSize(2);
236
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
250     @Test
251     void testQueryElements() throws Exception {
252         createEntryInDB();
253
254         Invocation.Builder invocationBuilder = super.sendRequest(
255                 COMMISSIONING_ENDPOINT + "/elements" + "?name=org.onap.domain.pmsh.PMSHControlLoopDefinition");
256         Response rawresp = invocationBuilder.buildGet().invoke();
257         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
258         List<?> entityList = rawresp.readEntity(List.class);
259         assertNotNull(entityList);
260         assertThat(entityList).hasSize(4);
261
262     }
263
264     @Test
265     void testDeleteBadRequest() throws Exception {
266         createEntryInDB();
267
268         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
269         // Call delete with no info
270         Response resp = invocationBuilder.delete();
271         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
272
273     }
274
275     @Test
276     void testDelete() throws Exception {
277         createEntryInDB();
278
279         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name="
280                 + serviceTemplate.getName() + "&version=" + serviceTemplate.getVersion());
281         // Call delete with no info
282         Response resp = invocationBuilder.delete();
283         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
284
285         try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory()
286                 .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) {
287             List<ToscaServiceTemplate> templatesInDB = modelsProvider.getServiceTemplateList(null, null);
288             assertThat(templatesInDB).isEmpty();
289         }
290
291     }
292
293     private synchronized void createEntryInDB() throws Exception {
294         try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory()
295                 .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) {
296             deleteEntryInDB(commonPropertiesServiceTemplate.getName(), commonPropertiesServiceTemplate.getVersion());
297             modelsProvider.createServiceTemplate(serviceTemplate);
298         }
299     }
300
301     // Delete entries from the DB after relevant tests
302     private synchronized void deleteEntryInDB(String name, String version) throws Exception {
303         try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory()
304                 .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) {
305             if (!modelsProvider.getServiceTemplateList(null, null).isEmpty()) {
306                 modelsProvider.deleteServiceTemplate(name, version);
307             }
308         }
309     }
310
311     private synchronized void createFullEntryInDbWithCommonProps() throws Exception {
312         try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory()
313                 .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) {
314             deleteEntryInDB(commonPropertiesServiceTemplate.getName(), commonPropertiesServiceTemplate.getVersion());
315             modelsProvider.createServiceTemplate(commonPropertiesServiceTemplate);
316         }
317     }
318 }