Skip to content

Commit

Permalink
Use LocalDatastoreHelper for integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
wsh committed Oct 26, 2016
1 parent 5328ac0 commit c6c7539
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,26 @@
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.KeyFactory;

import java.util.concurrent.atomic.AtomicReference;

//[START all]
public class Persistence {
private static AtomicReference<Datastore> datastore = new AtomicReference<>();

public static Datastore getDatastore() {
return DatastoreOptions.builder().projectId("your-project-id-here").build().service();
if (datastore.get() == null) {
datastore.set(DatastoreOptions.builder().projectId("your-project-id-here").build().service());
}

return datastore.get();
}

public static KeyFactory getKeyFactory(Class<?> c) {
return getDatastore().newKeyFactory().kind(c.getSimpleName());
}

public static void setDatastore(Datastore datastore) {
Persistence.datastore.set(datastore);
}
}
//[END all]
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import static org.junit.Assert.assertTrue;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -28,7 +30,7 @@
public class GreetingTest {
@Before
public void setUp() {
TestUtils.wipeDatastore();
TestUtils.startDatastore();
}

@Test
Expand All @@ -41,4 +43,9 @@ public void testSaveGreeting() throws Exception {
assertTrue(greetings.size() == 1);
assertEquals(greeting, greetings.get(0));
}

@After
public void tearDown() {
TestUtils.stopDatastore();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -46,7 +48,7 @@ public void setUp() throws Exception {
helper.setUp();

signGuestbookServlet = new SignGuestbookServlet();
TestUtils.wipeDatastore();
TestUtils.startDatastore();
}

@Test
Expand All @@ -63,4 +65,9 @@ public void doPost_userNotLoggedIn() throws Exception {
assertTrue(greetings.size() == 1);
assertTrue(greetings.get(0).content.equals(testGreeting));
}

@After
public void tearDown() {
TestUtils.stopDatastore();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,33 @@
import com.google.cloud.datastore.Key;
import com.google.cloud.datastore.Query;
import com.google.cloud.datastore.QueryResults;
import com.google.cloud.datastore.testing.LocalDatastoreHelper;
import com.google.common.collect.Lists;

import java.io.IOException;
import java.util.ArrayList;

public class TestUtils {
static LocalDatastoreHelper datastore = LocalDatastoreHelper.create();

public static void startDatastore() {
try {
datastore.start();
Persistence.setDatastore(datastore.options().service());
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}

public static void stopDatastore() {
try {
datastore.stop();
Persistence.setDatastore(null);
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}

public static void wipeDatastore() {
Datastore datastore = getDatastore();
QueryResults<Key> guestbooks = datastore.run(Query.keyQueryBuilder().kind("Greeting").build());
Expand Down

0 comments on commit c6c7539

Please sign in to comment.