aboutsummaryrefslogtreecommitdiffstats
path: root/vid-automation/src/test/java/org/onap/vid/api/OperationalEnvironmentControllerApiTest.java
blob: e5df2d3b973b7588fc992bcd541e54bf0cd56fae (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package org.onap.vid.api;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import vid.automation.test.services.SimulatorApi;
import vid.automation.test.services.SimulatorApi.RegistrationStrategy;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.stream.Collectors;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.METHOD_NOT_ALLOWED;

public class OperationalEnvironmentControllerApiTest extends BaseMsoApiTest {
    private static final String UUID = "927befca-e32c-4f7d-be8d-b4107e0ac31e";
    public static final String GET_STATUS_REQUEST_UUID = "3212b08c-0dcd-4d20-8c84-51e4f325c14a";
    private static final String BASIC_DEACTIVATE_REQUEST_BODY = "{}";
    private static final String BASIC_ACTIVATE_REQUEST_BODY = getResourceAsString("operationalEnvironmentController/activateOperationalEnvironment.json");
    private static final String BASIC_CREATE_REQUEST_BODY = getResourceAsString("operationalEnvironmentController/createOperationalEnvironment.json");
    private final String MSO_OK_RESPONSE_FOR_DEACTIVATE = "mso_ok_response_for_deactivate.json";
    public static final String GET_CLOUD_RESOURCES_REQUEST_STATUS = "get_cloud_resources_request_status.json";
    private final String MSO_ERROR_RESPONSE_FOR_DEACTIVATE = "mso_error_response_for_deactivate.json";
    private final String MSO_ERROR_RESPONSE_FOR_STATUS = "mso_error_response_for_status.json";
    private final String MSO_OK_RESPONSE_FOR_POST_OPERATIONAL_ENVIRONMENT = "mso_ok_response_for_post_operational_environmnet.json";
    private final String MSO_ERROR_RESPONSE_FOR_POST_OPERATIONAL_ENVIRONMENT = "mso_error_response_for_post_operational_environmnet.json";
    private final String missingParamErrorText = "Required String parameter 'operationalEnvironment' is not present";

    /*
    # DEACTIVATION

    ## Tests
    [x]  -  Try all methods: only POST is working
    [x]  -  Send wrong requests; all are responded with 400 and nice description
    [x]      -  missing param
    [x]      -  missing body
    [x]      -  operationalEnvironment value is empty
         -  Simulate MSO responses (status and body); verify all are propagated inside a VID's 200 OK
    [x]      -  [ 200, 202, 400, 404, 500 ]

         -  Positive cases
    [x]      - Request body is just '{}'
    [x]      - Request body is with some fields
    [x]      - URI with more query params

    ### Always verify
    [x]  -  Payload to MSO is the valid schema and values
    [ ]  -  RequestorId is ok

     */

    @Override
    @BeforeClass
    public void login() {
        super.login();
    }


    @Test(dataProvider = "wrongHttpMethodsForAllUris")
    public void tryAllWrongMethods(HttpMethod httpMethod, String uri) throws IOException {
        try {
            ResponseEntity<String> responseEntity = restTemplate.exchange(uri, httpMethod, new HttpEntity<ImmutableMap>(ImmutableMap.of()), String.class);
            assertThat("Response should be method not allowed => " + responseEntity, responseEntity.getBody(), containsString("Request method '" + httpMethod + "' not supported"));
        } catch (HttpClientErrorException e) {
            assertThat("Response should be method not allowed (by error code)", e.getStatusCode(), is(METHOD_NOT_ALLOWED));
        }

    }


    @Test
    public void activateWithAdditionalQueryParam() throws IOException {
        doWithFineRequest(BASIC_ACTIVATE_REQUEST_BODY, getActivationTargetUri(ACTIVATION_URI_UUID_MODE.EXTENDED), "/activate\"");
    }

    @Test(expectedExceptions = { HttpClientErrorException.class })
    public void activateWithMissingOperationalEnvironmentParam() throws IOException {
        doWithBadRequest(BASIC_ACTIVATE_REQUEST_BODY, missingParamErrorText, getActivationTargetUri(ACTIVATION_URI_UUID_MODE.MISSING));
    }

    @Test(expectedExceptions = { HttpClientErrorException.class })
    public void activateWithNoValueForOperationalEnvironmentParam() throws IOException {
        doWithBadRequest(BASIC_ACTIVATE_REQUEST_BODY, missingParamErrorText, getActivationTargetUri(ACTIVATION_URI_UUID_MODE.NO_VALUE));
    }

    @Test
    public void deactivateWithAdditionalQueryParam() throws IOException {
        doWithFineRequest(BASIC_DEACTIVATE_REQUEST_BODY, getDeactivationTargetUri(ACTIVATION_URI_UUID_MODE.EXTENDED), "/deactivate\"");
    }

    @Test(expectedExceptions = { HttpClientErrorException.class })
    public void deactivateWithMissingOperationalEnvironmentParam() throws IOException {
        doWithBadRequest(BASIC_DEACTIVATE_REQUEST_BODY, missingParamErrorText, getDeactivationTargetUri(ACTIVATION_URI_UUID_MODE.MISSING));
    }

    @Test(expectedExceptions = { HttpClientErrorException.class })
    public void deactivateWithNoValueForOperationalEnvironmentParam() throws IOException {
        doWithBadRequest(BASIC_DEACTIVATE_REQUEST_BODY, missingParamErrorText, getDeactivationTargetUri(ACTIVATION_URI_UUID_MODE.NO_VALUE));
    }

    @Test(dataProvider = "requestPayloads")
    public void activateWithBody(String requestBody) throws IOException {
        doWithFineRequest(requestBody, getActivationTargetUri(ACTIVATION_URI_UUID_MODE.OK), "/activate\"");
    }

    @Test(dataProvider = "requestPayloads")
    public void deactivateWithBody(String requestBody) throws IOException {
        doWithFineRequest(requestBody, getDeactivationTargetUri(ACTIVATION_URI_UUID_MODE.OK), "/deactivate\"");
    }


    private void doWithFineRequest(String requestBody, String targetUri, String v1) throws IOException {
        final String expectedResult = "" +
                "{" +
                "  \"requestReferences\": {" +
                "     \"requestId\": \"dbe54591-c8ed-46d3-abc7-d3a24873dfbd\"," +
                "     \"instanceId\": \"" + UUID + "\"" +
                "  }" +
                "}";
        callMsoWithFineRequest(MSO_OK_RESPONSE_FOR_DEACTIVATE, ImmutableMap.of(
                    "/deactivate\"", v1,
                    "UUID", UUID)
                ,targetUri,requestBody,HttpStatus.ACCEPTED.value(),expectedResult, HttpMethod.POST);
       }

    @Test(dataProvider = "errorCodes")
    public void deactivateWithErrorResponse(int errorCode) throws IOException {
        doWithSimulatedErrorResponse(errorCode, getDeactivationTargetUri(ACTIVATION_URI_UUID_MODE.OK), BASIC_DEACTIVATE_REQUEST_BODY, "/deactivate\"", MSO_ERROR_RESPONSE_FOR_DEACTIVATE, HttpMethod.POST);
    }

    @Test(dataProvider = "errorCodes")
    public void activateWithErrorResponse(int errorCode) throws IOException {
        doWithSimulatedErrorResponse(errorCode, getActivationTargetUri(ACTIVATION_URI_UUID_MODE.OK), BASIC_ACTIVATE_REQUEST_BODY, "/activate\"", MSO_ERROR_RESPONSE_FOR_DEACTIVATE, HttpMethod.POST);
    }

    @Test(dataProvider = "errorCodes")
    public void testStatusWithErrorResponse(int errorCode) throws IOException {
        doWithSimulatedErrorResponse(errorCode, getStatusTargetUri(STATUS_URI_UUID_MODE.OK), "", "", MSO_ERROR_RESPONSE_FOR_STATUS, HttpMethod.GET);
    }

    private void doWithSimulatedErrorResponse(int errorCode, String targetUri, String basicRequestBody, String msoPathSuffix, String expectationTemplateFilename, HttpMethod method) throws IOException {
        final String expectedResult = "" +
                "<head>Huston, you have a problem<head>";
        callMsoWithSimulatedErrorResponse(expectationTemplateFilename, ImmutableMap.of(
                    "/deactivate\"", msoPathSuffix,
                    "UUID", UUID,
                    "500", errorCode,
                    "ERROR_PAYLOAD", StringEscapeUtils.escapeJson(expectedResult)
                    ),targetUri,basicRequestBody,errorCode, expectedResult, method);
    }

    @Test(
            dataProvider = "requestPayloads",
            expectedExceptions = { HttpClientErrorException.class }
    )
    public void activateWithBadRequest(String requestBody) throws IOException {
        doWithBadRequest(requestBody, "HttpMessageNotReadableException", getActivationTargetUri(ACTIVATION_URI_UUID_MODE.OK));
    }

    @Test(
            dataProvider = "activateBadHalfBakedPayloads",
            expectedExceptions = { HttpClientErrorException.class }
    )
    public void activateWithBadHalfBakedPayload(String requestBody) throws IOException {
        doWithBadRequest(requestBody, "HttpMessageNotReadableException", getActivationTargetUri(ACTIVATION_URI_UUID_MODE.OK));
    }

    @Test(
            dataProvider = "requestPayloads",
            expectedExceptions = { HttpClientErrorException.class }
    )
    public void deactivateWithBadRequest(String requestBody) throws IOException {
        doWithBadRequest(requestBody, "HttpMessageNotReadableException", getDeactivationTargetUri(ACTIVATION_URI_UUID_MODE.OK));
    }

    private void doWithBadRequest(String requestBody, String httpMessageNotReadableException, String targetUri) throws IOException {
        SimulatorApi.registerExpectation(MSO_OK_RESPONSE_FOR_DEACTIVATE, ImmutableMap.of("UUID", UUID), RegistrationStrategy.CLEAR_THEN_SET);

        try {
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(targetUri, requestBody, String.class);
        } catch (HttpClientErrorException e) {
            assertThat("Response should be Bad Request (by error code)", e.getStatusCode(), is(BAD_REQUEST));
            assertThat("Response should be Bad Request (by body)", e.getResponseBodyAsString(), containsString(httpMessageNotReadableException));
            throw e;
        }

    }


    @DataProvider
    public Object[][] wrongHttpMethodsForAllUris() {
        ImmutableList<String> uris = ImmutableList.of(
                getDeactivationTargetUri(ACTIVATION_URI_UUID_MODE.OK),
                getActivationTargetUri(ACTIVATION_URI_UUID_MODE.OK)
        );
        
        return Arrays.stream(HttpMethod.values())
                .filter(Streams.not(ImmutableList.of(
                        HttpMethod.POST,    // because POST *should* work
                        HttpMethod.PATCH,   // because PATCH is invalid method for Java.net
                        HttpMethod.OPTIONS, // because OPTIONS is somehow valid...  :-(  => Allow=[GET, HEAD, POST, PUT, DELETE, OPTIONS, PATCH]
                        HttpMethod.HEAD     // because HEAD is like POST/GET but without body, so error is hidden
                )::contains))
                .flatMap(httpMethod -> uris.stream()
                        .map(uri -> new Object[]{ httpMethod, uri})  // pair given method for each of the URIs
                ) 
                .collect(Collectors.toList())
                .toArray(new Object[][] {});
    }

    @DataProvider
    public static Object[][] requestPayloads(Method test) {
        switch (test.getName()) {
            case "deactivateWithBody":
                return new Object[][]{
                        {BASIC_DEACTIVATE_REQUEST_BODY}
                        , {"{ \"a\": \"b\" }"}
                        , {"{ \"a\": [ 55 ] }"}
                };
            case "activateWithBody":
                return new Object[][]{
                        {BASIC_ACTIVATE_REQUEST_BODY}
                };
            default: // bad payloads
                return new Object[][]{
                        {null}
                        , {""}
                        , {"{"}
                        , {"foo"}
                };
        }
    }

    @DataProvider
    public static Object[][] activateBadHalfBakedPayloads(Method test) {
        final ImmutableList<String> strings = ImmutableList.of(
                "\"relatedInstanceId\": \"1117887f-068f-46d7-9e43-9b73bef17af8\"",
                "\"relatedInstanceName\": \"managing ECOMP Operational Environment\"",
                "\"workloadContext\": \"VNF_E2E-IST\"",
                "\"manifest\": {" +
                        "  \"serviceModelList\": [" +
                        "    {" +
                        "      \"serviceModelVersionId\": \"uuid2\"," +
                        "      \"recoveryAction\": \"retry\"" +
                        "    }" +
                        "  ]" +
                        "}"
        );

        final LinkedList<String> tests = new LinkedList<>();
        for (int i = 0; i < strings.size(); i++) {
            final ArrayList<String> aCase = new ArrayList<>(strings);
            aCase.remove(i);
            tests.add("{" + String.join(", ", aCase) + "}");
        }

        return tests.stream().map(o -> new Object[] { o }).collect(Collectors.toList()).toArray(new Object[][]{});
    }

    @DataProvider
    public static Object[][] errorCodes(Method test) {
        return new Object[][]{
                {500}, {505}, {400}, {401}, {405}
        };
    }

    @DataProvider
    public static Object[][] statusLegitUri(Method test) {
        return new Object[][]{
                {STATUS_URI_UUID_MODE.OK}, {STATUS_URI_UUID_MODE.EXTENDED}
        };
    }

    @DataProvider
    public static Object[][] statusNotLegitUri(Method test) {
        return new Object[][]{
                {STATUS_URI_UUID_MODE.MISSING}, {STATUS_URI_UUID_MODE.NO_VALUE}
        };
    }

    private enum ACTIVATION_URI_UUID_MODE {
        MISSING(""),
        NO_VALUE("?operationalEnvironment="),
        OK(NO_VALUE.val + UUID),
        EXTENDED(OK.val + "&anotherParam=6");

        final String val;

        ACTIVATION_URI_UUID_MODE(String val) {
            this.val = val;
        }
    }

    private enum STATUS_URI_UUID_MODE {
        MISSING(""),
        NO_VALUE("?requestId="),
        OK(NO_VALUE.val + GET_STATUS_REQUEST_UUID),
        EXTENDED(OK.val + "&anotherParam=6");

        final String val;

        STATUS_URI_UUID_MODE(String val) {
            this.val = val;
        }
    }

    private String getDeactivationTargetUri(ACTIVATION_URI_UUID_MODE uriUuidMode) {
        return uri.toASCIIString() + "/operationalEnvironment/deactivate" + uriUuidMode.val;
    }

    private String getActivationTargetUri(ACTIVATION_URI_UUID_MODE uriUuidMode) {
        return uri.toASCIIString() + "/operationalEnvironment/activate" + uriUuidMode.val;
    }

    private String getStatusTargetUri(STATUS_URI_UUID_MODE uriUuidMode) {
        return uri.toASCIIString() + "/operationalEnvironment/requestStatus" + uriUuidMode.val;
    }

    private String getCreateOperationEnvironmentUri() {
        return uri.toASCIIString() + "/operationalEnvironment/create";
    }

    @Test
    public void createWithSimplestBody()throws IOException {

        final String expectedResult = "" +
                "{" +
                "  \"requestReferences\": {" +
                "     \"requestId\": \"dbe54591-c8ed-46d3-abc7-d3a24873dfbd\","+
                "     \"instanceId\": \"" + UUID + "\"" +
                "  }" +
                "}";
        callMsoWithFineRequest(MSO_OK_RESPONSE_FOR_POST_OPERATIONAL_ENVIRONMENT, ImmutableMap.of(
                "UUID", UUID), getCreateOperationEnvironmentUri(), BASIC_CREATE_REQUEST_BODY, HttpStatus.ACCEPTED.value(), expectedResult, HttpMethod.POST);
   }

    @Test(dataProvider = "errorCodes")
    public void createWithErrorResponse(int errorCode) throws IOException {
        final String expectedResult = "" +
                "<head>Huston, you have a problem<head>";
        callMsoWithSimulatedErrorResponse(MSO_ERROR_RESPONSE_FOR_POST_OPERATIONAL_ENVIRONMENT,ImmutableMap.of(
                "500", errorCode,
                "ERROR_PAYLOAD", StringEscapeUtils.escapeJson(expectedResult)
        ),  getCreateOperationEnvironmentUri(), BASIC_CREATE_REQUEST_BODY, errorCode,expectedResult, HttpMethod.POST);
    }




    @Test(dataProvider = "statusLegitUri")
    public void testStatusWithLegitUri(STATUS_URI_UUID_MODE statusUriMode) throws IOException {

        String uri = getStatusTargetUri(statusUriMode);

        final String expectedResult = "" +
                "{" +
                "  \"request\": {" +
                "    \"requestId\": \"3212b08c-0dcd-4d20-8c84-51e4f325c14a\"," +
                "    \"startTime\": \"Thu, 02 Jun 2017 02:51:59 GMT\"," +
                "    \"instanceReferences\": {" +
                "      \"operationalEnvironmentInstanceId\": \"bc305d54-75b4-431b-adb2-eb6b9e546014\"" +
                "    }," +
                "    \"requestScope\": \"operationalEnvironment\"," +
                "    \"requestType\": \"deactivate\"," +
                "    \"requestDetails\": {" +
                "      \"requestInfo\": {" +
                "        \"resourceType\": \"operationalEnvironment\"," +
                "        \"source\": \"VID\"," +
                "        \"requestorId\": \"az2017\"" +
                "      }," +
                "      \"requestParameters\": {" +
                "        \"operationalEnvironmentType\": \"VNF\"" +
                "      }" +
                "    }," +
                "    \"requestStatus\": {" +
                "      \"timestamp\": \"Thu, 02 Jun 2017 02:53:39 GMT\"," +
                "      \"requestState\": \"COMPLETE\"," +
                "      \"statusMessage\": \"Operational Environment successfully deactivated\"," +
                "      \"percentProgress\": \"100\"" +
                "    }" +
                "  }" +
                "}";

        callMsoWithFineRequest(GET_CLOUD_RESOURCES_REQUEST_STATUS, ImmutableMap.of()
                ,uri,"",HttpStatus.OK.value(),expectedResult, HttpMethod.GET);

    }

    @Test(
            expectedExceptions = {HttpClientErrorException.class},
            dataProvider = "statusNotLegitUri"
    )
    public void testStatusWithBadRequest(STATUS_URI_UUID_MODE statusUriMode) throws IOException {
        SimulatorApi.registerExpectation(GET_CLOUD_RESOURCES_REQUEST_STATUS, RegistrationStrategy.CLEAR_THEN_SET);

        String uri = getStatusTargetUri(statusUriMode);

        try {
            ResponseEntity<String> responseEntity = restTemplate.getForEntity(uri, String.class);
        } catch (HttpClientErrorException e) {
            assertThat("Response should be Bad Request (by error code)", e.getStatusCode(), is(BAD_REQUEST));
            assertThat("Response should be Bad Request (by body)", e.getResponseBodyAsString(), containsString("'requestId' is not present"));
            throw e;
        }
    }

    @Test
    public void testStatusWithWrongMethodPost() throws IOException {
        SimulatorApi.registerExpectation(GET_CLOUD_RESOURCES_REQUEST_STATUS, RegistrationStrategy.CLEAR_THEN_SET);

        String myUri = getStatusTargetUri(STATUS_URI_UUID_MODE.OK);

        String response = restTemplate.postForObject(myUri, "", String.class);
        assertThat("Response should be method not allowed => " + response, response, containsString("Request method '" + HttpMethod.POST + "' not supported"));
    }
}