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