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 {
96 deleteEntryInDB(serviceTemplate.getName(), serviceTemplate.getVersion());
97 deleteEntryInDB(commonPropertiesServiceTemplate.getName(), commonPropertiesServiceTemplate.getVersion());
101 void testSwagger() throws Exception {
102 super.testSwagger(COMMISSIONING_ENDPOINT);
106 void testUnauthorizedCreate() throws Exception {
107 assertUnauthorizedPost(COMMISSIONING_ENDPOINT, Entity.json(serviceTemplate));
111 void testUnauthorizedQuery() throws Exception {
112 assertUnauthorizedGet(COMMISSIONING_ENDPOINT);
116 void testUnauthorizedQueryElements() throws Exception {
117 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/elements");
121 void testUnauthorizedDelete() throws Exception {
122 assertUnauthorizedDelete(COMMISSIONING_ENDPOINT);
126 void testUnauthorizedQueryToscaServiceTemplate() throws Exception {
127 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
131 void testUnauthorizedQueryToscaServiceTemplateSchema() throws Exception {
132 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
136 void testUnauthorizedQueryToscaServiceCommonOrInstanceProperties() throws Exception {
137 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/getCommonOrInstanceProperties");
141 void testQueryToscaServiceTemplate() throws Exception {
142 createFullEntryInDbWithCommonProps();
144 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
145 Response rawresp = invocationBuilder.buildGet().invoke();
146 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
147 ToscaServiceTemplate template = rawresp.readEntity(ToscaServiceTemplate.class);
148 assertNotNull(template);
149 assertThat(template.getNodeTypes()).hasSize(8);
154 void testQueryToscaServiceTemplateSchema() throws Exception {
155 createFullEntryInDbWithCommonProps();
157 Invocation.Builder invocationBuilder =
158 super.sendRequest(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
159 Response rawresp = invocationBuilder.buildGet().invoke();
160 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
161 String schema = rawresp.readEntity(String.class);
162 assertNotNull(schema);
167 void testQueryCommonOrInstanceProperties() throws Exception {
168 createFullEntryInDbWithCommonProps();
170 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT
171 + "/getCommonOrInstanceProperties" + "?common=true&name=ToscaServiceTemplateSimple&version=1.0.0");
172 Response rawresp = invocationBuilder.buildGet().invoke();
173 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
175 @SuppressWarnings("unchecked")
176 Map<String, ToscaNodeTemplate> commonProperties = rawresp.readEntity(Map.class);
178 assertNotNull(commonProperties);
179 assertThat(commonProperties).hasSize(6);
184 void testCreateBadRequest() throws Exception {
185 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
186 Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte"));
188 assertThat(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).isEqualTo(resp.getStatus());
189 CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
190 assertThat(commissioningResponse.getErrorDetails()).isNotNull();
191 assertThat(commissioningResponse.getAffectedControlLoopDefinitions()).isNull();
195 void testCreate() throws Exception {
196 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
197 Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
198 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
199 CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
201 assertNotNull(commissioningResponse);
202 assertNull(commissioningResponse.getErrorDetails());
203 // Response should return the number of node templates present in the service template
204 assertThat(commissioningResponse.getAffectedControlLoopDefinitions()).hasSize(13);
205 for (String nodeTemplateName : serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().keySet()) {
206 assertTrue(commissioningResponse.getAffectedControlLoopDefinitions().stream()
207 .anyMatch(ac -> ac.getName().equals(nodeTemplateName)));
213 void testQuery_NoResultWithThisName() throws Exception {
216 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name=noResultWithThisName");
217 Response rawresp = invocationBuilder.buildGet().invoke();
218 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
219 List<?> entityList = rawresp.readEntity(List.class);
220 assertThat(entityList).isEmpty();
225 void testQuery() throws Exception {
228 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
229 Response rawresp = invocationBuilder.buildGet().invoke();
230 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
231 List<?> entityList = rawresp.readEntity(List.class);
232 assertNotNull(entityList);
233 assertThat(entityList).hasSize(2);
238 void testQueryElementsBadRequest() throws Exception {
241 // Call get elements with no info
242 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/elements");
243 Response resp = invocationBuilder.buildGet().invoke();
244 assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), resp.getStatus());
249 void testQueryElements() throws Exception {
252 Invocation.Builder invocationBuilder = super.sendRequest(
253 COMMISSIONING_ENDPOINT + "/elements" + "?name=org.onap.domain.pmsh.PMSHControlLoopDefinition");
254 Response rawresp = invocationBuilder.buildGet().invoke();
255 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
256 List<?> entityList = rawresp.readEntity(List.class);
257 assertNotNull(entityList);
258 assertThat(entityList).hasSize(4);
263 void testDeleteBadRequest() throws Exception {
266 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
267 // Call delete with no info
268 Response resp = invocationBuilder.delete();
269 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
274 void testDelete() throws Exception {
277 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name="
278 + serviceTemplate.getName() + "&version=" + serviceTemplate.getVersion());
279 // Call delete with no info
280 Response resp = invocationBuilder.delete();
281 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
283 List<ToscaServiceTemplate> templatesInDB = serviceTemplateProvider.getServiceTemplateList(null, null);
284 assertThat(templatesInDB).isEmpty();
288 private synchronized void createEntryInDB() throws Exception {
289 deleteEntryInDB(commonPropertiesServiceTemplate.getName(), commonPropertiesServiceTemplate.getVersion());
290 serviceTemplateProvider.createServiceTemplate(serviceTemplate);
293 // Delete entries from the DB after relevant tests
294 private synchronized void deleteEntryInDB(String name, String version) throws Exception {
295 if (!serviceTemplateProvider.getServiceTemplateList(null, null).isEmpty()) {
296 serviceTemplateProvider.deleteServiceTemplate(name, version);
300 private synchronized void createFullEntryInDbWithCommonProps() throws Exception {
301 deleteEntryInDB(commonPropertiesServiceTemplate.getName(), commonPropertiesServiceTemplate.getVersion());
302 serviceTemplateProvider.createServiceTemplate(commonPropertiesServiceTemplate);