Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 1 | /*-
|
| 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 |
|
| 23 | package org.onap.osam.mso.rest;
|
| 24 |
|
| 25 | import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp;
|
| 26 | import static com.xebialabs.restito.builder.verify.VerifyHttp.verifyHttp;
|
| 27 | import static com.xebialabs.restito.semantics.Action.contentType;
|
| 28 | import static com.xebialabs.restito.semantics.Action.status;
|
| 29 | import static com.xebialabs.restito.semantics.Action.stringContent;
|
| 30 | import static com.xebialabs.restito.semantics.Condition.delete;
|
| 31 | import static com.xebialabs.restito.semantics.Condition.get;
|
| 32 | import static com.xebialabs.restito.semantics.Condition.method;
|
| 33 | import static com.xebialabs.restito.semantics.Condition.post;
|
| 34 | import static com.xebialabs.restito.semantics.Condition.uri;
|
| 35 | import static com.xebialabs.restito.semantics.Condition.withHeader;
|
| 36 |
|
| 37 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 38 | import com.xebialabs.restito.semantics.Action;
|
| 39 | import com.xebialabs.restito.server.StubServer;
|
| 40 | import java.io.IOException;
|
| 41 | import java.util.function.BiFunction;
|
| 42 | import java.util.function.Function;
|
| 43 | import javax.ws.rs.core.HttpHeaders;
|
| 44 | import javax.ws.rs.core.MediaType;
|
| 45 |
|
| 46 | import org.glassfish.grizzly.http.Method;
|
| 47 | import org.glassfish.grizzly.http.util.HttpStatus;
|
| 48 | import org.json.JSONObject;
|
| 49 | import org.junit.Assert;
|
| 50 | import org.onap.portalsdk.core.util.SystemProperties;
|
| 51 | import org.onap.osam.mso.MsoResponseWrapper;
|
| 52 |
|
| 53 | class 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 |
|