17
17
import com .google .api .client .googleapis .json .GoogleJsonErrorTest .ErrorTransport ;
18
18
import com .google .api .client .http .HttpRequest ;
19
19
import com .google .api .client .http .HttpResponse ;
20
+ import com .google .api .client .http .HttpStatusCodes ;
20
21
import com .google .api .client .http .HttpTransport ;
21
22
import com .google .api .client .json .Json ;
22
23
import com .google .api .client .testing .http .HttpTesting ;
23
24
import com .google .api .client .testing .http .MockHttpTransport ;
25
+ import com .google .api .client .testing .http .MockLowLevelHttpResponse ;
26
+ import java .io .InputStream ;
24
27
import junit .framework .TestCase ;
25
28
26
29
/**
@@ -37,7 +40,8 @@ public void testFrom_noDetails() throws Exception {
37
40
request .setThrowExceptionOnExecuteError (false );
38
41
HttpResponse response = request .execute ();
39
42
GoogleJsonResponseException ge =
40
- GoogleJsonResponseException .from (GoogleJsonErrorTest .FACTORY , response );
43
+ GoogleJsonResponseException .from (
44
+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
41
45
assertNull (ge .getDetails ());
42
46
assertTrue (ge .getMessage ().startsWith ("200" ));
43
47
}
@@ -49,8 +53,12 @@ public void testFrom_withDetails() throws Exception {
49
53
request .setThrowExceptionOnExecuteError (false );
50
54
HttpResponse response = request .execute ();
51
55
GoogleJsonResponseException ge =
52
- GoogleJsonResponseException .from (GoogleJsonErrorTest .FACTORY , response );
53
- assertEquals (GoogleJsonErrorTest .ERROR , GoogleJsonErrorTest .FACTORY .toString (ge .getDetails ()));
56
+ GoogleJsonResponseException .from (
57
+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
58
+ assertEquals (
59
+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .ERROR ,
60
+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY .toString (
61
+ ge .getDetails ()));
54
62
assertTrue (ge .getMessage ().startsWith ("403" ));
55
63
}
56
64
@@ -61,7 +69,8 @@ public void testFrom_detailsMissingContent() throws Exception {
61
69
request .setThrowExceptionOnExecuteError (false );
62
70
HttpResponse response = request .execute ();
63
71
GoogleJsonResponseException ge =
64
- GoogleJsonResponseException .from (GoogleJsonErrorTest .FACTORY , response );
72
+ GoogleJsonResponseException .from (
73
+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
65
74
assertNull (ge .getDetails ());
66
75
assertTrue (ge .getMessage ().startsWith ("403" ));
67
76
}
@@ -73,7 +82,8 @@ public void testFrom_detailsArbitraryJsonContent() throws Exception {
73
82
request .setThrowExceptionOnExecuteError (false );
74
83
HttpResponse response = request .execute ();
75
84
GoogleJsonResponseException ge =
76
- GoogleJsonResponseException .from (GoogleJsonErrorTest .FACTORY , response );
85
+ GoogleJsonResponseException .from (
86
+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
77
87
assertNull (ge .getDetails ());
78
88
assertTrue (ge .getMessage ().startsWith ("403" ));
79
89
}
@@ -85,7 +95,8 @@ public void testFrom_detailsArbitraryXmlContent() throws Exception {
85
95
request .setThrowExceptionOnExecuteError (false );
86
96
HttpResponse response = request .execute ();
87
97
GoogleJsonResponseException ge =
88
- GoogleJsonResponseException .from (GoogleJsonErrorTest .FACTORY , response );
98
+ GoogleJsonResponseException .from (
99
+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
89
100
assertNull (ge .getDetails ());
90
101
assertTrue (ge .getMessage ().startsWith ("403" ));
91
102
assertTrue (ge .getMessage ().contains ("<foo>" ));
@@ -98,7 +109,8 @@ public void testFrom_errorNoContentButWithJsonContentType() throws Exception {
98
109
request .setThrowExceptionOnExecuteError (false );
99
110
HttpResponse response = request .execute ();
100
111
GoogleJsonResponseException ge =
101
- GoogleJsonResponseException .from (GoogleJsonErrorTest .FACTORY , response );
112
+ GoogleJsonResponseException .from (
113
+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
102
114
assertNull (ge .getDetails ());
103
115
assertTrue (ge .getMessage ().startsWith ("403" ));
104
116
}
@@ -110,7 +122,8 @@ public void testFrom_errorEmptyContentButWithJsonContentType() throws Exception
110
122
request .setThrowExceptionOnExecuteError (false );
111
123
HttpResponse response = request .execute ();
112
124
GoogleJsonResponseException ge =
113
- GoogleJsonResponseException .from (GoogleJsonErrorTest .FACTORY , response );
125
+ GoogleJsonResponseException .from (
126
+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
114
127
assertNull (ge .getDetails ());
115
128
assertTrue (ge .getMessage ().startsWith ("403" ));
116
129
}
@@ -125,7 +138,8 @@ public void testFrom_detailsErrorObject() throws Exception {
125
138
request .setThrowExceptionOnExecuteError (false );
126
139
HttpResponse response = request .execute ();
127
140
GoogleJsonResponseException ge =
128
- GoogleJsonResponseException .from (GoogleJsonErrorTest .FACTORY , response );
141
+ GoogleJsonResponseException .from (
142
+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
129
143
assertNotNull (ge .getDetails ());
130
144
assertEquals ("invalid_token" , ge .getDetails ().getMessage ());
131
145
assertTrue (ge .getMessage ().contains ("403" ));
@@ -141,7 +155,8 @@ public void testFrom_detailsErrorString() throws Exception {
141
155
request .setThrowExceptionOnExecuteError (false );
142
156
HttpResponse response = request .execute ();
143
157
GoogleJsonResponseException ge =
144
- GoogleJsonResponseException .from (GoogleJsonErrorTest .FACTORY , response );
158
+ GoogleJsonResponseException .from (
159
+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
145
160
assertNull (ge .getDetails ());
146
161
assertTrue (ge .getMessage ().contains ("403" ));
147
162
assertTrue (ge .getMessage ().contains ("invalid_token" ));
@@ -155,8 +170,43 @@ public void testFrom_detailsNoErrorField() throws Exception {
155
170
request .setThrowExceptionOnExecuteError (false );
156
171
HttpResponse response = request .execute ();
157
172
GoogleJsonResponseException ge =
158
- GoogleJsonResponseException .from (GoogleJsonErrorTest .FACTORY , response );
173
+ GoogleJsonResponseException .from (
174
+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
159
175
assertNull (ge .getDetails ());
160
176
assertTrue (ge .getMessage ().startsWith ("403" ));
161
177
}
178
+
179
+ public void testFrom_detailsWithInvalidParameter () throws Exception {
180
+ String DETAILS_ERROR =
181
+ "{"
182
+ + "\" code\" :400,"
183
+ + "\" details\" :[{"
184
+ + "\" @type\" :\" type.googleapis.com/google.dataflow.v1beta3.InvalidTemplateParameters\" ,"
185
+ + "\" parameterViolations\" :[{"
186
+ + "\" description\" :\" Parameter didn't match regex '^[0-9a-zA-Z_]+$'\" ,"
187
+ + "\" parameter\" :\" safeBrowsingApiKey\" "
188
+ + "}]},{"
189
+ + "\" @type\" :\" type.googleapis.com/google.rpc.DebugInfo\" ,"
190
+ + "\" detail\" :\" test detail\" }],"
191
+ + "\" message\" :\" The template parameters are invalid.\" ,"
192
+ + "\" status\" :\" INVALID_ARGUMENT\" "
193
+ + "}" ;
194
+ InputStream errorContent =
195
+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .class .getResourceAsStream (
196
+ "error.json" );
197
+ HttpTransport transport =
198
+ new ErrorTransport (
199
+ new MockLowLevelHttpResponse ()
200
+ .setContent (errorContent )
201
+ .setContentType (Json .MEDIA_TYPE )
202
+ .setStatusCode (HttpStatusCodes .STATUS_CODE_FORBIDDEN ));
203
+ HttpRequest request =
204
+ transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
205
+ request .setThrowExceptionOnExecuteError (false );
206
+ HttpResponse response = request .execute ();
207
+ GoogleJsonResponseException ge =
208
+ GoogleJsonResponseException .from (
209
+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
210
+ assertNotNull (ge .getDetails ().getDetails ());
211
+ }
162
212
}
0 commit comments