Search in sources :

Example 21 with DocumentWriter

use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.

the class CodecStructureTest method evaluate.

private void evaluate(Document expected, Expression value) {
    DocumentWriter writer = new DocumentWriter(getMapper());
    document(writer, () -> {
        value.encode(getDs(), writer, EncoderContext.builder().build());
    });
    Document actual = writer.getDocument();
    assertDocumentEquals(actual, expected);
}
Also used : DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) Document(org.bson.Document)

Example 22 with DocumentWriter

use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.

the class TestBase method toDocument.

protected Document toDocument(Object entity) {
    final Class<?> type = getMapper().getEntityModel(entity.getClass()).getType();
    DocumentWriter writer = new DocumentWriter(getMapper());
    ((Codec) getDs().getCodecRegistry().get(type)).encode(writer, entity, EncoderContext.builder().build());
    return writer.getDocument();
}
Also used : Codec(org.bson.codecs.Codec) DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter)

Example 23 with DocumentWriter

use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.

the class ExpressionsTestBase method assertAndCheckDocShape.

@SuppressWarnings("unchecked")
protected void assertAndCheckDocShape(String expectedString, Expression value, Object expectedValue) {
    Document expected = Document.parse(expectedString);
    DocumentWriter writer = new DocumentWriter(getMapper());
    document(writer, () -> {
        value.encode(getDs(), writer, EncoderContext.builder().build());
    });
    Document actual = writer.getDocument();
    assertDocumentEquals(actual, expected);
    Document test = getDs().aggregate(User.class).project(Projection.project().include("test", value)).execute(Document.class).next();
    assertEquals(test.get("test"), expectedValue);
}
Also used : User(dev.morphia.test.models.User) DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) Document(org.bson.Document)

Aggregations

DocumentWriter (dev.morphia.mapping.codec.writer.DocumentWriter)23 Document (org.bson.Document)18 Test (org.testng.annotations.Test)9 Codec (org.bson.codecs.Codec)5 Datastore (dev.morphia.Datastore)3 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)2 OperationTarget (dev.morphia.query.OperationTarget)2 Filter (dev.morphia.query.experimental.filters.Filter)2 Sample (dev.morphia.aggregation.experimental.stages.Sample)1 Skip (dev.morphia.aggregation.experimental.stages.Skip)1 Stage (dev.morphia.aggregation.experimental.stages.Stage)1 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)1 PropertyHandler (dev.morphia.mapping.codec.pojo.PropertyHandler)1 ReferenceException (dev.morphia.mapping.lazy.proxy.ReferenceException)1 NearFilter (dev.morphia.query.experimental.filters.NearFilter)1 User (dev.morphia.test.models.User)1 ArrayList (java.util.ArrayList)1 EncoderContext (org.bson.codecs.EncoderContext)1 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)1