ccc54b93f822f9ccd8524a5ddcb319a10b1c023b
[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.instantiation;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import org.junit.Assert;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.mockito.Mockito;
33 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
37 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand;
38 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse;
39 import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningHandler;
40 import org.onap.policy.clamp.controlloop.runtime.supervision.SupervisionHandler;
41 import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData;
42 import org.onap.policy.common.endpoints.event.comm.TopicSink;
43 import org.onap.policy.models.base.PfModelException;
44 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
46
47 /**
48  * Class to perform unit test of {@link ControlLoopInstantiationProvider}}.
49  *
50  */
51 public class ControlLoopInstantiationProviderTest {
52
53     private static final String CL_INSTANTIATION_CREATE_JSON = "src/test/resources/rest/controlloops/ControlLoops.json";
54     private static final String CL_INSTANTIATION_UPDATE_JSON =
55             "src/test/resources/rest/controlloops/ControlLoopsUpdate.json";
56     private static final String CL_INSTANTIATION_CHANGE_STATE_JSON =
57             "src/test/resources/rest/controlloops/PassiveCommand.json";
58     private static final String CL_INSTANTIATION_DEFINITION_NAME_NOT_FOUND_JSON =
59             "src/test/resources/rest/controlloops/ControlLoopElementsNotFound.json";
60     private static final String CL_INSTANTIATION_CONTROLLOOP_DEFINITION_NOT_FOUND_JSON =
61             "src/test/resources/rest/controlloops/ControlLoopsNotFound.json";
62     private static final String TOSCA_TEMPLATE_YAML =
63             "src/test/resources/rest/servicetemplates/pmsh_multiple_cl_tosca.yaml";
64     private static final String CONTROL_LOOP_NOT_FOUND = "Control Loop not found";
65     private static final String DELETE_BAD_REQUEST = "Control Loop State is still %s";
66     private static final String ORDERED_STATE_INVALID = "ordered state invalid or not specified on command";
67     private static final String CONTROLLOOP_ELEMENT_NAME_NOT_FOUND =
68             "\"ControlLoops\" INVALID, item has status INVALID\n"
69             + "  \"entry org.onap.domain.pmsh.PMSHControlLoopDefinition\" INVALID, item has status INVALID\n"
70             + "    \"entry org.onap.domain.pmsh.DCAEMicroservice\" INVALID, Not FOUND\n"
71             + "  \"entry org.onap.domain.pmsh.PMSHControlLoopDefinition\" INVALID, item has status INVALID\n"
72             + "    \"entry org.onap.domain.pmsh.DCAEMicroservice\" INVALID, Not FOUND\n";
73
74     private static final String CONTROLLOOP_DEFINITION_NOT_FOUND = "\"ControlLoops\" INVALID, item has status INVALID\n"
75             + "  \"entry org.onap.domain.PMSHControlLoopDefinition\" INVALID, item has status INVALID\n"
76             + "    item \"ControlLoop\" value \"org.onap.domain.PMSHControlLoopDefinition\" INVALID,"
77             + " Commissioned control loop definition not FOUND\n"
78             + "  \"entry org.onap.domain.PMSHControlLoopDefinition\" INVALID, item has status INVALID\n"
79             + "    item \"ControlLoop\" value \"org.onap.domain.PMSHControlLoopDefinition\" INVALID,"
80             + " Commissioned control loop definition not FOUND\n";
81
82     private static PolicyModelsProviderParameters databaseProviderParameters;
83     private static SupervisionHandler supervisionHandler;
84     private static CommissioningHandler commissioningHandler;
85
86     /**
87      * setup Db Provider Parameters.
88      *
89      * @throws PfModelException if an error occurs
90      */
91     @BeforeClass
92     public static void setupDbProviderParameters() throws PfModelException {
93         databaseProviderParameters =
94                 CommonTestData.geParameterGroup(0, "instantproviderdb").getDatabaseProviderParameters();
95         commissioningHandler = new CommissioningHandler(CommonTestData.geParameterGroup(0, "instantproviderdb"));
96         commissioningHandler.startProviders();
97         supervisionHandler = new SupervisionHandler(CommonTestData.geParameterGroup(0, "instantproviderdb"));
98         supervisionHandler.startProviders();
99         supervisionHandler.startAndRegisterPublishers(Collections.singletonList(Mockito.mock(TopicSink.class)));
100     }
101
102     @Test
103     public void testInstantiationCrud() throws Exception {
104         ControlLoops controlLoopsCreate =
105                 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Crud");
106         ControlLoops controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate);
107         assertThat(controlLoopsDb.getControlLoopList()).isEmpty();
108         try (ControlLoopInstantiationProvider instantiationProvider =
109                      new ControlLoopInstantiationProvider(databaseProviderParameters)) {
110
111             // to validate control Loop, it needs to define ToscaServiceTemplate
112             InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, databaseProviderParameters);
113
114             InstantiationResponse instantiationResponse = instantiationProvider.createControlLoops(controlLoopsCreate);
115             InstantiationUtils.assertInstantiationResponse(instantiationResponse, controlLoopsCreate);
116
117             controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate);
118             assertThat(controlLoopsDb.getControlLoopList()).isNotEmpty();
119             Assert.assertEquals(controlLoopsCreate, controlLoopsDb);
120
121             for (ControlLoop controlLoop : controlLoopsCreate.getControlLoopList()) {
122                 ControlLoops controlLoopsGet =
123                         instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion());
124                 assertThat(controlLoopsGet.getControlLoopList()).hasSize(1);
125                 Assert.assertEquals(controlLoop, controlLoopsGet.getControlLoopList().get(0));
126             }
127
128             ControlLoops controlLoopsUpdate =
129                     InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_UPDATE_JSON, "Crud");
130             Assert.assertNotEquals(controlLoopsUpdate, controlLoopsDb);
131
132             instantiationResponse = instantiationProvider.updateControlLoops(controlLoopsUpdate);
133             InstantiationUtils.assertInstantiationResponse(instantiationResponse, controlLoopsUpdate);
134
135             controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate);
136             assertThat(controlLoopsDb.getControlLoopList()).isNotEmpty();
137             Assert.assertEquals(controlLoopsUpdate, controlLoopsDb);
138
139             InstantiationCommand instantiationCommand =
140                     InstantiationUtils.getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON, "Crud");
141             instantiationResponse = instantiationProvider.issueControlLoopCommand(instantiationCommand);
142             InstantiationUtils.assertInstantiationResponse(instantiationResponse, instantiationCommand);
143
144             for (ToscaConceptIdentifier toscaConceptIdentifier : instantiationCommand.getControlLoopIdentifierList()) {
145                 ControlLoops controlLoopsGet = instantiationProvider.getControlLoops(toscaConceptIdentifier.getName(),
146                         toscaConceptIdentifier.getVersion());
147                 assertThat(controlLoopsGet.getControlLoopList()).hasSize(1);
148                 Assert.assertEquals(instantiationCommand.getOrderedState(),
149                         controlLoopsGet.getControlLoopList().get(0).getOrderedState());
150             }
151
152             // in order to delete a controlLoop the state must be UNINITIALISED
153             controlLoopsCreate.getControlLoopList().forEach(cl -> cl.setState(ControlLoopState.UNINITIALISED));
154             instantiationProvider.updateControlLoops(controlLoopsCreate);
155
156             for (ControlLoop controlLoop : controlLoopsCreate.getControlLoopList()) {
157                 instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion());
158             }
159
160             controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate);
161             assertThat(controlLoopsDb.getControlLoopList()).isEmpty();
162         }
163     }
164
165     private ControlLoops getControlLoopsFromDb(ControlLoops controlLoopsSource) throws Exception {
166         ControlLoops controlLoopsDb = new ControlLoops();
167         controlLoopsDb.setControlLoopList(new ArrayList<>());
168
169         try (ControlLoopInstantiationProvider instantiationProvider =
170                      new ControlLoopInstantiationProvider(databaseProviderParameters)) {
171
172             for (ControlLoop controlLoop : controlLoopsSource.getControlLoopList()) {
173                 ControlLoops controlLoopsFromDb =
174                         instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion());
175                 controlLoopsDb.getControlLoopList().addAll(controlLoopsFromDb.getControlLoopList());
176             }
177             return controlLoopsDb;
178         }
179     }
180
181     @Test
182     public void testInstantiationDelete() throws Exception {
183         ControlLoops controlLoops =
184                 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Delete");
185         assertThat(getControlLoopsFromDb(controlLoops).getControlLoopList()).isEmpty();
186
187         ControlLoop controlLoop0 = controlLoops.getControlLoopList().get(0);
188
189         try (ControlLoopInstantiationProvider instantiationProvider =
190                      new ControlLoopInstantiationProvider(databaseProviderParameters)) {
191
192             // to validate control Loop, it needs to define ToscaServiceTemplate
193             InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, databaseProviderParameters);
194
195             assertThatThrownBy(
196                     () -> instantiationProvider.deleteControlLoop(controlLoop0.getName(), controlLoop0.getVersion()))
197                     .hasMessageMatching(CONTROL_LOOP_NOT_FOUND);
198
199             InstantiationUtils.assertInstantiationResponse(instantiationProvider.createControlLoops(controlLoops),
200                     controlLoops);
201
202             for (ControlLoopState state : ControlLoopState.values()) {
203                 if (!ControlLoopState.UNINITIALISED.equals(state)) {
204                     assertThatDeleteThrownBy(controlLoops, state);
205                 }
206             }
207
208             controlLoop0.setState(ControlLoopState.UNINITIALISED);
209             instantiationProvider.updateControlLoops(controlLoops);
210
211             for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
212                 instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion());
213             }
214
215             for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
216                 ControlLoops controlLoopsGet =
217                         instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion());
218                 assertThat(controlLoopsGet.getControlLoopList()).isEmpty();
219             }
220         }
221     }
222
223     private void assertThatDeleteThrownBy(ControlLoops controlLoops, ControlLoopState state) throws Exception {
224         ControlLoop controlLoop = controlLoops.getControlLoopList().get(0);
225
226         controlLoop.setState(state);
227
228         try (ControlLoopInstantiationProvider instantiationProvider =
229                      new ControlLoopInstantiationProvider(databaseProviderParameters)) {
230
231             instantiationProvider.updateControlLoops(controlLoops);
232             assertThatThrownBy(
233                     () -> instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion()))
234                     .hasMessageMatching(String.format(DELETE_BAD_REQUEST, state));
235         }
236     }
237
238     @Test
239     public void testCreateControlLoops_NoDuplicates() throws Exception {
240         ControlLoops controlLoopsCreate =
241                 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "NoDuplicates");
242
243         ControlLoops controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate);
244         assertThat(controlLoopsDb.getControlLoopList()).isEmpty();
245
246         try (ControlLoopInstantiationProvider instantiationProvider =
247                      new ControlLoopInstantiationProvider(databaseProviderParameters)) {
248
249             // to validate control Loop, it needs to define ToscaServiceTemplate
250             InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, databaseProviderParameters);
251
252             InstantiationResponse instantiationResponse = instantiationProvider.createControlLoops(controlLoopsCreate);
253             InstantiationUtils.assertInstantiationResponse(instantiationResponse, controlLoopsCreate);
254
255             assertThatThrownBy(() -> instantiationProvider.createControlLoops(controlLoopsCreate)).hasMessageMatching(
256                     controlLoopsCreate.getControlLoopList().get(0).getKey().asIdentifier() + " already defined");
257
258             for (ControlLoop controlLoop : controlLoopsCreate.getControlLoopList()) {
259                 instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion());
260             }
261         }
262     }
263
264     @Test
265     public void testCreateControlLoops_CommissionedClElementNotFound() throws Exception {
266         ControlLoops controlLoops = InstantiationUtils
267                 .getControlLoopsFromResource(CL_INSTANTIATION_DEFINITION_NAME_NOT_FOUND_JSON, "ClElementNotFound");
268
269         try (ControlLoopInstantiationProvider provider =
270                      new ControlLoopInstantiationProvider(databaseProviderParameters)) {
271
272             // to validate control Loop, it needs to define ToscaServiceTemplate
273             InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, databaseProviderParameters);
274
275             assertThat(getControlLoopsFromDb(controlLoops).getControlLoopList()).isEmpty();
276
277             assertThatThrownBy(() -> provider.createControlLoops(controlLoops))
278                     .hasMessageMatching(CONTROLLOOP_ELEMENT_NAME_NOT_FOUND);
279         }
280     }
281
282     @Test
283     public void testCreateControlLoops_CommissionedClNotFound() throws Exception {
284         ControlLoops controlLoops = InstantiationUtils
285                 .getControlLoopsFromResource(CL_INSTANTIATION_CONTROLLOOP_DEFINITION_NOT_FOUND_JSON, "ClNotFound");
286
287         assertThat(getControlLoopsFromDb(controlLoops).getControlLoopList()).isEmpty();
288
289         try (ControlLoopInstantiationProvider provider =
290                      new ControlLoopInstantiationProvider(databaseProviderParameters)) {
291             assertThatThrownBy(() -> provider.createControlLoops(controlLoops))
292                     .hasMessageMatching(CONTROLLOOP_DEFINITION_NOT_FOUND);
293         }
294     }
295
296     @Test
297     public void testIssueControlLoopCommand_OrderedStateInvalid() throws ControlLoopRuntimeException, IOException {
298         try (ControlLoopInstantiationProvider instantiationProvider =
299                      new ControlLoopInstantiationProvider(databaseProviderParameters)) {
300             assertThatThrownBy(() -> instantiationProvider.issueControlLoopCommand(new InstantiationCommand()))
301                     .hasMessageMatching(ORDERED_STATE_INVALID);
302         }
303     }
304
305     @Test
306     public void testInstantiationVersions() throws Exception {
307
308         // create controlLoops V1
309         ControlLoops controlLoopsV1 =
310                 InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "V1");
311         assertThat(getControlLoopsFromDb(controlLoopsV1).getControlLoopList()).isEmpty();
312
313         try (ControlLoopInstantiationProvider instantiationProvider =
314                      new ControlLoopInstantiationProvider(databaseProviderParameters)) {
315
316             // to validate control Loop, it needs to define ToscaServiceTemplate
317             InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, databaseProviderParameters);
318
319             InstantiationUtils.assertInstantiationResponse(instantiationProvider.createControlLoops(controlLoopsV1),
320                     controlLoopsV1);
321
322             // create controlLoops V2
323             ControlLoops controlLoopsV2 =
324                     InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "V2");
325             assertThat(getControlLoopsFromDb(controlLoopsV2).getControlLoopList()).isEmpty();
326             InstantiationUtils.assertInstantiationResponse(instantiationProvider.createControlLoops(controlLoopsV2),
327                     controlLoopsV2);
328
329             // GET controlLoops V2
330             for (ControlLoop controlLoop : controlLoopsV2.getControlLoopList()) {
331                 ControlLoops controlLoopsGet =
332                         instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion());
333                 assertThat(controlLoopsGet.getControlLoopList()).hasSize(1);
334                 Assert.assertEquals(controlLoop, controlLoopsGet.getControlLoopList().get(0));
335             }
336
337             // DELETE controlLoops V1
338             for (ControlLoop controlLoop : controlLoopsV1.getControlLoopList()) {
339                 instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion());
340             }
341
342             // GET controlLoops V1 is not available
343             for (ControlLoop controlLoop : controlLoopsV1.getControlLoopList()) {
344                 ControlLoops controlLoopsGet =
345                         instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion());
346                 assertThat(controlLoopsGet.getControlLoopList()).isEmpty();
347             }
348
349             // GET controlLoops V2 is still available
350             for (ControlLoop controlLoop : controlLoopsV2.getControlLoopList()) {
351                 ControlLoops controlLoopsGet =
352                         instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion());
353                 assertThat(controlLoopsGet.getControlLoopList()).hasSize(1);
354                 Assert.assertEquals(controlLoop, controlLoopsGet.getControlLoopList().get(0));
355             }
356
357             // DELETE controlLoops V2
358             for (ControlLoop controlLoop : controlLoopsV2.getControlLoopList()) {
359                 instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion());
360             }
361
362             // GET controlLoops V2 is not available
363             for (ControlLoop controlLoop : controlLoopsV2.getControlLoopList()) {
364                 ControlLoops controlLoopsGet =
365                         instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion());
366                 assertThat(controlLoopsGet.getControlLoopList()).isEmpty();
367             }
368         }
369     }
370 }