Search in sources :

Example 1 with SimpleModel

use of org.bson.codecs.pojo.entities.SimpleModel in project mongo-java-driver by mongodb.

the class PojoCodecProviderTest method testClassNotFound.

@Test
public void testClassNotFound() {
    PojoCodecProvider provider = PojoCodecProvider.builder().build();
    CodecRegistry registry = fromProviders(provider, new ValueCodecProvider());
    Codec<SimpleModel> codec = provider.get(SimpleModel.class, registry);
    assertNull(codec);
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) SimpleModel(org.bson.codecs.pojo.entities.SimpleModel) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Test(org.junit.Test)

Example 2 with SimpleModel

use of org.bson.codecs.pojo.entities.SimpleModel in project mongo-java-driver by mongodb.

the class PojoCodecCyclicalLookupTest method testSimpleModel.

@Test
void testSimpleModel() {
    SimpleModel model = getSimpleModel();
    LookupCountingCodecRegistry registry = createRegistry(SimpleModel.class);
    roundTrip(registry, model, SIMPLE_MODEL_JSON);
    // Looked up in encodesTo & decodesTo
    assertEquals(2, registry.counters.get(SimpleModel.class).get());
    // Lookup on encode then cached (PropertyCodecRegistry)
    assertEquals(1, registry.counters.get(String.class).get());
    // Lookup on encode then cached (PropertyCodecRegistry)
    assertEquals(1, registry.counters.get(Integer.class).get());
}
Also used : SimpleModel(org.bson.codecs.pojo.entities.SimpleModel) Test(org.junit.jupiter.api.Test)

Example 3 with SimpleModel

use of org.bson.codecs.pojo.entities.SimpleModel in project mongo-java-driver by mongodb.

the class PojoCodecProviderTest method testAutomatic.

@Test
public void testAutomatic() {
    PojoCodecProvider provider = PojoCodecProvider.builder().automatic(true).build();
    CodecRegistry registry = fromProviders(provider, new ValueCodecProvider());
    Codec<SimpleModel> codec = provider.get(SimpleModel.class, registry);
    assertNotNull(codec);
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) SimpleModel(org.bson.codecs.pojo.entities.SimpleModel) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Test(org.junit.Test)

Example 4 with SimpleModel

use of org.bson.codecs.pojo.entities.SimpleModel in project mongo-java-driver by mongodb.

the class PojoCustomTest method testCanHandleNullValuesForNestedModels.

@Test
@SuppressWarnings("unchecked")
public void testCanHandleNullValuesForNestedModels() {
    SimpleNestedPojoModel model = getSimpleNestedPojoModel();
    model.setSimple(null);
    ClassModelBuilder<SimpleNestedPojoModel> classModel = ClassModel.builder(SimpleNestedPojoModel.class);
    ((PropertyModelBuilder<SimpleModel>) classModel.getProperty("simple")).propertySerialization(new PropertySerialization<SimpleModel>() {

        @Override
        public boolean shouldSerialize(final SimpleModel value) {
            return true;
        }
    });
    ClassModelBuilder<SimpleModel> classModelSimple = ClassModel.builder(SimpleModel.class);
    roundTrip(getPojoCodecProviderBuilder(classModel, classModelSimple), model, "{'simple': null}");
}
Also used : SimpleModel(org.bson.codecs.pojo.entities.SimpleModel) SimpleNestedPojoModel(org.bson.codecs.pojo.entities.SimpleNestedPojoModel) Test(org.junit.Test)

Example 5 with SimpleModel

use of org.bson.codecs.pojo.entities.SimpleModel in project mongo-java-driver by mongodb.

the class PojoCustomTest method testCustomPropertySerializer.

@Test
@SuppressWarnings("unchecked")
public void testCustomPropertySerializer() {
    SimpleModel model = getSimpleModel();
    model.setIntegerField(null);
    ClassModelBuilder<SimpleModel> classModel = ClassModel.builder(SimpleModel.class);
    ((PropertyModelBuilder<Integer>) classModel.getProperty("integerField")).propertySerialization(new PropertySerialization<Integer>() {

        @Override
        public boolean shouldSerialize(final Integer value) {
            return true;
        }
    });
    roundTrip(getPojoCodecProviderBuilder(classModel), model, "{'integerField': null, 'stringField': 'myString'}");
}
Also used : SimpleModel(org.bson.codecs.pojo.entities.SimpleModel) Test(org.junit.Test)

Aggregations

SimpleModel (org.bson.codecs.pojo.entities.SimpleModel)9 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)2 Arrays.asList (java.util.Arrays.asList)2 Collections.singletonList (java.util.Collections.singletonList)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ValueCodecProvider (org.bson.codecs.ValueCodecProvider)2 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 CollectionNestedPojoModel (org.bson.codecs.pojo.entities.CollectionNestedPojoModel)1 ConventionModel (org.bson.codecs.pojo.entities.ConventionModel)1 GenericHolderModel (org.bson.codecs.pojo.entities.GenericHolderModel)1 NestedGenericHolderModel (org.bson.codecs.pojo.entities.NestedGenericHolderModel)1 NestedGenericHolderSimpleGenericsModel (org.bson.codecs.pojo.entities.NestedGenericHolderSimpleGenericsModel)1 NestedSelfReferentialGenericHolderModel (org.bson.codecs.pojo.entities.NestedSelfReferentialGenericHolderModel)1