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.UUID;
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.AcDefinitionProvider;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
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 = "compositions";
64 private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
65 private UUID compositionId;
68 private AcDefinitionProvider acDefinitionProvider;
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);
82 public void setUpPort() {
83 super.setHttpPrefix(randomServerPort);
87 public void cleanDatabase() throws Exception {
93 super.testSwagger(COMMISSIONING_ENDPOINT);
97 void testUnauthorizedCreate() {
98 assertUnauthorizedPost(COMMISSIONING_ENDPOINT, Entity.json(serviceTemplate));
102 void testUnauthorizedQuery() {
103 assertUnauthorizedGet(COMMISSIONING_ENDPOINT);
107 void testUnauthorizedDelete() {
108 assertUnauthorizedDelete(COMMISSIONING_ENDPOINT);
112 void testCreateBadRequest() {
113 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
114 Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte"));
116 assertThat(Response.Status.BAD_REQUEST.getStatusCode()).isEqualTo(resp.getStatus());
117 CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
118 assertThat(commissioningResponse.getErrorDetails()).isNotNull();
119 assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).isNull();
124 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
125 Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
126 assertEquals(Response.Status.CREATED.getStatusCode(), resp.getStatus());
127 CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
128 compositionId = commissioningResponse.getCompositionId();
129 assertNotNull(commissioningResponse);
130 assertNull(commissioningResponse.getErrorDetails());
131 // Response should return the number of node templates present in the service template
132 assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).hasSize(7);
133 for (String nodeTemplateName : serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().keySet()) {
134 assertTrue(commissioningResponse.getAffectedAutomationCompositionDefinitions().stream()
135 .anyMatch(ac -> ac.getName().equals(nodeTemplateName)));
141 var invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
142 var resp = invocationBuilder.post(Entity.json(serviceTemplate));
143 assertEquals(Response.Status.CREATED.getStatusCode(), resp.getStatus());
144 var commissioningResponse = resp.readEntity(CommissioningResponse.class);
145 compositionId = commissioningResponse.getCompositionId();
147 var toscaDataType = new ToscaDataType();
148 toscaDataType.setName("org.onap.datatypes.policy.clamp.Configuration");
149 toscaDataType.setDerivedFrom("tosca.datatypes.Root");
150 toscaDataType.setProperties(new HashMap<>());
151 var toscaProperty = new ToscaProperty();
152 toscaProperty.setName("configurationEntityId");
153 toscaProperty.setType("onap.datatypes.ToscaConceptIdentifier");
154 toscaDataType.getProperties().put(toscaProperty.getName(), toscaProperty);
156 var serviceTemplateUpdate = new ToscaServiceTemplate(serviceTemplate);
157 serviceTemplateUpdate.getDataTypes().put(toscaDataType.getName(), toscaDataType);
158 invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/" + compositionId);
159 resp = invocationBuilder.put(Entity.json(serviceTemplateUpdate));
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(7);
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 var templatesInDB = acDefinitionProvider.getAllAcDefinitions();
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.getAllAcDefinitions();
232 if (!list.isEmpty()) {
233 acDefinitionProvider.deleteAcDefintion(compositionId);