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