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