1 package org.onap.vid.mso.rest;
3 import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp;
4 import static com.xebialabs.restito.builder.verify.VerifyHttp.verifyHttp;
5 import static com.xebialabs.restito.semantics.Action.contentType;
6 import static com.xebialabs.restito.semantics.Action.status;
7 import static com.xebialabs.restito.semantics.Action.stringContent;
8 import static com.xebialabs.restito.semantics.Condition.delete;
9 import static com.xebialabs.restito.semantics.Condition.get;
10 import static com.xebialabs.restito.semantics.Condition.method;
11 import static com.xebialabs.restito.semantics.Condition.post;
12 import static com.xebialabs.restito.semantics.Condition.uri;
13 import static com.xebialabs.restito.semantics.Condition.withHeader;
15 import com.fasterxml.jackson.databind.ObjectMapper;
16 import com.xebialabs.restito.semantics.Action;
17 import com.xebialabs.restito.server.StubServer;
18 import java.io.IOException;
19 import java.util.function.BiFunction;
20 import java.util.function.Function;
21 import javax.ws.rs.core.HttpHeaders;
22 import javax.ws.rs.core.MediaType;
24 import org.glassfish.grizzly.http.Method;
25 import org.glassfish.grizzly.http.util.HttpStatus;
26 import org.json.JSONObject;
27 import org.junit.Assert;
28 import org.onap.portalsdk.core.util.SystemProperties;
29 import org.onap.vid.changeManagement.RequestDetailsWrapper;
30 import org.onap.vid.mso.MsoResponseWrapper;
32 class MsoRestClientTestUtil implements AutoCloseable {
33 private final StubServer server;
34 private final String endpoint;
35 private final String responsePayload;
36 private final HttpStatus expectedStatus;
37 private final String expectedResponseStr;
39 MsoRestClientTestUtil(StubServer server, String endpoint, HttpStatus expectedStatus,
40 String responsePayload,
41 String expectedResponseStr) {
43 this.endpoint = endpoint;
44 this.responsePayload = responsePayload;
45 this.expectedStatus = expectedStatus;
46 this.expectedResponseStr = expectedResponseStr;
49 void executePost(String jsonPayload, BiFunction<RequestDetails, String, MsoResponseWrapper> func) throws IOException {
51 .match(post(endpoint))
52 .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
54 RequestDetails sampleRequestDetails =
55 new ObjectMapper().readValue(jsonPayload, RequestDetails.class);
57 MsoResponseWrapper response = func.apply(sampleRequestDetails, endpoint);
58 JSONObject actualJson = new JSONObject(response.getEntity());
60 Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
61 Assert.assertEquals(expectedResponseStr, actualJson.toString());
62 verifyServer(server, endpoint, Method.POST);
65 void executePostCall(String jsonPayload, BiFunction<RequestDetailsWrapper, String, MsoResponseWrapper> func) throws IOException {
67 .match(post(endpoint))
68 .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
70 RequestDetailsWrapper sampleRequestDetails =
71 new ObjectMapper().readValue(jsonPayload, RequestDetailsWrapper.class);
73 MsoResponseWrapper response = func.apply(sampleRequestDetails, endpoint);
74 JSONObject actualJson = new JSONObject(response.getEntity());
76 Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
77 Assert.assertEquals(expectedResponseStr, actualJson.toString());
78 verifyServer(server, endpoint, Method.POST);
81 void executeDelete(String jsonPayload, BiFunction<RequestDetails, String, MsoResponseWrapper> func)
84 .match(delete(endpoint))
85 .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
87 RequestDetails sampleRequestDetails =
88 new ObjectMapper().readValue(jsonPayload, RequestDetails.class);
89 MsoResponseWrapper response = func.apply(sampleRequestDetails, endpoint);
91 Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
92 verifyServer(server, endpoint, Method.DELETE);
95 void executeGet(Function<String, MsoResponseWrapper> func) {
98 .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
100 MsoResponseWrapper response = func.apply(endpoint);
102 Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
103 verifyServer(server, endpoint, Method.GET);
106 private void verifyServer(StubServer server, String endpoint, Method httpMethod) {
107 verifyHttp(server).once(
110 withHeader(HttpHeaders.AUTHORIZATION),
111 withHeader(HttpHeaders.ACCEPT),
112 withHeader(HttpHeaders.CONTENT_TYPE),
113 withHeader(MsoRestClientNew.X_FROM_APP_ID),
114 withHeader(SystemProperties.ECOMP_REQUEST_ID));
117 private Action jsonContent(String str) {
118 return stringContent(str);
122 public void close() {