use of com.googlecode.objectify.Objectify in project java-docs-samples by GoogleCloudPlatform.
the class OpenedServlet method doPost.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
// TODO(you): In practice, you should validate the user has permission to post to the given Game
String gameId = request.getParameter("gameKey");
Objectify ofy = ObjectifyService.ofy();
Game game = ofy.load().type(Game.class).id(gameId).safe();
game.sendUpdateToClients(getServletContext());
}
use of com.googlecode.objectify.Objectify in project java-docs-samples by GoogleCloudPlatform.
the class MoveServletTest method doPost_myTurn_move.
@Test
public void doPost_myTurn_move() 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);
when(mockRequest.getParameter("cell")).thenReturn("1");
// 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);
game = ofy.load().type(Game.class).id(gameKey).safe();
assertEquals(game.board, " X ");
verify(mockHttpTransport, times(2)).buildRequest(eq("PATCH"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$"));
}
use of com.googlecode.objectify.Objectify in project java-docs-samples by GoogleCloudPlatform.
the class OpenedServletTest method doPost_open.
@Test
public void doPost_open() 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(2)).buildRequest(eq("PATCH"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$"));
}
use of com.googlecode.objectify.Objectify in project java-docs-samples by GoogleCloudPlatform.
the class TicTacToeServletTest method doGet_noGameKey.
@Test
public void doGet_noGameKey() 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;
Mockito.doReturn(null).when(servletUnderTest).getServletContext();
servletUnderTest.doGet(mockRequest, mockResponse);
// Make sure the game object was created for a new game
Objectify ofy = ObjectifyService.ofy();
Game game = ofy.load().type(Game.class).first().safe();
assertEquals(game.userX, 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());
}
use of com.googlecode.objectify.Objectify in project java-docs-samples by GoogleCloudPlatform.
the class MoveServlet 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();
int cell = new Integer(request.getParameter("cell"));
if (!game.makeMove(getServletContext(), cell, currentUserId)) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
} else {
ofy.save().entity(game).now();
}
}
Aggregations