2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 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.acm.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;
29 import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_SERVICE_TEMPLATE_YAML;
30 import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_ST_TEMPLATE_YAML;
32 import java.util.List;
34 import javax.ws.rs.client.Entity;
35 import javax.ws.rs.client.Invocation;
36 import javax.ws.rs.core.Response;
37 import org.junit.jupiter.api.AfterEach;
38 import org.junit.jupiter.api.BeforeAll;
39 import org.junit.jupiter.api.BeforeEach;
40 import org.junit.jupiter.api.Test;
41 import org.junit.jupiter.api.extension.ExtendWith;
42 import org.junit.jupiter.api.parallel.Execution;
43 import org.junit.jupiter.api.parallel.ExecutionMode;
44 import org.onap.policy.clamp.acm.runtime.instantiation.InstantiationUtils;
45 import org.onap.policy.clamp.acm.runtime.util.rest.CommonRestController;
46 import org.onap.policy.clamp.models.acm.messages.rest.commissioning.CommissioningResponse;
47 import org.onap.policy.clamp.models.acm.persistence.provider.ServiceTemplateProvider;
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.ActiveProfiles;
55 import org.springframework.test.context.junit.jupiter.SpringExtension;
57 @ExtendWith(SpringExtension.class)
58 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
59 @ActiveProfiles("test")
60 @Execution(ExecutionMode.SAME_THREAD)
61 class CommissioningControllerTest extends CommonRestController {
63 private static final String COMMISSIONING_ENDPOINT = "commission";
64 private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
65 private static ToscaServiceTemplate commonPropertiesServiceTemplate = new ToscaServiceTemplate();
68 private ServiceTemplateProvider serviceTemplateProvider;
71 private int randomServerPort;
74 * starts Main and inserts a commissioning template.
77 public static void setUpBeforeClass() {
78 serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
79 commonPropertiesServiceTemplate =
80 InstantiationUtils.getToscaServiceTemplate(TOSCA_ST_TEMPLATE_YAML);
84 public void setUpPort() {
85 super.setHttpPrefix(randomServerPort);
89 public void cleanDatabase() throws Exception {
94 void testSwagger() throws Exception {
95 super.testSwagger(COMMISSIONING_ENDPOINT);
99 void testUnauthorizedCreate() throws Exception {
100 assertUnauthorizedPost(COMMISSIONING_ENDPOINT, Entity.json(serviceTemplate));
104 void testUnauthorizedQuery() throws Exception {
105 assertUnauthorizedGet(COMMISSIONING_ENDPOINT);
109 void testUnauthorizedQueryElements() throws Exception {
110 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/elements");
114 void testUnauthorizedDelete() throws Exception {
115 assertUnauthorizedDelete(COMMISSIONING_ENDPOINT);
119 void testUnauthorizedQueryToscaServiceTemplate() throws Exception {
120 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
124 void testUnauthorizedQueryToscaServiceTemplateSchema() throws Exception {
125 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
129 void testUnauthorizedQueryToscaServiceCommonOrInstanceProperties() throws Exception {
130 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/getCommonOrInstanceProperties");
134 void testQueryToscaServiceTemplate() throws Exception {
135 createFullEntryInDbWithCommonProps();
137 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
138 Response rawresp = invocationBuilder.buildGet().invoke();
139 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
140 ToscaServiceTemplate template = rawresp.readEntity(ToscaServiceTemplate.class);
141 assertNotNull(template);
142 assertThat(template.getNodeTypes()).hasSize(7);
146 void testQueryToscaServiceTemplateSchema() throws Exception {
147 createFullEntryInDbWithCommonProps();
149 Invocation.Builder invocationBuilder =
150 super.sendRequest(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
151 Response rawresp = invocationBuilder.buildGet().invoke();
152 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
153 String schema = rawresp.readEntity(String.class);
154 assertNotNull(schema);
158 void testQueryCommonOrInstanceProperties() throws Exception {
159 createFullEntryInDbWithCommonProps();
161 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT
162 + "/getCommonOrInstanceProperties" + "?common=true&name=ToscaServiceTemplateSimple&version=1.0.0");
163 Response rawresp = invocationBuilder.buildGet().invoke();
164 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
166 @SuppressWarnings("unchecked")
167 Map<String, ToscaNodeTemplate> commonProperties = rawresp.readEntity(Map.class);
169 assertNotNull(commonProperties);
170 assertThat(commonProperties).hasSize(5);
174 void testCreateBadRequest() throws Exception {
175 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
176 Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte"));
178 assertThat(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).isEqualTo(resp.getStatus());
179 CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
180 assertThat(commissioningResponse.getErrorDetails()).isNotNull();
181 assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).isNull();
185 void testCreate() throws Exception {
186 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
187 Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
188 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
189 CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
191 assertNotNull(commissioningResponse);
192 assertNull(commissioningResponse.getErrorDetails());
193 // Response should return the number of node templates present in the service template
194 assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).hasSize(13);
195 for (String nodeTemplateName : serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().keySet()) {
196 assertTrue(commissioningResponse.getAffectedAutomationCompositionDefinitions().stream()
197 .anyMatch(ac -> ac.getName().equals(nodeTemplateName)));
202 void testQuery_NoResultWithThisName() throws Exception {
205 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name=noResultWithThisName");
206 Response rawresp = invocationBuilder.buildGet().invoke();
207 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
208 List<?> entityList = rawresp.readEntity(List.class);
209 assertThat(entityList).isEmpty();
213 void testQuery() throws Exception {
216 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
217 Response rawresp = invocationBuilder.buildGet().invoke();
218 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
219 List<?> entityList = rawresp.readEntity(List.class);
220 assertNotNull(entityList);
221 assertThat(entityList).hasSize(2);
225 void testQueryElementsBadRequest() throws Exception {
228 // Call get elements with no info
229 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/elements");
230 Response resp = invocationBuilder.buildGet().invoke();
231 assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), resp.getStatus());
235 void testQueryElements() throws Exception {
238 Invocation.Builder invocationBuilder = super.sendRequest(
239 COMMISSIONING_ENDPOINT + "/elements" + "?name=org.onap.domain.pmsh.PMSHAutomationCompositionDefinition");
240 Response rawresp = invocationBuilder.buildGet().invoke();
241 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
242 List<?> entityList = rawresp.readEntity(List.class);
243 assertNotNull(entityList);
244 assertThat(entityList).hasSize(4);
248 void testDeleteBadRequest() throws Exception {
251 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
252 // Call delete with no info
253 Response resp = invocationBuilder.delete();
254 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
258 void testDelete() throws Exception {
259 var serviceTemplateCreated = createEntryInDB();
261 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name="
262 + serviceTemplateCreated.getName() + "&version=" + serviceTemplateCreated.getVersion());
263 // Call delete with no info
264 Response resp = invocationBuilder.delete();
265 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
267 List<ToscaServiceTemplate> templatesInDB = serviceTemplateProvider.getAllServiceTemplates();
268 assertThat(templatesInDB).isEmpty();
271 private synchronized ToscaServiceTemplate createEntryInDB() throws Exception {
273 return serviceTemplateProvider.createServiceTemplate(serviceTemplate);
276 // Delete entries from the DB after relevant tests
277 private synchronized void deleteEntryInDB() throws Exception {
278 var list = serviceTemplateProvider.getAllServiceTemplates();
279 if (!list.isEmpty()) {
280 serviceTemplateProvider.deleteServiceTemplate(list.get(0).getName(), list.get(0).getVersion());
284 private synchronized void createFullEntryInDbWithCommonProps() throws Exception {
286 serviceTemplateProvider.createServiceTemplate(commonPropertiesServiceTemplate);