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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.controlloop.runtime.commissioning.rest;
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;
30 import java.util.List;
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;
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";
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 = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
87 commonPropertiesServiceTemplate =
88 InstantiationUtils.getToscaServiceTemplate(COMMON_TOSCA_SERVICE_TEMPLATE_YAML);
92 public void setUpPort() {
93 super.setHttpPrefix(randomServerPort);
97 public void cleanDatabase() throws Exception {
98 deleteEntryInDB(serviceTemplate.getName(), serviceTemplate.getVersion());
99 deleteEntryInDB(commonPropertiesServiceTemplate.getName(), commonPropertiesServiceTemplate.getVersion());
103 void testSwagger() throws Exception {
104 super.testSwagger(COMMISSIONING_ENDPOINT);
108 void testUnauthorizedCreate() throws Exception {
109 assertUnauthorizedPost(COMMISSIONING_ENDPOINT, Entity.json(serviceTemplate));
113 void testUnauthorizedQuery() throws Exception {
114 assertUnauthorizedGet(COMMISSIONING_ENDPOINT);
118 void testUnauthorizedQueryElements() throws Exception {
119 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/elements");
123 void testUnauthorizedDelete() throws Exception {
124 assertUnauthorizedDelete(COMMISSIONING_ENDPOINT);
128 void testUnauthorizedQueryToscaServiceTemplate() throws Exception {
129 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
133 void testUnauthorizedQueryToscaServiceTemplateSchema() throws Exception {
134 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
138 void testUnauthorizedQueryToscaServiceCommonOrInstanceProperties() throws Exception {
139 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/getCommonOrInstanceProperties");
143 void testQueryToscaServiceTemplate() throws Exception {
144 createFullEntryInDbWithCommonProps();
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);
156 void testQueryToscaServiceTemplateSchema() throws Exception {
157 createFullEntryInDbWithCommonProps();
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);
169 void testQueryCommonOrInstanceProperties() throws Exception {
170 createFullEntryInDbWithCommonProps();
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());
177 @SuppressWarnings("unchecked")
178 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(
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);
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);