1 package org.onap.vid.api;
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;
18 import java.io.IOException;
19 import java.lang.reflect.Method;
20 import java.util.List;
22 import static org.hamcrest.core.Is.is;
23 import static org.junit.Assert.assertThat;
25 public class BaseMsoApiTest extends BaseApiTest {
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);
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);
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);
47 private void callMsoAndAssertError(String targetUri, String basicRequestBody, int expectedErrorCode, String expectedResult, HttpMethod method) throws IOException {
49 MsoResponseWrapper2 responseWrapper = callMsoForResponseWrapper(method, targetUri, basicRequestBody);
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));
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);
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);
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);
74 private void callMsoAndAssertSuccess(String targetUri, String requestBody, int expectedStatusCode, String expectedResult, HttpMethod method) throws IOException {
75 MsoResponseWrapper2 responseWrapper = callMsoForResponseWrapper(method, targetUri, requestBody);
77 assertThat("Wrong propagated status from MSO", responseWrapper.getStatus(), is(expectedStatusCode));
79 JSONAssert.assertEquals("Wrong propagated body from MSO", expectedResult, getCleanJsonString(responseWrapper.getEntity()), JSONCompareMode.NON_EXTENSIBLE);
80 } catch (JSONException e) {
81 throw new RuntimeException(e);
85 protected MsoResponseWrapper2 callMsoForResponseWrapper(HttpMethod method, String uri, String body) {
86 MsoResponseWrapper2 responseWrapper;
89 responseWrapper = restTemplate.postForObject(uri, body, MsoResponseWrapper2.class);
93 responseWrapper = restTemplate.getForObject(uri, MsoResponseWrapper2.class);
97 System.out.println("response: " + responseWrapper);
99 return responseWrapper;
103 public static Object[][] errorCodes(Method test) {
104 return new Object[][]{
105 {500},{505}, {400}, {401}, {404}, {405}