1717import com .google .api .client .googleapis .json .GoogleJsonErrorTest .ErrorTransport ;
1818import com .google .api .client .http .HttpRequest ;
1919import com .google .api .client .http .HttpResponse ;
20+ import com .google .api .client .http .HttpStatusCodes ;
2021import com .google .api .client .http .HttpTransport ;
2122import com .google .api .client .json .Json ;
2223import com .google .api .client .testing .http .HttpTesting ;
2324import com .google .api .client .testing .http .MockHttpTransport ;
25+ import com .google .api .client .testing .http .MockLowLevelHttpResponse ;
26+ import java .io .InputStream ;
2427import junit .framework .TestCase ;
2528
2629/**
@@ -37,7 +40,8 @@ public void testFrom_noDetails() throws Exception {
3740 request .setThrowExceptionOnExecuteError (false );
3841 HttpResponse response = request .execute ();
3942 GoogleJsonResponseException ge =
40- GoogleJsonResponseException .from (GoogleJsonErrorTest .FACTORY , response );
43+ GoogleJsonResponseException .from (
44+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
4145 assertNull (ge .getDetails ());
4246 assertTrue (ge .getMessage ().startsWith ("200" ));
4347 }
@@ -49,8 +53,12 @@ public void testFrom_withDetails() throws Exception {
4953 request .setThrowExceptionOnExecuteError (false );
5054 HttpResponse response = request .execute ();
5155 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 ()));
5462 assertTrue (ge .getMessage ().startsWith ("403" ));
5563 }
5664
@@ -61,7 +69,8 @@ public void testFrom_detailsMissingContent() throws Exception {
6169 request .setThrowExceptionOnExecuteError (false );
6270 HttpResponse response = request .execute ();
6371 GoogleJsonResponseException ge =
64- GoogleJsonResponseException .from (GoogleJsonErrorTest .FACTORY , response );
72+ GoogleJsonResponseException .from (
73+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
6574 assertNull (ge .getDetails ());
6675 assertTrue (ge .getMessage ().startsWith ("403" ));
6776 }
@@ -73,7 +82,8 @@ public void testFrom_detailsArbitraryJsonContent() throws Exception {
7382 request .setThrowExceptionOnExecuteError (false );
7483 HttpResponse response = request .execute ();
7584 GoogleJsonResponseException ge =
76- GoogleJsonResponseException .from (GoogleJsonErrorTest .FACTORY , response );
85+ GoogleJsonResponseException .from (
86+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
7787 assertNull (ge .getDetails ());
7888 assertTrue (ge .getMessage ().startsWith ("403" ));
7989 }
@@ -85,7 +95,8 @@ public void testFrom_detailsArbitraryXmlContent() throws Exception {
8595 request .setThrowExceptionOnExecuteError (false );
8696 HttpResponse response = request .execute ();
8797 GoogleJsonResponseException ge =
88- GoogleJsonResponseException .from (GoogleJsonErrorTest .FACTORY , response );
98+ GoogleJsonResponseException .from (
99+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
89100 assertNull (ge .getDetails ());
90101 assertTrue (ge .getMessage ().startsWith ("403" ));
91102 assertTrue (ge .getMessage ().contains ("<foo>" ));
@@ -98,7 +109,8 @@ public void testFrom_errorNoContentButWithJsonContentType() throws Exception {
98109 request .setThrowExceptionOnExecuteError (false );
99110 HttpResponse response = request .execute ();
100111 GoogleJsonResponseException ge =
101- GoogleJsonResponseException .from (GoogleJsonErrorTest .FACTORY , response );
112+ GoogleJsonResponseException .from (
113+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
102114 assertNull (ge .getDetails ());
103115 assertTrue (ge .getMessage ().startsWith ("403" ));
104116 }
@@ -110,7 +122,8 @@ public void testFrom_errorEmptyContentButWithJsonContentType() throws Exception
110122 request .setThrowExceptionOnExecuteError (false );
111123 HttpResponse response = request .execute ();
112124 GoogleJsonResponseException ge =
113- GoogleJsonResponseException .from (GoogleJsonErrorTest .FACTORY , response );
125+ GoogleJsonResponseException .from (
126+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
114127 assertNull (ge .getDetails ());
115128 assertTrue (ge .getMessage ().startsWith ("403" ));
116129 }
@@ -125,7 +138,8 @@ public void testFrom_detailsErrorObject() throws Exception {
125138 request .setThrowExceptionOnExecuteError (false );
126139 HttpResponse response = request .execute ();
127140 GoogleJsonResponseException ge =
128- GoogleJsonResponseException .from (GoogleJsonErrorTest .FACTORY , response );
141+ GoogleJsonResponseException .from (
142+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
129143 assertNotNull (ge .getDetails ());
130144 assertEquals ("invalid_token" , ge .getDetails ().getMessage ());
131145 assertTrue (ge .getMessage ().contains ("403" ));
@@ -141,7 +155,8 @@ public void testFrom_detailsErrorString() throws Exception {
141155 request .setThrowExceptionOnExecuteError (false );
142156 HttpResponse response = request .execute ();
143157 GoogleJsonResponseException ge =
144- GoogleJsonResponseException .from (GoogleJsonErrorTest .FACTORY , response );
158+ GoogleJsonResponseException .from (
159+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
145160 assertNull (ge .getDetails ());
146161 assertTrue (ge .getMessage ().contains ("403" ));
147162 assertTrue (ge .getMessage ().contains ("invalid_token" ));
@@ -155,8 +170,43 @@ public void testFrom_detailsNoErrorField() throws Exception {
155170 request .setThrowExceptionOnExecuteError (false );
156171 HttpResponse response = request .execute ();
157172 GoogleJsonResponseException ge =
158- GoogleJsonResponseException .from (GoogleJsonErrorTest .FACTORY , response );
173+ GoogleJsonResponseException .from (
174+ com .google .api .client .googleapis .json .GoogleJsonErrorTest .FACTORY , response );
159175 assertNull (ge .getDetails ());
160176 assertTrue (ge .getMessage ().startsWith ("403" ));
161177 }
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+ }
162212}
0 commit comments