Search in sources :

Example 1 with PersonPojoStringId

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

the class MappingMongoConverterUnitTests method convertsObjectIdStringsToObjectIdCorrectly.

@Test
void convertsObjectIdStringsToObjectIdCorrectly() {
    PersonPojoStringId p1 = new PersonPojoStringId("1234567890", "Text-1");
    org.bson.Document doc1 = new org.bson.Document();
    converter.write(p1, doc1);
    assertThat(doc1.get("_id")).isInstanceOf(String.class);
    PersonPojoStringId p2 = new PersonPojoStringId(new ObjectId().toString(), "Text-1");
    org.bson.Document doc2 = new org.bson.Document();
    converter.write(p2, doc2);
    assertThat(doc2.get("_id")).isInstanceOf(ObjectId.class);
}
Also used : ObjectId(org.bson.types.ObjectId) PersonPojoStringId(org.springframework.data.mongodb.core.mapping.PersonPojoStringId) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Example 2 with PersonPojoStringId

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

the class ApplicationContextEventTests method loadEventsOnAggregation.

// DATAMONGO-1256
@Test
public void loadEventsOnAggregation() {
    template.insert(new PersonPojoStringId("1", "Text"));
    template.aggregate(Aggregation.newAggregation(Aggregation.project("text")), PersonPojoStringId.class, PersonPojoStringId.class);
    assertThat(listener.onAfterLoadEvents).hasSize(1);
    assertThat(listener.onAfterLoadEvents.get(0).getCollectionName()).isEqualTo(COLLECTION_NAME);
    assertThat(listener.onBeforeConvertEvents).hasSize(1);
    assertThat(listener.onBeforeConvertEvents.get(0).getCollectionName()).isEqualTo(COLLECTION_NAME);
    assertThat(listener.onAfterConvertEvents).hasSize(1);
    assertThat(listener.onAfterConvertEvents.get(0).getCollectionName()).isEqualTo(COLLECTION_NAME);
}
Also used : PersonPojoStringId(org.springframework.data.mongodb.core.mapping.PersonPojoStringId) Test(org.junit.jupiter.api.Test)

Example 3 with PersonPojoStringId

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

the class ApplicationContextEventTests method loadAndConvertEvents.

// DATAMONGO-1256
@Test
public void loadAndConvertEvents() {
    PersonPojoStringId entity = new PersonPojoStringId("1", "Text");
    template.insert(entity);
    template.findOne(query(where("id").is(entity.getId())), PersonPojoStringId.class);
    assertThat(listener.onAfterLoadEvents).hasSize(1);
    assertThat(listener.onAfterLoadEvents.get(0).getCollectionName()).isEqualTo(COLLECTION_NAME);
    assertThat(listener.onBeforeConvertEvents).hasSize(1);
    assertThat(listener.onBeforeConvertEvents.get(0).getCollectionName()).isEqualTo(COLLECTION_NAME);
    assertThat(listener.onAfterConvertEvents).hasSize(1);
    assertThat(listener.onAfterConvertEvents.get(0).getCollectionName()).isEqualTo(COLLECTION_NAME);
}
Also used : PersonPojoStringId(org.springframework.data.mongodb.core.mapping.PersonPojoStringId) Test(org.junit.jupiter.api.Test)

Example 4 with PersonPojoStringId

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

the class ApplicationContextEventTests method beforeSaveEvent.

@Test
@SuppressWarnings("unchecked")
public void beforeSaveEvent() {
    PersonBeforeSaveListener personBeforeSaveListener = applicationContext.getBean(PersonBeforeSaveListener.class);
    AfterSaveListener afterSaveListener = applicationContext.getBean(AfterSaveListener.class);
    assertThat(personBeforeSaveListener.seenEvents).isEmpty();
    assertThat(afterSaveListener.seenEvents).isEmpty();
    assertThat(listener.onBeforeSaveEvents).isEmpty();
    assertThat(listener.onAfterSaveEvents).isEmpty();
    PersonPojoStringId p = new PersonPojoStringId("1", "Text");
    template.insert(p);
    assertThat(personBeforeSaveListener.seenEvents).hasSize(1);
    assertThat(afterSaveListener.seenEvents).hasSize(1);
    assertThat(listener.onBeforeSaveEvents).hasSize(1);
    assertThat(listener.onAfterSaveEvents).hasSize(1);
    assertThat(listener.onBeforeSaveEvents.get(0).getCollectionName()).isEqualTo(COLLECTION_NAME);
    assertThat(listener.onAfterSaveEvents.get(0).getCollectionName()).isEqualTo(COLLECTION_NAME);
    assertThat(personBeforeSaveListener.seenEvents.get(0) instanceof BeforeSaveEvent<?>).isTrue();
    assertThat(afterSaveListener.seenEvents.get(0) instanceof AfterSaveEvent<?>).isTrue();
    BeforeSaveEvent<PersonPojoStringId> beforeSaveEvent = (BeforeSaveEvent<PersonPojoStringId>) personBeforeSaveListener.seenEvents.get(0);
    PersonPojoStringId p2 = beforeSaveEvent.getSource();
    org.bson.Document document = beforeSaveEvent.getDocument();
    comparePersonAndDocument(p, p2, document);
    AfterSaveEvent<Object> afterSaveEvent = (AfterSaveEvent<Object>) afterSaveListener.seenEvents.get(0);
    assertThat(afterSaveEvent.getSource() instanceof PersonPojoStringId).isTrue();
    p2 = (PersonPojoStringId) afterSaveEvent.getSource();
    document = beforeSaveEvent.getDocument();
    comparePersonAndDocument(p, p2, document);
}
Also used : PersonPojoStringId(org.springframework.data.mongodb.core.mapping.PersonPojoStringId) Test(org.junit.jupiter.api.Test)

Example 5 with PersonPojoStringId

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

the class ApplicationContextEventTests method deleteEvents.

// DATAMONGO-1256
@Test
public void deleteEvents() {
    PersonPojoStringId entity = new PersonPojoStringId("1", "Text");
    template.insert(entity);
    template.remove(entity);
    assertThat(listener.onBeforeDeleteEvents).hasSize(1);
    assertThat(listener.onBeforeDeleteEvents.get(0).getCollectionName()).isEqualTo(COLLECTION_NAME);
    assertThat(listener.onAfterDeleteEvents).hasSize(1);
    assertThat(listener.onAfterDeleteEvents.get(0).getCollectionName()).isEqualTo(COLLECTION_NAME);
}
Also used : PersonPojoStringId(org.springframework.data.mongodb.core.mapping.PersonPojoStringId) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)6 PersonPojoStringId (org.springframework.data.mongodb.core.mapping.PersonPojoStringId)6 ObjectId (org.bson.types.ObjectId)1 Document (org.springframework.data.mongodb.core.mapping.Document)1