Skip to content

Commit acb7de3

Browse files
committed
Added methods to return the JSON for both the item and list.
1 parent 529d54a commit acb7de3

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

src/test/java/org/gitlab4j/api/FakeResponse.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,59 @@
11
package org.gitlab4j.api;
22

33
import java.io.ByteArrayInputStream;
4-
import java.io.InputStreamReader;
54
import java.util.List;
65

76
import javax.ws.rs.core.GenericType;
87
import javax.ws.rs.core.Response;
98

109
/**
11-
* This class can be used as a spy for Mockito to test the individual APIs getXxxxx() methods without the
12-
* need to have a GitLab server available.
10+
* This class can be used as a spy for Mockito to test the individual APIs
11+
* getXxxxx() methods without the need to have a GitLab server available.
1312
*
14-
* Supports getXxxxx() methods that return a List of items, single items, and Optional items.
13+
* Supports getXxxxx() methods that return a List of items, single items,
14+
* Optional items, and Pagers of items.
1515
*/
1616
public abstract class FakeResponse extends Response {
1717

1818
private List<?> responseList;
19-
private ByteArrayInputStream responseInputStream;
2019
private Object responseItem;
2120
private int perPage = 20;
2221

22+
private String itemJson;
23+
private String listJson;
24+
2325
public <T> void init(Class<T> type, String itemFilename, String listFilename) throws Exception {
2426

2527
if (itemFilename != null) {
26-
InputStreamReader reader = new InputStreamReader(TestGitLabApiBeans.class.getResourceAsStream(itemFilename));
27-
responseItem = JsonUtils.unmarshal(type, reader);
28+
itemJson = JsonUtils.readResource(itemFilename);
29+
responseItem = JsonUtils.unmarshal(type, itemJson);
2830
} else {
2931
responseItem = null;
3032
}
3133

3234
if (listFilename != null) {
33-
String listJson = JsonUtils.readResource(listFilename);
35+
listJson = JsonUtils.readResource(listFilename);
3436
responseList = (List<?>) JsonUtils.unmarshalList(type, listJson);
35-
responseInputStream = new ByteArrayInputStream(listJson.getBytes());
3637
} else {
3738
responseList = null;
38-
responseInputStream = null;
3939
}
4040
}
4141

42+
public String getItemJson() {
43+
return (itemJson);
44+
}
45+
46+
public String getListJson() {
47+
return (listJson);
48+
}
49+
4250
public void setPerPageHeaderValue(int perPage) {
4351
this.perPage = perPage;
4452
}
4553

54+
// The below methods allow this abstract class to act as a fake Resource
55+
// instance.
56+
4657
@SuppressWarnings("unchecked")
4758
@Override
4859
public <T> T readEntity(GenericType<T> entityType) {
@@ -57,12 +68,7 @@ public <T> T readEntity(Class<T> classType) {
5768

5869
@Override
5970
public Object getEntity() {
60-
if (responseInputStream == null) {
61-
return (null);
62-
}
63-
64-
responseInputStream.reset();
65-
return (responseInputStream);
71+
return (listJson != null ? new ByteArrayInputStream(listJson.getBytes()) : null);
6672
}
6773

6874
@Override
@@ -72,7 +78,8 @@ public String getHeaderString(String name) {
7278
return (responseList != null ? Integer.toString(perPage) : "0");
7379

7480
case Constants.TOTAL_PAGES_HEADER:
75-
return (responseList != null ? Integer.toString((int)Math.ceil((double)responseList.size() / 20.0)) : "0");
81+
return (responseList != null ? Integer.toString((int) Math.ceil((double) responseList.size() / 20.0))
82+
: "0");
7683

7784
case Constants.TOTAL_HEADER:
7885
return (responseList != null ? Integer.toString(responseList.size()) : "0");

0 commit comments

Comments
 (0)