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;
26 import static org.junit.jupiter.api.Assertions.fail;
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;
42 * Utility methods supporting tests for Instantiation.
44 public class InstantiationUtils {
46 private static final Coder CODER = new StandardCoder();
47 private static final StandardYamlCoder YAML_TRANSLATOR = new StandardYamlCoder();
50 * Gets the ControlLoops from Resource.
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
57 public static ControlLoops getControlLoopsFromResource(final String path, final String suffix)
58 throws CoderException {
59 ControlLoops controlLoops = CODER.decode(new File(path), ControlLoops.class);
61 // add suffix to all names
62 controlLoops.getControlLoopList().forEach(controlLoop -> controlLoop.setName(controlLoop.getName() + suffix));
67 * Gets InstantiationCommand from Resource.
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
74 public static InstantiationCommand getInstantiationCommandFromResource(final String path, final String suffix)
75 throws CoderException {
76 InstantiationCommand instantiationCommand = CODER.decode(new File(path), InstantiationCommand.class);
78 // add suffix to all names
79 instantiationCommand.getControlLoopIdentifierList().forEach(cl -> cl.setName(cl.getName() + suffix));
80 return instantiationCommand;
84 * Assert that Instantiation Response contains proper ControlLoops.
86 * @param response InstantiationResponse
87 * @param controlLoops ControlLoops
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());
100 * Assert that Instantiation Response contains proper ControlLoops.
102 * @param response InstantiationResponse
103 * @param command InstantiationCommand
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());
115 * Assert that Instantiation Response contains ControlLoop equals to controlLoop.
117 * @param response InstantiationResponse
118 * @param controlLoop ControlLoop
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()));
128 * Get ToscaServiceTemplate from resource.
130 * @param path path of the resource
132 public static ToscaServiceTemplate getToscaServiceTemplate(String path) {
135 return YAML_TRANSLATOR.decode(ResourceUtils.getResourceAsStream(path), ToscaServiceTemplate.class);
136 } catch (CoderException e) {
137 fail("Cannot read or decode " + path);