85bb2d1645d697b09678369694065f792d96b2bd
[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.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertNull;
27 import static org.junit.jupiter.api.Assertions.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.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.extension.ExtendWith;
37 import org.onap.policy.clamp.controlloop.models.messages.rest.commissioning.CommissioningResponse;
38 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
39 import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController;
40 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
41 import org.onap.policy.common.utils.resources.ResourceUtils;
42 import org.onap.policy.models.provider.PolicyModelsProvider;
43 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.boot.test.context.SpringBootTest;
47 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
48 import org.springframework.boot.web.server.LocalServerPort;
49 import org.springframework.test.context.TestPropertySource;
50 import org.springframework.test.context.junit.jupiter.SpringExtension;
51
52 @ExtendWith(SpringExtension.class)
53 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
54 @TestPropertySource(locations = {"classpath:application_test.properties"})
55 class CommissioningControllerTest extends CommonRestController {
56
57     private static final String TOSCA_SERVICE_TEMPLATE_YAML =
58             "src/test/resources/rest/servicetemplates/pmsh_multiple_cl_tosca.yaml";
59     private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
60     private static final String COMMISSIONING_ENDPOINT = "commission";
61     private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
62
63     @Autowired
64     private ClRuntimeParameterGroup clRuntimeParameterGroup;
65
66     @LocalServerPort
67     private int randomServerPort;
68
69     /**
70      * starts Main and inserts a commissioning template.
71      *
72      * @throws Exception if an error occurs
73      */
74     @BeforeAll
75     public static void setUpBeforeClass() throws Exception {
76
77         serviceTemplate = yamlTranslator.fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML),
78                 ToscaServiceTemplate.class);
79     }
80
81     @BeforeEach
82     public void setUpPort() {
83         super.setHttpPrefix(randomServerPort);
84     }
85
86     @Test
87     void testSwagger() throws Exception {
88         super.testSwagger(COMMISSIONING_ENDPOINT);
89     }
90
91     @Test
92     void testUnauthorizedCreate() throws Exception {
93         assertUnauthorizedPost(COMMISSIONING_ENDPOINT, Entity.json(serviceTemplate));
94     }
95
96     @Test
97     void testUnauthorizedQuery() throws Exception {
98         assertUnauthorizedGet(COMMISSIONING_ENDPOINT);
99     }
100
101     @Test
102     void testUnauthorizedQueryElements() throws Exception {
103         assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/elements");
104     }
105
106     @Test
107     void testUnauthorizedDelete() throws Exception {
108         assertUnauthorizedDelete(COMMISSIONING_ENDPOINT);
109     }
110
111     @Test
112     void testCreateBadRequest() throws Exception {
113         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
114         Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte"));
115
116         assertThat(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).isEqualTo(resp.getStatus());
117         CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
118         assertThat(commissioningResponse.getErrorDetails()).isNotNull();
119         assertThat(commissioningResponse.getAffectedControlLoopDefinitions()).isNull();
120     }
121
122     @Test
123     void testCreate() throws Exception {
124         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
125         Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
126         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
127         CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class);
128
129         assertNotNull(commissioningResponse);
130         assertNull(commissioningResponse.getErrorDetails());
131         // Response should return the number of node templates present in the service template
132         assertThat(commissioningResponse.getAffectedControlLoopDefinitions()).hasSize(13);
133         for (String nodeTemplateName : serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().keySet()) {
134             assertTrue(commissioningResponse.getAffectedControlLoopDefinitions().stream()
135                     .anyMatch(ac -> ac.getName().equals(nodeTemplateName)));
136         }
137     }
138
139     @Test
140     void testQuery_NoResultWithThisName() throws Exception {
141         createEntryInDB();
142
143         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name=noResultWithThisName");
144         Response rawresp = invocationBuilder.buildGet().invoke();
145         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
146         List<?> entityList = rawresp.readEntity(List.class);
147         assertThat(entityList).isEmpty();
148     }
149
150     @Test
151     void testQuery() throws Exception {
152         createEntryInDB();
153
154         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
155         Response rawresp = invocationBuilder.buildGet().invoke();
156         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
157         List<?> entityList = rawresp.readEntity(List.class);
158         assertNotNull(entityList);
159         assertThat(entityList).hasSize(2);
160     }
161
162     @Test
163     void testQueryElementsBadRequest() throws Exception {
164         createEntryInDB();
165
166         //Call get elements with no info
167         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/elements");
168         Response resp = invocationBuilder.buildGet().invoke();
169         assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), resp.getStatus());
170     }
171
172     @Test
173     void testQueryElements() throws Exception {
174         createEntryInDB();
175
176         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/elements"
177                 + "?name=org.onap.domain.pmsh.PMSHControlLoopDefinition");
178         Response rawresp = invocationBuilder.buildGet().invoke();
179         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
180         List<?> entityList = rawresp.readEntity(List.class);
181         assertNotNull(entityList);
182         assertThat(entityList).hasSize(4);
183     }
184
185     @Test
186     void testDeleteBadRequest() throws Exception {
187         createEntryInDB();
188
189         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT);
190         //Call delete with no info
191         Response resp = invocationBuilder.delete();
192         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
193     }
194
195     @Test
196     void testDelete() throws Exception {
197         createEntryInDB();
198
199         Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name="
200                 + serviceTemplate.getName() + "&version=" + serviceTemplate.getVersion());
201         //Call delete with no info
202         Response resp = invocationBuilder.delete();
203         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
204
205         try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory()
206                 .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) {
207             List<ToscaServiceTemplate> templatesInDB = modelsProvider.getServiceTemplateList(null, null);
208             assertThat(templatesInDB).isEmpty();
209         }
210     }
211
212     private synchronized void createEntryInDB() throws Exception {
213         try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory()
214                 .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) {
215             modelsProvider.createServiceTemplate(serviceTemplate);
216         }
217     }
218 }