use of org.springframework.data.mongodb.core.mapping.Document in project spring-data-mongodb by spring-projects.
the class MappingMongoConverterUnitTests method shouldReadEntityWithGeoSphereCorrectly.
// DATAMONGO-858
@Test
void shouldReadEntityWithGeoSphereCorrectly() {
ClassWithGeoSphere object = new ClassWithGeoSphere();
object.sphere = new Sphere(new Point(1, 2), 3);
org.bson.Document document = new org.bson.Document();
converter.write(object, document);
ClassWithGeoSphere result = converter.read(ClassWithGeoSphere.class, document);
assertThat(result).isNotNull();
assertThat(result.sphere).isEqualTo(object.sphere);
}
use of org.springframework.data.mongodb.core.mapping.Document in project spring-data-mongodb by spring-projects.
the class MappingMongoConverterUnitTests method mappingConsidersCustomConvertersNotWritingTypeInformation.
// DATAMONGO-724
@Test
void mappingConsidersCustomConvertersNotWritingTypeInformation() {
Person person = new Person();
person.firstname = "Dave";
ClassWithMapProperty entity = new ClassWithMapProperty();
entity.mapOfPersons = new HashMap<String, Person>();
entity.mapOfPersons.put("foo", person);
entity.mapOfObjects = new HashMap<String, Object>();
entity.mapOfObjects.put("foo", person);
CustomConversions conversions = new MongoCustomConversions(Arrays.asList(new Converter<Person, org.bson.Document>() {
@Override
public org.bson.Document convert(Person source) {
return //
new org.bson.Document().append("firstname", source.firstname).append("_class", Person.class.getName());
}
}, new Converter<org.bson.Document, Person>() {
@Override
public Person convert(org.bson.Document source) {
Person person = new Person();
person.firstname = source.get("firstname").toString();
person.lastname = "converter";
return person;
}
}));
MongoMappingContext context = new MongoMappingContext();
context.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
context.afterPropertiesSet();
MappingMongoConverter mongoConverter = new MappingMongoConverter(resolver, context);
mongoConverter.setCustomConversions(conversions);
mongoConverter.afterPropertiesSet();
org.bson.Document document = new org.bson.Document();
mongoConverter.write(entity, document);
ClassWithMapProperty result = mongoConverter.read(ClassWithMapProperty.class, document);
assertThat(result.mapOfPersons).isNotNull();
Person personCandidate = result.mapOfPersons.get("foo");
assertThat(personCandidate).isNotNull();
assertThat(personCandidate.firstname).isEqualTo("Dave");
assertThat(result.mapOfObjects).isNotNull();
Object value = result.mapOfObjects.get("foo");
assertThat(value).isNotNull();
assertThat(value).isInstanceOf(Person.class);
assertThat(((Person) value).firstname).isEqualTo("Dave");
assertThat(((Person) value).lastname).isEqualTo("converter");
}
use of org.springframework.data.mongodb.core.mapping.Document in project spring-data-mongodb by spring-projects.
the class MappingMongoConverterUnitTests method shouldWriteCglibProxiedClassTypeInformationCorrectly.
// DATAMONGO-1001, DATAMONGO-1509
@Test
void shouldWriteCglibProxiedClassTypeInformationCorrectly() {
ProxyFactory factory = new ProxyFactory();
factory.setTargetClass(GenericType.class);
factory.setProxyTargetClass(true);
GenericType<?> proxied = (GenericType<?>) factory.getProxy();
org.bson.Document document = new org.bson.Document();
converter.write(proxied, document);
assertTypeHint(document, GenericType.class);
}
use of org.springframework.data.mongodb.core.mapping.Document in project spring-data-mongodb by spring-projects.
the class QueryMapperUnitTests method resolvesSimpleNestedFieldnameWithUnderscoresCorrectly.
// GH-3601
@Test
void resolvesSimpleNestedFieldnameWithUnderscoresCorrectly() {
Query query = query(where("simple.fieldname_with_underscores").exists(true));
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), context.getPersistentEntity(WrapperAroundWithPropertyUsingUnderscoreInName.class));
assertThat(document).isEqualTo(new org.bson.Document("simple.fieldname_with_underscores", new org.bson.Document("$exists", true)));
}
use of org.springframework.data.mongodb.core.mapping.Document in project spring-data-mongodb by spring-projects.
the class QueryMapperUnitTests method shouldUseExplicitlySetFieldnameForIdPropertyCandidatesUsedInSortClause.
// DATAMONGO-1050
@Test
void shouldUseExplicitlySetFieldnameForIdPropertyCandidatesUsedInSortClause() {
Query query = new Query().with(Sort.by("nested.id"));
org.bson.Document document = mapper.getMappedSort(query.getSortObject(), context.getPersistentEntity(RootForClassWithExplicitlyRenamedIdField.class));
assertThat(document).isEqualTo(new org.bson.Document().append("nested.id", 1));
}
Aggregations