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.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;
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 {
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";
66 private static final String COMMISSIONING_ENDPOINT = "commission";
67 private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
68 private static ToscaServiceTemplate commonPropertiesServiceTemplate = new ToscaServiceTemplate();
71 private ServiceTemplateProvider serviceTemplateProvider;
74 private int randomServerPort;
77 * starts Main and inserts a commissioning template.
79 * @throws Exception if an error occurs
82 public static void setUpBeforeClass() throws Exception {
84 serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
85 commonPropertiesServiceTemplate =
86 InstantiationUtils.getToscaServiceTemplate(COMMON_TOSCA_SERVICE_TEMPLATE_YAML);
90 public void setUpPort() {
91 super.setHttpPrefix(randomServerPort);
95 public void cleanDatabase() throws Exception {
100 void testSwagger() throws Exception {
101 super.testSwagger(COMMISSIONING_ENDPOINT);
105 void testUnauthorizedCreate() throws Exception {
106 assertUnauthorizedPost(COMMISSIONING_ENDPOINT, Entity.json(serviceTemplate));
110 void testUnauthorizedQuery() throws Exception {
111 assertUnauthorizedGet(COMMISSIONING_ENDPOINT);
115 void testUnauthorizedQueryElements() throws Exception {
116 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/elements");
120 void testUnauthorizedDelete() throws Exception {
121 assertUnauthorizedDelete(COMMISSIONING_ENDPOINT);
125 void testUnauthorizedQueryToscaServiceTemplate() throws Exception {
126 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
130 void testUnauthorizedQueryToscaServiceTemplateSchema() throws Exception {
131 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
135 void testUnauthorizedQueryToscaServiceCommonOrInstanceProperties() throws Exception {
136 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/getCommonOrInstanceProperties");
140 void testQueryToscaServiceTemplate() throws Exception {
141 createFullEntryInDbWithCommonProps();
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);
153 void testQueryToscaServiceTemplateSchema() throws Exception {
154 createFullEntryInDbWithCommonProps();
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);
166 void testQueryCommonOrInstanceProperties() throws Exception {
167 createFullEntryInDbWithCommonProps();
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());
174 @SuppressWarnings("unchecked")
175 Map<String, ToscaNodeTemplate> commonProperties = rawresp.readEntity(Map.class);
177 assertNotNull(commonProperties);
178 assertThat(commonProperties).hasSize(6);
183 void testCreateBadRequest() throws Exception {
184 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
185 Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte"));
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();
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);
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)));
212 void testQuery_NoResultWithThisName() throws Exception {
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();
224 void testQuery() throws Exception {
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);
237 void testQueryElementsBadRequest() throws Exception {
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());
248 void testQueryElements() throws Exception {
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);
262 void testDeleteBadRequest() throws Exception {
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());
273 void testDelete() throws Exception {
274 var serviceTemplateCreated = createEntryInDB();
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());
282 List<ToscaServiceTemplate> templatesInDB = serviceTemplateProvider.getAllServiceTemplates();
283 assertThat(templatesInDB).isEmpty();
286 private synchronized ToscaServiceTemplate createEntryInDB() throws Exception {
288 return serviceTemplateProvider.createServiceTemplate(serviceTemplate);
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());
299 private synchronized void createFullEntryInDbWithCommonProps() throws Exception {
301 serviceTemplateProvider.createServiceTemplate(commonPropertiesServiceTemplate);