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