minor formatting cleanup
This commit is contained in:
parent
8ccec9a413
commit
2e733c3867
@ -20,7 +20,7 @@ import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
// @PropertySource("persistence-h2.properties")
|
||||
// @PropertySource("persistence-hsqldb.properties")
|
||||
// @PropertySource("persistence-derby.properties")
|
||||
//@PropertySource("persistence-sqlite.properties")
|
||||
// @PropertySource("persistence-sqlite.properties")
|
||||
public class DbConfig {
|
||||
|
||||
@Autowired
|
||||
@ -65,21 +65,23 @@ public class DbConfig {
|
||||
@Configuration
|
||||
@Profile("h2")
|
||||
@PropertySource("classpath:persistence-h2.properties")
|
||||
class H2Config {}
|
||||
class H2Config {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Profile("hsqldb")
|
||||
@PropertySource("classpath:persistence-hsqldb.properties")
|
||||
class HsqldbConfig {}
|
||||
|
||||
class HsqldbConfig {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Profile("derby")
|
||||
@PropertySource("classpath:persistence-derby.properties")
|
||||
class DerbyConfig {}
|
||||
|
||||
class DerbyConfig {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Profile("sqlite")
|
||||
@PropertySource("classpath:persistence-sqlite.properties")
|
||||
class SqliteConfig {}
|
||||
class SqliteConfig {
|
||||
}
|
||||
|
@ -11,11 +11,11 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
public class MvcConfig implements WebMvcConfigurer {
|
||||
|
||||
public MvcConfig(){
|
||||
|
||||
public MvcConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
||||
configurer.enable();
|
||||
@ -27,7 +27,7 @@ public class MvcConfig implements WebMvcConfigurer {
|
||||
}
|
||||
|
||||
@Bean
|
||||
BookEventHandler bookEventHandler(){
|
||||
BookEventHandler bookEventHandler() {
|
||||
return new BookEventHandler();
|
||||
}
|
||||
|
||||
|
@ -12,10 +12,9 @@ import org.springframework.http.HttpMethod;
|
||||
public class RestConfig implements RepositoryRestConfigurer {
|
||||
|
||||
@Override
|
||||
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration repositoryRestConfiguration){
|
||||
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration repositoryRestConfiguration) {
|
||||
repositoryRestConfiguration.getProjectionConfiguration().addProjection(CustomBook.class);
|
||||
ExposureConfiguration config = repositoryRestConfiguration.getExposureConfiguration();
|
||||
config.forDomainType(WebsiteUser.class).withItemExposure((metadata, httpMethods) ->
|
||||
httpMethods.disable(HttpMethod.PATCH));
|
||||
config.forDomainType(WebsiteUser.class).withItemExposure((metadata, httpMethods) -> httpMethods.disable(HttpMethod.PATCH));
|
||||
}
|
||||
}
|
||||
|
@ -24,13 +24,7 @@ public class ValidatorEventRegister implements InitializingBean {
|
||||
List<String> events = Arrays.asList("beforeCreate", "afterCreate", "beforeSave", "afterSave", "beforeLinkSave", "afterLinkSave", "beforeDelete", "afterDelete");
|
||||
|
||||
for (Map.Entry<String, Validator> entry : validators.entrySet()) {
|
||||
events
|
||||
.stream()
|
||||
.filter(p -> entry
|
||||
.getKey()
|
||||
.startsWith(p))
|
||||
.findFirst()
|
||||
.ifPresent(p -> validatingRepositoryEventListener.addValidator(p, entry.getValue()));
|
||||
events.stream().filter(p -> entry.getKey().startsWith(p)).findFirst().ifPresent(p -> validatingRepositoryEventListener.addValidator(p, entry.getValue()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,33 +8,34 @@ import java.util.logging.Logger;
|
||||
|
||||
@RepositoryEventHandler
|
||||
public class AuthorEventHandler {
|
||||
Logger logger = Logger.getLogger("Class AuthorEventHandler");
|
||||
public AuthorEventHandler(){
|
||||
super();
|
||||
}
|
||||
Logger logger = Logger.getLogger("Class AuthorEventHandler");
|
||||
|
||||
@HandleBeforeCreate
|
||||
public void handleAuthorBeforeCreate(Author author){
|
||||
logger.info("Inside Author Before Create....");
|
||||
String name = author.getName();
|
||||
}
|
||||
public AuthorEventHandler() {
|
||||
super();
|
||||
}
|
||||
|
||||
@HandleAfterCreate
|
||||
public void handleAuthorAfterCreate(Author author){
|
||||
logger.info("Inside Author After Create ....");
|
||||
String name = author.getName();
|
||||
}
|
||||
@HandleBeforeCreate
|
||||
public void handleAuthorBeforeCreate(Author author) {
|
||||
logger.info("Inside Author Before Create....");
|
||||
String name = author.getName();
|
||||
}
|
||||
|
||||
@HandleBeforeDelete
|
||||
public void handleAuthorBeforeDelete(Author author){
|
||||
logger.info("Inside Author Before Delete ....");
|
||||
String name = author.getName();
|
||||
}
|
||||
@HandleAfterCreate
|
||||
public void handleAuthorAfterCreate(Author author) {
|
||||
logger.info("Inside Author After Create ....");
|
||||
String name = author.getName();
|
||||
}
|
||||
|
||||
@HandleAfterDelete
|
||||
public void handleAuthorAfterDelete(Author author){
|
||||
logger.info("Inside Author After Delete ....");
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -10,17 +10,18 @@ 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 logger = Logger.getLogger("Class BookEventHandler");
|
||||
|
||||
logger.info("Inside Book Before Create ....");
|
||||
book.getAuthors();
|
||||
}
|
||||
@HandleBeforeCreate
|
||||
public void handleBookBeforeCreate(Book book) {
|
||||
|
||||
@HandleBeforeCreate
|
||||
public void handleAuthorBeforeCreate(Author author){
|
||||
logger.info("Inside Author Before Create ....");
|
||||
author.getBooks();
|
||||
}
|
||||
logger.info("Inside Book Before Create ....");
|
||||
book.getAuthors();
|
||||
}
|
||||
|
||||
@HandleBeforeCreate
|
||||
public void handleAuthorBeforeCreate(Author author) {
|
||||
logger.info("Inside Author Before Create ....");
|
||||
author.getBooks();
|
||||
}
|
||||
}
|
||||
|
@ -19,12 +19,7 @@ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionH
|
||||
public ResponseEntity<Object> handleAccessDeniedException(Exception ex, WebRequest request) {
|
||||
RepositoryConstraintViolationException nevEx = (RepositoryConstraintViolationException) ex;
|
||||
|
||||
String errors = nevEx
|
||||
.getErrors()
|
||||
.getAllErrors()
|
||||
.stream()
|
||||
.map(ObjectError::toString)
|
||||
.collect(Collectors.joining("\n"));
|
||||
String errors = nevEx.getErrors().getAllErrors().stream().map(ObjectError::toString).collect(Collectors.joining("\n"));
|
||||
return new ResponseEntity<>(errors, new HttpHeaders(), HttpStatus.NOT_ACCEPTABLE);
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,7 @@ import javax.persistence.OneToOne;
|
||||
public class Address {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
|
@ -16,7 +16,7 @@ import javax.persistence.ManyToMany;
|
||||
public class Author {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
|
@ -16,14 +16,14 @@ import javax.persistence.ManyToOne;
|
||||
public class Book {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String title;
|
||||
|
||||
|
||||
private String isbn;
|
||||
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "library_id")
|
||||
private Library library;
|
||||
@ -63,7 +63,6 @@ public class Book {
|
||||
this.isbn = isbn;
|
||||
}
|
||||
|
||||
|
||||
public Library getLibrary() {
|
||||
return library;
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ import org.springframework.data.rest.core.annotation.RestResource;
|
||||
public class Library {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
@Column
|
||||
|
@ -10,7 +10,7 @@ import javax.persistence.Id;
|
||||
public class Subject {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
|
@ -8,15 +8,15 @@ import org.springframework.data.rest.core.config.Projection;
|
||||
import com.baeldung.models.Author;
|
||||
import com.baeldung.models.Book;
|
||||
|
||||
@Projection(name = "customBook", types = { Book.class })
|
||||
@Projection(name = "customBook", types = { Book.class })
|
||||
public interface CustomBook {
|
||||
@Value("#{target.id}")
|
||||
long getId();
|
||||
|
||||
long getId();
|
||||
|
||||
String getTitle();
|
||||
|
||||
|
||||
List<Author> getAuthors();
|
||||
|
||||
|
||||
@Value("#{target.getAuthors().size()}")
|
||||
int getAuthorCount();
|
||||
}
|
||||
|
@ -7,4 +7,5 @@ import com.baeldung.models.Book;
|
||||
import com.baeldung.projections.CustomBook;
|
||||
|
||||
@RepositoryRestResource(excerptProjection = CustomBook.class)
|
||||
public interface BookRepository extends CrudRepository<Book, Long> {}
|
||||
public interface BookRepository extends CrudRepository<Book, Long> {
|
||||
}
|
||||
|
@ -8,8 +8,8 @@ import org.springframework.data.rest.core.annotation.RestResource;
|
||||
import com.baeldung.models.Subject;
|
||||
|
||||
public interface SubjectRepository extends PagingAndSortingRepository<Subject, Long> {
|
||||
|
||||
|
||||
@RestResource(path = "nameContains")
|
||||
public Page<Subject> findByNameContaining(@Param("name") String name, Pageable p);
|
||||
|
||||
|
||||
}
|
@ -9,21 +9,21 @@ import static org.mockito.Mockito.mock;
|
||||
|
||||
public class AuthorEventHandlerUnitTest {
|
||||
|
||||
@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 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();
|
||||
@Test
|
||||
public void whenDeleteAuthorThenSuccess() {
|
||||
Author author = mock(Author.class);
|
||||
AuthorEventHandler authorEventHandler = new AuthorEventHandler();
|
||||
authorEventHandler.handleAuthorAfterDelete(author);
|
||||
Mockito.verify(author, Mockito.times(1)).getName();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,21 +8,21 @@ import org.mockito.Mockito;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
public class BookEventHandlerUnitTest {
|
||||
@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 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();
|
||||
@Test
|
||||
public void whenCreateAuthorThenSuccess() {
|
||||
Author author = mock(Author.class);
|
||||
BookEventHandler bookEventHandler = new BookEventHandler();
|
||||
bookEventHandler.handleAuthorBeforeCreate(author);
|
||||
Mockito.verify(author, Mockito.times(1)).getBooks();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -29,16 +29,15 @@ public class SpringDataProjectionLiveTest {
|
||||
private static final String BOOK_ENDPOINT = "http://localhost:8080/books";
|
||||
private static final String AUTHOR_ENDPOINT = "http://localhost:8080/authors";
|
||||
|
||||
|
||||
@Autowired
|
||||
private BookRepository bookRepo;
|
||||
|
||||
@Autowired
|
||||
private AuthorRepository authorRepo;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup(){
|
||||
if(bookRepo.findById(1L) == null){
|
||||
public void setup() {
|
||||
if (bookRepo.findById(1L) == null) {
|
||||
Book book = new Book("Animal Farm");
|
||||
book.setIsbn("978-1943138425");
|
||||
book = bookRepo.save(book);
|
||||
@ -48,45 +47,44 @@ public class SpringDataProjectionLiveTest {
|
||||
author = authorRepo.save(author);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenGetBook_thenOK(){
|
||||
final Response response = RestAssured.get(BOOK_ENDPOINT+"/1");
|
||||
|
||||
public void whenGetBook_thenOK() {
|
||||
final Response response = RestAssured.get(BOOK_ENDPOINT + "/1");
|
||||
|
||||
assertEquals(200, response.getStatusCode());
|
||||
assertTrue(response.asString().contains("isbn"));
|
||||
assertFalse(response.asString().contains("authorCount"));
|
||||
// System.out.println(response.asString());
|
||||
// System.out.println(response.asString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void whenGetBookProjection_thenOK(){
|
||||
final Response response = RestAssured.get(BOOK_ENDPOINT+"/1?projection=customBook");
|
||||
|
||||
public void whenGetBookProjection_thenOK() {
|
||||
final Response response = RestAssured.get(BOOK_ENDPOINT + "/1?projection=customBook");
|
||||
|
||||
assertEquals(200, response.getStatusCode());
|
||||
assertFalse(response.asString().contains("isbn"));
|
||||
assertTrue(response.asString().contains("authorCount"));
|
||||
// System.out.println(response.asString());
|
||||
assertTrue(response.asString().contains("authorCount"));
|
||||
// System.out.println(response.asString());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenGetAllBooks_thenOK(){
|
||||
public void whenGetAllBooks_thenOK() {
|
||||
final Response response = RestAssured.get(BOOK_ENDPOINT);
|
||||
|
||||
|
||||
assertEquals(200, response.getStatusCode());
|
||||
assertFalse(response.asString().contains("isbn"));
|
||||
assertTrue(response.asString().contains("authorCount"));
|
||||
// System.out.println(response.asString());
|
||||
assertTrue(response.asString().contains("authorCount"));
|
||||
// System.out.println(response.asString());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenGetAuthorBooks_thenOK(){
|
||||
final Response response = RestAssured.get(AUTHOR_ENDPOINT+"/1/books");
|
||||
|
||||
public void whenGetAuthorBooks_thenOK() {
|
||||
final Response response = RestAssured.get(AUTHOR_ENDPOINT + "/1/books");
|
||||
|
||||
assertEquals(200, response.getStatusCode());
|
||||
assertFalse(response.asString().contains("isbn"));
|
||||
assertTrue(response.asString().contains("authorCount"));
|
||||
assertTrue(response.asString().contains("authorCount"));
|
||||
System.out.println(response.asString());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user