Search in sources :

Example 31 with Document

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

the class MappingMongoConverterUnitTests method convertsJodaTimeTypesCorrectly.

@Test
void convertsJodaTimeTypesCorrectly() {
    converter = new MappingMongoConverter(resolver, mappingContext);
    converter.afterPropertiesSet();
    Person person = new Person();
    person.birthDate = new LocalDate();
    org.bson.Document document = new org.bson.Document();
    converter.write(person, document);
    assertThat(document.get("birthDate")).isInstanceOf(Date.class);
    Person result = converter.read(Person.class, document);
    assertThat(result.birthDate).isNotNull();
}
Also used : Document(org.springframework.data.mongodb.core.mapping.Document) LocalDate(org.joda.time.LocalDate) Test(org.junit.jupiter.api.Test)

Example 32 with Document

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

the class MappingMongoConverterUnitTests method writesCollectionWithInterfaceCorrectly.

// DATAMONGO-145
@Test
void writesCollectionWithInterfaceCorrectly() {
    Person person = new Person();
    person.firstname = "Oliver";
    CollectionWrapper wrapper = new CollectionWrapper();
    wrapper.contacts = Arrays.asList((Contact) person);
    org.bson.Document document = new org.bson.Document();
    converter.write(wrapper, document);
    Object result = document.get("contacts");
    assertThat(result).isInstanceOf(List.class);
    List<Object> contacts = (List<Object>) result;
    org.bson.Document personDocument = (org.bson.Document) contacts.get(0);
    assertThat(personDocument.get("foo").toString()).isEqualTo("Oliver");
    assertThat((String) personDocument.get(DefaultMongoTypeMapper.DEFAULT_TYPE_KEY)).isEqualTo(Person.class.getName());
}
Also used : DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Example 33 with Document

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

the class MappingMongoConverterUnitTests method shouldReadEntityWithGeoCircleCorrectly.

// DATAMONGO-858
@Test
void shouldReadEntityWithGeoCircleCorrectly() {
    ClassWithGeoCircle object = new ClassWithGeoCircle();
    object.circle = new Circle(new Point(1, 2), 3);
    org.bson.Document document = new org.bson.Document();
    converter.write(object, document);
    ClassWithGeoCircle result = converter.read(ClassWithGeoCircle.class, document);
    assertThat(result).isNotNull();
    assertThat(result.circle).isEqualTo(result.circle);
}
Also used : Circle(org.springframework.data.geo.Circle) Point(org.springframework.data.geo.Point) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Example 34 with Document

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

the class MappingMongoConverterUnitTests method resolveDBRefMapValueShouldInvokeCallbacks.

// DATAMONGO-2479
@Test
void resolveDBRefMapValueShouldInvokeCallbacks() {
    AfterConvertCallback<Person> afterConvertCallback = spy(new ReturningAfterConvertCallback());
    converter.setEntityCallbacks(EntityCallbacks.create(afterConvertCallback));
    when(resolver.fetch(Mockito.any(DBRef.class))).thenReturn(new org.bson.Document());
    DBRef dbRef = mock(DBRef.class);
    org.bson.Document refMap = new org.bson.Document("foo", dbRef);
    org.bson.Document document = new org.bson.Document("personMap", refMap);
    DBRefWrapper result = converter.read(DBRefWrapper.class, document);
    verify(afterConvertCallback).onAfterConvert(eq(result.personMap.get("foo")), eq(new org.bson.Document()), any());
}
Also used : DBRef(com.mongodb.DBRef) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Example 35 with Document

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

the class MappingMongoConverterUnitTests method shouldWriteEntityWithGeoSphereCorrectly.

// DATAMONGO-858
@Test
void shouldWriteEntityWithGeoSphereCorrectly() {
    ClassWithGeoSphere object = new ClassWithGeoSphere();
    Sphere sphere = new Sphere(new Point(1, 2), 3);
    Distance radius = sphere.getRadius();
    object.sphere = sphere;
    org.bson.Document document = new org.bson.Document();
    converter.write(object, document);
    assertThat(document).isNotNull();
    assertThat(document.get("sphere")).isInstanceOf(org.bson.Document.class);
    assertThat(document.get("sphere")).isEqualTo((Object) new org.bson.Document("center", new org.bson.Document("x", sphere.getCenter().getX()).append("y", sphere.getCenter().getY())).append("radius", radius.getNormalizedValue()).append("metric", radius.getMetric().toString()));
}
Also used : Sphere(org.springframework.data.mongodb.core.geo.Sphere) Point(org.springframework.data.geo.Point) Document(org.springframework.data.mongodb.core.mapping.Document) Distance(org.springframework.data.geo.Distance) Test(org.junit.jupiter.api.Test)

Aggregations

Document (org.springframework.data.mongodb.core.mapping.Document)55 Test (org.junit.jupiter.api.Test)53 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)16 Query (org.springframework.data.mongodb.core.query.Query)16 TextQuery (org.springframework.data.mongodb.core.query.TextQuery)16 Point (org.springframework.data.geo.Point)12 BasicDBList (com.mongodb.BasicDBList)8 BasicDBObject (com.mongodb.BasicDBObject)6 DBObject (com.mongodb.DBObject)5 DBRef (com.mongodb.DBRef)5 Sphere (org.springframework.data.mongodb.core.geo.Sphere)5 ObjectId (org.bson.types.ObjectId)4 Distance (org.springframework.data.geo.Distance)4 LocalDate (org.joda.time.LocalDate)3 MongoException (com.mongodb.MongoException)2 BigInteger (java.math.BigInteger)2 HashSet (java.util.HashSet)2 DataAccessException (org.springframework.dao.DataAccessException)2 Circle (org.springframework.data.geo.Circle)2 Polygon (org.springframework.data.geo.Polygon)2