aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/mso/MsoOperationalEnvironmentTest.java
blob: 5c5d6fd417b019252b1a9c335965c31f8e4e05d3 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package org.onap.vid.mso;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import org.apache.commons.io.IOUtils;
import org.onap.vid.changeManagement.RequestDetailsWrapper;
import org.onap.vid.controllers.OperationalEnvironmentController;
import org.onap.vid.controllers.OperationalEnvironmentController.OperationalEnvironmentManifest;
import org.onap.vid.mso.MsoBusinessLogic;
import org.onap.vid.mso.MsoBusinessLogicImpl;
import org.onap.vid.mso.model.OperationalEnvironmentActivateInfo;
import org.onap.vid.mso.model.OperationalEnvironmentDeactivateInfo;
import org.onap.vid.mso.rest.OperationalEnvironment.OperationEnvironmentRequestDetails;
import org.onap.vid.mso.rest.RequestDetails;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Test
public class MsoOperationalEnvironmentTest {

    private MsoBusinessLogic msoBusinessLogic = new MsoBusinessLogicImpl(null,null);

    @Test(dataProvider = "getOperationalEnvironmentActivationPermutations")
    public void testJsonResultOfOperationalEnvironmentActivationRequestDetails(HashMap<String, String> permutation) throws IOException {

        // Convert 'manifest' to model
        final OperationalEnvironmentManifest manifest = new ObjectMapper().readerFor(OperationalEnvironmentManifest.class).readValue(permutation.get("<manifest>"));

        // build OperationalEnvironmentActivateInfo
        OperationalEnvironmentActivateInfo inputUnderTest = createOperationalEnvironmentActivateInfo("<instanceId>", permutation.get("<userId>"), manifest, permutation.get("<relatedInstanceId>"), permutation.get("<relatedInstanceName>"), permutation.get("<workloadContext>"));

        // transform to RequestDetails, and serialize to json
        //   -> actually, the function "msoBusinessLogic.createOperationalEnvironmentActivationRequestDetails"
        //      is the code under test here
        final RequestDetailsWrapper<RequestDetails> operationalEnvironmentActivationRequestDetails = msoBusinessLogic.createOperationalEnvironmentActivationRequestDetails(inputUnderTest);

        String expected = buildExpectation("payload_jsons/activateOperationalEnvironmentsPayloadToMso.json", permutation);

        assertThatExpectationIsLikeObject(expected, operationalEnvironmentActivationRequestDetails);
    }

    @DataProvider
    private Object[][] getOperationalEnvironmentActivationPermutations() {
        final String manifest = "" +
                "{" +
                "  \"serviceModelList\": [" +
                "    {" +
                "      \"serviceModelVersionId\": \"uuid2\"," +
                "      \"recoveryAction\": \"retry\"" +
                "    }" +
                "  ]" +
                "}";


        final ImmutableListMultimap<String, String> options = ImmutableListMultimap.<String, String>builder()
                .putAll("<instanceId>", "instanceId")
                .putAll("<userId>", "1", "0198adb8-87fd-46ef-94ae-258816629c8b")
                .putAll("<relatedInstanceId>", "relatedInstanceId", "2744cf56-4f00-4e48-917b-c3bd3b1f8984")
                .putAll("<relatedInstanceName>", "relatedInstanceName", "Brooklynn Puanani")
                .putAll("<workloadContext>", "workloadContext", "VNF_E2E-extreme")
                .putAll("<manifest>", manifest)
                .build();

        List<HashMap<String, String>> permutations = permuteOptions(options);

        return permutations.stream().map(m -> new Object[] { m }).collect(Collectors.toList()).toArray(new Object[0][0]);
    }

    @Test(dataProvider = "getOperationalEnvironmentCreationPermutations")
    public void testJsonResultOfOperationalEnvironmentCreationRequestDetails(HashMap<String, String> permutation) throws IOException {

        // build OperationalEnvironmentCreateBody
        OperationalEnvironmentController.OperationalEnvironmentCreateBody inputUnderTest = createOperationalEnvironmentCreateBody(permutation.get("<instanceName>"), permutation.get("<ecompInstanceId>"), permutation.get("<ecompInstanceName>"), permutation.get("<operationalEnvType>"), permutation.get("<tenantContext>"), permutation.get("<workloadContext>"));

        // transform to RequestDetails, and serialize to json
        //   -> actually, the function "msoBusinessLogic.createOperationalEnvironmentActivationRequestDetails"
        //      is the code under test here
        final RequestDetailsWrapper<OperationEnvironmentRequestDetails> operationalEnvironmentCreationRequestDetails = msoBusinessLogic.convertParametersToRequestDetails(inputUnderTest, permutation.get("<userId>"));

        String expected = buildExpectation("payload_jsons/createOperationalEnvironmentsPayloadToMso.json", permutation);

        assertThatExpectationIsLikeObject(expected, operationalEnvironmentCreationRequestDetails);
    }

