use of com.seleniumtests.ut.exceptions.TestConfigurationException in project seleniumRobot by bhecquet.
the class ConnectorsTest method createServerMock.
protected HttpRequest createServerMock(String serverUrl, String requestType, String apiPath, int statusCode, final List<Object> replyData, String responseType) throws UnirestException {
if (replyData.isEmpty()) {
throw new TestConfigurationException("No replyData specified");
}
@SuppressWarnings("unchecked") HttpResponse<String> response = mock(HttpResponse.class);
HttpResponse<JsonNode> jsonResponse = mock(HttpResponse.class);
HttpResponse<File> streamResponse = mock(HttpResponse.class);
HttpResponse<byte[]> bytestreamResponse = mock(HttpResponse.class);
HttpRequest request = mock(HttpRequest.class);
JsonNode json = mock(JsonNode.class);
HttpRequestWithBody postRequest = spy(HttpRequestWithBody.class);
// for asPaged method
PagedList<JsonNode> pageList = new PagedList<>();
when(request.getUrl()).thenReturn(serverUrl);
if (replyData.get(0) instanceof String) {
when(response.getStatus()).thenReturn(statusCode);
// when(response.getBody()).thenReturn(replyData.toArray(new String[] {}));
when(response.getBody()).then(new Answer<String>() {
private int count = -1;
public String answer(InvocationOnMock invocation) {
count++;
if (count >= replyData.size() - 1) {
return (String) replyData.get(replyData.size() - 1);
} else {
return (String) replyData.get(count);
}
}
});
when(response.getStatusText()).thenReturn("TEXT");
when(jsonResponse.getStatus()).thenReturn(statusCode);
when(jsonResponse.getBody()).thenReturn(json);
when(jsonResponse.getStatusText()).thenReturn("TEXT");
try {
// check data is compatible with JSON
for (Object d : replyData) {
if (((String) d).isEmpty()) {
d = "{}";
}
new JSONObject((String) d);
}
// JSONObject jsonReply = new JSONObject((String)replyData);
// when(json.getObject()).thenReturn(jsonReply);
when(json.getObject()).then(new Answer<JSONObject>() {
private int count = -1;
public JSONObject answer(InvocationOnMock invocation) {
count++;
String reply;
if (count >= replyData.size() - 1) {
reply = (String) replyData.get(replyData.size() - 1);
} else {
reply = (String) replyData.get(count);
}
if (reply.isEmpty()) {
reply = "{}";
}
return new JSONObject(reply);
}
});
pageList = new PagedList<>();
pageList.add(jsonResponse);
} catch (JSONException e) {
}
} else if (replyData.get(0) instanceof File) {
when(streamResponse.getStatus()).thenReturn(statusCode);
when(streamResponse.getStatusText()).thenReturn("TEXT");
when(streamResponse.getBody()).then(new Answer<File>() {
private int count = -1;
public File answer(InvocationOnMock invocation) {
count++;
if (count >= replyData.size() - 1) {
return (File) replyData.get(replyData.size() - 1);
} else {
return (File) replyData.get(count);
}
}
});
// when(bytestreamResponse.getBody()).thenReturn(FileUtils.readFileToByteArray((File)replyData));
when(bytestreamResponse.getBody()).then(new Answer<byte[]>() {
private int count = -1;
public byte[] answer(InvocationOnMock invocation) throws IOException {
count++;
if (count >= replyData.size() - 1) {
return (byte[]) FileUtils.readFileToByteArray((File) replyData.get(replyData.size() - 1));
} else {
return (byte[]) FileUtils.readFileToByteArray((File) replyData.get(count));
}
}
});
when(bytestreamResponse.getStatus()).thenReturn(statusCode);
when(bytestreamResponse.getStatusText()).thenReturn("BYTES");
}
switch(requestType) {
case "GET":
GetRequest getRequest = mock(GetRequest.class);
when(Unirest.get(serverUrl + apiPath)).thenReturn(getRequest);
when(getRequest.socketTimeout(anyInt())).thenReturn(getRequest);
when(unirestInstance.get(serverUrl + apiPath)).thenReturn(getRequest);
when(getRequest.header(anyString(), anyString())).thenReturn(getRequest);
when(getRequest.asString()).thenReturn(response);
when(getRequest.asJson()).thenReturn(jsonResponse);
when(getRequest.asFile(anyString())).thenReturn(streamResponse);
when(getRequest.asBytes()).thenReturn(bytestreamResponse);
when(getRequest.queryString(anyString(), anyString())).thenReturn(getRequest);
when(getRequest.queryString(anyString(), anyInt())).thenReturn(getRequest);
when(getRequest.queryString(anyString(), anyBoolean())).thenReturn(getRequest);
when(getRequest.queryString(anyString(), any(SessionId.class))).thenReturn(getRequest);
when(getRequest.getUrl()).thenReturn(serverUrl);
when(getRequest.basicAuth(anyString(), anyString())).thenReturn(getRequest);
when(getRequest.headerReplace(anyString(), anyString())).thenReturn(getRequest);
when(getRequest.asPaged(any(), (Function<HttpResponse<JsonNode>, String>) any(Function.class))).thenReturn(pageList);
return getRequest;
case "POST":
when(Unirest.post(serverUrl + apiPath)).thenReturn(postRequest);
when(unirestInstance.post(serverUrl + apiPath)).thenReturn(postRequest);
return preparePostRequest(serverUrl, responseType, postRequest, response, jsonResponse);
case "PATCH":
when(Unirest.patch(serverUrl + apiPath)).thenReturn(postRequest);
when(unirestInstance.patch(serverUrl + apiPath)).thenReturn(postRequest);
return preparePostRequest(serverUrl, responseType, postRequest, response, jsonResponse);
case "PUT":
when(Unirest.put(serverUrl + apiPath)).thenReturn(postRequest);
when(unirestInstance.put(serverUrl + apiPath)).thenReturn(postRequest);
return preparePostRequest(serverUrl, responseType, postRequest, response, jsonResponse);
}
return null;
}
Aggregations