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