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 com.google.gson.Gson;
33 import com.google.gson.GsonBuilder;
34 import com.google.gson.JsonObject;
35 import com.google.gson.JsonParser;
36 import com.google.gson.JsonPrimitive;
37 import com.google.gson.JsonSerializer;
38 import java.util.List;
40 import javax.ws.rs.client.Entity;
41 import javax.ws.rs.client.Invocation;
42 import javax.ws.rs.core.Response;
43 import org.junit.jupiter.api.AfterEach;
44 import org.junit.jupiter.api.BeforeAll;
45 import org.junit.jupiter.api.BeforeEach;
46 import org.junit.jupiter.api.Test;
47 import org.junit.jupiter.api.extension.ExtendWith;
48 import org.junit.jupiter.api.parallel.Execution;
49 import org.junit.jupiter.api.parallel.ExecutionMode;
50 import org.onap.policy.clamp.acm.runtime.instantiation.InstantiationUtils;
51 import org.onap.policy.clamp.acm.runtime.util.rest.CommonRestController;
52 import org.onap.policy.clamp.models.acm.messages.rest.commissioning.CommissioningResponse;
53 import org.onap.policy.clamp.models.acm.persistence.provider.ServiceTemplateProvider;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.boot.test.context.SpringBootTest;
58 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
59 import org.springframework.boot.web.server.LocalServerPort;
60 import org.springframework.test.context.ActiveProfiles;
61 import org.springframework.test.context.junit.jupiter.SpringExtension;
63 @ExtendWith(SpringExtension.class)
64 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
65 @ActiveProfiles("test")
66 @Execution(ExecutionMode.SAME_THREAD)
67 class CommissioningControllerTest extends CommonRestController {
69 private static final String COMMISSIONING_ENDPOINT = "commission";
70 private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
71 private static ToscaServiceTemplate commonPropertiesServiceTemplate = new ToscaServiceTemplate();
74 private ServiceTemplateProvider serviceTemplateProvider;
77 private int randomServerPort;
80 * starts Main and inserts a commissioning template.
83 public static void setUpBeforeClass() {
84 serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
85 commonPropertiesServiceTemplate =
86 InstantiationUtils.getToscaServiceTemplate(TOSCA_ST_TEMPLATE_YAML);
90 public void setUpPort() {
91 super.setHttpPrefix(randomServerPort);
95 public void cleanDatabase() throws Exception {
100 void testSwagger() throws Exception {
101 super.testSwagger(COMMISSIONING_ENDPOINT);
105 void testUnauthorizedCreate() throws Exception {
106 assertUnauthorizedPost(COMMISSIONING_ENDPOINT, Entity.json(serviceTemplate));
110 void testUnauthorizedQuery() throws Exception {
111 assertUnauthorizedGet(COMMISSIONING_ENDPOINT);
115 void testUnauthorizedQueryElements() throws Exception {
116 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/elements");
120 void testUnauthorizedDelete() throws Exception {
121 assertUnauthorizedDelete(COMMISSIONING_ENDPOINT);
125 void testUnauthorizedQueryToscaServiceTemplate() throws Exception {
126 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
130 void testUnauthorizedQueryToscaServiceTemplateSchema() throws Exception {
131 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
135 void testUnauthorizedQueryToscaServiceCommonOrInstanceProperties() throws Exception {
136 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/getCommonOrInstanceProperties");
140 void testQueryToscaServiceTemplate() throws Exception {
141 createFullEntryInDbWithCommonProps();
143 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
144 Response rawresp = invocationBuilder.buildGet().invoke();
145 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
146 String template = rawresp.readEntity(String.class);
147 final JsonObject jsonObject = new JsonParser().parse(template).getAsJsonObject();
149 Gson gson = new Gson();
150 GsonBuilder builder = new GsonBuilder();
151 builder.registerTypeAdapter(ToscaNodeTemplate.class,
152 (JsonSerializer<ToscaNodeTemplate>) (src, typeOfSrc, context) -> new JsonPrimitive(src.toString()));
153 builder.setPrettyPrinting();
154 gson = builder.create();
156 ToscaNodeTemplate toscaNodeTemplate = gson.fromJson(jsonObject, ToscaNodeTemplate.class);
157 assertNotNull(toscaNodeTemplate);
161 void testQueryToscaServiceTemplateSchema() throws Exception {
162 createFullEntryInDbWithCommonProps();
164 Invocation.Builder invocationBuilder =
165 super.sendRequest(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
166 Response rawresp = invocationBuilder.buildGet().invoke();
167 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
168 String schema = rawresp.readEntity(String.class);
169 assertNotNull(schema);
173 void testQueryCommonOrInstanceProperties() throws Exception {
174 createFullEntryInDbWithCommonProps();
176 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT
177 + "/getCommonOrInstanceProperties" + "?common=true&name=ToscaServiceTemplateSimple&version=1.0.0");
178 Response rawresp = invocationBuilder.buildGet().invoke();
179 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
181 @SuppressWarnings("unchecked")
182 Map<String, ToscaNodeTemplate> commonProperties = rawresp.readEntity(Map.class);
184 assertNotNull(commonProperties);
185 assertThat(commonProperties).hasSize(5);
189 void testCreateBadRequest() throws Exception {
190 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
191 Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte"));
193 assertThat(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).isEqualTo(resp.getStatus());
194 CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
195 assertThat(commissioningResponse.getErrorDetails()).isNotNull();
196 assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).isNull();
200 void testCreate() throws Exception {
201 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
202 Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
203 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
204 CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
206 assertNotNull(commissioningResponse);
207 assertNull(commissioningResponse.getErrorDetails());
208 // Response should return the number of node templates present in the service template
209 assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).hasSize(13);
210 for (String nodeTemplateName : serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().keySet()) {
211 assertTrue(commissioningResponse.getAffectedAutomationCompositionDefinitions().stream()
212 .anyMatch(ac -> ac.getName().equals(nodeTemplateName)));
217 void testQuery_NoResultWithThisName() throws Exception {
220 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name=noResultWithThisName");
221 Response rawresp = invocationBuilder.buildGet().invoke();
222 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
223 List<?> entityList = rawresp.readEntity(List.class);
224 assertThat(entityList).isEmpty();
228 void testQuery() throws Exception {
231 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
232 Response rawresp = invocationBuilder.buildGet().invoke();
233 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
234 List<?> entityList = rawresp.readEntity(List.class);
235 assertNotNull(entityList);
236 assertThat(entityList).hasSize(2);
240 void testQueryElementsBadRequest() throws Exception {
243 // Call get elements with no info
244 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/elements");
245 Response resp = invocationBuilder.buildGet().invoke();
246 assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), resp.getStatus());
250 void testQueryElements() throws Exception {
253 Invocation.Builder invocationBuilder = super.sendRequest(
254 COMMISSIONING_ENDPOINT + "/elements" + "?name=org.onap.domain.pmsh.PMSHAutomationCompositionDefinition");
255 Response rawresp = invocationBuilder.buildGet().invoke();
256 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
257 List<?> entityList = rawresp.readEntity(List.class);
258 assertNotNull(entityList);
259 assertThat(entityList).hasSize(4);
263 void testDeleteBadRequest() throws Exception {
266 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
267 // Call delete with no info
268 Response resp = invocationBuilder.delete();
269 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
273 void testDelete() throws Exception {
274 var serviceTemplateCreated = createEntryInDB();
276 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name="
277 + serviceTemplateCreated.getName() + "&version=" + serviceTemplateCreated.getVersion());
278 // Call delete with no info
279 Response resp = invocationBuilder.delete();
280 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
282 List<ToscaServiceTemplate> templatesInDB = serviceTemplateProvider.getAllServiceTemplates();
283 assertThat(templatesInDB).isEmpty();
286 private synchronized ToscaServiceTemplate createEntryInDB() throws Exception {
288 return serviceTemplateProvider.createServiceTemplate(serviceTemplate);
291 // Delete entries from the DB after relevant tests
292 private synchronized void deleteEntryInDB() throws Exception {
293 var list = serviceTemplateProvider.getAllServiceTemplates();
294 if (!list.isEmpty()) {
295 serviceTemplateProvider.deleteServiceTemplate(list.get(0).getName(), list.get(0).getVersion());
299 private synchronized void createFullEntryInDbWithCommonProps() throws Exception {
301 serviceTemplateProvider.createServiceTemplate(commonPropertiesServiceTemplate);