Merge from ECOMP's repository
[vid.git] / vid-automation / src / test / java / org / onap / vid / api / BaseMsoApiTest.java
1 package org.onap.vid.api;
2
3 import com.google.common.collect.ImmutableMap;
4 import org.json.JSONException;
5 import org.onap.vid.model.mso.MsoResponseWrapper2;
6 import org.skyscreamer.jsonassert.JSONAssert;
7 import org.skyscreamer.jsonassert.JSONCompareMode;
8 import org.springframework.http.HttpMethod;
9 import org.springframework.web.client.HttpClientErrorException;
10 import org.springframework.web.client.HttpServerErrorException;
11 import org.testng.annotations.BeforeClass;
12 import org.testng.annotations.DataProvider;
13 import vid.automation.test.services.SimulatorApi;
14 import vid.automation.test.services.SimulatorApi.RegistrationStrategy;
15
16 import java.io.IOException;
17 import java.lang.reflect.Method;
18
19 import static org.hamcrest.MatcherAssert.assertThat;
20 import static org.hamcrest.core.Is.is;
21
22 public class BaseMsoApiTest extends BaseApiTest {
23
24     @BeforeClass
25     public void login() {
26         super.login();
27     }
28
29     protected void callMsoWithSimulatedErrorResponse(String expectationJsonFileName, ImmutableMap<String, Object> replacementsForJson, String targetUri, String basicRequestBody, int expectedErrorCode, String expectedResult, HttpMethod method) throws IOException {
30         SimulatorApi.registerExpectation(expectationJsonFileName, replacementsForJson, RegistrationStrategy.CLEAR_THEN_SET);
31         try {
32             MsoResponseWrapper2 responseWrapper = callMsoForResponseWrapper(method, targetUri, basicRequestBody);
33
34             assertThat("Wrong propagated status from MSO", responseWrapper.getStatus(), is(expectedErrorCode));
35             assertThat("Wrong propagated body from MSO", getCleanJsonString(responseWrapper.getEntity()), is(expectedResult));
36         }catch(HttpClientErrorException | HttpServerErrorException e) {
37             assertThat("Wrong propagated status from MSO", e.getStatusCode().value(), is(expectedErrorCode));
38         }
39     }
40
41
42     protected void callMsoWithFineRequest(String expectationJsonFileName, ImmutableMap<String, Object> replacementsForJson, String targetUri, String requestBody, int expectedStatusCode, String expectedResult, HttpMethod method) throws IOException {
43         SimulatorApi.registerExpectation(expectationJsonFileName, replacementsForJson, RegistrationStrategy.CLEAR_THEN_SET);
44
45         MsoResponseWrapper2 responseWrapper = callMsoForResponseWrapper(method, targetUri, requestBody);
46
47         assertThat("Wrong propagated status from MSO", responseWrapper.getStatus(), is(expectedStatusCode));
48         try {
49             JSONAssert.assertEquals("Wrong propagated body from MSO", expectedResult, getCleanJsonString(responseWrapper.getEntity()), JSONCompareMode.NON_EXTENSIBLE);
50         } catch (JSONException e) {
51             throw new RuntimeException(e);
52         }
53     }
54
55     protected MsoResponseWrapper2 callMsoForResponseWrapper(HttpMethod method, String uri, String body) {
56         MsoResponseWrapper2 responseWrapper;
57         switch (method) {
58             case POST:
59                 responseWrapper = restTemplate.postForObject(uri, body, MsoResponseWrapper2.class);
60                 break;
61             case GET:
62             default:
63                 responseWrapper = restTemplate.getForObject(uri, MsoResponseWrapper2.class);
64                 break;
65         }
66
67         System.out.println("response: " + responseWrapper);
68
69         return responseWrapper;
70     }
71
72     @DataProvider
73     public static Object[][] errorCodes(Method test) {
74         return new Object[][]{
75                 {500},{505}, {400}, {401}, {404}, {405}
76         };
77     }
78 }