package leigh.mecha.db.mongo;

import com.mongodb.client.*;
import leigh.eo.EO;
import leigh.eo.mongo.EOBsonSerializer;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import org.bson.Document;
import org.junit.Test;

import java.util.UUID;

public class MongoTest {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(MongoTest.class);

    @Test
    public void connectTest() {
        MongoClient client = MongoClients.create("mongodb://10.52.4.61");
        MongoDatabase db = client.getDatabase("junit");
        String colName = UUID.randomUUID().toString();
        MongoCollection<Document> col = db.getCollection(colName);

        EO obj = new EO("junit.test");
        obj.setValue("firstName", "Nicole");
        obj.setValue("lastName", "Windon");
        obj.setLabel("firstName", "Nikki");

        col.insertOne(EOBsonSerializer.eoToDoc(obj));

        FindIterable<Document> iter = col.find();
        for(Document eoDoc : iter) {
            EO readBack = EOBsonSerializer.docToEo(eoDoc);
            logger.info("Read-back EO: {}", readBack);
        }

        db.drop();
    }
}
