Bael 1674 (#4335)
* Handle events - BAEL - 1674 * BAEL -1674 - test * test - BAEL-1674 * Change system.out to logger * minor change
This commit is contained in:
		
							parent
							
								
									7dc605f7b0
								
							
						
					
					
						commit
						60cb1d7d49
					
				| @ -1,5 +1,7 @@ | |||||||
| package com.baeldung.config; | 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.Bean; | ||||||
| import org.springframework.context.annotation.ComponentScan; | import org.springframework.context.annotation.ComponentScan; | ||||||
| import org.springframework.context.annotation.Configuration; | import org.springframework.context.annotation.Configuration; | ||||||
| @ -22,4 +24,14 @@ public class MvcConfig extends WebMvcConfigurerAdapter{ | |||||||
|         configurer.enable(); |         configurer.enable(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     @Bean | ||||||
|  |     AuthorEventHandler authorEventHandler() { | ||||||
|  |         return new AuthorEventHandler(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Bean | ||||||
|  |     BookEventHandler bookEventHandler(){ | ||||||
|  |         return new BookEventHandler(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -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(); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -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(); | ||||||
|  |         } | ||||||
|  | } | ||||||
| @ -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(); | ||||||
|  | 
 | ||||||
|  |         } | ||||||
|  | } | ||||||
| @ -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(); | ||||||
|  | 
 | ||||||
|  |         } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user