Update license headers
[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 - 2019 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.Call;
27 import com.xebialabs.restito.semantics.Condition;
28 import com.xebialabs.restito.server.StubServer;
29
30 import java.util.List;
31
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;
35
36 public class StubServerUtil {
37
38     private static final String APPLICATION_JSON = "application/json";
39     private ObjectMapper objectMapper;
40
41
42     private StubServer stubServer;
43
44     public StubServerUtil() {
45         this.objectMapper = new ObjectMapper();
46         this.stubServer = new StubServer();
47     }
48
49
50     public void runServer() {
51         stubServer.run();
52     }
53
54     public void runSecuredServer(){
55         stubServer.secured().run();
56     }
57
58     public void stopServer() {
59         stubServer.stop();
60     }
61
62
63     public String constructTargetUrl(String protocol, String relativePath) {
64         return String.format("%s://localhost:%s/%s", protocol, stubServer.getPort(), relativePath);
65     }
66
67     public void prepareGetCall(String path, Action actionToReturn,Object returnObj, Action expectedAction, String contentType) throws JsonProcessingException {
68         whenHttp(stubServer)
69                 .match(Condition.get(path))
70                 .then(expectedAction, actionToReturn, contentType(contentType));
71     }
72
73
74     public void prepareGetCall(String path, Object returnObj, Action expectedAction) throws JsonProcessingException {
75         prepareGetCall(path, jsonContent(returnObj),returnObj, expectedAction, APPLICATION_JSON);
76     }
77
78     public void prepareDeleteCall(String path, Object returnObj, Action expectedAction) throws JsonProcessingException {
79         whenHttp(stubServer)
80                 .match(Condition.delete(path))
81                 .then(expectedAction, jsonContent(returnObj), contentType(APPLICATION_JSON));
82     }
83
84     public void preparePostCall(String path, Object returnObj, Action expectedAction) throws JsonProcessingException {
85         whenHttp(stubServer)
86                 .match(Condition.post(path),
87                         Condition.withPostBodyContaining(objectMapper.writeValueAsString(returnObj)))
88                 .then(expectedAction, jsonContent(returnObj), contentType(APPLICATION_JSON));
89     }
90
91     public void preparePutCall(String path, Object returnObj, Action expectedStatus) throws JsonProcessingException {
92         whenHttp(stubServer)
93                 .match(Condition.put(path),
94                         Condition.withPostBodyContaining(objectMapper.writeValueAsString(returnObj)))
95                 .then(expectedStatus, jsonContent(returnObj), contentType(APPLICATION_JSON));
96     }
97
98     public List<Call> getServerCalls() {
99         return stubServer.getCalls();
100     }
101
102     private Action jsonContent(Object returnObj) throws JsonProcessingException {
103         return stringContent(objectMapper.writeValueAsString(returnObj));
104     }
105
106 }