8529c0c9be6df07c4b8d91430a590c0fbd6c38fb
[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.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26
27 import java.io.File;
28 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
31 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstancePropertiesResponse;
32 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand;
33 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse;
34 import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider;
35 import org.onap.policy.common.utils.coder.Coder;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40 import org.onap.policy.models.base.PfModelException;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43
44 /**
45  * Utility methods supporting tests for Instantiation.
46  */
47 public class InstantiationUtils {
48
49     private static final Coder CODER = new StandardCoder();
50     private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
51
52     /**
53      * Gets the ControlLoops from Resource.
54      *
55      * @param path path of the resource
56      * @param suffix suffix to add to all names in ControlLoops
57      * @return the ControlLoops from Resource
58      * @throws CoderException if an error occurs
59      */
60     public static ControlLoops getControlLoopsFromResource(final String path, final String suffix)
61             throws CoderException {
62         ControlLoops controlLoops = CODER.decode(new File(path), ControlLoops.class);
63
64         // add suffix to all names
65         controlLoops.getControlLoopList().forEach(controlLoop -> controlLoop.setName(controlLoop.getName() + suffix));
66         return controlLoops;
67     }
68
69     /**
70      * Gets InstantiationCommand from Resource.
71      *
72      * @param path path of the resource
73      * @param suffix suffix to add to all names in ControlLoops
74      * @return the InstantiationCommand
75      * @throws CoderException if an error occurs
76      */
77     public static InstantiationCommand getInstantiationCommandFromResource(final String path, final String suffix)
78             throws CoderException {
79         InstantiationCommand instantiationCommand = CODER.decode(new File(path), InstantiationCommand.class);
80
81         // add suffix to all names
82         instantiationCommand.getControlLoopIdentifierList().forEach(cl -> cl.setName(cl.getName() + suffix));
83         return instantiationCommand;
84     }
85
86     /**
87      * Assert that Instantiation Response contains proper ControlLoops.
88      *
89      * @param response InstantiationResponse
90      * @param controlLoops ControlLoops
91      */
92     public static void assertInstantiationResponse(InstantiationResponse response, ControlLoops controlLoops) {
93         assertThat(response).isNotNull();
94         assertThat(response.getErrorDetails()).isNull();
95         assertThat(response.getAffectedControlLoops().size()).isEqualTo(controlLoops.getControlLoopList().size());
96         for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
97             assertTrue(response.getAffectedControlLoops().stream()
98                     .filter(ac -> ac.equals(controlLoop.getKey().asIdentifier())).findAny().isPresent());
99         }
100     }
101
102     /**
103      * Assert that Instantiation Response contains proper ControlLoops.
104      *
105      * @param response InstantiationResponse
106      * @param command InstantiationCommand
107      */
108     public static void assertInstantiationResponse(InstantiationResponse response, InstantiationCommand command) {
109         assertThat(response).isNotNull();
110         assertEquals(response.getAffectedControlLoops().size(), command.getControlLoopIdentifierList().size());
111         for (ToscaConceptIdentifier toscaConceptIdentifier : command.getControlLoopIdentifierList()) {
112             assertTrue(response.getAffectedControlLoops().stream()
113                     .filter(ac -> ac.compareTo(toscaConceptIdentifier) == 0).findAny().isPresent());
114         }
115     }
116
117     /**
118      * Assert that Instantiation Response contains ControlLoop equals to controlLoop.
119      *
120      * @param response InstantiationResponse
121      * @param controlLoop ControlLoop
122      */
123     public static void assertInstantiationResponse(InstantiationResponse response, ControlLoop controlLoop) {
124         assertThat(response).isNotNull();
125         assertThat(response.getErrorDetails()).isNull();
126         assertEquals(1, response.getAffectedControlLoops().size());
127         assertEquals(0, response.getAffectedControlLoops().get(0).compareTo(controlLoop.getKey().asIdentifier()));
128     }
129
130     /**
131      * Store ToscaServiceTemplate from resource to DB.
132      *
133      * @param path path of the resource
134      * @param commissioningProvider The CommissioningProvider
135      * @throws PfModelException if an error occurs
136      */
137     public static void storeToscaServiceTemplate(String path, CommissioningProvider commissioningProvider)
138         throws PfModelException, ControlLoopException {
139
140         ToscaServiceTemplate template =
141                 yamlTranslator.fromYaml(ResourceUtils.getResourceAsString(path), ToscaServiceTemplate.class);
142
143         commissioningProvider.deleteControlLoopDefinition(null, null);
144
145         commissioningProvider.createControlLoopDefinitions(template);
146     }
147
148     /**
149      * Assert that instance properties has been properly saved.
150      *
151      * @param response InstancePropertiesResponse
152      * @throws PfModelException if an error occurs
153      */
154     public static void assertInstancePropertiesResponse(InstancePropertiesResponse response) throws PfModelException {
155
156         assertThat(response).isNotNull();
157         assertThat(response.getErrorDetails()).isNull();
158         assertThat(response.getAffectedInstanceProperties()).hasSize(8);
159
160         boolean containsInstance = response.getAffectedInstanceProperties().stream()
161             .anyMatch(identifier -> identifier.getName().contains("_Instance"));
162
163         assertThat(containsInstance).isTrue();
164
165     }
166 }