Search in sources :

Example 1 with SimpleGenericsModel

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

the class ClassModelBuilderTest method testDefaults.

@Test
public void testDefaults() {
    Class<SimpleGenericsModel> clazz = SimpleGenericsModel.class;
    ClassModelBuilder<SimpleGenericsModel> builder = ClassModel.builder(clazz);
    assertEquals(4, builder.getPropertyModelBuilders().size());
    for (Field field : clazz.getDeclaredFields()) {
        assertEquals(field.getName(), builder.getProperty(field.getName()).getWriteName());
    }
    Map<String, TypeParameterMap> fieldNameToTypeParameterMap = new HashMap<String, TypeParameterMap>();
    fieldNameToTypeParameterMap.put("myIntegerField", TypeParameterMap.builder().build());
    fieldNameToTypeParameterMap.put("myGenericField", TypeParameterMap.builder().addIndex(0).build());
    fieldNameToTypeParameterMap.put("myListField", TypeParameterMap.builder().addIndex(0, 1).build());
    fieldNameToTypeParameterMap.put("myMapField", TypeParameterMap.builder().addIndex(0, TypeParameterMap.builder().build()).addIndex(1, 2).build());
    assertEquals(fieldNameToTypeParameterMap, builder.getPropertyNameToTypeParameterMap());
    assertEquals(3, builder.getConventions().size());
    assertTrue(builder.getAnnotations().isEmpty());
    assertEquals(clazz, builder.getType());
    assertNull(builder.getIdPropertyName());
    assertFalse(builder.useDiscriminator());
    assertNull(builder.getDiscriminator());
}
Also used : Field(java.lang.reflect.Field) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SimpleGenericsModel(org.bson.codecs.pojo.entities.SimpleGenericsModel) Test(org.junit.Test)

Example 2 with SimpleGenericsModel

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

the class PojoCustomTest method testMultiplePojoProviders.

@Test
public void testMultiplePojoProviders() {
    NestedGenericHolderFieldWithMultipleTypeParamsModel model = getNestedGenericHolderFieldWithMultipleTypeParamsModel();
    PojoCodecProvider provider1 = PojoCodecProvider.builder().register(NestedGenericHolderFieldWithMultipleTypeParamsModel.class).build();
    PojoCodecProvider provider2 = PojoCodecProvider.builder().register(PropertyWithMultipleTypeParamsModel.class).build();
    PojoCodecProvider provider3 = PojoCodecProvider.builder().register(SimpleGenericsModel.class).build();
    PojoCodecProvider provider4 = PojoCodecProvider.builder().register(GenericHolderModel.class).build();
    CodecRegistry registry = fromProviders(provider1, provider2, provider3, provider4);
    CodecRegistry actualRegistry = fromRegistries(fromProviders(new BsonValueCodecProvider(), new ValueCodecProvider()), registry);
    String json = "{'nested': {'myGenericField': {_t: 'PropertyWithMultipleTypeParamsModel', " + "'simpleGenericsModel': {_t: 'org.bson.codecs.pojo.entities.SimpleGenericsModel', 'myIntegerField': 42, " + "'myGenericField': {'$numberLong': '101'}, 'myListField': ['B', 'C'], 'myMapField': {'D': 2, 'E': 3, 'F': 4 }}}," + "'myLongField': {'$numberLong': '42'}}}";
    roundTrip(actualRegistry, model, json);
}
Also used : BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) ValueCodecProvider(org.bson.codecs.ValueCodecProvider) NestedGenericHolderFieldWithMultipleTypeParamsModel(org.bson.codecs.pojo.entities.NestedGenericHolderFieldWithMultipleTypeParamsModel) SimpleGenericsModel(org.bson.codecs.pojo.entities.SimpleGenericsModel) BsonRepresentationUnsupportedString(org.bson.codecs.pojo.entities.BsonRepresentationUnsupportedString) PropertyWithMultipleTypeParamsModel(org.bson.codecs.pojo.entities.PropertyWithMultipleTypeParamsModel) GenericHolderModel(org.bson.codecs.pojo.entities.GenericHolderModel) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 3 with SimpleGenericsModel

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

the class PojoTestCase method getNestedGenericHolderSimpleGenericsModel.

static NestedGenericHolderSimpleGenericsModel getNestedGenericHolderSimpleGenericsModel() {
    SimpleModel simpleModel = getSimpleModel();
    Map<String, SimpleModel> map = new HashMap<String, SimpleModel>();
    map.put("A", simpleModel);
    Map<String, Map<String, SimpleModel>> mapB = new HashMap<String, Map<String, SimpleModel>>();
    mapB.put("A", map);
    SimpleGenericsModel<Integer, List<SimpleModel>, Map<String, SimpleModel>> simpleGenericsModel = new SimpleGenericsModel<Integer, List<SimpleModel>, Map<String, SimpleModel>>(42, 42, singletonList(singletonList(simpleModel)), mapB);
    GenericHolderModel<SimpleGenericsModel<Integer, List<SimpleModel>, Map<String, SimpleModel>>> nested = new GenericHolderModel<SimpleGenericsModel<Integer, List<SimpleModel>, Map<String, SimpleModel>>>(simpleGenericsModel, 42L);
    return new NestedGenericHolderSimpleGenericsModel(nested);
}
Also used : SimpleModel(org.bson.codecs.pojo.entities.SimpleModel) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) NestedGenericHolderSimpleGenericsModel(org.bson.codecs.pojo.entities.NestedGenericHolderSimpleGenericsModel) SimpleGenericsModel(org.bson.codecs.pojo.entities.SimpleGenericsModel) NestedGenericHolderSimpleGenericsModel(org.bson.codecs.pojo.entities.NestedGenericHolderSimpleGenericsModel) GenericHolderModel(org.bson.codecs.pojo.entities.GenericHolderModel) NestedSelfReferentialGenericHolderModel(org.bson.codecs.pojo.entities.NestedSelfReferentialGenericHolderModel) NestedGenericHolderModel(org.bson.codecs.pojo.entities.NestedGenericHolderModel) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Aggregations

SimpleGenericsModel (org.bson.codecs.pojo.entities.SimpleGenericsModel)3 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 GenericHolderModel (org.bson.codecs.pojo.entities.GenericHolderModel)2 Test (org.junit.Test)2 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Collections.singletonList (java.util.Collections.singletonList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 BsonValueCodecProvider (org.bson.codecs.BsonValueCodecProvider)1 ValueCodecProvider (org.bson.codecs.ValueCodecProvider)1 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)1 BsonRepresentationUnsupportedString (org.bson.codecs.pojo.entities.BsonRepresentationUnsupportedString)1 NestedGenericHolderFieldWithMultipleTypeParamsModel (org.bson.codecs.pojo.entities.NestedGenericHolderFieldWithMultipleTypeParamsModel)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