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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.runtime.commissioning.rest;
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;
29 import java.util.List;
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;
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 {
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();
73 private ClRuntimeParameterGroup clRuntimeParameterGroup;
76 private int randomServerPort;
79 * starts Main and inserts a commissioning template.
81 * @throws Exception if an error occurs
84 public static void setUpBeforeClass() throws Exception {
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);
94 public void setUpPort() {
95 super.setHttpPrefix(randomServerPort);
99 public void cleanDatabase() throws Exception {
100 deleteEntryInDB(serviceTemplate.getName(), serviceTemplate.getVersion());
101 deleteEntryInDB(commonPropertiesServiceTemplate.getName(), commonPropertiesServiceTemplate.getVersion());
105 void testSwagger() throws Exception {
106 super.testSwagger(COMMISSIONING_ENDPOINT);
110 void testUnauthorizedCreate() throws Exception {
111 assertUnauthorizedPost(COMMISSIONING_ENDPOINT, Entity.json(serviceTemplate));
115 void testUnauthorizedQuery() throws Exception {
116 assertUnauthorizedGet(COMMISSIONING_ENDPOINT);
120 void testUnauthorizedQueryElements() throws Exception {
121 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/elements");
125 void testUnauthorizedDelete() throws Exception {
126 assertUnauthorizedDelete(COMMISSIONING_ENDPOINT);
130 void testUnauthorizedQueryToscaServiceTemplate() throws Exception {
131 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
135 void testUnauthorizedQueryToscaServiceTemplateSchema() throws Exception {
136 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
140 void testUnauthorizedQueryToscaServiceCommonOrInstanceProperties() throws Exception {
141 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/getCommonOrInstanceProperties");
145 void testQueryToscaServiceTemplate() throws Exception {
146 createFullEntryInDbWithCommonProps();
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);
159 void testQueryToscaServiceTemplateSchema() throws Exception {
160 createFullEntryInDbWithCommonProps();
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);
172 void testQueryCommonOrInstanceProperties() throws Exception {
173 createFullEntryInDbWithCommonProps();
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);
186 void testCreateBadRequest() throws Exception {
187 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
188 Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte"));
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();
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);
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)));
215 void testQuery_NoResultWithThisName() throws Exception {
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();
227 void testQuery() throws Exception {
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);
240 void testQueryElementsBadRequest() throws Exception {
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());
251 void testQueryElements() throws Exception {
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);
265 void testDeleteBadRequest() throws Exception {
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());
276 void testDelete() throws Exception {
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());
285 try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory()
286 .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) {
287 List<ToscaServiceTemplate> templatesInDB = modelsProvider.getServiceTemplateList(null, null);
288 assertThat(templatesInDB).isEmpty();
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);
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);
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);