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