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;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.UUID;
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.AcDefinitionProvider;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.boot.test.context.SpringBootTest;
53 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
54 import org.springframework.boot.web.server.LocalServerPort;
55 import org.springframework.test.context.ActiveProfiles;
56 import org.springframework.test.context.junit.jupiter.SpringExtension;
58 @ExtendWith(SpringExtension.class)
59 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
60 @ActiveProfiles("test")
61 @Execution(ExecutionMode.SAME_THREAD)
62 class CommissioningControllerTest extends CommonRestController {
64 private static final String COMMISSIONING_ENDPOINT = "commission";
65 private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
66 private UUID compositionId;
69 private AcDefinitionProvider acDefinitionProvider;
72 private int randomServerPort;
75 * starts Main and inserts a commissioning template.
78 public static void setUpBeforeClass() {
79 serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_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 testUnauthorizedDelete() {
109 assertUnauthorizedDelete(COMMISSIONING_ENDPOINT);
113 void testCreateBadRequest() {
114 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
115 Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte"));
117 assertThat(Response.Status.BAD_REQUEST.getStatusCode()).isEqualTo(resp.getStatus());
118 CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
119 assertThat(commissioningResponse.getErrorDetails()).isNotNull();
120 assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).isNull();
125 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
126 Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
127 assertEquals(Response.Status.CREATED.getStatusCode(), resp.getStatus());
128 CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
129 compositionId = commissioningResponse.getCompositionId();
130 assertNotNull(commissioningResponse);
131 assertNull(commissioningResponse.getErrorDetails());
132 // Response should return the number of node templates present in the service template
133 assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).hasSize(13);
134 for (String nodeTemplateName : serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().keySet()) {
135 assertTrue(commissioningResponse.getAffectedAutomationCompositionDefinitions().stream()
136 .anyMatch(ac -> ac.getName().equals(nodeTemplateName)));
142 var invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
143 var resp = invocationBuilder.post(Entity.json(serviceTemplate));
144 assertEquals(Response.Status.CREATED.getStatusCode(), resp.getStatus());
145 var commissioningResponse = resp.readEntity(CommissioningResponse.class);
146 compositionId = commissioningResponse.getCompositionId();
148 var toscaDataType = new ToscaDataType();
149 toscaDataType.setName("org.onap.datatypes.policy.clamp.Configuration");
150 toscaDataType.setDerivedFrom("tosca.datatypes.Root");
151 toscaDataType.setProperties(new HashMap<>());
152 var toscaProperty = new ToscaProperty();
153 toscaProperty.setName("configurationEntityId");
154 toscaProperty.setType("onap.datatypes.ToscaConceptIdentifier");
155 toscaDataType.getProperties().put(toscaProperty.getName(), toscaProperty);
157 serviceTemplate.getDataTypes().put(toscaDataType.getName(), toscaDataType);
158 invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/" + compositionId);
159 resp = invocationBuilder.put(Entity.json(serviceTemplate));
160 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
161 commissioningResponse = resp.readEntity(CommissioningResponse.class);
162 assertNotNull(commissioningResponse);
163 assertNull(commissioningResponse.getErrorDetails());
164 // Response should return the number of node templates present in the service template
165 assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).hasSize(13);
166 for (String nodeTemplateName : serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().keySet()) {
167 assertTrue(commissioningResponse.getAffectedAutomationCompositionDefinitions().stream()
168 .anyMatch(ac -> ac.getName().equals(nodeTemplateName)));
171 invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/" + compositionId);
172 resp = invocationBuilder.buildGet().invoke();
173 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
174 var entity = resp.readEntity(ToscaServiceTemplate.class);
175 assertThat(entity.getDataTypes()).containsKey(toscaDataType.getName());
179 void testQuery_NoResultWithThisName() throws Exception {
182 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name=noResultWithThisName");
183 Response rawresp = invocationBuilder.buildGet().invoke();
184 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
185 var entityList = rawresp.readEntity(ToscaServiceTemplate.class);
186 assertThat(entityList.getNodeTypes()).isNull();
190 void testQuery() throws Exception {
193 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
194 Response rawresp = invocationBuilder.buildGet().invoke();
195 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
196 var entityList = rawresp.readEntity(ToscaServiceTemplate.class);
197 assertNotNull(entityList);
201 void testDeleteBadRequest() throws Exception {
204 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/" + UUID.randomUUID());
205 // Call delete with no info
206 Response resp = invocationBuilder.delete();
207 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), resp.getStatus());
211 void testDelete() throws Exception {
214 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/" + compositionId);
215 // Call delete with no info
216 Response resp = invocationBuilder.delete();
217 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
219 List<ToscaServiceTemplate> templatesInDB = acDefinitionProvider.getAllServiceTemplates();
220 assertThat(templatesInDB).isEmpty();
223 private synchronized void createEntryInDB() throws Exception {
225 var acmDefinition = acDefinitionProvider.createAutomationCompositionDefinition(serviceTemplate);
226 compositionId = acmDefinition.getCompositionId();
229 // Delete entries from the DB after relevant tests
230 private synchronized void deleteEntryInDB() throws Exception {
231 var list = acDefinitionProvider.getAllServiceTemplates();
232 if (!list.isEmpty()) {
233 acDefinitionProvider.deleteAcDefintion(compositionId);