58ae1f6582af41915c328819f7f70d9fedf22642
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.runtime.commissioning.rest;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28
29 import java.util.List;
30 import java.util.Map;
31 import javax.ws.rs.client.Entity;
32 import javax.ws.rs.client.Invocation;
33 import javax.ws.rs.core.Response;
34 import org.junit.jupiter.api.AfterEach;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.junit.jupiter.api.parallel.Execution;
40 import org.junit.jupiter.api.parallel.ExecutionMode;
41 import org.onap.policy.clamp.controlloop.models.messages.rest.commissioning.CommissioningResponse;
42 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
43 import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController;
44 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
45 import org.onap.policy.common.utils.resources.ResourceUtils;
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     private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
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 = yamlTranslator.fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML),
87                 ToscaServiceTemplate.class);
88         commonPropertiesServiceTemplate = yamlTranslator.fromYaml(ResourceUtils
89                 .getResourceAsString(COMMON_TOSCA_SERVICE_TEMPLATE_YAML),
90             ToscaServiceTemplate.class);
91     }
92
93     @BeforeEach
94     public void setUpPort() {
95         super.setHttpPrefix(randomServerPort);
96     }
97
98     @AfterEach
99     public void cleanDatabase() throws Exception {
100         deleteEntryInDB(serviceTemplate.getName(), serviceTemplate.getVersion());
101         deleteEntryInDB(commonPropertiesServiceTemplate.getName(), commonPropertiesServiceTemplate.getVersion());
102     }
103
104     @Test
105     void testSwagger() throws Exception {
106         super.testSwagger(COMMISSIONING_ENDPOINT);
107     }
108
109     @Test
110     void testUnauthorizedCreate() throws Exception {
111         assertUnauthorizedPost(COMMISSIONING_ENDPOINT, Entity.json(serviceTemplate));
112     }
113
114     @Test
115     void testUnauthorizedQuery() throws Exception {
116         assertUnauthorizedGet(COMMISSIONING_ENDPOINT);
117     }
118
119     @Test
120     void testUnauthorizedQueryElements() throws Exception {
121         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/elements");
122     }
123
124     @Test
125     void testUnauthorizedDelete() throws Exception {
126         assertUnauthorizedDelete(COMMISSIONING_ENDPOINT);
127     }
128
129     @Test
130     void testUnauthorizedQueryToscaServiceTemplate() throws Exception {
131         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
132     }
133
134     @Test
135     void testUnauthorizedQueryToscaServiceTemplateSchema() throws Exception {
136         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
137     }
138
139     @Test
140     void testUnauthorizedQueryToscaServiceCommonOrInstanceProperties() throws Exception {
141         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/getCommonOrInstanceProperties");
142     }
143
144     @Test
145     void testQueryToscaServiceTemplate() throws Exception {
146         createFullEntryInDbWithCommonProps();
147
148         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT
149             + "/toscaservicetemplate");
150         Response rawresp = invocationBuilder.buildGet().invoke();
151         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
152         ToscaServiceTemplate template = rawresp.readEntity(ToscaServiceTemplate.class);
153         assertNotNull(template);
154         assertThat(template.getNodeTypes()).hasSize(8);
155
156     }
157
158     @Test
159     void testQueryToscaServiceTemplateSchema() throws Exception {
160         createFullEntryInDbWithCommonProps();
161
162         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT
163             + "/toscaServiceTemplateSchema");
164         Response rawresp = invocationBuilder.buildGet().invoke();
165         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
166         String schema = rawresp.readEntity(String.class);
167         assertNotNull(schema);
168
169     }
170
171     @Test
172     void testQueryCommonOrInstanceProperties() throws Exception {
173         createFullEntryInDbWithCommonProps();
174
175         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT
176             + "/getCommonOrInstanceProperties" + "?common=true&name=ToscaServiceTemplateSimple&version=1.0.0");
177         Response rawresp = invocationBuilder.buildGet().invoke();
178         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
179         Map<String, ToscaNodeTemplate> commonProperties = rawresp.readEntity(Map.class);
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(COMMISSIONING_ENDPOINT + "/elements"
255                 + "?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 }