aboutsummaryrefslogtreecommitdiffstats
path: root/vid-automation/src/test/java/org/onap/vid/api/BaseApiAaiTest.java
blob: 69a6f66cc98c018c802571e9066fde297f76ff6b (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
package org.onap.vid.api;

import com.google.common.collect.ImmutableMap;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.HttpStatusCodeException;
import org.testng.annotations.BeforeClass;
import vid.automation.test.services.SimulatorApi;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import static net.javacrumbs.jsonunit.JsonMatchers.jsonStringEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.either;
import static org.hamcrest.core.Is.is;
import static vid.automation.test.services.SimulatorApi.registerExpectation;

/**
 * Created by Oren on 11/1/17.
 */
public class BaseApiAaiTest extends BaseApiTest {

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


    protected void callAaiWithSimulatedErrorResponse(String [] expectationJsonFileNames, ImmutableMap<String, Object> replacementsForJson, String targetUri, String basicRequestBody, int expectedErrorCode, String expectedResult, HttpMethod method) throws IOException, URISyntaxException {

        registerExpectation(expectationJsonFileNames, replacementsForJson, SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET);
        RequestEntity<String> request;
        switch (method) {
            case POST:
                //not supported yet
                break;

            case PUT:
               request = RequestEntity
                        .put(new URI(targetUri))
                        .body(basicRequestBody);
                try {
                   restTemplate.exchange(request, String.class);
                }
                catch(HttpStatusCodeException e) {
                    assertThat("Wrong propagated status from AAI", e.getStatusCode().value(), is(expectedErrorCode));
                }


            case GET:
                try {
                    ResponseEntity<String> responseWrapper = restTemplate.getForEntity(targetUri, String.class);
                    assertThat("Wrong propagated status from AAI", responseWrapper.getStatusCode().value(), is(expectedErrorCode));
                    assertThat("The response is in the format of JSON", responseWrapper.getBody(),
                            either(is(expectedResult)).or(jsonStringEquals(expectedResult)));
                }
                catch(HttpClientErrorException | HttpServerErrorException e) {
                    assertThat("Wrong propagated status from AAI", e.getStatusCode().value(), is(expectedErrorCode));
                }
                break;
        }


    }
}