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;
23 import org.glassfish.grizzly.http.Method;
24 import org.glassfish.grizzly.http.util.HttpStatus;
25 import org.json.JSONObject;
26 import org.junit.Assert;
27 import org.onap.portalsdk.core.util.SystemProperties;
28 import org.onap.vid.mso.MsoResponseWrapper;
30 class MsoRestClientTestUtil implements AutoCloseable {
31 private final StubServer server;
32 private final String endpoint;
33 private final String responsePayload;
34 private final HttpStatus expectedStatus;
35 private final String expectedResponseStr;
37 MsoRestClientTestUtil(StubServer server, String endpoint, HttpStatus expectedStatus,
38 String responsePayload,
39 String expectedResponseStr) {
41 this.endpoint = endpoint;
42 this.responsePayload = responsePayload;
43 this.expectedStatus = expectedStatus;
44 this.expectedResponseStr = expectedResponseStr;
47 void executePost(String jsonPayload, BiFunction<RequestDetails, String, MsoResponseWrapper> func) throws IOException {
49 .match(post(endpoint))
50 .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
52 RequestDetails sampleRequestDetails =
53 new ObjectMapper().readValue(jsonPayload, RequestDetails.class);
55 MsoResponseWrapper response = func.apply(sampleRequestDetails, endpoint);
56 JSONObject actualJson = new JSONObject(response.getEntity());
58 Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
59 Assert.assertEquals(expectedResponseStr, actualJson.toString());
60 verifyServer(server, endpoint, Method.POST);
64 void executeDelete(String jsonPayload, BiFunction<RequestDetails, String, MsoResponseWrapper> func)
67 .match(delete(endpoint))
68 .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
70 RequestDetails sampleRequestDetails =
71 new ObjectMapper().readValue(jsonPayload, RequestDetails.class);
72 MsoResponseWrapper response = func.apply(sampleRequestDetails, endpoint);
74 Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
75 verifyServer(server, endpoint, Method.DELETE);
78 void executeGet(Function<String, MsoResponseWrapper> func) {
81 .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
83 MsoResponseWrapper response = func.apply(endpoint);
85 Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
86 verifyServer(server, endpoint, Method.GET);
89 private void verifyServer(StubServer server, String endpoint, Method httpMethod) {
90 verifyHttp(server).once(
93 withHeader(HttpHeaders.AUTHORIZATION),
94 withHeader(HttpHeaders.ACCEPT),
95 withHeader(HttpHeaders.CONTENT_TYPE),
96 withHeader(MsoRestClientNew.X_FROM_APP_ID),
97 withHeader(SystemProperties.ECOMP_REQUEST_ID));
100 private Action jsonContent(String str) {
101 return stringContent(str);
105 public void close() {