2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Nokia Intellectual Property. All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.testUtils;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.xebialabs.restito.semantics.Action;
26 import com.xebialabs.restito.semantics.Call;
27 import com.xebialabs.restito.semantics.Condition;
28 import com.xebialabs.restito.server.StubServer;
30 import java.util.List;
32 import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp;
33 import static com.xebialabs.restito.semantics.Action.contentType;
34 import static com.xebialabs.restito.semantics.Action.stringContent;
36 public class StubServerUtil {
38 private static final String APPLICATION_JSON = "application/json";
39 private ObjectMapper objectMapper;
42 private StubServer stubServer;
44 public StubServerUtil() {
45 this.objectMapper = new ObjectMapper();
46 this.stubServer = new StubServer();
50 public void runServer() {
54 public void runSecuredServer(){
55 stubServer.secured().run();
58 public void stopServer() {
63 public String constructTargetUrl(String protocol, String relativePath) {
64 return String.format("%s://localhost:%s/%s", protocol, stubServer.getPort(), relativePath);
67 public void prepareGetCall(String path, Action actionToReturn,Object returnObj, Action expectedAction, String contentType) throws JsonProcessingException {
69 .match(Condition.get(path))
70 .then(expectedAction, actionToReturn, contentType(contentType));
74 public void prepareGetCall(String path, Object returnObj, Action expectedAction) throws JsonProcessingException {
75 prepareGetCall(path, jsonContent(returnObj),returnObj, expectedAction, APPLICATION_JSON);
78 public void prepareDeleteCall(String path, Object returnObj, Action expectedAction) throws JsonProcessingException {
80 .match(Condition.delete(path))
81 .then(expectedAction, jsonContent(returnObj), contentType(APPLICATION_JSON));
84 public void preparePostCall(String path, Object returnObj, Action expectedAction) throws JsonProcessingException {
86 .match(Condition.post(path),
87 Condition.withPostBodyContaining(objectMapper.writeValueAsString(returnObj)))
88 .then(expectedAction, jsonContent(returnObj), contentType(APPLICATION_JSON));
91 public void preparePutCall(String path, Object returnObj, Action expectedStatus) throws JsonProcessingException {
93 .match(Condition.put(path),
94 Condition.withPostBodyContaining(objectMapper.writeValueAsString(returnObj)))
95 .then(expectedStatus, jsonContent(returnObj), contentType(APPLICATION_JSON));
98 public List<Call> getServerCalls() {
99 return stubServer.getCalls();
102 private Action jsonContent(Object returnObj) throws JsonProcessingException {
103 return stringContent(objectMapper.writeValueAsString(returnObj));