package lc.mecha.db.mongo;

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

import java.util.UUID;

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

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