c57a90aefcbc622737b0e983ba463222f309f75b
[vid.git] / vid-automation / src / test / java / org / onap / vid / api / SampleApiTest.java
1 package org.onap.vid.api;
2
3 import com.google.common.collect.ImmutableMap;
4 import org.json.JSONException;
5 import org.skyscreamer.jsonassert.JSONAssert;
6 import org.skyscreamer.jsonassert.JSONCompareMode;
7 import org.springframework.http.HttpStatus;
8 import org.testng.annotations.BeforeClass;
9 import org.testng.annotations.Test;
10 import vid.automation.test.services.SimulatorApi;
11
12 import java.io.IOException;
13
14 import static org.hamcrest.core.Is.is;
15 import static org.junit.Assert.assertThat;
16
17 public class SampleApiTest extends BaseApiTest {
18
19     private static final String UUID = "927befca-e32c-4f7d-be8d-b4107e0ac31e";
20     private static final String FILE_NAME = "a_file_with_request_setup.json";
21     private static final String REQUEST_BODY = "{ \"foo\": \"bar\" }";
22
23     @BeforeClass
24     public void login() {
25         super.login();
26     }
27
28     @Test(enabled = false)
29     public void createWithSimplestBody() throws IOException, JSONException {
30         final String expectedResult = "" +
31                 "{" +
32                 "  \"requestReferences\": {" +
33                 "     \"requestId\": \"rq1234d1-5a33-55df-13ab-12abad84e331\","+
34                 "     \"instanceId\": \"" + UUID + "\"" +
35                 "  }" +
36                 "}";
37
38         callWithFineRequest(FILE_NAME,
39                 ImmutableMap.of("UUID", UUID),
40                 buildUri(), REQUEST_BODY,
41                 HttpStatus.ACCEPTED.value(), expectedResult);
42     }
43
44     private String buildUri() {
45         return uri + "/foo";
46     }
47
48     private void callWithFineRequest(String expectationJsonFileName, ImmutableMap<String, Object> replacementsForJson, String targetUri, String requestBody, int expectedStatusCode, String expectedResult) throws JSONException {
49         SimulatorApi.registerExpectation(expectationJsonFileName, replacementsForJson, SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET);
50
51         MyFooResponseType response = restTemplate.postForObject(targetUri, requestBody, MyFooResponseType.class);
52
53         assertThat("Wrong propagated status from MSO", response.getStatus(), is(expectedStatusCode));
54         JSONAssert.assertEquals("Wrong propagated body from MSO", expectedResult, getCleanJsonString(response.getEntity()), JSONCompareMode.NON_EXTENSIBLE);
55     }
56
57
58     private class MyFooResponseType {
59         public int getStatus() {
60             return 202;
61         }
62
63         public String getEntity() {
64             return "baz";
65         }
66     }
67 }