1 package org.onap.vid.api;
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;
17 import java.io.IOException;
18 import java.lang.reflect.Method;
20 import static org.hamcrest.core.Is.is;
21 import static org.junit.Assert.assertThat;
23 public class BaseMsoApiTest extends BaseApiTest {
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);
33 MsoResponseWrapper2 responseWrapper = callMsoForResponseWrapper(method, targetUri, basicRequestBody);
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));
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);
46 MsoResponseWrapper2 responseWrapper = callMsoForResponseWrapper(method, targetUri, requestBody);
48 assertThat("Wrong propagated status from MSO", responseWrapper.getStatus(), is(expectedStatusCode));
50 JSONAssert.assertEquals("Wrong propagated body from MSO", expectedResult, getCleanJsonString(responseWrapper.getEntity()), JSONCompareMode.NON_EXTENSIBLE);
51 } catch (JSONException e) {
52 throw new RuntimeException(e);
56 @Step(description = "method: ${method}, uri: ${uri}, body: ${body}")
57 protected MsoResponseWrapper2 callMsoForResponseWrapper(HttpMethod method, String uri, String body) {
58 MsoResponseWrapper2 responseWrapper;
61 responseWrapper = restTemplate.postForObject(uri, body, MsoResponseWrapper2.class);
65 responseWrapper = restTemplate.getForObject(uri, MsoResponseWrapper2.class);
69 System.out.println("response: " + responseWrapper);
71 return responseWrapper;
75 public static Object[][] errorCodes(Method test) {
76 return new Object[][]{
77 {500},{505}, {400}, {401}, {404}, {405}