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.junit.Assert.assertTrue;
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/{id}/board", gameId))
                .andExpect(status().isNotFound());
    }

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

        mockMvc.perform(put("/game/{id}", 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/{id}", gameId)).andExpect(status().isCreated());

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

        mockMvc.perform(post("/game/{id}/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/{id}/board", gameId))
                .andExpect(status().isOk())
                .andReturn();
        ChessBoardView response = fromJson(result, ChessBoardView.class);
        assertEquals(new ChessBoardView(expected), response);
    }

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

}

@@ -83,16 +83,41 @@
 n.Move;%0A
+import org.junit.Assert;%0A
 import o
@@ -133,16 +133,16 @@
 Before;%0A
-
 import o
@@ -275,16 +275,113 @@
 esult;%0A%0A
+import static org.hamcrest.CoreMatchers.is;%0Aimport static org.hamcrest.MatcherAssert.assertThat;%0A
 import s
@@ -421,51 +421,8 @@
 ls;%0A
-import static org.junit.Assert.assertTrue;%0A
 impo
@@ -1471,37 +1471,48 @@
 ;%0A        assert
-Equal
+That(response, i
 s(new ChessBoard
@@ -1545,26 +1545,17 @@
 gameId))
-, response
+)
 );%0A    %7D
@@ -2296,32 +2296,33 @@
 ardView.class);%0A
+%0A
         assertEq
@@ -2319,21 +2319,32 @@
   assert
-Equal
+That(response, i
 s(new Ch
@@ -2369,18 +2369,9 @@
 ted)
-, response
+)
 );%0A 

use hamcrest