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.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;
43 * Utility methods supporting tests for Instantiation.
45 public class InstantiationUtils {
47 private static final Coder CODER = new StandardCoder();
48 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
51 * Gets the ControlLoops from Resource.
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
58 public static ControlLoops getControlLoopsFromResource(final String path, final String suffix)
59 throws CoderException {
60 ControlLoops controlLoops = CODER.decode(new File(path), ControlLoops.class);
62 // add suffix to all names
63 controlLoops.getControlLoopList().forEach(controlLoop -> controlLoop.setName(controlLoop.getName() + suffix));
68 * Gets InstantiationCommand from Resource.
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
75 public static InstantiationCommand getInstantiationCommandFromResource(final String path, final String suffix)
76 throws CoderException {
77 InstantiationCommand instantiationCommand = CODER.decode(new File(path), InstantiationCommand.class);
79 // add suffix to all names
80 instantiationCommand.getControlLoopIdentifierList().forEach(cl -> cl.setName(cl.getName() + suffix));
81 return instantiationCommand;
85 * Assert that Instantiation Response contains proper ControlLoops.
87 * @param response InstantiationResponse
88 * @param controlLoops ControlLoops
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());
101 * Assert that Instantiation Response contains proper ControlLoops.
103 * @param response InstantiationResponse
104 * @param command InstantiationCommand
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());
116 * Assert that Instantiation Response contains ControlLoop equals to controlLoop.
118 * @param response InstantiationResponse
119 * @param controlLoop ControlLoop
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()));
129 * Store ToscaServiceTemplate from resource to DB.
131 * @param path path of the resource
132 * @param commissioningProvider The CommissioningProvider
133 * @throws PfModelException if an error occurs
135 public static void storeToscaServiceTemplate(String path, CommissioningProvider commissioningProvider)
136 throws PfModelException {
138 ToscaServiceTemplate template =
139 yamlTranslator.fromYaml(ResourceUtils.getResourceAsString(path), ToscaServiceTemplate.class);
141 commissioningProvider.createControlLoopDefinitions(template);