Merge "Increase tests coverage in backend"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / mso / rest / MsoRestClientTestUtil.java
1 package org.onap.vid.mso.rest;
2
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;
14
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
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;
31
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;
38
39   MsoRestClientTestUtil(StubServer server, String endpoint, HttpStatus expectedStatus,
40       String responsePayload,
41       String expectedResponseStr) {
42     this.server = server;
43     this.endpoint = endpoint;
44     this.responsePayload = responsePayload;
45     this.expectedStatus = expectedStatus;
46     this.expectedResponseStr = expectedResponseStr;
47   }
48
49   void executePost(String jsonPayload, BiFunction<RequestDetails, String, MsoResponseWrapper> func) throws IOException {
50     whenHttp(server)
51         .match(post(endpoint))
52         .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
53
54     RequestDetails sampleRequestDetails =
55         new ObjectMapper().readValue(jsonPayload, RequestDetails.class);
56
57     MsoResponseWrapper response = func.apply(sampleRequestDetails, endpoint);
58     JSONObject actualJson = new JSONObject(response.getEntity());
59
60     Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
61     Assert.assertEquals(expectedResponseStr, actualJson.toString());
62     verifyServer(server, endpoint, Method.POST);
63
64   }
65   void executePostCall(String jsonPayload, BiFunction<RequestDetailsWrapper, String, MsoResponseWrapper> func) throws IOException {
66     whenHttp(server)
67             .match(post(endpoint))
68             .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
69
70     RequestDetailsWrapper  sampleRequestDetails =
71             new ObjectMapper().readValue(jsonPayload, RequestDetailsWrapper.class);
72
73     MsoResponseWrapper response = func.apply(sampleRequestDetails, endpoint);
74     JSONObject actualJson = new JSONObject(response.getEntity());
75
76     Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
77     Assert.assertEquals(expectedResponseStr, actualJson.toString());
78     verifyServer(server, endpoint, Method.POST);
79   }
80
81   void executeDelete(String jsonPayload, BiFunction<RequestDetails, String, MsoResponseWrapper> func)
82       throws IOException {
83     whenHttp(server)
84         .match(delete(endpoint))
85         .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
86
87     RequestDetails sampleRequestDetails =
88         new ObjectMapper().readValue(jsonPayload, RequestDetails.class);
89     MsoResponseWrapper response = func.apply(sampleRequestDetails, endpoint);
90
91     Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
92     verifyServer(server, endpoint, Method.DELETE);
93   }
94
95   void executeGet(Function<String, MsoResponseWrapper> func) {
96     whenHttp(server)
97         .match(get(endpoint))
98         .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
99
100     MsoResponseWrapper response = func.apply(endpoint);
101
102     Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
103     verifyServer(server, endpoint, Method.GET);
104   }
105
106   private void verifyServer(StubServer server, String endpoint, Method httpMethod) {
107     verifyHttp(server).once(
108         method(httpMethod),
109         uri(endpoint),
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));
115   }
116
117   private Action jsonContent(String str) {
118     return stringContent(str);
119   }
120
121   @Override
122   public void close() {
123   }
124 }
125