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