Search in sources :

Example 6 with Objectify

use of com.googlecode.objectify.Objectify in project java-docs-samples by GoogleCloudPlatform.

the class TicTacToeServlet method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String gameKey = request.getParameter("gameKey");
    // 1. Create or fetch a Game object from the datastore
    Objectify ofy = ObjectifyService.ofy();
    Game game = null;
    String userId = UserServiceFactory.getUserService().getCurrentUser().getUserId();
    if (gameKey != null) {
        game = ofy.load().type(Game.class).id(gameKey).now();
        if (null == game) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        if (game.getUserO() == null && !userId.equals(game.getUserX())) {
            game.setUserO(userId);
        }
        ofy.save().entity(game).now();
    } else {
        // Initialize a new board. The board is represented as a String of 9 spaces, one for each
        // blank spot on the tic-tac-toe board.
        game = new Game(userId, null, "         ", true);
        ofy.save().entity(game).now();
        gameKey = game.getId();
    }
    // 2. Create this Game in the firebase db
    game.sendUpdateToClients(getServletContext());
    // 3. Inject a secure token into the client, so it can get game updates
    // [START pass_token]
    // The 'Game' object exposes a method which creates a unique string based on the game's key
    // and the user's id.
    String token = FirebaseChannel.getInstance(getServletContext()).createFirebaseToken(game, userId);
    request.setAttribute("token", token);
    // 4. More general template values
    request.setAttribute("game_key", gameKey);
    request.setAttribute("me", userId);
    request.setAttribute("channel_id", game.getChannelKey(userId));
    request.setAttribute("initial_message", new Gson().toJson(game));
    request.setAttribute("game_link", getGameUriWithGameParam(request, gameKey));
    request.getRequestDispatcher("/WEB-INF/view/index.jsp").forward(request, response);
// [END pass_token]
}
Also used : Objectify(com.googlecode.objectify.Objectify) Gson(com.google.gson.Gson)

Example 7 with Objectify

use of com.googlecode.objectify.Objectify in project java-docs-samples by GoogleCloudPlatform.

the class DeleteServletTest method doPost_deleteGame.

@Test
public void doPost_deleteGame() throws Exception {
    // Insert a game
    Objectify ofy = ObjectifyService.ofy();
    Game game = new Game(USER_ID, "my-opponent", "         ", true);
    ofy.save().entity(game).now();
    String gameKey = game.getId();
    when(mockRequest.getParameter("gameKey")).thenReturn(gameKey);
    // Mock out the firebase response. See
    // http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
    MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            return new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                    response.setStatusCode(200);
                    return response;
                }
            };
        }
    });
    FirebaseChannel.getInstance(null).httpTransport = mockHttpTransport;
    Mockito.doReturn(null).when(servletUnderTest).getServletContext();
    servletUnderTest.doPost(mockRequest, mockResponse);
    verify(mockHttpTransport, times(1)).buildRequest(eq("DELETE"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$"));
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) Objectify(com.googlecode.objectify.Objectify) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 8 with Objectify

use of com.googlecode.objectify.Objectify in project java-docs-samples by GoogleCloudPlatform.

the class MoveServletTest method doPost_notMyTurn_move.

@Ignore
public void doPost_notMyTurn_move() throws Exception {
    // Insert a game
    Objectify ofy = ObjectifyService.ofy();
    Game game = new Game(USER_ID, "my-opponent", "         ", false);
    ofy.save().entity(game).now();
    String gameKey = game.getId();
    when(mockRequest.getParameter("gameKey")).thenReturn(gameKey);
    when(mockRequest.getParameter("cell")).thenReturn("1");
    servletUnderTest.doPost(mockRequest, mockResponse);
    verify(mockResponse).sendError(401);
}
Also used : Objectify(com.googlecode.objectify.Objectify) Ignore(org.junit.Ignore)

Example 9 with Objectify

use of com.googlecode.objectify.Objectify in project java-docs-samples by GoogleCloudPlatform.

the class TicTacToeServletTest method doGet_existingGame.

@Test
public void doGet_existingGame() throws Exception {
    // Mock out the firebase response. See
    // http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
    MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            return new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                    response.setStatusCode(200);
                    return response;
                }
            };
        }
    });
    FirebaseChannel.getInstance(null).httpTransport = mockHttpTransport;
    // Insert a game
    Objectify ofy = ObjectifyService.ofy();
    Game game = new Game("some-other-user-id", null, "         ", true);
    ofy.save().entity(game).now();
    String gameKey = game.getId();
    when(mockRequest.getParameter("gameKey")).thenReturn(gameKey);
    Mockito.doReturn(null).when(servletUnderTest).getServletContext();
    servletUnderTest.doGet(mockRequest, mockResponse);
    // Make sure the game object was updated with the other player
    game = ofy.load().type(Game.class).first().safe();
    assertEquals(game.userX, "some-other-user-id");
    assertEquals(game.userO, USER_ID);
    verify(mockHttpTransport, times(2)).buildRequest(eq("PATCH"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$"));
    verify(requestDispatcher).forward(mockRequest, mockResponse);
    verify(mockRequest).setAttribute(eq("token"), anyString());
    verify(mockRequest).setAttribute("game_key", game.id);
    verify(mockRequest).setAttribute("me", USER_ID);
    verify(mockRequest).setAttribute("channel_id", USER_ID + gameKey);
    verify(mockRequest).setAttribute(eq("initial_message"), anyString());
    verify(mockRequest).setAttribute(eq("game_link"), anyString());
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) Objectify(com.googlecode.objectify.Objectify) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) Mockito.anyString(org.mockito.Mockito.anyString) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 10 with Objectify

use of com.googlecode.objectify.Objectify in project java-docs-samples by GoogleCloudPlatform.

the class DeleteServlet method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String gameId = request.getParameter("gameKey");
    Objectify ofy = ObjectifyService.ofy();
    Game game = ofy.load().type(Game.class).id(gameId).safe();
    UserService userService = UserServiceFactory.getUserService();
    String currentUserId = userService.getCurrentUser().getUserId();
    // TODO(you): In practice, first validate that the user has permission to delete the Game
    game.deleteChannel(getServletContext(), currentUserId);
}
Also used : Objectify(com.googlecode.objectify.Objectify) UserService(com.google.appengine.api.users.UserService)

Aggregations

Objectify (com.googlecode.objectify.Objectify)10 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)5 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)5 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)5 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)5 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)5 IOException (java.io.IOException)5 Test (org.junit.Test)5 UserService (com.google.appengine.api.users.UserService)2 Mockito.anyString (org.mockito.Mockito.anyString)2 Gson (com.google.gson.Gson)1 Ignore (org.junit.Ignore)1