StartupServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain"); DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Key isPopulatedKey = KeyFactory.createKey(IS_POPULATED_ENTITY, IS_POPULATED_KEY_NAME); boolean isAlreadyPopulated; try { datastore.get(isPopulatedKey); isAlreadyPopulated = true; } catch (EntityNotFoundException expected) { isAlreadyPopulated = false; } if (isAlreadyPopulated) { resp.getWriter().println("ok"); return; } ImmutableList.Builder<Entity> people = ImmutableList.builder(); for (String name : US_PRESIDENTS) { Entity person = new Entity(PERSON_ENTITY); person.setProperty(NAME_PROPERTY, name); people.add(person); } datastore.put(people.build()); datastore.put(new Entity(isPopulatedKey)); resp.getWriter().println("ok"); }    }
@Test public void doGet_emptyDatastore_writesOkay() throws Exception { servletUnderTest.doGet(mockRequest, mockResponse); assertWithMessage("StartupServlet response").that(responseWriter.toString()).isEqualTo("ok\n"); }
@Test public void doGet_emptyDatastore_writesPresidents() throws Exception { servletUnderTest.doGet(mockRequest, mockResponse); Filter nameFilter = new FilterPredicate("name", FilterOperator.EQUAL, "George Washington"); Query q = new Query("Person").setFilter(nameFilter); Entity result = datastore.prepare(q).asSingleEntity(); assertWithMessage("name").that(result.getProperty("name")).isEqualTo("George Washington"); }
@Test public void doGet_alreadyPopulated_writesOkay() throws Exception { datastore.put( new Entity(StartupServlet.IS_POPULATED_ENTITY, StartupServlet.IS_POPULATED_KEY_NAME)); servletUnderTest.doGet(mockRequest, mockResponse); assertWithMessage("StartupServlet response").that(responseWriter.toString()).isEqualTo("ok\n"); }