35 Commits
0621de40f3
...
92568740e8
17 changed files with 835 additions and 2 deletions
-
15fh.fd.ci.server/src/main/java/de/fd/fh/ServerApp.java
-
25fh.fd.ci.server/src/main/java/de/fd/fh/server/game/Field.java
-
22fh.fd.ci.server/src/main/java/de/fd/fh/server/game/Figure.java
-
29fh.fd.ci.server/src/main/java/de/fd/fh/server/game/Game.java
-
25fh.fd.ci.server/src/main/java/de/fd/fh/server/game/GameId.java
-
53fh.fd.ci.server/src/main/java/de/fd/fh/server/game/GameRepository.java
-
110fh.fd.ci.server/src/main/java/de/fd/fh/server/game/GameService.java
-
83fh.fd.ci.server/src/main/java/de/fd/fh/server/game/web/GameController.java
-
2fh.fd.ci.server/src/main/java/de/fd/fh/server/user/User.java
-
1fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserId.java
-
52fh.fd.ci.server/src/test/java/de/fd/fh/ServerAppTest.java
-
20fh.fd.ci.server/src/test/java/de/fd/fh/server/game/FieldTest.java
-
19fh.fd.ci.server/src/test/java/de/fd/fh/server/game/FigureTest.java
-
114fh.fd.ci.server/src/test/java/de/fd/fh/server/game/GameRepositoryTest.java
-
107fh.fd.ci.server/src/test/java/de/fd/fh/server/game/GameServiceTest.java
-
146fh.fd.ci.server/src/test/java/de/fd/fh/server/game/web/GameControllerTest.java
-
14fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/CreateGameRequest.java
@ -0,0 +1,25 @@ |
|||
package de.fd.fh.server.game; |
|||
|
|||
import dev.morphia.annotations.Embedded; |
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Getter; |
|||
import lombok.NoArgsConstructor; |
|||
|
|||
@NoArgsConstructor |
|||
@AllArgsConstructor |
|||
@Getter |
|||
public class Field |
|||
{ |
|||
private int x; |
|||
|
|||
private int y; |
|||
|
|||
private Color color; |
|||
@Embedded |
|||
private Figure figure; |
|||
|
|||
public enum Color |
|||
{ |
|||
WHITE, BLACK |
|||
} |
|||
} |
@ -0,0 +1,22 @@ |
|||
package de.fd.fh.server.game; |
|||
|
|||
import de.fd.fh.server.user.UserId; |
|||
import dev.morphia.annotations.Embedded; |
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Getter; |
|||
import lombok.NoArgsConstructor; |
|||
|
|||
@Getter |
|||
@NoArgsConstructor |
|||
@AllArgsConstructor |
|||
public class Figure |
|||
{ |
|||
private Type type; |
|||
@Embedded |
|||
private UserId player; |
|||
|
|||
public enum Type |
|||
{ |
|||
KING, QUEEN, BISHOP, KNIGHT, ROOK, PAWN |
|||
} |
|||
} |
@ -0,0 +1,29 @@ |
|||
package de.fd.fh.server.game; |
|||
|
|||
import de.fd.fh.server.user.UserId; |
|||
import dev.morphia.annotations.Embedded; |
|||
import dev.morphia.annotations.Id; |
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Getter; |
|||
import lombok.NoArgsConstructor; |
|||
|
|||
import java.util.Set; |
|||
|
|||
@NoArgsConstructor |
|||
@AllArgsConstructor |
|||
@Getter |
|||
public class Game |
|||
{ |
|||
@Id |
|||
private GameId id; |
|||
@Embedded |
|||
private UserId whitePlayer; |
|||
@Embedded |
|||
private UserId blackPlayer; |
|||
@Embedded |
|||
private UserId currentPlayer; |
|||
@Embedded |
|||
private Set<Field> fields; |
|||
@Embedded |
|||
private UserId winner; |
|||
} |
@ -0,0 +1,25 @@ |
|||
package de.fd.fh.server.game; |
|||
|
|||
import dev.morphia.annotations.Embedded; |
|||
import lombok.*; |
|||
import org.bson.types.ObjectId; |
|||
|
|||
@Getter |
|||
@Embedded |
|||
@NoArgsConstructor |
|||
@AllArgsConstructor(access = AccessLevel.PRIVATE) |
|||
@EqualsAndHashCode(of = {"identifier"}) |
|||
public class GameId |
|||
{ |
|||
private String identifier; |
|||
|
|||
public static GameId of(final String identifier) |
|||
{ |
|||
return new GameId(identifier); |
|||
} |
|||
|
|||
public static GameId random() |
|||
{ |
|||
return new GameId(new ObjectId().toHexString()); |
|||
} |
|||
} |
@ -0,0 +1,53 @@ |
|||
package de.fd.fh.server.game; |
|||
|
|||
import com.mongodb.MongoClient; |
|||
import de.fd.fh.server.user.UserId; |
|||
import dev.morphia.Datastore; |
|||
import dev.morphia.Key; |
|||
import dev.morphia.Morphia; |
|||
import dev.morphia.query.Query; |
|||
|
|||
import java.util.List; |
|||
|
|||
public class GameRepository |
|||
{ |
|||
private final Datastore datastore; |
|||
|
|||
public GameRepository() |
|||
{ |
|||
System.out.println("GameRepo"); |
|||
final Morphia morphia = new Morphia(); |
|||
|
|||
morphia.mapPackage("de.fd.fh.server.game"); |
|||
|
|||
this.datastore = morphia.createDatastore(new MongoClient(), "smartwarfare"); |
|||
datastore.ensureIndexes(); |
|||
} |
|||
|
|||
GameRepository(final Datastore datastore) |
|||
{ |
|||
this.datastore = datastore; |
|||
} |
|||
|
|||
public Key<Game> save(final Game access) |
|||
{ |
|||
return datastore.save(access); |
|||
} |
|||
|
|||
Game findByGameId(final GameId gameId) |
|||
{ |
|||
return datastore.createQuery(Game.class) |
|||
.field("id.identifier").equal(gameId.getIdentifier()).first(); |
|||
} |
|||
|
|||
List<Game> findRunningGamesOfPlayer(final UserId userId) |
|||
{ |
|||
final Query<Game> query = datastore.createQuery(Game.class); |
|||
query.or( |
|||
query.criteria("whitePlayer.identifier").equal(userId.getIdentifier()), |
|||
query.criteria("blackPlayer.identifier").equal(userId.getIdentifier())); |
|||
query.and(query.criteria("winner").doesNotExist()); |
|||
|
|||
return query.find().toList(); |
|||
} |
|||
} |
@ -0,0 +1,110 @@ |
|||
package de.fd.fh.server.game; |
|||
|
|||
import de.fd.fh.server.user.UserId; |
|||
import lombok.RequiredArgsConstructor; |
|||
|
|||
import java.util.HashSet; |
|||
import java.util.List; |
|||
import java.util.Set; |
|||
|
|||
@RequiredArgsConstructor |
|||
public class GameService |
|||
{ |
|||
private final GameRepository repository; |
|||
|
|||
public GameId createGame(final Game game) |
|||
{ |
|||
return (GameId) repository.save(game).getId(); |
|||
} |
|||
|
|||
public GameId createGame(final UserId blackUser, final UserId whiteUser) |
|||
{ |
|||
final Set<Field> fields = initFields(blackUser, whiteUser); |
|||
|
|||
final Game game = new Game(null, whiteUser, blackUser, whiteUser, fields, null); |
|||
|
|||
return (GameId) repository.save(game).getId(); |
|||
} |
|||
|
|||
public Game findGameById(final GameId gameId) |
|||
{ |
|||
return repository.findByGameId(gameId); |
|||
} |
|||
|
|||
public List<Game> findRunningGamesOfUser(final UserId userId) |
|||
{ |
|||
return repository.findRunningGamesOfPlayer(userId); |
|||
} |
|||
|
|||
private Set<Field> initFields(UserId blackUser, UserId whiteUser) |
|||
{ |
|||
final Set<Field> fields = new HashSet<>(); |
|||
for (int x = 0; x < 8; x++) |
|||
{ |
|||
for (int y = 0; y < 8; y++) |
|||
{ |
|||
fields.add(new Field(x, y, getColor(x, y) , getFigure(x, y, whiteUser, blackUser))); |
|||
} |
|||
} |
|||
return fields; |
|||
} |
|||
|
|||
private Field.Color getColor(final int x, final int y) |
|||
{ |
|||
return (x + y) % 2 != 1 ? Field.Color.WHITE : Field.Color.BLACK; |
|||
} |
|||
|
|||
private Figure getFigure(final int x, final int y, final UserId whitePlayer, final UserId blackPlayer) |
|||
{ |
|||
if (x == 0) |
|||
{ |
|||
return getFigureOfGroundLevel(y, whitePlayer); |
|||
} |
|||
|
|||
if (x == 1) |
|||
{ |
|||
return new Figure(Figure.Type.PAWN, whitePlayer); |
|||
} |
|||
|
|||
if (x == 7) |
|||
{ |
|||
return getFigureOfGroundLevel(y, blackPlayer); |
|||
} |
|||
|
|||
if (x == 6) |
|||
{ |
|||
return new Figure(Figure.Type.PAWN, blackPlayer); |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
private Figure getFigureOfGroundLevel(final int y, final UserId player) |
|||
{ |
|||
if (y == 0 || y == 7) |
|||
{ |
|||
return new Figure(Figure.Type.ROOK, player); |
|||
} |
|||
|
|||
if (y == 1 || y == 6) |
|||
{ |
|||
return new Figure(Figure.Type.KNIGHT, player); |
|||
} |
|||
|
|||
if (y == 2 || y == 5) |
|||
{ |
|||
return new Figure(Figure.Type.BISHOP, player); |
|||
} |
|||
|
|||
if (y == 3) |
|||
{ |
|||
return new Figure(Figure.Type.QUEEN, player); |
|||
} |
|||
|
|||
if (y == 4) |
|||
{ |
|||
return new Figure(Figure.Type.KING, player); |
|||
} |
|||
return null; |
|||
} |
|||
} |
@ -0,0 +1,83 @@ |
|||
package de.fd.fh.server.game.web; |
|||
|
|||
import com.fasterxml.jackson.core.JsonProcessingException; |
|||
import com.fasterxml.jackson.databind.ObjectMapper; |
|||
import de.fd.fh.server.game.Game; |
|||
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; |
|||
import java.util.List; |
|||
|
|||
@RequiredArgsConstructor |
|||
public class GameController |
|||
{ |
|||
private final ObjectMapper objectMapper = new ObjectMapper(); |
|||
|
|||
private final GameService service; |
|||
|
|||
public Response 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); |
|||
} |
|||
|
|||
return response; |
|||
} |
|||
|
|||
public Response findGameById(final Request request, final Response response) throws JsonProcessingException |
|||
{ |
|||
final GameId gameId = GameId.of(request.params(":game_id")); |
|||
|
|||
final Game game = service.findGameById(gameId); |
|||
|
|||
if (game == null) |
|||
{ |
|||
response.status(404); |
|||
} |
|||
else |
|||
{ |
|||
response.body(objectMapper.writeValueAsString(game)); |
|||
response.status(200); |
|||
} |
|||
|
|||
return response; |
|||
} |
|||
|
|||
public Response findRunningGamesOfUser(final Request request, final Response response) throws JsonProcessingException |
|||
{ |
|||
final UserId userId = UserId.of(request.params(":user_id")); |
|||
|
|||
final List<Game> games = service.findRunningGamesOfUser(userId); |
|||
|
|||
if (games == null) |
|||
{ |
|||
response.status(404); |
|||
} |
|||
else |
|||
{ |
|||
response.body(objectMapper.writeValueAsString(games)); |
|||
response.status(200); |
|||
} |
|||
|
|||
return response; |
|||
} |
|||
} |
@ -0,0 +1,20 @@ |
|||
package de.fd.fh.server.game; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
|
|||
class FieldTest |
|||
{ |
|||
@Test |
|||
void fieldInitTest() |
|||
{ |
|||
final Field field = new Field(1, 2, Field.Color.WHITE, new Figure()); |
|||
|
|||
assertEquals(1, field.getX()); |
|||
assertEquals(2, field.getY()); |
|||
assertEquals(Field.Color.WHITE, field.getColor()); |
|||
assertEquals("WHITE", field.getColor().name()); |
|||
assertNotNull(field.getFigure()); |
|||
} |
|||
} |
@ -0,0 +1,19 @@ |
|||
package de.fd.fh.server.game; |
|||
|
|||
import de.fd.fh.server.user.UserId; |
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
|
|||
class FigureTest |
|||
{ |
|||
@Test |
|||
void figureInitTest() |
|||
{ |
|||
final Figure figure = new Figure(Figure.Type.KING, UserId.of("12345")); |
|||
|
|||
assertEquals(UserId.of("12345"), figure.getPlayer()); |
|||
assertEquals(Figure.Type.KING, figure.getType()); |
|||
assertEquals("KING", figure.getType().name()); |
|||
} |
|||
} |
@ -0,0 +1,114 @@ |
|||
package de.fd.fh.server.game; |
|||
|
|||
import com.mongodb.MongoClient; |
|||
import com.mongodb.client.MongoDatabase; |
|||
import de.fd.fh.server.user.UserId; |
|||
import dev.morphia.Datastore; |
|||
import dev.morphia.Key; |
|||
import dev.morphia.Morphia; |
|||
import dev.morphia.mapping.MappedClass; |
|||
import org.junit.jupiter.api.AfterAll; |
|||
import org.junit.jupiter.api.BeforeAll; |
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import java.util.HashSet; |
|||
import java.util.List; |
|||
import java.util.Set; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.assertEquals; |
|||
import static org.junit.jupiter.api.Assertions.assertNotNull; |
|||
|
|||
class GameRepositoryTest |
|||
{ |
|||
public static final String DB_NAME = "db_for_morphia"; |
|||
|
|||
private static MongoClient mongo; |
|||
private static MongoDatabase db; |
|||
private static Datastore ds; |
|||
private static final Morphia morphia = new Morphia(); |
|||
|
|||
private static GameRepository repository; |
|||
|
|||
// @BeforeAll |
|||
public static void beforeClass() { |
|||
mongo = new MongoClient(); |
|||
db = mongo.getDatabase(DB_NAME); |
|||
ds = morphia.createDatastore(mongo, db.getName()); |
|||
repository = new GameRepository(ds); |
|||
} |
|||
|
|||
// @AfterAll |
|||
public static void afterClass() { |
|||
cleanup(); |
|||
mongo.close(); |
|||
} |
|||
|
|||
private static void cleanup() { |
|||
for (final MappedClass mc : morphia.getMapper().getMappedClasses()) { |
|||
db.getCollection(mc.getCollectionName()).drop(); |
|||
} |
|||
} |
|||
|
|||
|
|||
// @Test |
|||
void saveTest() |
|||
{ |
|||
final Set<Field> fields = new HashSet<>(); |
|||
fields.add(new Field(0, 0, Field.Color.BLACK, new Figure(Figure.Type.BISHOP, UserId.of("12345")))); |
|||
fields.add(new Field(0, 2, Field.Color.WHITE, new Figure(Figure.Type.BISHOP, UserId.of("98765")))); |
|||
|
|||
final Game game = new Game(GameId.random(), UserId.of("12345"), UserId.of("98765"), UserId.of("12345"), fields, UserId.of("12345")); |
|||
|
|||
final Key<Game> key = repository.save(game); |
|||
|
|||
assertEquals(Game.class, key.getType()); |
|||
final Game result = ds.find(key.getType()).filter("id", key.getId()).first(); |
|||
|
|||
assertNotNull(result); |
|||
assertEquals("12345", result.getWhitePlayer().getIdentifier()); |
|||
assertEquals("12345", result.getCurrentPlayer().getIdentifier()); |
|||
assertEquals("98765", result.getBlackPlayer().getIdentifier()); |
|||
assertEquals("12345", result.getWinner().getIdentifier()); |
|||
assertNotNull(result.getId()); |
|||
assertEquals(2, result.getFields().size()); |
|||
} |
|||
|
|||
// @Test |
|||
void findByGameIdTest() |
|||
{ |
|||
final Set<Field> fields = new HashSet<>(); |
|||
fields.add(new Field(0, 0, Field.Color.BLACK, new Figure(Figure.Type.BISHOP, UserId.of("12345")))); |
|||
fields.add(new Field(0, 2, Field.Color.WHITE, new Figure(Figure.Type.BISHOP, UserId.of("98765")))); |
|||
|
|||
final Game game = new Game(GameId.of("4242"), UserId.of("12345"), UserId.of("98765"), UserId.of("12345"), fields, UserId.of("12345")); |
|||
|
|||
ds.save(game); |
|||
|
|||
final Game result = repository.findByGameId(GameId.of("4242")); |
|||
|
|||
assertNotNull(result); |
|||
assertEquals("12345", result.getWhitePlayer().getIdentifier()); |
|||
assertEquals("12345", result.getCurrentPlayer().getIdentifier()); |
|||
assertEquals("98765", result.getBlackPlayer().getIdentifier()); |
|||
assertEquals("12345", result.getWinner().getIdentifier()); |
|||
assertNotNull(result.getId()); |
|||
assertEquals(2, result.getFields().size()); |
|||
} |
|||
|
|||
// @Test |
|||
void getRunningGamesOfPlayerTest() |
|||
{ |
|||
final Set<Field> fields = new HashSet<>(); |
|||
fields.add(new Field(0, 0, Field.Color.BLACK, new Figure(Figure.Type.BISHOP, UserId.of("12345")))); |
|||
fields.add(new Field(0, 2, Field.Color.WHITE, new Figure(Figure.Type.BISHOP, UserId.of("98765")))); |
|||
|
|||
final Game game = new Game(GameId.of("4242"), UserId.of("12345"), UserId.of("98765"), UserId.of("12345"), fields, null); |
|||
|
|||
ds.save(game); |
|||
|
|||
final List<Game> result = repository.findRunningGamesOfPlayer(UserId.of("98765")); |
|||
|
|||
assertNotNull(result); |
|||
assertEquals(1, result.size()); |
|||
} |
|||
} |
@ -0,0 +1,107 @@ |
|||
package de.fd.fh.server.game; |
|||
|
|||
import de.fd.fh.server.user.UserId; |
|||
import dev.morphia.Key; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.mockito.ArgumentCaptor; |
|||
|
|||
import java.util.List; |
|||
import java.util.Set; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
import static org.mockito.ArgumentMatchers.any; |
|||
import static org.mockito.BDDMockito.then; |
|||
import static org.mockito.Mockito.*; |
|||
|
|||
class GameServiceTest |
|||
{ |
|||
@Test |
|||
void testCreateGame() |
|||
{ |
|||
final GameRepository repository = when(mock(GameRepository.class).save(any())) |
|||
.thenReturn(new Key<>(Game.class, "testCollection", GameId.of("98765"))) |
|||
.getMock(); |
|||
|
|||
final Game game = new Game(); |
|||
|
|||
final GameId result = new GameService(repository).createGame(game); |
|||
|
|||
assertNotNull(result); |
|||
assertEquals("98765", result.getIdentifier()); |
|||
} |
|||
|
|||
@Test |
|||
void given_twoUserIds_when_initGame_should_initGame() |
|||
{ |
|||
final UserId blackPlayer = UserId.of("12345"); |
|||
final UserId whitePlayer = UserId.of("98765"); |
|||
|
|||
final GameRepository repository = when(mock(GameRepository.class).save(any())) |
|||
.thenReturn(new Key<>(Game.class, "testCollection", GameId.of("4242"))) |
|||
.getMock(); |
|||
|
|||
final ArgumentCaptor<Game> captor = ArgumentCaptor.forClass(Game.class); |
|||
|
|||
final GameId result = new GameService(repository).createGame(blackPlayer, whitePlayer); |
|||
|
|||
verify(repository).save(captor.capture()); |
|||
|
|||
assertEquals("4242", result.getIdentifier()); |
|||
|
|||
assertNotNull(captor.getValue()); |
|||
|
|||
final Game savedGame = captor.getValue(); |
|||
|
|||
assertEquals("12345", savedGame.getBlackPlayer().getIdentifier()); |
|||
assertEquals("98765", savedGame.getWhitePlayer().getIdentifier()); |
|||
assertEquals("98765", savedGame.getCurrentPlayer().getIdentifier()); |
|||
assertEquals(64, savedGame.getFields().size()); |
|||
|
|||
final Set<Field> fields = savedGame.getFields(); |
|||
|
|||
fields.forEach(field -> { |
|||
|
|||
final Field.Color expected = (field.getX() + field.getY()) % 2 != 1 ? Field.Color.WHITE : Field.Color.BLACK; |
|||
|
|||
if (field.getX() == 0 || field.getX() == 1 || field.getX() == 6 || field.getX() == 7) |
|||
{ |
|||
assertNotNull(field.getFigure()); |
|||
} |
|||
else |
|||
{ |
|||
assertNull(field.getFigure()); |
|||
} |
|||
|
|||
assertEquals(expected, field.getColor()); |
|||
}); |
|||
|
|||
then(repository).should().save(any(Game.class)); |
|||
then(repository).shouldHaveNoMoreInteractions(); |
|||
} |
|||
|
|||
@Test |
|||
void given_storedGame_when_findGameById_should_returnGame() |
|||
{ |
|||
final GameRepository repository = when(mock(GameRepository.class).findByGameId(any(GameId.class))) |
|||
.thenReturn(new Game(null, null, null, null, null, null)) |
|||
.getMock(); |
|||
|
|||
final Game result = new GameService(repository).findGameById(GameId.of("12345")); |
|||
|
|||
assertNotNull(result); |
|||
} |
|||
|
|||
@Test |
|||
void given_storedGames_when_findGamesOfUser_should_returnSetOfGames() |
|||
{ |
|||
final GameRepository repository = when(mock(GameRepository.class).findRunningGamesOfPlayer(any(UserId.class))) |
|||
.thenReturn(List.of( |
|||
new Game(GameId.random(), UserId.of("12345"), UserId.random(), null, null, null), |
|||
new Game(GameId.random(), UserId.random(), UserId.of("12345"), null, null, null))) |
|||
.getMock(); |
|||
|
|||
final List<Game> result = new GameService(repository).findRunningGamesOfUser(UserId.of("12345")); |
|||
|
|||
assertEquals(2, result.size()); |
|||
} |
|||
} |
@ -0,0 +1,146 @@ |
|||
package de.fd.fh.server.game.web; |
|||
|
|||
import com.fasterxml.jackson.core.JsonProcessingException; |
|||
import de.fd.fh.server.game.Game; |
|||
import de.fd.fh.server.game.GameId; |
|||
import de.fd.fh.server.game.GameService; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.mockito.ArgumentCaptor; |
|||
import org.mockito.Mock; |
|||
import org.mockito.MockitoAnnotations; |
|||
import spark.Request; |
|||
import spark.Response; |
|||
|
|||
import java.io.IOException; |
|||
import java.util.List; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
import static org.mockito.ArgumentMatchers.any; |
|||
import static org.mockito.Mockito.verify; |
|||
import static org.mockito.Mockito.when; |
|||
|
|||
public class GameControllerTest |
|||
{ |
|||
@Mock |
|||
Request request; |
|||
@Mock |
|||
Response response; |
|||
@Mock |
|||
GameService service; |
|||
|
|||
@BeforeEach |
|||
void before() |
|||
{ |
|||
MockitoAnnotations.openMocks(this); |
|||
} |
|||
|
|||
@Test |
|||
void testCreateGame() throws IOException |
|||
{ |
|||
when(request.body()).thenReturn("{\"whiteUserId\":\"12345\",\"blackUserId\":\"98765\"}"); |
|||
when(service.createGame(any(), any())).thenReturn(GameId.of("56789")); |
|||
|
|||
final ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class); |
|||
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class); |
|||
|
|||
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); |
|||
|
|||
final 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()); |
|||
} |
|||
|
|||
@Test |
|||
void testFindGameById() throws IOException |
|||
{ |
|||
when(request.params(any())).thenReturn("12345"); |
|||
when(service.findGameById(any())).thenReturn(new Game()); |
|||
|
|||
final ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class); |
|||
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class); |
|||
|
|||
new GameController(service).findGameById(request, response); |
|||
|
|||
verify(response).body(bodyCaptor.capture()); |
|||
verify(response).status(statusCaptor.capture()); |
|||
|
|||
assertNotNull(bodyCaptor.getValue()); |
|||
|
|||
assertNotNull(statusCaptor.getValue()); |
|||
assertEquals(200, statusCaptor.getValue()); |
|||
} |
|||
|
|||
@Test |
|||
void testFailedFindGameById() throws IOException |
|||
{ |
|||
when(request.params(any())).thenReturn("12345"); |
|||
when(service.findGameById(any())).thenReturn(null); |
|||
|
|||
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class); |
|||
|
|||
new GameController(service).findGameById(request, response); |
|||
|
|||
verify(response).status(statusCaptor.capture()); |
|||
|
|||
assertNotNull(statusCaptor.getValue()); |
|||
assertEquals(404, statusCaptor.getValue()); |
|||
} |
|||
|
|||
@Test |
|||
void testFindRunningGamesOfUser() throws JsonProcessingException |
|||
{ |
|||
when(request.params(any())).thenReturn("12345"); |
|||
when(service.findRunningGamesOfUser(any())).thenReturn(List.of(new Game(), new Game())); |
|||
|
|||
final ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class); |
|||
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class); |
|||
|
|||
new GameController(service).findRunningGamesOfUser(request, response); |
|||
|
|||
verify(response).body(bodyCaptor.capture()); |
|||
verify(response).status(statusCaptor.capture()); |
|||
|
|||
assertNotNull(bodyCaptor.getValue()); |
|||
|
|||
assertNotNull(statusCaptor.getValue()); |
|||
assertEquals(200, statusCaptor.getValue()); |
|||
} |
|||
|
|||
@Test |
|||
void testFailedFindRunningGamesOfUser() throws JsonProcessingException |
|||
{ |
|||
when(request.params(any())).thenReturn("12345"); |
|||
when(service.findRunningGamesOfUser(any())).thenReturn(null); |
|||
|
|||
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class); |
|||
|
|||
new GameController(service).findRunningGamesOfUser(request, response); |
|||
|
|||
verify(response).status(statusCaptor.capture()); |
|||
|
|||
assertNotNull(statusCaptor.getValue()); |
|||
assertEquals(404, statusCaptor.getValue()); |
|||
} |
|||
} |
@ -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; |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue