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.models.tosca.authorative.concepts.ToscaConceptIdentifier;
39 * Utility methods supporting tests for Instantiation.
41 public class InstantiationUtils {
43 private static final Coder CODER = new StandardCoder();
46 * Gets the ControlLoops from Resource.
48 * @param path path of the resource
49 * @param suffix suffix to add to all names in ControlLoops
50 * @return the ControlLoops from Resource
51 * @throws CoderException if an error occurs
53 public static ControlLoops getControlLoopsFromResource(final String path, final String suffix)
54 throws CoderException {
55 ControlLoops controlLoops = CODER.decode(new File(path), ControlLoops.class);
57 // add suffix to all names
58 controlLoops.getControlLoopList().forEach(controlLoop -> controlLoop.setName(controlLoop.getName() + suffix));
63 * Gets InstantiationCommand from Resource.
65 * @param path path of the resource
66 * @param suffix suffix to add to all names in ControlLoops
67 * @return the InstantiationCommand
68 * @throws CoderException if an error occurs
70 public static InstantiationCommand getInstantiationCommandFromResource(final String path, final String suffix)
71 throws CoderException {
72 InstantiationCommand instantiationCommand = CODER.decode(new File(path), InstantiationCommand.class);
74 // add suffix to all names
75 instantiationCommand.getControlLoopIdentifierList().forEach(cl -> cl.setName(cl.getName() + suffix));
76 return instantiationCommand;
80 * Assert that Instantiation Response contains proper ControlLoops.
82 * @param response InstantiationResponse
83 * @param controlLoops ControlLoops
85 public static void assertInstantiationResponse(InstantiationResponse response, ControlLoops controlLoops) {
86 assertNotNull(response);
87 Assert.assertNull(response.getErrorDetails());
88 assertEquals(response.getAffectedControlLoops().size(), controlLoops.getControlLoopList().size());
89 for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
90 assertTrue(response.getAffectedControlLoops().stream()
91 .filter(ac -> ac.equals(controlLoop.getKey().asIdentifier())).findAny().isPresent());
96 * Assert that Instantiation Response contains proper ControlLoops.
98 * @param response InstantiationResponse
99 * @param command InstantiationCommand
101 public static void assertInstantiationResponse(InstantiationResponse response, InstantiationCommand command) {
102 assertNotNull(response);
103 assertEquals(response.getAffectedControlLoops().size(), command.getControlLoopIdentifierList().size());
104 for (ToscaConceptIdentifier toscaConceptIdentifier : command.getControlLoopIdentifierList()) {
105 assertTrue(response.getAffectedControlLoops().stream()
106 .filter(ac -> ac.compareTo(toscaConceptIdentifier) == 0).findAny().isPresent());
111 * Assert that Instantiation Response contains ControlLoop equals to controlLoop.
113 * @param response InstantiationResponse
114 * @param controlLoop ControlLoop
116 public static void assertInstantiationResponse(InstantiationResponse response, ControlLoop controlLoop) {
117 assertNotNull(response);
118 Assert.assertNull(response.getErrorDetails());
119 assertEquals(1, response.getAffectedControlLoops().size());
120 assertEquals(0, response.getAffectedControlLoops().get(0).compareTo(controlLoop.getKey().asIdentifier()));