JAVA-11535 Move spring-data related modules to persistence-modules (#12126)

This commit is contained in:
anuragkumawat
2022-04-26 19:28:06 +05:30
committed by GitHub
parent 8735a58dd0
commit 4553469318
74 changed files with 6 additions and 9 deletions
@@ -0,0 +1,17 @@
package com.baeldung;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.books.SpringDataRestApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringDataRestApplication.class)
public class SpringContextTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}
@@ -0,0 +1,31 @@
package com.baeldung.books.events;
import com.baeldung.books.events.AuthorEventHandler;
import com.baeldung.books.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 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 whenDeleteAuthorThenSuccess() {
Author author = mock(Author.class);
AuthorEventHandler authorEventHandler = new AuthorEventHandler();
authorEventHandler.handleAuthorAfterDelete(author);
Mockito.verify(author, Mockito.times(1)).getName();
}
}
@@ -0,0 +1,30 @@
package com.baeldung.books.events;
import com.baeldung.books.events.BookEventHandler;
import com.baeldung.books.models.Author;
import com.baeldung.books.models.Book;
import org.junit.Test;
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 whenCreateAuthorThenSuccess() {
Author author = mock(Author.class);
BookEventHandler bookEventHandler = new BookEventHandler();
bookEventHandler.handleAuthorBeforeCreate(author);
Mockito.verify(author, Mockito.times(1)).getBooks();
}
}
@@ -0,0 +1,90 @@
package com.baeldung.books.projections;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.books.SpringDataRestApplication;
import com.baeldung.books.models.Author;
import com.baeldung.books.models.Book;
import com.baeldung.books.repositories.AuthorRepository;
import com.baeldung.books.repositories.BookRepository;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringDataRestApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT)
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).isPresent()) {
Book book = new Book("Animal Farm");
book.setIsbn("978-1943138425");
book = bookRepo.save(book);
Author author = new Author("George Orwell");
author = authorRepo.save(author);
author.setBooks(Arrays.asList(book));
author = authorRepo.save(author);
}
}
@Test
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());
}
@Test
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());
}
@Test
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());
}
@Test
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"));
System.out.println(response.asString());
}
}