1
1
package org .gitlab4j .api ;
2
2
3
3
import java .io .ByteArrayInputStream ;
4
- import java .io .InputStreamReader ;
5
4
import java .util .List ;
6
5
7
6
import javax .ws .rs .core .GenericType ;
8
7
import javax .ws .rs .core .Response ;
9
8
10
9
/**
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.
13
12
*
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.
15
15
*/
16
16
public abstract class FakeResponse extends Response {
17
17
18
18
private List <?> responseList ;
19
- private ByteArrayInputStream responseInputStream ;
20
19
private Object responseItem ;
21
20
private int perPage = 20 ;
22
21
22
+ private String itemJson ;
23
+ private String listJson ;
24
+
23
25
public <T > void init (Class <T > type , String itemFilename , String listFilename ) throws Exception {
24
26
25
27
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 );
28
30
} else {
29
31
responseItem = null ;
30
32
}
31
33
32
34
if (listFilename != null ) {
33
- String listJson = JsonUtils .readResource (listFilename );
35
+ listJson = JsonUtils .readResource (listFilename );
34
36
responseList = (List <?>) JsonUtils .unmarshalList (type , listJson );
35
- responseInputStream = new ByteArrayInputStream (listJson .getBytes ());
36
37
} else {
37
38
responseList = null ;
38
- responseInputStream = null ;
39
39
}
40
40
}
41
41
42
+ public String getItemJson () {
43
+ return (itemJson );
44
+ }
45
+
46
+ public String getListJson () {
47
+ return (listJson );
48
+ }
49
+
42
50
public void setPerPageHeaderValue (int perPage ) {
43
51
this .perPage = perPage ;
44
52
}
45
53
54
+ // The below methods allow this abstract class to act as a fake Resource
55
+ // instance.
56
+
46
57
@ SuppressWarnings ("unchecked" )
47
58
@ Override
48
59
public <T > T readEntity (GenericType <T > entityType ) {
@@ -57,12 +68,7 @@ public <T> T readEntity(Class<T> classType) {
57
68
58
69
@ Override
59
70
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 );
66
72
}
67
73
68
74
@ Override
@@ -72,7 +78,8 @@ public String getHeaderString(String name) {
72
78
return (responseList != null ? Integer .toString (perPage ) : "0" );
73
79
74
80
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" );
76
83
77
84
case Constants .TOTAL_HEADER :
78
85
return (responseList != null ? Integer .toString (responseList .size ()) : "0" );
0 commit comments