Browse Source

new GameRepository with findRunningGamesOfPlayer-Method and Tests

feat-serverGameRepresentation
Steffen Nitschke 4 years ago
parent
commit
3ba614577f
  1. 17
      fh.fd.ci.server/src/main/java/de/fd/fh/server/game/GameRepository.java
  2. 20
      fh.fd.ci.server/src/test/java/de/fd/fh/server/game/FieldTest.java
  3. 19
      fh.fd.ci.server/src/test/java/de/fd/fh/server/game/FigureTest.java
  4. 17
      fh.fd.ci.server/src/test/java/de/fd/fh/server/game/GameRepositoryTest.java

17
fh.fd.ci.server/src/main/java/de/fd/fh/server/game/GameRepository.java

@ -1,9 +1,13 @@
package de.fd.fh.server.game; package de.fd.fh.server.game;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
import de.fd.fh.server.user.UserId;
import dev.morphia.Datastore; import dev.morphia.Datastore;
import dev.morphia.Key; import dev.morphia.Key;
import dev.morphia.Morphia; import dev.morphia.Morphia;
import dev.morphia.query.Query;
import java.util.List;
public class GameRepository public class GameRepository
{ {
@ -11,7 +15,7 @@ public class GameRepository
public GameRepository() public GameRepository()
{ {
System.out.println("AccessRepo");
System.out.println("GameRepo");
final Morphia morphia = new Morphia(); final Morphia morphia = new Morphia();
morphia.mapPackage("de.fd.fh.server.game"); morphia.mapPackage("de.fd.fh.server.game");
@ -35,4 +39,15 @@ public class GameRepository
return datastore.createQuery(Game.class) return datastore.createQuery(Game.class)
.field("id.identifier").equal(gameId.getIdentifier()).first(); .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();
}
} }

20
fh.fd.ci.server/src/test/java/de/fd/fh/server/game/FieldTest.java

@ -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());
}
}

19
fh.fd.ci.server/src/test/java/de/fd/fh/server/game/FigureTest.java

@ -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());
}
}

17
fh.fd.ci.server/src/test/java/de/fd/fh/server/game/GameRepositoryTest.java

@ -12,6 +12,7 @@ import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -94,4 +95,20 @@ class GameRepositoryTest
assertEquals(2, result.getFields().size()); 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());
}
} }
Loading…
Cancel
Save