package de.movope.web;

import de.movope.domain.ChessBoard;
import de.movope.domain.Move;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MvcResult;

import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

public class GameControllerRestTest extends SystemTest {

    @Autowired
    private ChessGameRepository chessGameRepository;

    @Before
    public void setup() throws Exception {
        super.setup();
        this.chessGameRepository.deleteAll();
    }

    @Test
    public void Status404ReturnedIfGameDoesNotExist() throws Exception {
        String gameId = "notExisting";
        mockMvc.perform(get("/game/{gameId}/board", gameId))
                .andExpect(status().isNotFound());
    }

    @Test
    public void newGameCanBeStarted() throws Exception {
        String gameId = "ben";

        mockMvc.perform(put("/game/" + gameId)).andExpect(status().isCreated());

        MvcResult result = mockMvc.perform(get("/game/{gameId}/board", gameId)).andExpect(status().isOk()).andReturn();

        ChessBoardView response = fromJson(result, ChessBoardView.class);
        assertEquals(new ChessBoardView(ChessBoard.createNew(gameId)), response);
    }

    @Test
    public void aValidMoveCanBeExecuted() throws Exception {
        String gameId = "ben";
        mockMvc.perform(put("/game/" + gameId)).andExpect(status().isCreated());

        Move move = Move.create("A2", "A4");

        mockMvc.perform(post("/game/{gameId}/move", gameId)
                .content(this.json(toResource(move)))
                .contentType(contentType))
                .andExpect(status().isOk());

        ChessBoard expected = ChessBoard.createNew("ben");
        expected.execute(move);

        MvcResult result = mockMvc.perform(get("/game/{gameId}/board", gameId))
                .andExpect(status().isOk())
                .andReturn();
        ChessBoardView response = fromJson(result, ChessBoardView.class);
        assertEquals(new ChessBoardView(expected), response);
    }

    private MoveResource toResource(Move move) {
        MoveResource resource = new MoveResource();
        resource.setTo(move.to().toString());
        resource.setFrom(move.from().toString());
        return resource;
    }

}

@@ -877,37 +877,33 @@
 orm(get(%22/game/%7B
-gameI
+i
 d%7D/board%22, gameI
@@ -1090,35 +1090,38 @@
 form(put(%22/game/
-%22 +
+%7Bid%7D%22,
  gameId)).andExp
@@ -1573,11 +1573,14 @@
 ame/
-%22 +
+%7Bid%7D%22,
  gam
@@ -1702,21 +1702,17 @@
 %22/game/%7B
-gameI
+i
 d%7D/move%22
@@ -2003,37 +2003,33 @@
 orm(get(%22/game/%7B
-gameI
+i
 d%7D/board%22, gameI

rename id