From 1332efb29fc4b6e8473df634b3aca89224e47537 Mon Sep 17 00:00:00 2001 From: Steffen Nitschke Date: Mon, 1 Feb 2021 18:27:37 +0100 Subject: [PATCH] new GameRepository with Test --- .../main/java/de/fd/fh/server/game/Field.java | 2 +- .../java/de/fd/fh/server/game/Figure.java | 12 +-- .../java/de/fd/fh/server/game/GameId.java | 6 +- .../de/fd/fh/server/game/GameRepository.java | 32 ++++++++ .../java/de/fd/fh/server/user/UserId.java | 1 + .../fd/fh/server/game/GameRepositoryTest.java | 74 +++++++++++++++++++ 6 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 fh.fd.ci.server/src/main/java/de/fd/fh/server/game/GameRepository.java create mode 100644 fh.fd.ci.server/src/test/java/de/fd/fh/server/game/GameRepositoryTest.java diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/game/Field.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/game/Field.java index ee8ec3c..c1d8bbd 100644 --- a/fh.fd.ci.server/src/main/java/de/fd/fh/server/game/Field.java +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/server/game/Field.java @@ -18,7 +18,7 @@ public class Field @Embedded private Figure figure; - private enum Color + public enum Color { WHITE, BLACK } diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/game/Figure.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/game/Figure.java index 5b36193..a6cea0f 100644 --- a/fh.fd.ci.server/src/main/java/de/fd/fh/server/game/Figure.java +++ b/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 dev.morphia.annotations.Embedded; +import lombok.AllArgsConstructor; import lombok.Getter; -import lombok.RequiredArgsConstructor; +import lombok.NoArgsConstructor; -@RequiredArgsConstructor @Getter +@NoArgsConstructor +@AllArgsConstructor public class Figure { - private final Type type; + private Type type; @Embedded - private final UserId player; + private UserId player; - private enum Type + public enum Type { KING, QUEEN, BISHOP, KNIGHT, ROOK, PAWN } diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/game/GameId.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/game/GameId.java index 87b4b3a..568d74c 100644 --- a/fh.fd.ci.server/src/main/java/de/fd/fh/server/game/GameId.java +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/server/game/GameId.java @@ -1,14 +1,12 @@ package de.fd.fh.server.game; import dev.morphia.annotations.Embedded; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.EqualsAndHashCode; -import lombok.Getter; +import lombok.*; import org.bson.types.ObjectId; @Getter @Embedded +@NoArgsConstructor @AllArgsConstructor(access = AccessLevel.PRIVATE) @EqualsAndHashCode(of = {"identifier"}) public class GameId diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/game/GameRepository.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/game/GameRepository.java new file mode 100644 index 0000000..a66eb54 --- /dev/null +++ b/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 save(final Game access) + { + return datastore.save(access); + } +} diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserId.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserId.java index e6599af..d978069 100644 --- a/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserId.java +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserId.java @@ -6,6 +6,7 @@ import org.bson.types.ObjectId; @Getter @Embedded +@NoArgsConstructor @AllArgsConstructor(access = AccessLevel.PRIVATE) @EqualsAndHashCode(of = {"identifier"}) public class UserId diff --git a/fh.fd.ci.server/src/test/java/de/fd/fh/server/game/GameRepositoryTest.java b/fh.fd.ci.server/src/test/java/de/fd/fh/server/game/GameRepositoryTest.java new file mode 100644 index 0000000..a585d5b --- /dev/null +++ b/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 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 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()); + } +} \ No newline at end of file