Skip to content

Commit 1809167

Browse files
committed
Add reset() method to LocalDatastoreHelper
1 parent 65f236e commit 1809167

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.cloud.RetryParams;
2424
import com.google.cloud.datastore.DatastoreOptions;
2525
import com.google.common.base.Strings;
26+
import com.google.common.io.CharStreams;
2627

2728
import java.io.BufferedInputStream;
2829
import java.io.BufferedOutputStream;
@@ -508,6 +509,24 @@ public static boolean sendQuitRequest(int port) {
508509
return result.toString().startsWith(shutdownMsg);
509510
}
510511

512+
public String sendPostRequest(String request) throws IOException {
513+
URL url = new URL("http", "localhost", this.port, request);
514+
HttpURLConnection con = (HttpURLConnection) url.openConnection();
515+
con.setRequestMethod("POST");
516+
con.setDoOutput(true);
517+
OutputStream out = con.getOutputStream();
518+
out.write("".getBytes());
519+
out.flush();
520+
521+
InputStream in = con.getInputStream();
522+
String response = CharStreams.toString(new InputStreamReader(con.getInputStream()));
523+
in.close();
524+
return response;
525+
}
526+
527+
/**
528+
* Quit the local emulator and related local service.
529+
*/
511530
public void stop() throws IOException, InterruptedException {
512531
sendQuitRequest(port);
513532
if (processReader != null) {
@@ -521,6 +540,13 @@ public void stop() throws IOException, InterruptedException {
521540
}
522541
}
523542

543+
/**
544+
* Reset the internal state of the emulator.
545+
*/
546+
public void reset() throws IOException {
547+
sendPostRequest("/reset");
548+
}
549+
524550
private static void deleteRecurse(Path path) throws IOException {
525551
if (path == null || !Files.exists(path)) {
526552
return;

google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/LocalDatastoreHelperTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,36 @@
1717
package com.google.cloud.datastore.testing;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertNull;
2022
import static org.junit.Assert.assertSame;
2123
import static org.junit.Assert.assertTrue;
2224

2325
import com.google.cloud.AuthCredentials;
26+
import com.google.cloud.datastore.Datastore;
27+
import com.google.cloud.datastore.DatastoreException;
2428
import com.google.cloud.datastore.DatastoreOptions;
29+
import com.google.cloud.datastore.Entity;
30+
import com.google.cloud.datastore.Key;
2531

32+
import org.junit.Rule;
2633
import org.junit.Test;
34+
import org.junit.rules.ExpectedException;
2735
import org.junit.runner.RunWith;
2836
import org.junit.runners.JUnit4;
2937

38+
import java.io.IOException;
39+
3040
@RunWith(JUnit4.class)
3141
public class LocalDatastoreHelperTest {
3242

3343
private static final double TOLERANCE = 0.00001;
3444
private static final String PROJECT_ID_PREFIX = "test-project-";
3545
private static final String NAMESPACE = "namespace";
3646

47+
@Rule
48+
public ExpectedException thrown = ExpectedException.none();
49+
3750
@Test
3851
public void testCreate() {
3952
LocalDatastoreHelper helper = LocalDatastoreHelper.create(0.75);
@@ -57,4 +70,19 @@ public void testOptions() {
5770
assertSame(AuthCredentials.noAuth(), options.authCredentials());
5871
assertEquals(NAMESPACE, options.namespace());
5972
}
73+
74+
@Test
75+
public void testStartStopReset() throws IOException, InterruptedException {
76+
LocalDatastoreHelper helper = LocalDatastoreHelper.create();
77+
helper.start();
78+
Datastore datastore = helper.options().service();
79+
Key key = datastore.newKeyFactory().kind("kind").newKey("name");
80+
datastore.put(Entity.builder(key).build());
81+
assertNotNull(datastore.get(key));
82+
helper.reset();
83+
assertNull(datastore.get(key));
84+
helper.stop();
85+
thrown.expect(DatastoreException.class);
86+
datastore.get(key);
87+
}
6088
}

0 commit comments

Comments
 (0)