TicTacToeServlet extends HttpServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String gameKey = request.getParameter("gameKey"); 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 { game = new Game(userId, null, " ", true); ofy.save().entity(game).now(); gameKey = game.getId(); } game.sendUpdateToClients(); String token = FirebaseChannel.getInstance().createFirebaseToken(game, userId); request.setAttribute("token", token); 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); }  @Override void doGet(HttpServletRequest request, HttpServletResponse response);  }
@Test public void doGet_noGameKey() throws Exception { 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().httpTransport = mockHttpTransport; servletUnderTest.doGet(mockRequest, mockResponse); Objectify ofy = ObjectifyService.ofy(); Game game = ofy.load().type(Game.class).first().safe(); assertThat(game.userX).isEqualTo(USER_ID); verify(mockHttpTransport, times(1)) .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 + game.id); verify(mockRequest).setAttribute(eq("initial_message"), anyString()); verify(mockRequest).setAttribute(eq("game_link"), anyString()); }
@Test public void doGet_existingGame() throws Exception { 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().httpTransport = mockHttpTransport; 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); servletUnderTest.doGet(mockRequest, mockResponse); game = ofy.load().type(Game.class).first().safe(); assertThat(game.userX).isEqualTo("some-other-user-id"); assertThat(game.userO).isEqualTo(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()); }
@Test public void doGet_nonExistentGame() throws Exception { when(mockRequest.getParameter("gameKey")).thenReturn("does-not-exist"); servletUnderTest.doGet(mockRequest, mockResponse); verify(mockResponse).sendError(404); }