blob: 1066be459a04e52760a57fdecdfaabd26056b9ca [file] [log] [blame]
Aharoni, Pavel (pa0916)ca3cb012018-10-22 15:29:57 +03001/*-
2 * ============LICENSE_START=======================================================
3 * OSAM
4 * ================================================================================
5 * Copyright (C) 2018 AT&T
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20
21
22
23package org.onap.osam.mso.rest;
24
25import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp;
26import static com.xebialabs.restito.builder.verify.VerifyHttp.verifyHttp;
27import static com.xebialabs.restito.semantics.Action.contentType;
28import static com.xebialabs.restito.semantics.Action.status;
29import static com.xebialabs.restito.semantics.Action.stringContent;
30import static com.xebialabs.restito.semantics.Condition.delete;
31import static com.xebialabs.restito.semantics.Condition.get;
32import static com.xebialabs.restito.semantics.Condition.method;
33import static com.xebialabs.restito.semantics.Condition.post;
34import static com.xebialabs.restito.semantics.Condition.uri;
35import static com.xebialabs.restito.semantics.Condition.withHeader;
36
37import com.fasterxml.jackson.databind.ObjectMapper;
38import com.xebialabs.restito.semantics.Action;
39import com.xebialabs.restito.server.StubServer;
40import java.io.IOException;
41import java.util.function.BiFunction;
42import java.util.function.Function;
43import javax.ws.rs.core.HttpHeaders;
44import javax.ws.rs.core.MediaType;
45
46import org.glassfish.grizzly.http.Method;
47import org.glassfish.grizzly.http.util.HttpStatus;
48import org.json.JSONObject;
49import org.junit.Assert;
50import org.onap.portalsdk.core.util.SystemProperties;
51import org.onap.osam.mso.MsoResponseWrapper;
52
53class MsoRestClientTestUtil implements AutoCloseable {
54 private final StubServer server;
55 private final String endpoint;
56 private final String responsePayload;
57 private final HttpStatus expectedStatus;
58 private final String expectedResponseStr;
59
60 MsoRestClientTestUtil(StubServer server, String endpoint, HttpStatus expectedStatus,
61 String responsePayload,
62 String expectedResponseStr) {
63 this.server = server;
64 this.endpoint = endpoint;
65 this.responsePayload = responsePayload;
66 this.expectedStatus = expectedStatus;
67 this.expectedResponseStr = expectedResponseStr;
68 }
69
70 void executePost(String jsonPayload, BiFunction<RequestDetails, String, MsoResponseWrapper> func) throws IOException {
71 whenHttp(server)
72 .match(post(endpoint))
73 .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
74
75 RequestDetails sampleRequestDetails =
76 new ObjectMapper().readValue(jsonPayload, RequestDetails.class);
77
78 MsoResponseWrapper response = func.apply(sampleRequestDetails, endpoint);
79 JSONObject actualJson = new JSONObject(response.getEntity());
80
81 Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
82 Assert.assertEquals(expectedResponseStr, actualJson.toString());
83 verifyServer(server, endpoint, Method.POST);
84
85 }
86 void executePostCall(String jsonPayload, BiFunction<RequestDetailsWrapper, String, MsoResponseWrapper> func) throws IOException {
87 whenHttp(server)
88 .match(post(endpoint))
89 .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
90
91 RequestDetailsWrapper sampleRequestDetails =
92 new ObjectMapper().readValue(jsonPayload, RequestDetailsWrapper.class);
93
94 MsoResponseWrapper response = func.apply(sampleRequestDetails, endpoint);
95 JSONObject actualJson = new JSONObject(response.getEntity());
96
97 Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
98 Assert.assertEquals(expectedResponseStr, actualJson.toString());
99 verifyServer(server, endpoint, Method.POST);
100 }
101
102 void executeDelete(String jsonPayload, BiFunction<RequestDetails, String, MsoResponseWrapper> func)
103 throws IOException {
104 whenHttp(server)
105 .match(delete(endpoint))
106 .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
107
108 RequestDetails sampleRequestDetails =
109 new ObjectMapper().readValue(jsonPayload, RequestDetails.class);
110 MsoResponseWrapper response = func.apply(sampleRequestDetails, endpoint);
111
112 Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
113 verifyServer(server, endpoint, Method.DELETE);
114 }
115
116 void executeGet(Function<String, MsoResponseWrapper> func) {
117 whenHttp(server)
118 .match(get(endpoint))
119 .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
120
121 MsoResponseWrapper response = func.apply(endpoint);
122
123 Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
124 verifyServer(server, endpoint, Method.GET);
125 }
126
127 private void verifyServer(StubServer server, String endpoint, Method httpMethod) {
128 verifyHttp(server).once(
129 method(httpMethod),
130 uri(endpoint),
131 withHeader(HttpHeaders.AUTHORIZATION),
132 withHeader(HttpHeaders.ACCEPT),
133 withHeader(HttpHeaders.CONTENT_TYPE),
134 withHeader(MsoRestClientNew.X_FROM_APP_ID),
135 withHeader(SystemProperties.ECOMP_REQUEST_ID));
136 }
137
138 private Action jsonContent(String str) {
139 return stringContent(str);
140 }
141
142 @Override
143 public void close() {
144 }
145}
146