Browse Source

add createGame-Method and Test

feat-serverGameRepresentation
Steffen Nitschke 4 years ago
parent
commit
373b63dc53
  1. 40
      fh.fd.ci.server/src/main/java/de/fd/fh/server/game/web/GameController.java
  2. 44
      fh.fd.ci.server/src/test/java/de/fd/fh/server/game/web/GameControllerTest.java
  3. 14
      fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/CreateGameRequest.java

40
fh.fd.ci.server/src/main/java/de/fd/fh/server/game/web/GameController.java

@ -0,0 +1,40 @@
package de.fd.fh.server.game.web;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.fd.fh.server.game.GameId;
import de.fd.fh.server.game.GameService;
import de.fd.fh.server.user.UserId;
import de.fd.fh.shared.network.messages.CreateGameRequest;
import lombok.RequiredArgsConstructor;
import spark.Request;
import spark.Response;
import java.io.IOException;
@RequiredArgsConstructor
public class GameController
{
private final ObjectMapper objectMapper = new ObjectMapper();
private final GameService service;
public void createGame(final Request request, final Response response) throws IOException
{
final CreateGameRequest createRequest = objectMapper.readValue(request.body(), CreateGameRequest.class);
final UserId whiteUser = UserId.of(createRequest.getWhiteUserId());
final UserId blackUser = UserId.of(createRequest.getBlackUserId());
final GameId gameId = service.createGame(blackUser, whiteUser);
if (gameId == null)
{
response.status(400);
}
else
{
response.body(objectMapper.writeValueAsString(gameId));
response.status(201);
}
}
}

44
fh.fd.ci.server/src/test/java/de/fd/fh/server/game/web/GameControllerTest.java

@ -1,17 +1,20 @@
package de.fd.fh.server.game.web; package de.fd.fh.server.game.web;
import de.fd.fh.server.access.AccessToken;
import de.fd.fh.server.game.GameId; import de.fd.fh.server.game.GameId;
import de.fd.fh.server.game.GameService; import de.fd.fh.server.game.GameService;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import spark.Request; import spark.Request;
import spark.Response; import spark.Response;
import spark.Session;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
public class GameControllerTest public class GameControllerTest
@ -21,8 +24,6 @@ public class GameControllerTest
@Mock @Mock
Response response; Response response;
@Mock @Mock
Session session;
@Mock
GameService service; GameService service;
@BeforeEach @BeforeEach
@ -32,12 +33,39 @@ public class GameControllerTest
} }
@Test @Test
void testCreateGameTest()
void testCreateGame() throws IOException
{ {
when(request.body()).thenReturn("{\"userName\":\"TestName\",\"password\":\"TestPassword\"}");
when(request.session()).thenReturn(session);
when(service.createGame(any())).thenReturn(GameId.random());
when(request.body()).thenReturn("{\"whiteUserId\":\"12345\",\"blackUserId\":\"98765\"}");
when(service.createGame(any(), any())).thenReturn(GameId.of("56789"));
ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new GameController(service).createGame(request, response); new GameController(service).createGame(request, response);
verify(response).body(bodyCaptor.capture());
verify(response).status(statusCaptor.capture());
assertNotNull(bodyCaptor.getValue());
assertTrue(bodyCaptor.getValue().contains("56789"));
assertNotNull(statusCaptor.getValue());
assertEquals(201, statusCaptor.getValue());
}
@Test
void testFailedCreateGame() throws IOException
{
when(request.body()).thenReturn("{\"whiteUserId\":\"12345\",\"blackUserId\":\"98765\"}");
when(service.createGame(any(), any())).thenReturn(null);
ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new GameController(service).createGame(request, response);
verify(response).status(statusCaptor.capture());
assertNotNull(statusCaptor.getValue());
assertEquals(400, statusCaptor.getValue());
} }
} }

14
fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/CreateGameRequest.java

@ -0,0 +1,14 @@
package de.fd.fh.shared.network.messages;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class CreateGameRequest
{
private String whiteUserId;
private String blackUserId;
}
Loading…
Cancel
Save