2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.runtime.commissioning.rest;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
29 import java.util.List;
30 import javax.ws.rs.client.Entity;
31 import javax.ws.rs.client.Invocation;
32 import javax.ws.rs.core.Response;
33 import org.junit.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.extension.ExtendWith;
37 import org.onap.policy.clamp.controlloop.models.messages.rest.commissioning.CommissioningResponse;
38 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
39 import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController;
40 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
41 import org.onap.policy.common.utils.resources.ResourceUtils;
42 import org.onap.policy.models.provider.PolicyModelsProvider;
43 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.boot.test.context.SpringBootTest;
47 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
48 import org.springframework.boot.web.server.LocalServerPort;
49 import org.springframework.test.context.TestPropertySource;
50 import org.springframework.test.context.junit.jupiter.SpringExtension;
52 @ExtendWith(SpringExtension.class)
53 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
54 @TestPropertySource(locations = {"classpath:application_test.properties"})
55 class CommissioningControllerTest extends CommonRestController {
57 private static final String TOSCA_SERVICE_TEMPLATE_YAML =
58 "src/test/resources/rest/servicetemplates/pmsh_multiple_cl_tosca.yaml";
59 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
60 private static final String COMMISSIONING_ENDPOINT = "commission";
61 private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
64 private ClRuntimeParameterGroup clRuntimeParameterGroup;
67 private int randomServerPort;
70 * starts Main and inserts a commissioning template.
72 * @throws Exception if an error occurs
75 public static void setUpBeforeClass() throws Exception {
77 serviceTemplate = yamlTranslator.fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML),
78 ToscaServiceTemplate.class);
82 public void setUpPort() {
83 super.setHttpPrefix(randomServerPort);
87 void testSwagger() throws Exception {
88 super.testSwagger(COMMISSIONING_ENDPOINT);
92 void testUnauthorizedCreate() throws Exception {
93 assertUnauthorizedPost(COMMISSIONING_ENDPOINT, Entity.json(serviceTemplate));
97 void testUnauthorizedQuery() throws Exception {
98 assertUnauthorizedGet(COMMISSIONING_ENDPOINT);
102 void testUnauthorizedQueryElements() throws Exception {
103 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/elements");
107 void testUnauthorizedDelete() throws Exception {
108 assertUnauthorizedDelete(COMMISSIONING_ENDPOINT);
112 void testCreateBadRequest() throws Exception {
113 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
114 Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte"));
116 assertThat(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).isEqualTo(resp.getStatus());
117 CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
118 assertThat(commissioningResponse.getErrorDetails()).isNotNull();
119 assertThat(commissioningResponse.getAffectedControlLoopDefinitions()).isNull();
123 void testCreate() throws Exception {
124 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
125 Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
126 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
127 CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
129 assertNotNull(commissioningResponse);
130 assertNull(commissioningResponse.getErrorDetails());
131 // Response should return the number of node templates present in the service template
132 assertThat(commissioningResponse.getAffectedControlLoopDefinitions()).hasSize(13);
133 for (String nodeTemplateName : serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().keySet()) {
134 assertTrue(commissioningResponse.getAffectedControlLoopDefinitions().stream()
135 .anyMatch(ac -> ac.getName().equals(nodeTemplateName)));
140 void testQuery_NoResultWithThisName() throws Exception {
143 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name=noResultWithThisName");
144 Response rawresp = invocationBuilder.buildGet().invoke();
145 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
146 List<?> entityList = rawresp.readEntity(List.class);
147 assertThat(entityList).isEmpty();
151 void testQuery() throws Exception {
154 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
155 Response rawresp = invocationBuilder.buildGet().invoke();
156 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
157 List<?> entityList = rawresp.readEntity(List.class);
158 assertNotNull(entityList);
159 assertThat(entityList).hasSize(2);
163 void testQueryElementsBadRequest() throws Exception {
166 //Call get elements with no info
167 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/elements");
168 Response resp = invocationBuilder.buildGet().invoke();
169 assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), resp.getStatus());
173 void testQueryElements() throws Exception {
176 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/elements"
177 + "?name=org.onap.domain.pmsh.PMSHControlLoopDefinition");
178 Response rawresp = invocationBuilder.buildGet().invoke();
179 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
180 List<?> entityList = rawresp.readEntity(List.class);
181 assertNotNull(entityList);
182 assertThat(entityList).hasSize(4);
186 void testDeleteBadRequest() throws Exception {
189 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
190 //Call delete with no info
191 Response resp = invocationBuilder.delete();
192 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
196 void testDelete() throws Exception {
199 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name="
200 + serviceTemplate.getName() + "&version=" + serviceTemplate.getVersion());
201 //Call delete with no info
202 Response resp = invocationBuilder.delete();
203 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
205 try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory()
206 .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) {
207 List<ToscaServiceTemplate> templatesInDB = modelsProvider.getServiceTemplateList(null, null);
208 assertThat(templatesInDB).isEmpty();
212 private synchronized void createEntryInDB() throws Exception {
213 try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory()
214 .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) {
215 modelsProvider.createServiceTemplate(serviceTemplate);