1 package org.onap.vid.api;
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;
16 import java.io.IOException;
17 import java.lang.reflect.Method;
19 import static org.hamcrest.MatcherAssert.assertThat;
20 import static org.hamcrest.core.Is.is;
22 public class BaseMsoApiTest extends BaseApiTest {
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);
32 MsoResponseWrapper2 responseWrapper = callMsoForResponseWrapper(method, targetUri, basicRequestBody);
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));
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);
45 MsoResponseWrapper2 responseWrapper = callMsoForResponseWrapper(method, targetUri, requestBody);
47 assertThat("Wrong propagated status from MSO", responseWrapper.getStatus(), is(expectedStatusCode));
49 JSONAssert.assertEquals("Wrong propagated body from MSO", expectedResult, getCleanJsonString(responseWrapper.getEntity()), JSONCompareMode.NON_EXTENSIBLE);
50 } catch (JSONException e) {
51 throw new RuntimeException(e);
55 protected MsoResponseWrapper2 callMsoForResponseWrapper(HttpMethod method, String uri, String body) {
56 MsoResponseWrapper2 responseWrapper;
59 responseWrapper = restTemplate.postForObject(uri, body, MsoResponseWrapper2.class);
63 responseWrapper = restTemplate.getForObject(uri, MsoResponseWrapper2.class);
67 System.out.println("response: " + responseWrapper);
69 return responseWrapper;
73 public static Object[][] errorCodes(Method test) {
74 return new Object[][]{
75 {500},{505}, {400}, {401}, {404}, {405}