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.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
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;
45 * Utility methods supporting tests for Instantiation.
47 public class InstantiationUtils {
49 private static final Coder CODER = new StandardCoder();
50 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
53 * Gets the ControlLoops from Resource.
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
60 public static ControlLoops getControlLoopsFromResource(final String path, final String suffix)
61 throws CoderException {
62 ControlLoops controlLoops = CODER.decode(new File(path), ControlLoops.class);
64 // add suffix to all names
65 controlLoops.getControlLoopList().forEach(controlLoop -> controlLoop.setName(controlLoop.getName() + suffix));
70 * Gets InstantiationCommand from Resource.
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
77 public static InstantiationCommand getInstantiationCommandFromResource(final String path, final String suffix)
78 throws CoderException {
79 InstantiationCommand instantiationCommand = CODER.decode(new File(path), InstantiationCommand.class);
81 // add suffix to all names
82 instantiationCommand.getControlLoopIdentifierList().forEach(cl -> cl.setName(cl.getName() + suffix));
83 return instantiationCommand;
87 * Assert that Instantiation Response contains proper ControlLoops.
89 * @param response InstantiationResponse
90 * @param controlLoops ControlLoops
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());
103 * Assert that Instantiation Response contains proper ControlLoops.
105 * @param response InstantiationResponse
106 * @param command InstantiationCommand
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());
118 * Assert that Instantiation Response contains ControlLoop equals to controlLoop.
120 * @param response InstantiationResponse
121 * @param controlLoop ControlLoop
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()));
131 * Store ToscaServiceTemplate from resource to DB.
133 * @param path path of the resource
134 * @param commissioningProvider The CommissioningProvider
135 * @throws PfModelException if an error occurs
137 public static void storeToscaServiceTemplate(String path, CommissioningProvider commissioningProvider)
138 throws PfModelException, ControlLoopException {
140 ToscaServiceTemplate template =
141 yamlTranslator.fromYaml(ResourceUtils.getResourceAsString(path), ToscaServiceTemplate.class);
143 commissioningProvider.deleteControlLoopDefinition(null, null);
145 commissioningProvider.createControlLoopDefinitions(template);
149 * Assert that instance properties has been properly saved.
151 * @param response InstancePropertiesResponse
152 * @throws PfModelException if an error occurs
154 public static void assertInstancePropertiesResponse(InstancePropertiesResponse response) throws PfModelException {
156 assertThat(response).isNotNull();
157 assertThat(response.getErrorDetails()).isNull();
158 assertThat(response.getAffectedInstanceProperties()).hasSize(8);
160 boolean containsInstance = response.getAffectedInstanceProperties().stream()
161 .anyMatch(identifier -> identifier.getName().contains("_Instance"));
163 assertThat(containsInstance).isTrue();