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.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.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.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.clamp.controlloop.models.messages.rest.commissioning.CommissioningResponse;
37 import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController;
38 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40 import org.onap.policy.models.provider.PolicyModelsProvider;
41 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 public class CommissioningControllerTest extends CommonRestController {
46 private static final String TOSCA_SERVICE_TEMPLATE_YAML =
47 "src/test/resources/servicetemplates/pmsh_multiple_cl_tosca.yaml";
48 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
49 private static final String COMMISSIONING_ENDPOINT = "commission";
50 private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
53 * starts Main and inserts a commissioning template.
55 * @throws Exception if an error occurs
58 public static void setUpBeforeClass() throws Exception {
59 CommonRestController.setUpBeforeClass("CommissioningApi");
61 serviceTemplate = yamlTranslator.fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML),
62 ToscaServiceTemplate.class);
66 public static void teardownAfterClass() {
67 CommonRestController.teardownAfterClass();
71 public void testSwagger() throws Exception {
72 super.testSwagger(COMMISSIONING_ENDPOINT);
76 public void testUnauthorizedCreate() throws Exception {
77 assertUnauthorizedPost(COMMISSIONING_ENDPOINT, Entity.json(serviceTemplate));
81 public void testUnauthorizedQuery() throws Exception {
82 assertUnauthorizedGet(COMMISSIONING_ENDPOINT);
86 public void testUnauthorizedQueryElements() throws Exception {
87 assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/elements");
91 public void testUnauthorizedDelete() throws Exception {
92 assertUnauthorizedDelete(COMMISSIONING_ENDPOINT);
96 public void testCreateBadRequest() throws Exception {
97 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
98 Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte"));
100 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
101 CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
102 assertNotNull(commissioningResponse.getErrorDetails());
103 assertNull(commissioningResponse.getAffectedControlLoopDefinitions());
107 public void testCreate() throws Exception {
108 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
109 Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
110 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
111 CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
113 assertNotNull(commissioningResponse);
114 assertNull(commissioningResponse.getErrorDetails());
115 // Response should return the number of node templates present in the service template
116 assertThat(commissioningResponse.getAffectedControlLoopDefinitions()).hasSize(13);
117 for (String nodeTemplateName : serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().keySet()) {
118 assertTrue(commissioningResponse.getAffectedControlLoopDefinitions().stream()
119 .anyMatch(ac -> ac.getName().equals(nodeTemplateName)));
124 public void testQuery_NoResultWithThisName() throws Exception {
127 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name=noResultWithThisName");
128 Response rawresp = invocationBuilder.buildGet().invoke();
129 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
130 List entityList = rawresp.readEntity(List.class);
131 assertThat(entityList).isEmpty();
135 public void testQuery() throws Exception {
138 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
139 Response rawresp = invocationBuilder.buildGet().invoke();
140 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
141 List entityList = rawresp.readEntity(List.class);
142 assertNotNull(entityList);
143 assertThat(entityList).hasSize(2);
147 public void testQueryElementsBadRequest() throws Exception {
150 //Call get elements with no info
151 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/elements");
152 Response resp = invocationBuilder.buildGet().invoke();
153 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), resp.getStatus());
157 public void testQueryElements() throws Exception {
160 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/elements"
161 + "?name=org.onap.domain.pmsh.PMSHControlLoopDefinition");
162 Response rawresp = invocationBuilder.buildGet().invoke();
163 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
164 List entityList = rawresp.readEntity(List.class);
165 assertNotNull(entityList);
166 assertThat(entityList).hasSize(4);
170 public void testDeleteBadRequest() throws Exception {
173 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
174 //Call delete with no info
175 Response resp = invocationBuilder.delete();
176 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), resp.getStatus());
180 public void testDelete() throws Exception {
183 Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name="
184 + serviceTemplate.getName() + "&version=" + serviceTemplate.getVersion());
185 //Call delete with no info
186 Response resp = invocationBuilder.delete();
187 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
189 try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory()
190 .createPolicyModelsProvider(CommonRestController.getParameters())) {
191 List<ToscaServiceTemplate> templatesInDB = modelsProvider.getServiceTemplateList(null, null);
192 assertThat(templatesInDB).isEmpty();
196 private synchronized void createEntryInDB() throws Exception {
197 try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory()
198 .createPolicyModelsProvider(CommonRestController.getParameters())) {
199 modelsProvider.createServiceTemplate(serviceTemplate);