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.Condition;
27 import com.xebialabs.restito.server.StubServer;
29 import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp;
30 import static com.xebialabs.restito.semantics.Action.contentType;
31 import static com.xebialabs.restito.semantics.Action.stringContent;
33 public class StubServerUtil {
35 private static final String APPLICATION_JSON = "application/json";
36 private ObjectMapper objectMapper;
39 private StubServer stubServer;
41 public StubServerUtil() {
42 this.objectMapper = new ObjectMapper();
43 this.stubServer = new StubServer();
47 public void runServer() {
51 public void stopServer() {
56 public String constructTargetUrl(String protocol, String relativePath) {
57 return String.format("%s://localhost:%s/%s", protocol, stubServer.getPort(), relativePath);
60 public void prepareGetCall(String path, Object returnObj, Action expectedAction) throws JsonProcessingException {
62 .match(Condition.get(path))
63 .then(expectedAction, jsonContent(returnObj), contentType(APPLICATION_JSON));
66 public void prepareDeleteCall(String path, Object returnObj, Action expectedAction) throws JsonProcessingException {
68 .match(Condition.delete(path))
69 .then(expectedAction, jsonContent(returnObj), contentType(APPLICATION_JSON));
72 public void preparePostCall(String path, Object returnObj, Action expectedAction) throws JsonProcessingException {
74 .match(Condition.post(path),
75 Condition.withPostBodyContaining(objectMapper.writeValueAsString(returnObj)))
76 .then(expectedAction, jsonContent(returnObj), contentType(APPLICATION_JSON));
79 public void preparePutCall(String path, Object returnObj, Action expectedStatus) throws JsonProcessingException {
81 .match(Condition.put(path),
82 Condition.withPostBodyContaining(objectMapper.writeValueAsString(returnObj)))
83 .then(expectedStatus, jsonContent(returnObj), contentType(APPLICATION_JSON));
86 private Action jsonContent(Object returnObj) throws JsonProcessingException {
87 return stringContent(objectMapper.writeValueAsString(returnObj));