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

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

No related merge requests found
Showing with 24 additions and 17 deletions
+24 -17
package org.gitlab4j.api; package org.gitlab4j.api;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.util.List; import java.util.List;
import javax.ws.rs.core.GenericType; import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response; 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 * This class can be used as a spy for Mockito to test the individual APIs
* need to have a GitLab server available. * 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 { public abstract class FakeResponse extends Response {
private List<?> responseList; private List<?> responseList;
private ByteArrayInputStream responseInputStream;
private Object responseItem; private Object responseItem;
private int perPage = 20; private int perPage = 20;
private String itemJson;
private String listJson;
public <T> void init(Class<T> type, String itemFilename, String listFilename) throws Exception { public <T> void init(Class<T> type, String itemFilename, String listFilename) throws Exception {
if (itemFilename != null) { if (itemFilename != null) {
InputStreamReader reader = new InputStreamReader(TestGitLabApiBeans.class.getResourceAsStream(itemFilename)); itemJson = JsonUtils.readResource(itemFilename);
responseItem = JsonUtils.unmarshal(type, reader); responseItem = JsonUtils.unmarshal(type, itemJson);
} else { } else {
responseItem = null; responseItem = null;
} }
if (listFilename != null) { if (listFilename != null) {
String listJson = JsonUtils.readResource(listFilename); listJson = JsonUtils.readResource(listFilename);
responseList = (List<?>) JsonUtils.unmarshalList(type, listJson); responseList = (List<?>) JsonUtils.unmarshalList(type, listJson);
responseInputStream = new ByteArrayInputStream(listJson.getBytes());
} else { } else {
responseList = null; responseList = null;
responseInputStream = null;
} }
} }
public String getItemJson() {
return (itemJson);
}
public String getListJson() {
return (listJson);
}
public void setPerPageHeaderValue(int perPage) { public void setPerPageHeaderValue(int perPage) {
this.perPage = perPage; this.perPage = perPage;
} }
// The below methods allow this abstract class to act as a fake Resource
// instance.
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public <T> T readEntity(GenericType<T> entityType) { public <T> T readEntity(GenericType<T> entityType) {
...@@ -57,12 +68,7 @@ public abstract class FakeResponse extends Response { ...@@ -57,12 +68,7 @@ public abstract class FakeResponse extends Response {
@Override @Override
public Object getEntity() { public Object getEntity() {
if (responseInputStream == null) { return (listJson != null ? new ByteArrayInputStream(listJson.getBytes()) : null);
return (null);
}
responseInputStream.reset();
return (responseInputStream);
} }
@Override @Override
...@@ -72,7 +78,8 @@ public abstract class FakeResponse extends Response { ...@@ -72,7 +78,8 @@ public abstract class FakeResponse extends Response {
return (responseList != null ? Integer.toString(perPage) : "0"); return (responseList != null ? Integer.toString(perPage) : "0");
case Constants.TOTAL_PAGES_HEADER: 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: case Constants.TOTAL_HEADER:
return (responseList != null ? Integer.toString(responseList.size()) : "0"); 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