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;
33 import javax.ws.rs.client.Entity;
34 import javax.ws.rs.client.Invocation;
35 import javax.ws.rs.core.Response;
36 import org.junit.jupiter.api.AfterEach;
37 import org.junit.jupiter.api.BeforeAll;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.Test;
40 import org.junit.jupiter.api.extension.ExtendWith;
41 import org.junit.jupiter.api.parallel.Execution;
42 import org.junit.jupiter.api.parallel.ExecutionMode;
43 import org.onap.policy.clamp.acm.runtime.instantiation.InstantiationUtils;
44 import org.onap.policy.clamp.acm.runtime.util.rest.CommonRestController;
45 import org.onap.policy.clamp.models.acm.messages.rest.commissioning.CommissioningResponse;
46 import org.onap.policy.clamp.models.acm.persistence.provider.ServiceTemplateProvider;
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.ActiveProfiles;
53 import org.springframework.test.context.junit.jupiter.SpringExtension;
55 @ExtendWith(SpringExtension.class)
56 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
57 @ActiveProfiles("test")
58 @Execution(ExecutionMode.SAME_THREAD)
59 class CommissioningControllerTest extends CommonRestController {
61 private static final String COMMISSIONING_ENDPOINT = "commission";
62 private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
63 private static ToscaServiceTemplate commonPropertiesServiceTemplate = new ToscaServiceTemplate();
66 private ServiceTemplateProvider serviceTemplateProvider;
69 private int randomServerPort;
72 * starts Main and inserts a commissioning template.
75 public static void setUpBeforeClass() {
76 serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
77 commonPropertiesServiceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_ST_TEMPLATE_YAML);
81 public void setUpPort() {
82 super.setHttpPrefix(randomServerPort);
86 public void cleanDatabase() throws Exception {
92 super.testSwagger(COMMISSIONING_ENDPOINT);
96 void testUnauthorizedCreate() {
97 assertUnauthorizedPost(COMMISSIONING_ENDPOINT, Entity.json(serviceTemplate));
101 void testUnauthorizedQuery() {
102 assertUnauthorizedGet(COMMISSIONING_ENDPOINT);
106 void testUnauthorizedQueryElements() {
107 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/elements");
111 void testUnauthorizedDelete() {
112 assertUnauthorizedDelete(COMMISSIONING_ENDPOINT);
116 void testUnauthorizedQueryToscaServiceTemplate() {
117 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
121 void testUnauthorizedQueryToscaServiceTemplateSchema() {
122 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
126 void testUnauthorizedQueryToscaServiceCommonOrInstanceProperties() {
127 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/getCommonOrInstanceProperties");
131 void testQueryToscaServiceTemplate() throws Exception {
132 createFullEntryInDbWithCommonProps();
134 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
135 Response rawresp = invocationBuilder.buildGet().invoke();
136 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
137 ToscaServiceTemplate template = rawresp.readEntity(ToscaServiceTemplate.class);
138 assertNotNull(template);
139 assertThat(template.getNodeTypes()).hasSize(7);
143 void testCreateBadRequest() {
144 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
145 Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte"));
147 assertThat(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).isEqualTo(resp.getStatus());
148 CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
149 assertThat(commissioningResponse.getErrorDetails()).isNotNull();
150 assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).isNull();
155 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
156 Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
157 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
158 CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
160 assertNotNull(commissioningResponse);
161 assertNull(commissioningResponse.getErrorDetails());
162 // Response should return the number of node templates present in the service template
163 assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).hasSize(13);
164 for (String nodeTemplateName : serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().keySet()) {
165 assertTrue(commissioningResponse.getAffectedAutomationCompositionDefinitions().stream()
166 .anyMatch(ac -> ac.getName().equals(nodeTemplateName)));
171 void testQuery_NoResultWithThisName() throws Exception {
174 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name=noResultWithThisName");
175 Response rawresp = invocationBuilder.buildGet().invoke();
176 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
177 List<?> entityList = rawresp.readEntity(List.class);
178 assertThat(entityList).isEmpty();
182 void testQuery() throws Exception {
185 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
186 Response rawresp = invocationBuilder.buildGet().invoke();
187 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
188 List<?> entityList = rawresp.readEntity(List.class);
189 assertNotNull(entityList);
190 assertThat(entityList).hasSize(2);
194 void testDeleteBadRequest() throws Exception {
197 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
198 // Call delete with no info
199 Response resp = invocationBuilder.delete();
200 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
204 void testDelete() throws Exception {
205 var serviceTemplateCreated = createEntryInDB();
207 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name="
208 + serviceTemplateCreated.getName() + "&version=" + serviceTemplateCreated.getVersion());
209 // Call delete with no info
210 Response resp = invocationBuilder.delete();
211 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
213 List<ToscaServiceTemplate> templatesInDB = serviceTemplateProvider.getAllServiceTemplates();
214 assertThat(templatesInDB).isEmpty();
217 private synchronized ToscaServiceTemplate createEntryInDB() throws Exception {
219 return serviceTemplateProvider.createServiceTemplate(serviceTemplate);
222 // Delete entries from the DB after relevant tests
223 private synchronized void deleteEntryInDB() throws Exception {
224 var list = serviceTemplateProvider.getAllServiceTemplates();
225 if (!list.isEmpty()) {
226 serviceTemplateProvider.deleteServiceTemplate(list.get(0).getName(), list.get(0).getVersion());
230 private synchronized void createFullEntryInDbWithCommonProps() throws Exception {
232 serviceTemplateProvider.createServiceTemplate(commonPropertiesServiceTemplate);