Steffen Nitschke
4 years ago
6 changed files with 117 additions and 10 deletions
-
2fh.fd.ci.server/src/main/java/de/fd/fh/server/game/Field.java
-
12fh.fd.ci.server/src/main/java/de/fd/fh/server/game/Figure.java
-
6fh.fd.ci.server/src/main/java/de/fd/fh/server/game/GameId.java
-
32fh.fd.ci.server/src/main/java/de/fd/fh/server/game/GameRepository.java
-
1fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserId.java
-
74fh.fd.ci.server/src/test/java/de/fd/fh/server/game/GameRepositoryTest.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); |
|||
} |
|||
} |
@ -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()); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue