aboutsummaryrefslogtreecommitdiffstats
path: root/tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/InstantiationUtils.java
blob: a10156807c91f6b51842c503a48e6d7d573368bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2021 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.clamp.controlloop.runtime.instantiation;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import org.junit.Assert;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand;
import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse;
import org.onap.policy.common.utils.coder.Coder;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;

/**
 * Utility methods supporting tests for Instantiation.
 */
public class InstantiationUtils {

    private static final Coder CODER = new StandardCoder();

    /**
     * Gets the ControlLoops from Resource.
     *
     * @param path path of the resource
     * @param suffix suffix to add to all names in ControlLoops
     * @return the ControlLoops from Resource
     * @throws CoderException if an error occurs
     */
    public static ControlLoops getControlLoopsFromResource(final String path, final String suffix)
            throws CoderException {
        ControlLoops controlLoops = CODER.decode(new File(path), ControlLoops.class);

        // add suffix to all names
        controlLoops.getControlLoopList().forEach(controlLoop -> controlLoop.setName(controlLoop.getName() + suffix));
        return controlLoops;
    }

    /**
     * Gets InstantiationCommand from Resource.
     *
     * @param path path of the resource
     * @param suffix suffix to add to all names in ControlLoops
     * @return the InstantiationCommand
     * @throws CoderException if an error occurs
     */
    public static InstantiationCommand getInstantiationCommandFromResource(final String path, final String suffix)
            throws CoderException {
        InstantiationCommand instantiationCommand = CODER.decode(new File(path), InstantiationCommand.class);

        // add suffix to all names
        instantiationCommand.getControlLoopIdentifierList().forEach(cl -> cl.setName(cl.getName() + suffix));
        return instantiationCommand;
    }

    /**
     * Assert that Instantiation Response contains proper ControlLoops.
     *
     * @param response InstantiationResponse
     * @param controlLoops ControlLoops
     */
    public static void assertInstantiationResponse(InstantiationResponse response, ControlLoops controlLoops) {
        assertNotNull(response);
        Assert.assertNull(response.getErrorDetails());
        assertEquals(response.getAffectedControlLoops().size(), controlLoops.getControlLoopList().size());
        for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
            assertTrue(response.getAffectedControlLoops().stream()
                    .filter(ac -> ac.equals(controlLoop.getKey().asIdentifier())).findAny().isPresent());
        }
    }

    /**
     * Assert that Instantiation Response contains proper ControlLoops.
     *
     * @param response InstantiationResponse
     * @param command InstantiationCommand
     */
    public static void assertInstantiationResponse(InstantiationResponse response, InstantiationCommand command) {
        assertNotNull(response);
        assertEquals(response.getAffectedControlLoops().size(), command.getControlLoopIdentifierList().size());
        for (ToscaConceptIdentifier toscaConceptIdentifier : command.getControlLoopIdentifierList()) {
            assertTrue(response.getAffectedControlLoops().stream()
                    .filter(ac -> ac.compareTo(toscaConceptIdentifier) == 0).findAny().isPresent());
        }
    }

    /**
     * Assert that Instantiation Response contains ControlLoop equals to controlLoop.
     *
     * @param response InstantiationResponse
     * @param controlLoop ControlLoop
     */
    public static void assertInstantiationResponse(InstantiationResponse response, ControlLoop controlLoop) {
        assertNotNull(response);
        Assert.assertNull(response.getErrorDetails());
        assertEquals(1, response.getAffectedControlLoops().size());
        assertEquals(0, response.getAffectedControlLoops().get(0).compareTo(controlLoop.getKey().asIdentifier()));
    }
}