Move some tests from group 3 to group 5
[vid.git] / vid-automation / src / test / java / org / onap / vid / api / BaseApiAaiTest.java
1 package org.onap.vid.api;
2
3 import static net.javacrumbs.jsonunit.JsonMatchers.jsonStringEquals;
4 import static org.hamcrest.Matchers.either;
5 import static org.hamcrest.core.Is.is;
6 import static org.junit.Assert.assertThat;
7 import static vid.automation.test.services.SimulatorApi.registerExpectation;
8
9 import com.google.common.collect.ImmutableMap;
10 import java.io.IOException;
11 import java.net.URI;
12 import java.net.URISyntaxException;
13 import org.springframework.http.HttpMethod;
14 import org.springframework.http.RequestEntity;
15 import org.springframework.http.ResponseEntity;
16 import org.springframework.web.client.HttpClientErrorException;
17 import org.springframework.web.client.HttpServerErrorException;
18 import org.springframework.web.client.HttpStatusCodeException;
19 import org.testng.annotations.BeforeClass;
20 import vid.automation.test.services.CategoryParamsService;
21 import vid.automation.test.services.SimulatorApi;
22
23 public class BaseApiAaiTest extends BaseApiTest {
24
25     protected static final CategoryParamsService categoryParamsService = new CategoryParamsService();
26
27     @BeforeClass
28     public void login() {
29         super.login();
30     }
31
32
33     protected void callAaiWithSimulatedErrorResponse(String [] expectationJsonFileNames, ImmutableMap<String, Object> replacementsForJson, String targetUri, String basicRequestBody, int expectedErrorCode, String expectedResult, HttpMethod method) throws IOException, URISyntaxException {
34
35         registerExpectation(expectationJsonFileNames, replacementsForJson, SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET);
36         RequestEntity<String> request;
37         switch (method) {
38             case POST:
39                 //not supported yet
40                 break;
41
42             case PUT:
43                request = RequestEntity
44                         .put(new URI(targetUri))
45                         .body(basicRequestBody);
46                 try {
47                    restTemplate.exchange(request, String.class);
48                 }
49                 catch(HttpStatusCodeException e) {
50                     assertThat("Wrong propagated status from AAI", e.getStatusCode().value(), is(expectedErrorCode));
51                 }
52
53
54             case GET:
55                 try {
56                     ResponseEntity<String> responseWrapper = restTemplate.getForEntity(targetUri, String.class);
57                     assertThat("Wrong propagated status from AAI", responseWrapper.getStatusCode().value(), is(expectedErrorCode));
58                     assertThat("The response is in the format of JSON", responseWrapper.getBody(),
59                             either(is(expectedResult)).or(jsonStringEquals(expectedResult)));
60                 }
61                 catch(HttpClientErrorException | HttpServerErrorException e) {
62                     assertThat("Wrong propagated status from AAI", e.getStatusCode().value(), is(expectedErrorCode));
63                 }
64                 break;
65         }
66
67
68     }
69 }