Commit acb7de3e authored by Greg Messner's avatar Greg Messner
Browse files

Added methods to return the JSON for both the item and list.

parent 529d54a6
package org.gitlab4j.api;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.util.List;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
/**
* This class can be used as a spy for Mockito to test the individual APIs getXxxxx() methods without the
* need to have a GitLab server available.
* This class can be used as a spy for Mockito to test the individual APIs
* getXxxxx() methods without the need to have a GitLab server available.
*
* Supports getXxxxx() methods that return a List of items, single items, and Optional items.
* Supports getXxxxx() methods that return a List of items, single items,
* Optional items, and Pagers of items.
*/
public abstract class FakeResponse extends Response {
private List<?> responseList;
private ByteArrayInputStream responseInputStream;
private Object responseItem;
private int perPage = 20;
private String itemJson;
private String listJson;
public <T> void init(Class<T> type, String itemFilename, String listFilename) throws Exception {
if (itemFilename != null) {
InputStreamReader reader = new InputStreamReader(TestGitLabApiBeans.class.getResourceAsStream(itemFilename));
responseItem = JsonUtils.unmarshal(type, reader);
itemJson = JsonUtils.readResource(itemFilename);
responseItem = JsonUtils.unmarshal(type, itemJson);
} else {
responseItem = null;
}
if (listFilename != null) {
String listJson = JsonUtils.readResource(listFilename);
listJson = JsonUtils.readResource(listFilename);
responseList = (List<?>) JsonUtils.unmarshalList(type, listJson);
responseInputStream = new ByteArrayInputStream(listJson.getBytes());
} else {
responseList = null;
responseInputStream = null;
}
}
public String getItemJson() {
return (itemJson);
}
public String getListJson() {
return (listJson);
}
public void setPerPageHeaderValue(int perPage) {
this.perPage = perPage;
}
// The below methods allow this abstract class to act as a fake Resource
// instance.
@SuppressWarnings("unchecked")
@Override
public <T> T readEntity(GenericType<T> entityType) {
......@@ -57,12 +68,7 @@ public abstract class FakeResponse extends Response {
@Override
public Object getEntity() {
if (responseInputStream == null) {
return (null);
}
responseInputStream.reset();
return (responseInputStream);
return (listJson != null ? new ByteArrayInputStream(listJson.getBytes()) : null);
}
@Override
......@@ -72,7 +78,8 @@ public abstract class FakeResponse extends Response {
return (responseList != null ? Integer.toString(perPage) : "0");
case Constants.TOTAL_PAGES_HEADER:
return (responseList != null ? Integer.toString((int)Math.ceil((double)responseList.size() / 20.0)) : "0");
return (responseList != null ? Integer.toString((int) Math.ceil((double) responseList.size() / 20.0))
: "0");
case Constants.TOTAL_HEADER:
return (responseList != null ? Integer.toString(responseList.size()) : "0");
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment