a10156807c91f6b51842c503a48e6d7d573368bd
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.runtime.instantiation;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.File;
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;
37
38 /**
39  * Utility methods supporting tests for Instantiation.
40  */
41 public class InstantiationUtils {
42
43     private static final Coder CODER = new StandardCoder();
44
45     /**
46      * Gets the ControlLoops from Resource.
47      *
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
52      */
53     public static ControlLoops getControlLoopsFromResource(final String path, final String suffix)
54             throws CoderException {
55         ControlLoops controlLoops = CODER.decode(new File(path), ControlLoops.class);
56
57         // add suffix to all names
58         controlLoops.getControlLoopList().forEach(controlLoop -> controlLoop.setName(controlLoop.getName() + suffix));
59         return controlLoops;
60     }
61
62     /**
63      * Gets InstantiationCommand from Resource.
64      *
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
69      */
70     public static InstantiationCommand getInstantiationCommandFromResource(final String path, final String suffix)
71             throws CoderException {
72         InstantiationCommand instantiationCommand = CODER.decode(new File(path), InstantiationCommand.class);
73
74         // add suffix to all names
75         instantiationCommand.getControlLoopIdentifierList().forEach(cl -> cl.setName(cl.getName() + suffix));
76         return instantiationCommand;
77     }
78
79     /**
80      * Assert that Instantiation Response contains proper ControlLoops.
81      *
82      * @param response InstantiationResponse
83      * @param controlLoops ControlLoops
84      */
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());
92         }
93     }
94
95     /**
96      * Assert that Instantiation Response contains proper ControlLoops.
97      *
98      * @param response InstantiationResponse
99      * @param command InstantiationCommand
100      */
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());
107         }
108     }
109
110     /**
111      * Assert that Instantiation Response contains ControlLoop equals to controlLoop.
112      *
113      * @param response InstantiationResponse
114      * @param controlLoop ControlLoop
115      */
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()));
121     }
122 }