Search in sources :

Example 76 with MongoMappingContext

use of org.springframework.data.mongodb.core.mapping.MongoMappingContext in project spring-data-mongodb by spring-projects.

the class MappingMongoConverterBenchmark method setUp.

@Setup
public void setUp() throws Exception {
    client = new MongoClient(new ServerAddress());
    this.mappingContext = new MongoMappingContext();
    this.mappingContext.setInitialEntitySet(Collections.singleton(Customer.class));
    this.mappingContext.afterPropertiesSet();
    DbRefResolver dbRefResolver = new DefaultDbRefResolver(new SimpleMongoDbFactory(client, DB_NAME));
    this.converter = new MappingMongoConverter(dbRefResolver, mappingContext);
    this.converter.setCustomConversions(new MongoCustomConversions(Collections.emptyList()));
    this.converter.afterPropertiesSet();
    // just a flat document
    this.documentWith2Properties = new Document("firstname", "Dave").append("lastname", "Matthews");
    // document with a nested one
    Document address = new Document("zipCode", "ABCDE").append("city", "Some Place");
    this.documentWith2PropertiesAnd1Nested = // 
    new Document("firstname", "Dave").append("lastname", // 
    "Matthews").append("address", address);
    // object equivalent of documentWith2PropertiesAnd1Nested
    this.objectWith2PropertiesAnd1Nested = new Customer("Dave", "Matthews", new Address("zipCode", "City"));
    // a bit more challenging object with list & map conversion.
    objectWithFlatAndComplexPropertiesPlusListAndMap = new SlightlyMoreComplexObject();
    objectWithFlatAndComplexPropertiesPlusListAndMap.id = UUID.randomUUID().toString();
    objectWithFlatAndComplexPropertiesPlusListAndMap.addressList = Arrays.asList(new Address("zip-1", "city-1"), new Address("zip-2", "city-2"));
    objectWithFlatAndComplexPropertiesPlusListAndMap.customer = objectWith2PropertiesAnd1Nested;
    objectWithFlatAndComplexPropertiesPlusListAndMap.customerMap = new LinkedHashMap<>();
    objectWithFlatAndComplexPropertiesPlusListAndMap.customerMap.put("dave", objectWith2PropertiesAnd1Nested);
    objectWithFlatAndComplexPropertiesPlusListAndMap.customerMap.put("deborah", new Customer("Deborah Anne", "Dyer", new Address("?", "london")));
    objectWithFlatAndComplexPropertiesPlusListAndMap.customerMap.put("eddie", new Customer("Eddie", "Vedder", new Address("??", "Seattle")));
    objectWithFlatAndComplexPropertiesPlusListAndMap.intOne = Integer.MIN_VALUE;
    objectWithFlatAndComplexPropertiesPlusListAndMap.intTwo = Integer.MAX_VALUE;
    objectWithFlatAndComplexPropertiesPlusListAndMap.location = new Point(-33.865143, 151.209900);
    objectWithFlatAndComplexPropertiesPlusListAndMap.renamedField = "supercalifragilisticexpialidocious";
    objectWithFlatAndComplexPropertiesPlusListAndMap.stringOne = "¯\\_(ツ)_/¯";
    objectWithFlatAndComplexPropertiesPlusListAndMap.stringTwo = " (╯°□°)╯︵ ┻━┻";
    // JSON equivalent of objectWithFlatAndComplexPropertiesPlusListAndMap
    documentWithFlatAndComplexPropertiesPlusListAndMap = Document.parse("{ \"_id\" : \"517f6aee-e9e0-44f0-88ed-f3694a019f27\", \"intOne\" : -2147483648, \"intTwo\" : 2147483647, \"stringOne\" : \"¯\\\\_(ツ)_/¯\", \"stringTwo\" : \" (╯°□°)╯︵ ┻━┻\", \"explicit-field-name\" : \"supercalifragilisticexpialidocious\", \"location\" : { \"x\" : -33.865143, \"y\" : 151.2099 }, \"objectWith2PropertiesAnd1Nested\" : { \"firstname\" : \"Dave\", \"lastname\" : \"Matthews\", \"address\" : { \"zipCode\" : \"zipCode\", \"city\" : \"City\" } }, \"addressList\" : [{ \"zipCode\" : \"zip-1\", \"city\" : \"city-1\" }, { \"zipCode\" : \"zip-2\", \"city\" : \"city-2\" }], \"customerMap\" : { \"dave\" : { \"firstname\" : \"Dave\", \"lastname\" : \"Matthews\", \"address\" : { \"zipCode\" : \"zipCode\", \"city\" : \"City\" } }, \"deborah\" : { \"firstname\" : \"Deborah Anne\", \"lastname\" : \"Dyer\", \"address\" : { \"zipCode\" : \"?\", \"city\" : \"london\" } }, \"eddie\" : { \"firstname\" : \"Eddie\", \"lastname\" : \"Vedder\", \"address\" : { \"zipCode\" : \"??\", \"city\" : \"Seattle\" } } }, \"_class\" : \"org.springframework.data.mongodb.core.convert.MappingMongoConverterBenchmark$SlightlyMoreComplexObject\" }");
}
Also used : ServerAddress(com.mongodb.ServerAddress) ServerAddress(com.mongodb.ServerAddress) Point(org.springframework.data.geo.Point) Document(org.bson.Document) MongoClient(com.mongodb.MongoClient) SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Setup(org.openjdk.jmh.annotations.Setup)

