Browse Source

new GameRepository with Test

feat-serverGameRepresentation
Steffen Nitschke 4 years ago
parent
commit
1332efb29f
  1. 2
      fh.fd.ci.server/src/main/java/de/fd/fh/server/game/Field.java
  2. 12
      fh.fd.ci.server/src/main/java/de/fd/fh/server/game/Figure.java
  3. 6
      fh.fd.ci.server/src/main/java/de/fd/fh/server/game/GameId.java
  4. 32
      fh.fd.ci.server/src/main/java/de/fd/fh/server/game/GameRepository.java
  5. 1
      fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserId.java
  6. 74
      fh.fd.ci.server/src/test/java/de/fd/fh/server/game/GameRepositoryTest.java

2
fh.fd.ci.server/src/main/java/de/fd/fh/server/game/Field.java

@ -18,7 +18,7 @@ public class Field
@Embedded @Embedded
private Figure figure; private Figure figure;
private enum Color
public enum Color
{ {
WHITE, BLACK WHITE, BLACK
} }

12
fh.fd.ci.server/src/main/java/de/fd/fh/server/game/Figure.java

@ -2,18 +2,20 @@ package de.fd.fh.server.game;
import de.fd.fh.server.user.UserId; import de.fd.fh.server.user.UserId;
import dev.morphia.annotations.Embedded; import dev.morphia.annotations.Embedded;
import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.NoArgsConstructor;
@RequiredArgsConstructor
@Getter @Getter
@NoArgsConstructor
@AllArgsConstructor
public class Figure public class Figure
{ {
private final Type type;
private Type type;
@Embedded @Embedded
private final UserId player;
private UserId player;
private enum Type
public enum Type
{ {
KING, QUEEN, BISHOP, KNIGHT, ROOK, PAWN KING, QUEEN, BISHOP, KNIGHT, ROOK, PAWN
} }

6
fh.fd.ci.server/src/main/java/de/fd/fh/server/game/GameId.java

@ -1,14 +1,12 @@
package de.fd.fh.server.game; package de.fd.fh.server.game;
import dev.morphia.annotations.Embedded; import dev.morphia.annotations.Embedded;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.*;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
@Getter @Getter
@Embedded @Embedded
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE) @AllArgsConstructor(access = AccessLevel.PRIVATE)
@EqualsAndHashCode(of = {"identifier"}) @EqualsAndHashCode(of = {"identifier"})
public class GameId public class GameId

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

@ -0,0 +1,32 @@
package de.fd.fh.server.game;
import com.mongodb.MongoClient;
import dev.morphia.Datastore;
import dev.morphia.Key;
import dev.morphia.Morphia;
public class GameRepository
{
private final Datastore datastore;
public GameRepository()
{
System.out.println("AccessRepo");
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);
}
}

1
fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserId.java

@ -6,6 +6,7 @@ import org.bson.types.ObjectId;
@Getter @Getter
@Embedded @Embedded
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE) @AllArgsConstructor(access = AccessLevel.PRIVATE)
@EqualsAndHashCode(of = {"identifier"}) @EqualsAndHashCode(of = {"identifier"})
public class UserId public class UserId

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

@ -0,0 +1,74 @@
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.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());
}
}
Loading…
Cancel
Save