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