Replace SO client
[vid.git] / vid-app-common / src / test / java / org / onap / vid / testUtils / StubServerUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
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 package org.onap.vid.testUtils;
22
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;
28
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;
32
33 public class StubServerUtil {
34
35     private static final String APPLICATION_JSON = "application/json";
36     private ObjectMapper objectMapper;
37
38
39     private StubServer stubServer;
40
41     public StubServerUtil() {
42         this.objectMapper = new ObjectMapper();
43         this.stubServer = new StubServer();
44     }
45
46
47     public void runServer() {
48         stubServer.run();
49     }
50
51     public void stopServer() {
52         stubServer.stop();
53     }
54
55
56     public String constructTargetUrl(String protocol, String relativePath) {
57         return String.format("%s://localhost:%s/%s", protocol, stubServer.getPort(), relativePath);
58     }
59
60     public void prepareGetCall(String path, Object returnObj, Action expectedAction) throws JsonProcessingException {
61         whenHttp(stubServer)
62                 .match(Condition.get(path))
63                 .then(expectedAction, jsonContent(returnObj), contentType(APPLICATION_JSON));
64     }
65
66     public void prepareDeleteCall(String path, Object returnObj, Action expectedAction) throws JsonProcessingException {
67         whenHttp(stubServer)
68                 .match(Condition.delete(path))
69                 .then(expectedAction, jsonContent(returnObj), contentType(APPLICATION_JSON));
70     }
71
72     public void preparePostCall(String path, Object returnObj, Action expectedAction) throws JsonProcessingException {
73         whenHttp(stubServer)
74                 .match(Condition.post(path),
75                         Condition.withPostBodyContaining(objectMapper.writeValueAsString(returnObj)))
76                 .then(expectedAction, jsonContent(returnObj), contentType(APPLICATION_JSON));
77     }
78
79     public void preparePutCall(String path, Object returnObj, Action expectedStatus) throws JsonProcessingException {
80         whenHttp(stubServer)
81                 .match(Condition.put(path),
82                         Condition.withPostBodyContaining(objectMapper.writeValueAsString(returnObj)))
83                 .then(expectedStatus, jsonContent(returnObj), contentType(APPLICATION_JSON));
84     }
85
86     private Action jsonContent(Object returnObj) throws JsonProcessingException {
87         return stringContent(objectMapper.writeValueAsString(returnObj));
88     }
89
90 }