f34e5eff0dec032ded4d92000018da69b80a4713
[policy/clamp.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.acm.runtime.commissioning.rest;
23
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;
31
32 import java.util.List;
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.ServiceTemplateProvider;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.boot.test.context.SpringBootTest;
50 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
51 import org.springframework.boot.web.server.LocalServerPort;
52 import org.springframework.test.context.ActiveProfiles;
53 import org.springframework.test.context.junit.jupiter.SpringExtension;
54
55 @ExtendWith(SpringExtension.class)
56 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
57 @ActiveProfiles("test")
58 @Execution(ExecutionMode.SAME_THREAD)
59 class CommissioningControllerTest extends CommonRestController {
60
61     private static final String COMMISSIONING_ENDPOINT = "commission";
62     private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
63     private static ToscaServiceTemplate commonPropertiesServiceTemplate = new ToscaServiceTemplate();
64
65     @Autowired
66     private ServiceTemplateProvider serviceTemplateProvider;
67
68     @LocalServerPort
69     private int randomServerPort;
70
71     /**
72      * starts Main and inserts a commissioning template.
73      */
74     @BeforeAll
75     public static void setUpBeforeClass() {
76         serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
77         commonPropertiesServiceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_ST_TEMPLATE_YAML);
78     }
79
80     @BeforeEach
81     public void setUpPort() {
82         super.setHttpPrefix(randomServerPort);
83     }
84
85     @AfterEach
86     public void cleanDatabase() throws Exception {
87         deleteEntryInDB();
88     }
89
90     @Test
91     void testSwagger() {
92         super.testSwagger(COMMISSIONING_ENDPOINT);
93     }
94
95     @Test
96     void testUnauthorizedCreate() {
97         assertUnauthorizedPost(COMMISSIONING_ENDPOINT, Entity.json(serviceTemplate));
98     }
99
100     @Test
101     void testUnauthorizedQuery() {
102         assertUnauthorizedGet(COMMISSIONING_ENDPOINT);
103     }
104
105     @Test
106     void testUnauthorizedQueryElements() {
107         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/elements");
108     }
109
110     @Test
111     void testUnauthorizedDelete() {
112         assertUnauthorizedDelete(COMMISSIONING_ENDPOINT);
113     }
114
115     @Test
116     void testUnauthorizedQueryToscaServiceTemplate() {
117         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
118     }
119
120     @Test
121     void testUnauthorizedQueryToscaServiceTemplateSchema() {
122         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema");
123     }
124
125     @Test
126     void testUnauthorizedQueryToscaServiceCommonOrInstanceProperties() {
127         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/getCommonOrInstanceProperties");
128     }
129
130     @Test
131     void testQueryToscaServiceTemplate() throws Exception {
132         createFullEntryInDbWithCommonProps();
133
134         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/toscaservicetemplate");
135         Response rawresp = invocationBuilder.buildGet().invoke();
136         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
137         ToscaServiceTemplate template = rawresp.readEntity(ToscaServiceTemplate.class);
138         assertNotNull(template);
139         assertThat(template.getNodeTypes()).hasSize(7);
140     }
141
142     @Test
143     void testCreateBadRequest() {
144         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
145         Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte"));
146
147         assertThat(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).isEqualTo(resp.getStatus());
148         CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
149         assertThat(commissioningResponse.getErrorDetails()).isNotNull();
150         assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).isNull();
151     }
152
153     @Test
154     void testCreate() {
155         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
156         Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
157         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
158         CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
159
160         assertNotNull(commissioningResponse);
161         assertNull(commissioningResponse.getErrorDetails());
162         // Response should return the number of node templates present in the service template
163         assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).hasSize(13);
164         for (String nodeTemplateName : serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().keySet()) {
165             assertTrue(commissioningResponse.getAffectedAutomationCompositionDefinitions().stream()
166                     .anyMatch(ac -> ac.getName().equals(nodeTemplateName)));
167         }
168     }
169
170     @Test
171     void testQuery_NoResultWithThisName() throws Exception {
172         createEntryInDB();
173
174         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name=noResultWithThisName");
175         Response rawresp = invocationBuilder.buildGet().invoke();
176         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
177         List<?> entityList = rawresp.readEntity(List.class);
178         assertThat(entityList).isEmpty();
179     }
180
181     @Test
182     void testQuery() throws Exception {
183         createEntryInDB();
184
185         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
186         Response rawresp = invocationBuilder.buildGet().invoke();
187         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
188         List<?> entityList = rawresp.readEntity(List.class);
189         assertNotNull(entityList);
190         assertThat(entityList).hasSize(2);
191     }
192
193     @Test
194     void testDeleteBadRequest() throws Exception {
195         createEntryInDB();
196
197         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
198         // Call delete with no info
199         Response resp = invocationBuilder.delete();
200         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
201     }
202
203     @Test
204     void testDelete() throws Exception {
205         var serviceTemplateCreated = createEntryInDB();
206
207         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name="
208                 + serviceTemplateCreated.getName() + "&version=" + serviceTemplateCreated.getVersion());
209         // Call delete with no info
210         Response resp = invocationBuilder.delete();
211         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
212
213         List<ToscaServiceTemplate> templatesInDB = serviceTemplateProvider.getAllServiceTemplates();
214         assertThat(templatesInDB).isEmpty();
215     }
216
217     private synchronized ToscaServiceTemplate createEntryInDB() throws Exception {
218         deleteEntryInDB();
219         return serviceTemplateProvider.createServiceTemplate(serviceTemplate);
220     }
221
222     // Delete entries from the DB after relevant tests
223     private synchronized void deleteEntryInDB() throws Exception {
224         var list = serviceTemplateProvider.getAllServiceTemplates();
225         if (!list.isEmpty()) {
226             serviceTemplateProvider.deleteServiceTemplate(list.get(0).getName(), list.get(0).getVersion());
227         }
228     }
229
230     private synchronized void createFullEntryInDbWithCommonProps() throws Exception {
231         deleteEntryInDB();
232         serviceTemplateProvider.createServiceTemplate(commonPropertiesServiceTemplate);
233     }
234 }