11package org .gitlab4j .api ;
22
33import java .io .ByteArrayInputStream ;
4- import java .io .InputStreamReader ;
54import java .util .List ;
65
76import javax .ws .rs .core .GenericType ;
87import 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 */
1616public 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