diff --git a/spring-data-rest/src/main/java/com/baeldung/config/MvcConfig.java b/spring-data-rest/src/main/java/com/baeldung/config/MvcConfig.java index bed1a6f846..82cb936348 100644 --- a/spring-data-rest/src/main/java/com/baeldung/config/MvcConfig.java +++ b/spring-data-rest/src/main/java/com/baeldung/config/MvcConfig.java @@ -1,5 +1,7 @@ package com.baeldung.config; +import com.baeldung.events.AuthorEventHandler; +import com.baeldung.events.BookEventHandler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -22,4 +24,14 @@ public class MvcConfig extends WebMvcConfigurerAdapter{ configurer.enable(); } + @Bean + AuthorEventHandler authorEventHandler() { + return new AuthorEventHandler(); + } + + @Bean + BookEventHandler bookEventHandler(){ + return new BookEventHandler(); + } + } diff --git a/spring-data-rest/src/main/java/com/baeldung/events/AuthorEventHandler.java b/spring-data-rest/src/main/java/com/baeldung/events/AuthorEventHandler.java new file mode 100644 index 0000000000..5a8ae05c08 --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/events/AuthorEventHandler.java @@ -0,0 +1,40 @@ +package com.baeldung.events; + +import com.baeldung.models.Author; +import com.baeldung.models.Book; +import org.springframework.data.rest.core.annotation.*; + +import java.util.logging.Logger; + +@RepositoryEventHandler +public class AuthorEventHandler { + Logger logger = Logger.getLogger("Class AuthorEventHandler"); + public AuthorEventHandler(){ + super(); + } + + @HandleBeforeCreate + public void handleAuthorBeforeCreate(Author author){ + logger.info("Inside Author Before Create...."); + String name = author.getName(); + } + + @HandleAfterCreate + public void handleAuthorAfterCreate(Author author){ + logger.info("Inside Author After Create ...."); + String name = author.getName(); + } + + @HandleBeforeDelete + public void handleAuthorBeforeDelete(Author author){ + logger.info("Inside Author Before Delete ...."); + String name = author.getName(); + } + + @HandleAfterDelete + public void handleAuthorAfterDelete(Author author){ + logger.info("Inside Author After Delete ...."); + String name = author.getName(); + } + +} diff --git a/spring-data-rest/src/main/java/com/baeldung/events/BookEventHandler.java b/spring-data-rest/src/main/java/com/baeldung/events/BookEventHandler.java new file mode 100644 index 0000000000..3953e6ce0d --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/events/BookEventHandler.java @@ -0,0 +1,26 @@ +package com.baeldung.events; + +import java.util.logging.Logger; +import com.baeldung.models.Author; +import com.baeldung.models.Book; +import org.apache.commons.logging.Log; +import org.springframework.data.rest.core.annotation.HandleAfterDelete; +import org.springframework.data.rest.core.annotation.HandleBeforeCreate; +import org.springframework.data.rest.core.annotation.RepositoryEventHandler; + +@RepositoryEventHandler +public class BookEventHandler { + Logger logger = Logger.getLogger("Class BookEventHandler"); + @HandleBeforeCreate + public void handleBookBeforeCreate(Book book){ + + logger.info("Inside Book Before Create ...."); + book.getAuthors(); + } + + @HandleBeforeCreate + public void handleAuthorBeforeCreate(Author author){ + logger.info("Inside Author Before Create ...."); + author.getBooks(); + } +} diff --git a/spring-data-rest/src/test/java/com/baeldung/events/AuthorEventHandlerTest.java b/spring-data-rest/src/test/java/com/baeldung/events/AuthorEventHandlerTest.java new file mode 100644 index 0000000000..9fb2d1014e --- /dev/null +++ b/spring-data-rest/src/test/java/com/baeldung/events/AuthorEventHandlerTest.java @@ -0,0 +1,29 @@ +package com.baeldung.events; + +import com.baeldung.models.Author; +import org.junit.Test; +import org.mockito.Mockito; +import org.springframework.data.rest.core.annotation.RepositoryEventHandler; + +import static org.mockito.Mockito.mock; + +public class AuthorEventHandlerTest { + + @Test + public void whenCreateAuthorThenSuccess() { + Author author = mock(Author.class); + AuthorEventHandler authorEventHandler = new AuthorEventHandler(); + authorEventHandler.handleAuthorBeforeCreate(author); + Mockito.verify(author,Mockito.times(1)).getName(); + + } + + @Test + public void whenDeleteAuthorThenSuccess() { + Author author = mock(Author.class); + AuthorEventHandler authorEventHandler = new AuthorEventHandler(); + authorEventHandler.handleAuthorAfterDelete(author); + Mockito.verify(author,Mockito.times(1)).getName(); + + } +} diff --git a/spring-data-rest/src/test/java/com/baeldung/events/BookEventHandlerTest.java b/spring-data-rest/src/test/java/com/baeldung/events/BookEventHandlerTest.java new file mode 100644 index 0000000000..85699adb0d --- /dev/null +++ b/spring-data-rest/src/test/java/com/baeldung/events/BookEventHandlerTest.java @@ -0,0 +1,28 @@ +package com.baeldung.events; + +import com.baeldung.models.Author; +import com.baeldung.models.Book; +import org.junit.Test; +import org.mockito.Mockito; + +import static org.mockito.Mockito.mock; + +public class BookEventHandlerTest { + @Test + public void whenCreateBookThenSuccess() { + Book book = mock(Book.class); + BookEventHandler bookEventHandler = new BookEventHandler(); + bookEventHandler.handleBookBeforeCreate(book); + Mockito.verify(book,Mockito.times(1)).getAuthors(); + + } + + @Test + public void whenCreateAuthorThenSuccess() { + Author author = mock(Author.class); + BookEventHandler bookEventHandler = new BookEventHandler(); + bookEventHandler.handleAuthorBeforeCreate(author); + Mockito.verify(author,Mockito.times(1)).getBooks(); + + } +}