Search in sources :

Example 1 with GuestbookResponse

use of com.google.appengine.samples.angularjs_guestbook.domain.GuestbookResponse in project appengine-angular-guestbook-java by googlearchive.

the class GuestbookResourceTest method testSignGuestbook.

@Test
public void testSignGuestbook() throws ServletException, IOException {
    HttpServletRequest request = getMockedJsonRequest("{content:\"Test message\"}");
    final StringBuffer resultBuffer = new StringBuffer();
    HttpServletResponse response = getMockedServletResponse(resultBuffer);
    servletContainer.service(URI.create("http://localhost/"), URI.create("/guestbook/default"), request, response);
    logger.fine(resultBuffer.toString().trim());
    Gson gson = new GsonBuilder().create();
    GuestbookResponse result = gson.fromJson(resultBuffer.toString().trim(), GuestbookResponse.class);
    assertThat(result.getGuestbookName(), is("default"));
    assertThat(result.getGreetings().size(), is(1));
    Greeting greeting = result.getGreetings().get(0);
    assertThat(greeting.getContent(), is("Test message"));
    assertThat(greeting.getAuthor(), is("test@example.com"));
    assertThat(greeting.getDate(), instanceOf(Date.class));
    assertThat(result.getUserServiceInfo(), nullValue());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Greeting(com.google.appengine.samples.angularjs_guestbook.domain.Greeting) GsonBuilder(com.google.gson.GsonBuilder) GuestbookResponse(com.google.appengine.samples.angularjs_guestbook.domain.GuestbookResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Gson(com.google.gson.Gson) Test(org.junit.Test)

Example 2 with GuestbookResponse

use of com.google.appengine.samples.angularjs_guestbook.domain.GuestbookResponse in project appengine-angular-guestbook-java by googlearchive.

the class GuestbookResourceTest method testGetGuestbookData.

@Test
public void testGetGuestbookData() throws ServletException, IOException {
    HttpServletRequest request = mock(HttpServletRequest.class);
    final StringBuffer resultBuffer = new StringBuffer();
    HttpServletResponse response = getMockedServletResponse(resultBuffer);
    // Since jersey looks up the HTTP method and headers from the request, we mock those 2 calls.
    when(request.getMethod()).thenReturn("GET");
    when(request.getHeaderNames()).thenReturn(new Vector<String>().elements());
    servletContainer.service(URI.create("http://localhost/"), URI.create("/guestbook/default"), request, response);
    logger.fine(resultBuffer.toString().trim());
    Gson gson = new GsonBuilder().create();
    GuestbookResponse result = gson.fromJson(resultBuffer.toString().trim(), GuestbookResponse.class);
    assertThat(result.getGuestbookName(), is("default"));
    assertThat(result.getGreetings().size(), is(0));
    assertThat(result.getUserServiceInfo().getCurrentUser().getEmail(), is("test@example.com"));
    assertThat(result.getUserServiceInfo().getLoginUrl(), is("/_ah/login?continue=%2F"));
    assertThat(result.getUserServiceInfo().getLogoutUrl(), is("/_ah/logout?continue=%2F"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) GsonBuilder(com.google.gson.GsonBuilder) GuestbookResponse(com.google.appengine.samples.angularjs_guestbook.domain.GuestbookResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Gson(com.google.gson.Gson) Test(org.junit.Test)

Example 3 with GuestbookResponse

use of com.google.appengine.samples.angularjs_guestbook.domain.GuestbookResponse in project appengine-angular-guestbook-java by googlearchive.

the class GuestbookResource method signGuestbook.

@POST
@Path("/{guestbookName}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public GuestbookResponse signGuestbook(@DefaultValue("default") @PathParam("guestbookName") final String guestbookName, final Map<String, String> postData) {
    UserService userService = UserServiceFactory.getUserService();
    DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
    Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName);
    // We set the above parent key on each Greeting entity in order to make the queries strong
    // consistent. Please Note that as a trade off, we can not write to a single guestbook at a
    // rate more than 1 write/second.
    String content = postData.get("content");
    if (content != null && content.length() > 0) {
        Date date = new Date();
        Entity greeting = new Entity("Greeting", guestbookKey);
        greeting.setProperty("user", userService.getCurrentUser());
        greeting.setProperty("date", date);
        greeting.setProperty("content", content);
        datastoreService.put(greeting);
    }
    return new GuestbookResponse(guestbookName, getGreetings(guestbookName), null);
}
Also used : UserService(com.google.appengine.api.users.UserService) GuestbookResponse(com.google.appengine.samples.angularjs_guestbook.domain.GuestbookResponse)

Aggregations

GuestbookResponse (com.google.appengine.samples.angularjs_guestbook.domain.GuestbookResponse)3 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Test (org.junit.Test)2 UserService (com.google.appengine.api.users.UserService)1 Greeting (com.google.appengine.samples.angularjs_guestbook.domain.Greeting)1