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