2424
2525import com .google .api .client .http .HttpRequest ;
2626import com .google .api .client .http .HttpRequestInitializer ;
27+ import com .google .datastore .utils .testing .MockCredential ;
28+ import com .google .datastore .utils .testing .MockDatastoreFactory ;
2729import com .google .datastore .v1 .AllocateIdsRequest ;
2830import com .google .datastore .v1 .AllocateIdsResponse ;
2931import com .google .datastore .v1 .BeginTransactionRequest ;
4244import com .google .datastore .v1 .RunAggregationQueryResponse ;
4345import com .google .datastore .v1 .RunQueryRequest ;
4446import com .google .datastore .v1 .RunQueryResponse ;
45- import com .google .datastore .utils .testing .MockCredential ;
46- import com .google .datastore .utils .testing .MockDatastoreFactory ;
4747import com .google .protobuf .ByteString ;
4848import com .google .protobuf .Message ;
4949import com .google .rpc .Code ;
50-
5150import java .io .IOException ;
5251import java .lang .reflect .InvocationTargetException ;
5352import java .lang .reflect .Method ;
5453import java .net .SocketTimeoutException ;
55-
5654import org .junit .Test ;
5755import org .junit .runner .RunWith ;
5856import org .junit .runners .JUnit4 ;
5957
60- /**
61- * Tests for {@link DatastoreFactory} and {@link Datastore}.
62- */
58+ /** Tests for {@link DatastoreFactory} and {@link Datastore}. */
6359@ RunWith (JUnit4 .class )
6460public class DatastoreClientTest {
6561 private static final String PROJECT_ID = "project-id" ;
6662
6763 private DatastoreFactory factory = new MockDatastoreFactory ();
6864 private DatastoreOptions .Builder options =
69- new DatastoreOptions .Builder ().projectId (PROJECT_ID ).credential (new MockCredential ());
65+ new DatastoreOptions .Builder ().projectId (PROJECT_ID ).credential (new MockCredential ());
7066
7167 @ Test
7268 public void options_NoProjectIdOrProjectEndpoint () {
7369 IllegalArgumentException exception =
74- assertThrows (
75- IllegalArgumentException .class ,
76- () -> factory .create (new DatastoreOptions .Builder ().build ()));
70+ assertThrows (
71+ IllegalArgumentException .class ,
72+ () -> factory .create (new DatastoreOptions .Builder ().build ()));
7773 assertThat (exception )
78- .hasMessageThat ()
79- .contains ("Either project ID or project endpoint must be provided" );
74+ .hasMessageThat ()
75+ .contains ("Either project ID or project endpoint must be provided" );
8076 factory .create (options .build ());
8177 }
8278
8379 @ Test
8480 public void options_ProjectIdAndProjectEndpoint () throws Exception {
8581 IllegalArgumentException exception =
86- assertThrows (
87- IllegalArgumentException .class ,
88- () ->
89- new DatastoreOptions .Builder ()
90- .projectId (PROJECT_ID )
91- .projectEndpoint (
92- "http://localhost:1234/datastore/v1beta42/projects/project-id" ));
82+ assertThrows (
83+ IllegalArgumentException .class ,
84+ () ->
85+ new DatastoreOptions .Builder ()
86+ .projectId (PROJECT_ID )
87+ .projectEndpoint (
88+ "http://localhost:1234/datastore/v1beta42/projects/project-id" ));
9389 assertThat (exception )
94- .hasMessageThat ()
95- .contains ("Cannot set both project endpoint and project ID" );
90+ .hasMessageThat ()
91+ .contains ("Cannot set both project endpoint and project ID" );
9692 }
9793
9894 @ Test
9995 public void options_LocalHostAndProjectEndpoint () throws Exception {
10096 IllegalArgumentException exception =
101- assertThrows (
102- IllegalArgumentException .class ,
103- () ->
104- new DatastoreOptions .Builder ()
105- .localHost ("localhost:8080" )
106- .projectEndpoint (
107- "http://localhost:1234/datastore/v1beta42/projects/project-id" ));
97+ assertThrows (
98+ IllegalArgumentException .class ,
99+ () ->
100+ new DatastoreOptions .Builder ()
101+ .localHost ("localhost:8080" )
102+ .projectEndpoint (
103+ "http://localhost:1234/datastore/v1beta42/projects/project-id" ));
108104 assertThat (exception )
109- .hasMessageThat ()
110- .contains ("Can set at most one of project endpoint, host, and local host" );
105+ .hasMessageThat ()
106+ .contains ("Can set at most one of project endpoint, host, and local host" );
111107 }
112108
113109 @ Test
114110 public void options_HostAndProjectEndpoint () throws Exception {
115111 IllegalArgumentException exception =
116- assertThrows (
117- IllegalArgumentException .class ,
118- () ->
119- new DatastoreOptions .Builder ()
120- .host ("foo-datastore.googleapis.com" )
121- .projectEndpoint (
122- "http://localhost:1234/datastore/v1beta42/projects/project-id" ));
112+ assertThrows (
113+ IllegalArgumentException .class ,
114+ () ->
115+ new DatastoreOptions .Builder ()
116+ .host ("foo-datastore.googleapis.com" )
117+ .projectEndpoint (
118+ "http://localhost:1234/datastore/v1beta42/projects/project-id" ));
123119 assertThat (exception )
124- .hasMessageThat ()
125- .contains ("Can set at most one of project endpoint, host, and local host" );
120+ .hasMessageThat ()
121+ .contains ("Can set at most one of project endpoint, host, and local host" );
126122 }
127123
128124 @ Test
129125 public void options_HostAndLocalHost () throws Exception {
130126 IllegalArgumentException exception =
131- assertThrows (
132- IllegalArgumentException .class ,
133- () ->
134- new DatastoreOptions .Builder ()
135- .host ("foo-datastore.googleapis.com" )
136- .localHost ("localhost:8080" ));
127+ assertThrows (
128+ IllegalArgumentException .class ,
129+ () ->
130+ new DatastoreOptions .Builder ()
131+ .host ("foo-datastore.googleapis.com" )
132+ .localHost ("localhost:8080" ));
137133 assertThat (exception )
138- .hasMessageThat ()
139- .contains ("Can set at most one of project endpoint, host, and local host" );
134+ .hasMessageThat ()
135+ .contains ("Can set at most one of project endpoint, host, and local host" );
140136 }
141137
142138 @ Test
143139 public void options_InvalidLocalHost () throws Exception {
144140 IllegalArgumentException exception =
145- assertThrows (
146- IllegalArgumentException .class ,
147- () ->
148- factory .create (
149- new DatastoreOptions .Builder ()
150- .projectId (PROJECT_ID )
151- .localHost ("!not a valid url!" )
152- .build ()));
141+ assertThrows (
142+ IllegalArgumentException .class ,
143+ () ->
144+ factory .create (
145+ new DatastoreOptions .Builder ()
146+ .projectId (PROJECT_ID )
147+ .localHost ("!not a valid url!" )
148+ .build ()));
153149 assertThat (exception ).hasMessageThat ().contains ("Illegal character" );
154150 }
155151
156152 @ Test
157153 public void options_SchemeInLocalHost () {
158154 IllegalArgumentException exception =
159- assertThrows (
160- IllegalArgumentException .class ,
161- () -> new DatastoreOptions .Builder ().localHost ("http://localhost:8080" ));
155+ assertThrows (
156+ IllegalArgumentException .class ,
157+ () -> new DatastoreOptions .Builder ().localHost ("http://localhost:8080" ));
162158 assertThat (exception )
163- .hasMessageThat ()
164- .contains ("Local host \" http://localhost:8080\" must not include scheme" );
159+ .hasMessageThat ()
160+ .contains ("Local host \" http://localhost:8080\" must not include scheme" );
165161 }
166162
167163 @ Test
168164 public void options_InvalidHost () {
169165 IllegalArgumentException exception =
170- assertThrows (
171- IllegalArgumentException .class ,
172- () ->
173- factory .create (
174- new DatastoreOptions .Builder ()
175- .projectId (PROJECT_ID )
176- .host ("!not a valid url!" )
177- .build ()));
166+ assertThrows (
167+ IllegalArgumentException .class ,
168+ () ->
169+ factory .create (
170+ new DatastoreOptions .Builder ()
171+ .projectId (PROJECT_ID )
172+ .host ("!not a valid url!" )
173+ .build ()));
178174 assertThat (exception ).hasMessageThat ().contains ("Illegal character" );
179175 }
180176
181177 @ Test
182178 public void options_SchemeInHost () {
183179 IllegalArgumentException exception =
184- assertThrows (
185- IllegalArgumentException .class ,
186- () -> new DatastoreOptions .Builder ().host ("http://foo-datastore.googleapis.com" ));
180+ assertThrows (
181+ IllegalArgumentException .class ,
182+ () -> new DatastoreOptions .Builder ().host ("http://foo-datastore.googleapis.com" ));
187183
188184 assertThat (exception )
189- .hasMessageThat ()
190- .contains ("Host \" http://foo-datastore.googleapis.com\" must not include scheme." );
185+ .hasMessageThat ()
186+ .contains ("Host \" http://foo-datastore.googleapis.com\" must not include scheme." );
191187 }
192188
193189 @ Test
@@ -198,84 +194,84 @@ public void create_NullOptions() throws Exception {
198194 @ Test
199195 public void create_Host () {
200196 Datastore datastore =
201- factory .create (
202- new DatastoreOptions .Builder ()
203- .projectId (PROJECT_ID )
204- .host ("foo-datastore.googleapis.com" )
205- .build ());
197+ factory .create (
198+ new DatastoreOptions .Builder ()
199+ .projectId (PROJECT_ID )
200+ .host ("foo-datastore.googleapis.com" )
201+ .build ());
206202 assertThat (datastore .remoteRpc .getUrl ())
207- .isEqualTo ("https://foo-datastore.googleapis.com/v1/projects/project-id" );
203+ .isEqualTo ("https://foo-datastore.googleapis.com/v1/projects/project-id" );
208204 }
209205
210206 @ Test
211207 public void create_LocalHost () {
212208 Datastore datastore =
213- factory .create (
214- new DatastoreOptions .Builder ()
215- .projectId (PROJECT_ID )
216- .localHost ("localhost:8080" )
217- .build ());
209+ factory .create (
210+ new DatastoreOptions .Builder ()
211+ .projectId (PROJECT_ID )
212+ .localHost ("localhost:8080" )
213+ .build ());
218214 assertThat (datastore .remoteRpc .getUrl ())
219- .isEqualTo ("http://localhost:8080/v1/projects/project-id" );
215+ .isEqualTo ("http://localhost:8080/v1/projects/project-id" );
220216 }
221217
222218 @ Test
223219 public void create_LocalHostIp () {
224220 Datastore datastore =
225- factory .create (
226- new DatastoreOptions .Builder ()
227- .projectId (PROJECT_ID )
228- .localHost ("127.0.0.1:8080" )
229- .build ());
221+ factory .create (
222+ new DatastoreOptions .Builder ()
223+ .projectId (PROJECT_ID )
224+ .localHost ("127.0.0.1:8080" )
225+ .build ());
230226 assertThat (datastore .remoteRpc .getUrl ())
231- .isEqualTo ("http://127.0.0.1:8080/v1/projects/project-id" );
227+ .isEqualTo ("http://127.0.0.1:8080/v1/projects/project-id" );
232228 }
233229
234230 @ Test
235231 public void create_DefaultHost () {
236232 Datastore datastore =
237- factory .create (new DatastoreOptions .Builder ().projectId (PROJECT_ID ).build ());
233+ factory .create (new DatastoreOptions .Builder ().projectId (PROJECT_ID ).build ());
238234 assertThat (datastore .remoteRpc .getUrl ())
239- .isEqualTo ("https://datastore.googleapis.com/v1/projects/project-id" );
235+ .isEqualTo ("https://datastore.googleapis.com/v1/projects/project-id" );
240236 }
241237
242238 @ Test
243239 public void create_ProjectEndpoint () {
244240 Datastore datastore =
245- factory .create (
246- new DatastoreOptions .Builder ()
247- .projectEndpoint ("http://prom-qa/datastore/v1beta42/projects/project-id" )
248- .build ());
241+ factory .create (
242+ new DatastoreOptions .Builder ()
243+ .projectEndpoint ("http://prom-qa/datastore/v1beta42/projects/project-id" )
244+ .build ());
249245 assertThat (datastore .remoteRpc .getUrl ())
250- .isEqualTo ("http://prom-qa/datastore/v1beta42/projects/project-id" );
246+ .isEqualTo ("http://prom-qa/datastore/v1beta42/projects/project-id" );
251247 }
252248
253249 @ Test
254250 public void create_ProjectEndpointNoScheme () {
255251 IllegalArgumentException exception =
256- assertThrows (
257- IllegalArgumentException .class ,
258- () ->
259- factory .create (
260- new DatastoreOptions .Builder ()
261- .projectEndpoint ("localhost:1234/datastore/v1beta42/projects/project-id" )
262- .build ()));
252+ assertThrows (
253+ IllegalArgumentException .class ,
254+ () ->
255+ factory .create (
256+ new DatastoreOptions .Builder ()
257+ .projectEndpoint ("localhost:1234/datastore/v1beta42/projects/project-id" )
258+ .build ()));
263259 assertThat (exception )
264- .hasMessageThat ()
265- .contains (
266- "Project endpoint \" localhost:1234/datastore/v1beta42/projects/project-id\" must"
267- + " include scheme." );
260+ .hasMessageThat ()
261+ .contains (
262+ "Project endpoint \" localhost:1234/datastore/v1beta42/projects/project-id\" must"
263+ + " include scheme." );
268264 }
269265
270266 @ Test
271267 public void initializer () throws Exception {
272268 options .initializer (
273- new HttpRequestInitializer () {
274- @ Override
275- public void initialize (HttpRequest request ) {
276- request .getHeaders ().setCookie ("magic" );
277- }
278- });
269+ new HttpRequestInitializer () {
270+ @ Override
271+ public void initialize (HttpRequest request ) {
272+ request .getHeaders ().setCookie ("magic" );
273+ }
274+ });
279275 Datastore datastore = factory .create (options .build ());
280276 MockDatastoreFactory mockClient = (MockDatastoreFactory ) factory ;
281277 AllocateIdsRequest request = AllocateIdsRequest .newBuilder ().build ();
@@ -336,9 +332,9 @@ public void runQuery() throws Exception {
336332 request .getQueryBuilder ();
337333 RunQueryResponse .Builder response = RunQueryResponse .newBuilder ();
338334 response
339- .getBatchBuilder ()
340- .setEntityResultType (EntityResult .ResultType .FULL )
341- .setMoreResults (QueryResultBatch .MoreResultsType .NOT_FINISHED );
335+ .getBatchBuilder ()
336+ .setEntityResultType (EntityResult .ResultType .FULL )
337+ .setMoreResults (QueryResultBatch .MoreResultsType .NOT_FINISHED );
342338 expectRpc ("runQuery" , request .build (), response .build ());
343339 }
344340
0 commit comments