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