@Test public void shouldSendUpdateWhenOnePlayerRequested() throws UnsupportedEncodingException { sender.scheduleUpdate(updateRequestFor("vasya")); sender.sendUpdates(screenFor("vasya", "vasyaboard").asMap()); assertContainsPlayerBoard(response.getContentAsString(), "vasya", "vasyaboard"); }
@Test public void shouldIgnoreNonRequestedPlayerData() throws UnsupportedEncodingException { sender.scheduleUpdate(updateRequestFor("vasya")); sender.sendUpdates(screenFor("petya", "petyaboard").asMap()); assertTrue(asyncContext.isComplete()); }
@Test public void shouldCompleteResponseWhenExceptionOnWrite() throws IOException { response.setCharacterEncoding("NON_EXISTENT_ENCODING_FOR_IO_EXCEPTION"); sender.scheduleUpdate(updateRequestFor("vasya")); sender.sendUpdates(screenFor("vasya", "vasyaboard").asMap()); assertTrue(asyncContext.isComplete()); }
@Test public void shouldSendScores() throws UnsupportedEncodingException { sender.scheduleUpdate(updateRequestFor("vasya")); sender.sendUpdates(screenFor("vasya", 345, "vasyaboard").asMap()); JsonPath jsonPath = from(response.getContentAsString()); assertEquals(345, jsonPath.getInt("vasya.score")); }
@Test public void shouldSendToRequestedPlayersOnly() throws UnsupportedEncodingException { sender.scheduleUpdate(updateRequestFor("vasya")); sender.sendUpdates( screenFor("petya", "petyaboard").addScreenFor("vasya", 123, "vasyaboard").asMap()); JsonPath jsonPath = from(response.getContentAsString()); assertNull("Should contain only requested user screens", jsonPath.get("petyaboard")); }
@Test public void shouldSendUpdateForAllPlayersWhenRequested() throws UnsupportedEncodingException { sender.scheduleUpdate(new UpdateRequest(asyncContext, true, null)); sender.sendUpdates( screenFor("petya", "petyaboard").addScreenFor("vasya", 123, "vasyaboard").asMap()); assertContainsPlayerBoard(response.getContentAsString(), "vasya", "vasyaboard"); assertContainsPlayerBoard(response.getContentAsString(), "petya", "petyaboard"); }
@Test public void shouldRemoveRequestWhenProcessed() throws UnsupportedEncodingException { sender.scheduleUpdate(updateRequestFor("vasya")); sender.sendUpdates(screenFor("vasya", "vasyaboard").asMap()); response.setWriterAccessAllowed(false); try { sender.sendUpdates(screenFor("vasya", "vasyaboard").asMap()); } catch (Exception e) { fail("Should send only once"); } }
@Test public void shouldSendOtherUpdatesWhenExceptionOnWrite() throws UnsupportedEncodingException { MockHttpServletResponse exceptionResponse = new MockHttpServletResponse(); exceptionResponse.setCharacterEncoding("NON_EXISTENT_ENCODING_FOR_IO_EXCEPTION"); MockAsyncContext exceptionContext = new MockAsyncContext(exceptionResponse); sender.scheduleUpdate(new UpdateRequest(exceptionContext, "exception")); sender.scheduleUpdate(updateRequestFor("petya")); sender.sendUpdates( screenFor("petya", "petyaboard").addScreenFor("exception", 123, "exceptionboard").asMap()); assertContainsPlayerBoard(response.getContentAsString(), "petya", "petyaboard"); assertTrue(exceptionContext.isComplete()); }