DocumentServlet extends HttpServlet { public Document createDocument() { User currentUser = UserServiceFactory.getUserService().getCurrentUser(); String userEmail = currentUser == null ? "" : currentUser.getEmail(); String userDomain = currentUser == null ? "" : currentUser.getAuthDomain(); String myDocId = "PA6-5000"; Document doc = Document.newBuilder() .setId(myDocId) .addField(Field.newBuilder().setName("content").setText("the rain in spain")) .addField(Field.newBuilder().setName("email").setText(userEmail)) .addField(Field.newBuilder().setName("domain").setAtom(userDomain)) .addField(Field.newBuilder().setName("published").setDate(new Date())) .build(); return doc; }  Document createDocument(); @Override void doGet(HttpServletRequest req, HttpServletResponse resp);  }
@Test public void createDocument_withSignedInUser() throws Exception { String email = "tmatsuo@example.com"; String authDomain = "example.com"; helper.setEnvEmail(email); helper.setEnvAuthDomain(authDomain); helper.setEnvIsLoggedIn(true); Document doc = servletUnderTest.createDocument(); assertWithMessage("content") .that(doc.getOnlyField("content").getText()) .contains("the rain in spain"); assertWithMessage("email").that(doc.getOnlyField("email").getText()).isEqualTo(email); }
@Test public void createDocument_withoutSignedIn() throws Exception { helper.setEnvIsLoggedIn(false); Document doc = servletUnderTest.createDocument(); assertWithMessage("content") .that(doc.getOnlyField("content").getText()) .contains("the rain in spain"); assertWithMessage("email").that(doc.getOnlyField("email").getText()).isEmpty(); }