HttpPlayerController implements PlayerController { public void requestControl(final Player player, final Figure.Type type, final int x, final int y, final TetrisJoystik joystick, final List<Plot> plots, final List<Figure.Type> futureFigures) throws IOException { GameState gameState = new TetrisGameState(plots, type, x, y, futureFigures, true); transport.sendState(player.getName(), gameState); }  void requestControl(final Player player, final Figure.Type type, final int x, final int y, final TetrisJoystik joystick, final List<Plot> plots, final List<Figure.Type> futureFigures); void setListener(PlayerControllerListener listener); void setTransport(PlayerTransport transport); void registerPlayerTransport(Player player, TetrisJoystik joystick); void unregisterPlayerTransport(Player player);  }
@Test public void shouldSendRequestControlCommands() throws IOException, InterruptedException { controller.requestControl(vasya, Figure.Type.T, 4, 19, joystick, Collections.<Plot>emptyList(), Arrays.asList(Figure.Type.I, Figure.Type.J, Figure.Type.L, Figure.Type.O)); server.waitForRequest(); assertEquals("T", server.getRequestParameter("figure")); assertEquals("4", server.getRequestParameter("x")); assertEquals("19", server.getRequestParameter("y")); }
@Test public void shouldSendRequestControlCommandsNoTailSlash() throws IOException, InterruptedException { try { controller.requestControl(new Player("vasya", "http: Figure.Type.T, 1, 1, joystick, Collections.<Plot>emptyList(), Arrays.asList(Figure.Type.I, Figure.Type.J, Figure.Type.L, Figure.Type.O)); } catch (NumberFormatException e) { fail(); } }
@Test public void shouldSendGlassState() throws IOException, InterruptedException { controller.requestControl(vasya, Figure.Type.T, 4, 19, joystick, Arrays.asList(plot(0, 0)), Arrays.asList(Figure.Type.I, Figure.Type.J, Figure.Type.L, Figure.Type.O)); server.waitForRequest(); int times = 10 - 1; assertEquals("*" + spaces(times) + spaces(GLASS_WIDTH * (GLASS_HEIGHT - 1)), server.getRequestParameter("glass")); }
@Test public void shouldSendGlassStateWhenSeveralDropped() throws IOException, InterruptedException { controller.requestControl(vasya, Figure.Type.T, 4, 19, joystick, Arrays.asList(plot(5, 0), plot(6, 1)), Arrays.asList(Figure.Type.I)); server.waitForRequest(); assertEquals(spaces(5) + "*" + spaces(GLASS_WIDTH - 5 - 1) + spaces(6) + "*" + spaces(GLASS_WIDTH - 6 - 1) + spaces(GLASS_WIDTH * (GLASS_HEIGHT - 2)), server.getRequestParameter("glass")); }
@Test public void shouldLogHttpRequestResponse() throws IOException, InterruptedException { server.setResponse("left=1,right=2,rotate=3,drop"); controller.requestControl(vasya, Figure.Type.T, 4, 19, joystick, Arrays.asList(plot(0, 0)), Arrays.asList(Figure.Type.I)); server.waitForRequest(); int times = 1; verifyListenerCalled(times); assertTrue(requestCaptor.getValue().startsWith("/?figure=T&x=4&y=19&glass=*")); assertEquals("left=1,right=2,rotate=3,drop", responseCaptor.getValue()); assertSame(vasya, playerCaptor.getValue()); }
@Test public void shouldLogHttpRequestResponseWhenException() throws IOException, InterruptedException { setupListener(); server.setResponseException(new RuntimeException()); controller.requestControl(vasya, Figure.Type.T, 4, 19, joystick, Collections.<Plot>emptyList(), Arrays.asList(Figure.Type.I)); server.waitForRequest(); verifyListenerCalled(1); assertEquals("ERROR:500", responseCaptor.getValue()); }
@Test public void shouldSendFutureFigures() throws IOException, InterruptedException { controller.requestControl(vasya, Figure.Type.T, 4, 19, joystick, Arrays.asList(plot(0, 0)), Arrays.asList(Figure.Type.I, Figure.Type.J, Figure.Type.L, Figure.Type.O)); server.waitForRequest(); assertEquals("IJLO", server.getRequestParameter("next")); }