Example 77 with MongoMappingContext

use of org.springframework.data.mongodb.core.mapping.MongoMappingContext in project spring-data-mongodb by spring-projects.

the class TestAggregationContext method contextFor.

public static AggregationOperationContext contextFor(@Nullable Class<?> type) {
    MappingMongoConverter mongoConverter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, new MongoMappingContext());
    mongoConverter.afterPropertiesSet();
    return contextFor(type, mongoConverter);
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter)

Example 78 with MongoMappingContext

use of org.springframework.data.mongodb.core.mapping.MongoMappingContext in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplate method getDefaultMongoConverter.

private MappingMongoConverter getDefaultMongoConverter() {
    MongoCustomConversions conversions = new MongoCustomConversions(Collections.emptyList());
    MongoMappingContext context = new MongoMappingContext();
    context.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
    context.afterPropertiesSet();
    MappingMongoConverter converter = new MappingMongoConverter(NO_OP_REF_RESOLVER, context);
    converter.setCustomConversions(conversions);
    converter.setCodecRegistryProvider(this.mongoDatabaseFactory);
    converter.afterPropertiesSet();
    return converter;
}
Also used : MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter)

Example 79 with MongoMappingContext

use of org.springframework.data.mongodb.core.mapping.MongoMappingContext in project spring-data-mongodb by spring-projects.

the class DefaultIndexOperationsUnitTests method setUp.

@BeforeEach
void setUp() {
    when(factory.getMongoDatabase()).thenReturn(db);
    when(factory.getExceptionTranslator()).thenReturn(exceptionTranslator);
    when(db.getCollection(any(), any(Class.class))).thenReturn(collection);
    when(collection.createIndex(any(), any(IndexOptions.class))).thenReturn("OK");
    this.mappingContext = new MongoMappingContext();
    this.converter = spy(new MappingMongoConverter(new DefaultDbRefResolver(factory), mappingContext));
    this.template = new MongoTemplate(factory, converter);
}
Also used : IndexOptions(com.mongodb.client.model.IndexOptions) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 80 with MongoMappingContext

use of org.springframework.data.mongodb.core.mapping.MongoMappingContext in project spring-data-mongodb by spring-projects.

the class DefaultReactiveIndexOperationsUnitTests method setUp.

@BeforeEach
void setUp() {
    when(factory.getMongoDatabase()).thenReturn(Mono.just(db));
    when(factory.getExceptionTranslator()).thenReturn(exceptionTranslator);
    when(db.getCollection(any(), any(Class.class))).thenReturn(collection);
    when(collection.createIndex(any(), any(IndexOptions.class))).thenReturn(publisher);
    this.mappingContext = new MongoMappingContext();
    this.converter = spy(new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext));
    this.template = new ReactiveMongoTemplate(factory, converter);
}
Also used : IndexOptions(com.mongodb.client.model.IndexOptions) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

MongoMappingContext (org.springframework.data.mongodb.core.mapping.MongoMappingContext)118 MappingMongoConverter (org.springframework.data.mongodb.core.convert.MappingMongoConverter)56 Test (org.junit.jupiter.api.Test)41 BeforeEach (org.junit.jupiter.api.BeforeEach)31 Document (org.bson.Document)19 QueryMapper (org.springframework.data.mongodb.core.convert.QueryMapper)19 DefaultDbRefResolver (org.springframework.data.mongodb.core.convert.DefaultDbRefResolver)13 MongoCustomConversions (org.springframework.data.mongodb.core.convert.MongoCustomConversions)10 Bson (org.bson.conversions.Bson)8 Before (org.junit.Before)8 Test (org.junit.Test)8 MongoTemplate (org.springframework.data.mongodb.core.MongoTemplate)8 IndexOptions (com.mongodb.client.model.IndexOptions)7 CustomConversions (org.springframework.data.convert.CustomConversions)6 DbRefResolver (org.springframework.data.mongodb.core.convert.DbRefResolver)6 Document (org.springframework.data.mongodb.core.mapping.Document)6 Set (java.util.Set)5 MongoDbFactory (org.springframework.data.mongodb.MongoDbFactory)5 CountOptions (com.mongodb.client.model.CountOptions)4 List (java.util.List)4