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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.runtime.instantiation;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
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;
46 * Utility methods supporting tests for Instantiation.
48 public class InstantiationUtils {
50 private static final Coder CODER = new StandardCoder();
51 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
54 * Gets the ControlLoops from Resource.
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
61 public static ControlLoops getControlLoopsFromResource(final String path, final String suffix)
62 throws CoderException {
63 ControlLoops controlLoops = CODER.decode(new File(path), ControlLoops.class);
65 // add suffix to all names
66 controlLoops.getControlLoopList().forEach(controlLoop -> controlLoop.setName(controlLoop.getName() + suffix));
71 * Gets InstantiationCommand from Resource.
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
78 public static InstantiationCommand getInstantiationCommandFromResource(final String path, final String suffix)
79 throws CoderException {
80 InstantiationCommand instantiationCommand = CODER.decode(new File(path), InstantiationCommand.class);
82 // add suffix to all names
83 instantiationCommand.getControlLoopIdentifierList().forEach(cl -> cl.setName(cl.getName() + suffix));
84 return instantiationCommand;
88 * Assert that Instantiation Response contains proper ControlLoops.
90 * @param response InstantiationResponse
91 * @param controlLoops ControlLoops
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());
104 * Assert that Instantiation Response contains proper ControlLoops.
106 * @param response InstantiationResponse
107 * @param command InstantiationCommand
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());
119 * Assert that Instantiation Response contains ControlLoop equals to controlLoop.
121 * @param response InstantiationResponse
122 * @param controlLoop ControlLoop
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()));
132 * Store ToscaServiceTemplate from resource to DB.
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
138 public static void storeToscaServiceTemplate(String path, PolicyModelsProviderParameters parameters)
139 throws PfModelException {
141 ToscaServiceTemplate template =
142 yamlTranslator.fromYaml(ResourceUtils.getResourceAsString(path), ToscaServiceTemplate.class);
144 try (PolicyModelsProvider modelsProvider =
145 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) {
146 modelsProvider.createServiceTemplate(template);