Search in sources :

Example 1 with BeforeConvertCallback

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

the class MongoTemplateUnitTests method publishesEventsAndEntityCallbacksInOrder.

// DATAMONGO-2261
@Test
void publishesEventsAndEntityCallbacksInOrder() {
    BeforeConvertCallback<Person> beforeConvertCallback = new BeforeConvertCallback<Person>() {

        @Override
        public Person onBeforeConvert(Person entity, String collection) {
            assertThat(entity.id).isEqualTo("before-convert-event");
            entity.id = "before-convert-callback";
            return entity;
        }
    };
    BeforeSaveCallback<Person> beforeSaveCallback = new BeforeSaveCallback<Person>() {

        @Override
        public Person onBeforeSave(Person entity, Document document, String collection) {
            assertThat(entity.id).isEqualTo("before-save-event");
            entity.id = "before-save-callback";
            return entity;
        }
    };
    AbstractMongoEventListener<Person> eventListener = new AbstractMongoEventListener<Person>() {

        @Override
        public void onBeforeConvert(BeforeConvertEvent<Person> event) {
            assertThat(event.getSource().id).isEqualTo("init");
            event.getSource().id = "before-convert-event";
        }

        @Override
        public void onBeforeSave(BeforeSaveEvent<Person> event) {
            assertThat(event.getSource().id).isEqualTo("before-convert-callback");
            event.getSource().id = "before-save-event";
        }
    };
    StaticApplicationContext ctx = new StaticApplicationContext();
    ctx.registerBean(ApplicationListener.class, () -> eventListener);
    ctx.registerBean(BeforeConvertCallback.class, () -> beforeConvertCallback);
    ctx.registerBean(BeforeSaveCallback.class, () -> beforeSaveCallback);
    ctx.refresh();
    template.setApplicationContext(ctx);
    Person entity = new Person();
    entity.id = "init";
    entity.firstname = "luke";
    Person saved = template.save(entity);
    assertThat(saved.id).isEqualTo("before-save-callback");
}
Also used : BeforeSaveEvent(org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent) BeforeConvertCallback(org.springframework.data.mongodb.core.mapping.event.BeforeConvertCallback) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) BeforeSaveCallback(org.springframework.data.mongodb.core.mapping.event.BeforeSaveCallback) BeforeConvertEvent(org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent) Document(org.bson.Document) AbstractMongoEventListener(org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener) Test(org.junit.jupiter.api.Test)

Aggregations

Document (org.bson.Document)1 Test (org.junit.jupiter.api.Test)1 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)1 AbstractMongoEventListener (org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener)1 BeforeConvertCallback (org.springframework.data.mongodb.core.mapping.event.BeforeConvertCallback)1 BeforeConvertEvent (org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent)1 BeforeSaveCallback (org.springframework.data.mongodb.core.mapping.event.BeforeSaveCallback)1 BeforeSaveEvent (org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent)1