1 package org.onap.vid.api;
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;
19 import java.io.IOException;
20 import java.lang.reflect.Method;
21 import java.util.List;
23 import static org.hamcrest.core.Is.is;
24 import static org.junit.Assert.assertThat;
26 public class BaseMsoApiTest extends BaseApiTest {
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);
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);
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);
48 private void callMsoAndAssertError(String targetUri, String basicRequestBody, int expectedErrorCode, String expectedResult, HttpMethod method) throws IOException {
50 MsoResponseWrapper2 responseWrapper = callMsoForResponseWrapper(method, targetUri, basicRequestBody);
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));
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);
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);
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);
75 private void callMsoAndAssertSuccess(String targetUri, String requestBody, int expectedStatusCode, String expectedResult, HttpMethod method) throws IOException {
76 MsoResponseWrapper2 responseWrapper = callMsoForResponseWrapper(method, targetUri, requestBody);
78 assertThat("Wrong propagated status from MSO", responseWrapper.getStatus(), is(expectedStatusCode));
80 JSONAssert.assertEquals("Wrong propagated body from MSO", expectedResult, getCleanJsonString(responseWrapper.getEntity()), JSONCompareMode.NON_EXTENSIBLE);
81 } catch (JSONException e) {
82 throw new RuntimeException(e);
86 //@Step(description = "method: ${method}, uri: ${uri}, body: ${body}")
87 protected MsoResponseWrapper2 callMsoForResponseWrapper(HttpMethod method, String uri, String body) {
88 MsoResponseWrapper2 responseWrapper;
91 responseWrapper = restTemplate.postForObject(uri, body, MsoResponseWrapper2.class);
95 responseWrapper = restTemplate.getForObject(uri, MsoResponseWrapper2.class);
99 System.out.println("response: " + responseWrapper);
101 return responseWrapper;
105 public static Object[][] errorCodes(Method test) {
106 return new Object[][]{
107 {500},{505}, {400}, {401}, {404}, {405}