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 = InstantiationUtils.getToscaServiceTemplate(TOSCA_ST_TEMPLATE_YAML);
83 public void setUpPort() {
84 super.setHttpPrefix(randomServerPort);
88 public void cleanDatabase() throws Exception {
94 super.testSwagger(COMMISSIONING_ENDPOINT);
98 void testUnauthorizedCreate() {
99 assertUnauthorizedPost(COMMISSIONING_ENDPOINT, Entity.json(serviceTemplate));
103 void testUnauthorizedQuery() {
104 assertUnauthorizedGet(COMMISSIONING_ENDPOINT);
108 void testUnauthorizedQueryElements() {
109 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/elements");
113 void testUnauthorizedDelete() {
114 assertUnauthorizedDelete(COMMISSIONING_ENDPOINT);
118 void testUnauthorizedQueryToscaServiceTemplate() {
119 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
123 void testUnauthorizedQueryToscaServiceTemplateSchema() {
124 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
128 void testUnauthorizedQueryToscaServiceCommonOrInstanceProperties() {
129 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/getCommonOrInstanceProperties");
133 void testQueryToscaServiceTemplate() throws Exception {
134 createFullEntryInDbWithCommonProps();
136 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
137 Response rawresp = invocationBuilder.buildGet().invoke();
138 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
139 ToscaServiceTemplate template = rawresp.readEntity(ToscaServiceTemplate.class);
140 assertNotNull(template);
141 assertThat(template.getNodeTypes()).hasSize(7);
145 void testQueryToscaServiceTemplateSchema() throws Exception {
146 createFullEntryInDbWithCommonProps();
148 Invocation.Builder invocationBuilder =
149 super.sendRequest(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
150 Response rawresp = invocationBuilder.buildGet().invoke();
151 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
152 String schema = rawresp.readEntity(String.class);
153 assertNotNull(schema);
157 void testQueryCommonOrInstanceProperties() throws Exception {
158 createFullEntryInDbWithCommonProps();
160 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT
161 + "/getCommonOrInstanceProperties?common=true");
162 Response rawresp = invocationBuilder.buildGet().invoke();
163 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
165 @SuppressWarnings("unchecked")
166 Map<String, ToscaNodeTemplate> commonProperties = rawresp.readEntity(Map.class);
168 assertNotNull(commonProperties);
169 assertThat(commonProperties).hasSize(4);
173 void testCreateBadRequest() {
174 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
175 Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte"));
177 assertThat(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).isEqualTo(resp.getStatus());
178 CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
179 assertThat(commissioningResponse.getErrorDetails()).isNotNull();
180 assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).isNull();
185 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
186 Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
187 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
188 CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
190 assertNotNull(commissioningResponse);
191 assertNull(commissioningResponse.getErrorDetails());
192 // Response should return the number of node templates present in the service template
193 assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).hasSize(13);
194 for (String nodeTemplateName : serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().keySet()) {
195 assertTrue(commissioningResponse.getAffectedAutomationCompositionDefinitions().stream()
196 .anyMatch(ac -> ac.getName().equals(nodeTemplateName)));
201 void testQuery_NoResultWithThisName() throws Exception {
204 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name=noResultWithThisName");
205 Response rawresp = invocationBuilder.buildGet().invoke();
206 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
207 List<?> entityList = rawresp.readEntity(List.class);
208 assertThat(entityList).isEmpty();
212 void testQuery() throws Exception {
215 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
216 Response rawresp = invocationBuilder.buildGet().invoke();
217 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
218 List<?> entityList = rawresp.readEntity(List.class);
219 assertNotNull(entityList);
220 assertThat(entityList).hasSize(2);
224 void testQueryElementsBadRequest() throws Exception {
227 // Call get elements with no info
228 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/elements");
229 Response resp = invocationBuilder.buildGet().invoke();
230 assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), resp.getStatus());
234 void testQueryElements() throws Exception {
237 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/elements"
238 + "?name=org.onap.domain.pmsh.PMSHAutomationCompositionDefinition");
239 Response rawresp = invocationBuilder.buildGet().invoke();
240 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
241 List<?> entityList = rawresp.readEntity(List.class);
242 assertNotNull(entityList);
243 assertThat(entityList).hasSize(4);
247 void testDeleteBadRequest() throws Exception {
250 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
251 // Call delete with no info
252 Response resp = invocationBuilder.delete();
253 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
257 void testDelete() throws Exception {
258 var serviceTemplateCreated = createEntryInDB();
260 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name="
261 + serviceTemplateCreated.getName() + "&version=" + serviceTemplateCreated.getVersion());
262 // Call delete with no info
263 Response resp = invocationBuilder.delete();
264 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
266 List<ToscaServiceTemplate> templatesInDB = serviceTemplateProvider.getAllServiceTemplates();
267 assertThat(templatesInDB).isEmpty();
270 private synchronized ToscaServiceTemplate createEntryInDB() throws Exception {
272 return serviceTemplateProvider.createServiceTemplate(serviceTemplate);
275 // Delete entries from the DB after relevant tests
276 private synchronized void deleteEntryInDB() throws Exception {
277 var list = serviceTemplateProvider.getAllServiceTemplates();
278 if (!list.isEmpty()) {
279 serviceTemplateProvider.deleteServiceTemplate(list.get(0).getName(), list.get(0).getVersion());
283 private synchronized void createFullEntryInDbWithCommonProps() throws Exception {
285 serviceTemplateProvider.createServiceTemplate(commonPropertiesServiceTemplate);