    @DataProvider
    private Object[][] getOperationalEnvironmentCreationPermutations() {

        final ImmutableListMultimap<String, String> options = ImmutableListMultimap.<String, String>builder()
                // instanceName, ecompInstanceId, ecompInstanceName, operationalEnvType, tenantContext, workloadContext
                .putAll("<userId>", "1", "ceb60bba-7c18-49cd-a8f6-83ff2e1430b0", "Storm Landebert")
                .putAll("<instanceName>", "instanceName", "Slavica Hadrien")
                .putAll("<ecompInstanceId>", "ecompInstanceId", "58ec6753-957f-4124-8f92-c1c0bd2464a4")
                .putAll("<ecompInstanceName>", "ecompInstanceName", "Bente Keelin")
                .putAll("<operationalEnvType>", "operationalEnvType", "VNF")
                .putAll("<tenantContext>", "tenantContext", "Production")
                .putAll("<workloadContext>", "workloadContext", "E2E-extreme")
                .build();

        List<HashMap<String, String>> permutations = permuteOptions(options);

        return permutations.stream().map(m -> new Object[] { m }).collect(Collectors.toList()).toArray(new Object[0][0]);
    }

    @Test(dataProvider = "getOperationalEnvironmentDeactivationPermutations")
    public void testJsonResultOfOperationalEnvironmentDeactivationRequestDetails(HashMap<String, String> permutation) throws IOException {

        OperationalEnvironmentDeactivateInfo inputUnderTest = createOperationalEnvironmentDeactivateInfo("operationalEnvId>", permutation.get("<userId>"));

        final RequestDetailsWrapper<RequestDetails> operationalEnvironmentDeactivationRequestDetails = msoBusinessLogic.createOperationalEnvironmentDeactivationRequestDetails(inputUnderTest);

        String expected = buildExpectation("payload_jsons/deactivateOperationalEnvironmentsPayloadToMso.json", permutation);

        assertThatExpectationIsLikeObject(expected, operationalEnvironmentDeactivationRequestDetails);
    }

    @DataProvider
    private Object[][] getOperationalEnvironmentDeactivationPermutations() {

        final ImmutableListMultimap<String, String> options = ImmutableListMultimap.<String, String>builder()
                .putAll("<userId>", "instanceName", "Slavica Hadrien")
                .build();

        List<HashMap<String, String>> permutations = permuteOptions(options);

        return permutations.stream().map(m -> new Object[] { m }).collect(Collectors.toList()).toArray(new Object[0][0]);
    }

    private List<HashMap<String, String>> permuteOptions(ImmutableListMultimap<String, String> options) {
        // try any value, where the other keys are on the default one (index zero)
        // result it's not the whole world of permutations, but rather a skim set, which its
        // size is as the number of unique values in "options"

        HashMap<String, String> baseMutation = new HashMap<>();
        for (String key : options.keySet()) {
            baseMutation.put(key, options.get(key).get(0));
        }

        List<HashMap<String, String>> res = new LinkedList<>();
        res.add(baseMutation);

        for (String key : options.keySet()) {
            final ImmutableList<String> entry = options.get(key);
            for (String value : entry.subList(1, entry.size())) { // skip the first option at index zero
                HashMap<String, String> mutation = new HashMap<>();
                mutation.putAll(baseMutation);
                mutation.put(key, value);
                res.add(mutation);
            }
        }
        return res;
    }

    public static void assertThatExpectationIsLikeObject(String expected, Object requestDetails) throws JsonProcessingException {
        final String requestDetailsAsString = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(requestDetails);

        // assert for exact match
        try {
            JSONAssert.assertEquals("built mso request is not ok", expected, requestDetailsAsString, JSONCompareMode.STRICT);
        } catch (AssertionError | Exception e) {
            System.out.println("requestDetailsAsString: \n" + requestDetailsAsString);
            System.out.println("expected: \n" + expected);
            throw e;
        }
    }

    private String buildExpectation(String modelFileName, HashMap<String, String> permutation) throws IOException {
        // load expected result, and populate it with current input 'permutation' values
        final URL resource = this.getClass().getResource("/" + modelFileName);
        String expected = IOUtils.toString(resource, "UTF-8");

        for (Map.Entry<String, String> stringStringEntry : permutation.entrySet()) {
            expected = expected.replaceAll(stringStringEntry.getKey(), stringStringEntry.getValue());
        }
        return expected;
    }

    private OperationalEnvironmentActivateInfo createOperationalEnvironmentActivateInfo(String operationalEnvId, String userId, OperationalEnvironmentManifest manifest, String relatedInstanceId, String relatedInstanceName, String workloadContext) {
        OperationalEnvironmentController.OperationalEnvironmentActivateBody body = new OperationalEnvironmentController.OperationalEnvironmentActivateBody(relatedInstanceId, relatedInstanceName, workloadContext, manifest);
        return new OperationalEnvironmentActivateInfo(body, userId, operationalEnvId);
    }

    private OperationalEnvironmentDeactivateInfo createOperationalEnvironmentDeactivateInfo(String operationalEnvId, String userId) {
        return new OperationalEnvironmentDeactivateInfo(userId, operationalEnvId);
    }

    private OperationalEnvironmentController.OperationalEnvironmentCreateBody createOperationalEnvironmentCreateBody(String instanceName, String ecompInstanceId, String ecompInstanceName, String operationalEnvType, String tenantContext, String workloadContext) {
        return new OperationalEnvironmentController.OperationalEnvironmentCreateBody(instanceName, ecompInstanceId, ecompInstanceName, operationalEnvType, tenantContext, workloadContext);
    }
}