f7749e164a01871b1c46c514f954c848f0a6c98a
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.runtime.commissioning.rest;
22
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;
28
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;
43
44 public class CommissioningControllerTest extends CommonRestController {
45
46     private static final String TOSCA_SERVICE_TEMPLATE_YAML =
47             "src/test/resources/rest/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();
51
52     /**
53      * starts Main and inserts a commissioning template.
54      *
55      * @throws Exception if an error occurs
56      */
57     @BeforeClass
58     public static void setUpBeforeClass() throws Exception {
59         CommonRestController.setUpBeforeClass("CommissioningApi");
60
61         serviceTemplate = yamlTranslator.fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML),
62                 ToscaServiceTemplate.class);
63     }
64
65     @AfterClass
66     public static void teardownAfterClass() {
67         CommonRestController.teardownAfterClass();
68     }
69
70     @Test
71     public void testSwagger() throws Exception {
72         super.testSwagger(COMMISSIONING_ENDPOINT);
73     }
74
75     @Test
76     public void testUnauthorizedCreate() throws Exception {
77         assertUnauthorizedPost(COMMISSIONING_ENDPOINT, Entity.json(serviceTemplate));
78     }
79
80     @Test
81     public void testUnauthorizedQuery() throws Exception {
82         assertUnauthorizedGet(COMMISSIONING_ENDPOINT);
83     }
84
85     @Test
86     public void testUnauthorizedQueryElements() throws Exception {
87         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/elements");
88     }
89
90     @Test
91     public void testUnauthorizedDelete() throws Exception {
92         assertUnauthorizedDelete(COMMISSIONING_ENDPOINT);
93     }
94
95     @Test
96     public void testCreateBadRequest() throws Exception {
97         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
98         Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte"));
99
100         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
101         CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
102         assertNotNull(commissioningResponse.getErrorDetails());
103         assertNull(commissioningResponse.getAffectedControlLoopDefinitions());
104     }
105
106     @Test
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);
112
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)));
120         }
121     }
122
123     @Test
124     public void testQuery_NoResultWithThisName() throws Exception {
125         createEntryInDB();
126
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();
132     }
133
134     @Test
135     public void testQuery() throws Exception {
136         createEntryInDB();
137
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);
144     }
145
146     @Test
147     public void testQueryElementsBadRequest() throws Exception {
148         createEntryInDB();
149
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.NOT_ACCEPTABLE.getStatusCode(), resp.getStatus());
154     }
155
156     @Test
157     public void testQueryElements() throws Exception {
158         createEntryInDB();
159
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);
167     }
168
169     @Test
170     public void testDeleteBadRequest() throws Exception {
171         createEntryInDB();
172
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());
177     }
178
179     @Test
180     public void testDelete() throws Exception {
181         createEntryInDB();
182
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());
188
189         try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory()
190                 .createPolicyModelsProvider(CommonRestController.getParameters())) {
191             List<ToscaServiceTemplate> templatesInDB = modelsProvider.getServiceTemplateList(null, null);
192             assertThat(templatesInDB).isEmpty();
193         }
194     }
195
196     private synchronized void createEntryInDB() throws Exception {
197         try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory()
198                 .createPolicyModelsProvider(CommonRestController.getParameters())) {
199             modelsProvider.createServiceTemplate(serviceTemplate);
200         }
201     }
202 }