MoveServlet extends HttpServlet { @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(); int cell = new Integer(request.getParameter("cell")); if (!game.makeMove(cell, currentUserId)) { response.sendError(HttpServletResponse.SC_UNAUTHORIZED); } else { ofy.save().entity(game).now(); } }  @Override void doPost(HttpServletRequest request, HttpServletResponse response);  }
@Test public void doPost_myTurn_move() throws Exception { 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); when(mockRequest.getParameter("cell")).thenReturn("1"); 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.doPost(mockRequest, mockResponse); game = ofy.load().type(Game.class).id(gameKey).safe(); assertThat(game.board).isEqualTo(" X "); verify(mockHttpTransport, times(2)) .buildRequest(eq("PATCH"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$")); }
@Test public void doPost_notMyTurn_move() throws Exception { 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); }