Merge pull request #8125 from eugenp/revert-8119-BAEL-3275-2

Revert "BAEL-3275: Using blocking queue for pub-sub"
This commit is contained in:
Eric Martin
2019-10-31 20:43:47 -05:00
committed by GitHub
parent db85c8f275
commit 3225470df5
20543 changed files with 1642750 additions and 0 deletions
@@ -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());
}
}
@@ -0,0 +1,118 @@
package com.baeldung.books.validator;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
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.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;
import com.baeldung.books.SpringDataRestApplication;
import com.baeldung.books.models.WebsiteUser;
import com.fasterxml.jackson.databind.ObjectMapper;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringDataRestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureMockMvc
public class SpringDataRestValidatorIntegrationTest {
public static final String URL = "http://localhost";
@Autowired
private MockMvc mockMvc;
@Autowired
protected WebApplicationContext wac;
@Before
public void setup() {
mockMvc = webAppContextSetup(wac).build();
}
@Test
public void whenStartingApplication_thenCorrectStatusCode() throws Exception {
mockMvc.perform(get("/users")).andExpect(status().is2xxSuccessful());
};
@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
public void whenAddingNewCorrectUser_thenCorrectStatusCodeAndResponse() throws Exception {
WebsiteUser user = new WebsiteUser();
user.setEmail("john.doe@john.com");
user.setName("John Doe");
mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))).andExpect(status().is2xxSuccessful()).andExpect(redirectedUrl("http://localhost/users/1"));
}
@Test
public void whenAddingNewUserWithoutName_thenErrorStatusCodeAndResponse() throws Exception {
WebsiteUser user = new WebsiteUser();
user.setEmail("john.doe@john.com");
mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))).andExpect(status().isNotAcceptable()).andExpect(redirectedUrl(null));
}
@Test
public void whenAddingNewUserWithEmptyName_thenErrorStatusCodeAndResponse() throws Exception {
WebsiteUser user = new WebsiteUser();
user.setEmail("john.doe@john.com");
user.setName("");
mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))).andExpect(status().isNotAcceptable()).andExpect(redirectedUrl(null));
}
@Test
public void whenAddingNewUserWithoutEmail_thenErrorStatusCodeAndResponse() throws Exception {
WebsiteUser user = new WebsiteUser();
user.setName("John Doe");
mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))).andExpect(status().isNotAcceptable()).andExpect(redirectedUrl(null));
}
@Test
public void whenAddingNewUserWithEmptyEmail_thenErrorStatusCodeAndResponse() throws Exception {
WebsiteUser user = new WebsiteUser();
user.setName("John Doe");
user.setEmail("");
mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))).andExpect(status().isNotAcceptable()).andExpect(redirectedUrl(null));
}
@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
public void whenDeletingCorrectUser_thenCorrectStatusCodeAndResponse() throws Exception {
WebsiteUser user = new WebsiteUser();
user.setEmail("john.doe@john.com");
user.setName("John Doe");
mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))).andExpect(status().is2xxSuccessful()).andExpect(redirectedUrl("http://localhost/users/1"));
mockMvc.perform(delete("/users/1").contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))).andExpect(status().isMethodNotAllowed());
}
@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
public void whenSearchingByEmail_thenCorrectStatusCodeAndResponse() throws Exception {
WebsiteUser user = new WebsiteUser();
user.setEmail("john.doe@john.com");
user.setName("John Doe");
mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))).andExpect(status().is2xxSuccessful()).andExpect(redirectedUrl("http://localhost/users/1"));
mockMvc.perform(get("/users/search/byEmail").param("email", user.getEmail()).contentType(MediaType.APPLICATION_JSON)).andExpect(status().is2xxSuccessful());
}
@Test
public void whenSearchingByEmailWithOriginalMethodName_thenErrorStatusCodeAndResponse() throws Exception {
WebsiteUser user = new WebsiteUser();
user.setEmail("john.doe@john.com");
user.setName("John Doe");
mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))).andExpect(status().is2xxSuccessful()).andExpect(redirectedUrl("http://localhost/users/1"));
mockMvc.perform(get("/users/search/findByEmail").param("email", user.getEmail()).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isNotFound());
}
}
@@ -0,0 +1,58 @@
package com.baeldung.springdatawebsupport.application.test;
import com.baeldung.springdatawebsupport.application.controllers.UserController;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class UserControllerIntegrationTest {
@Autowired
private UserController userController;
@Autowired
private MockMvc mockMvc;
@Test
public void whenUserControllerInjected_thenNotNull() throws Exception {
assertThat(userController).isNotNull();
}
@Test
public void whenGetRequestToUsersEndPointWithIdPathVariable_thenCorrectResponse() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/users/{id}", "1").contentType(MediaType.APPLICATION_JSON_UTF8)).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.jsonPath("$.id").value("1"));
}
@Test
public void whenGetRequestToUsersEndPoint_thenCorrectResponse() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/users").contentType(MediaType.APPLICATION_JSON_UTF8)).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.jsonPath("$['pageable']['paged']").value("true"));
}
@Test
public void whenGetRequestToUserEndPointWithNameRequestParameter_thenCorrectResponse() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/users").param("name", "John").contentType(MediaType.APPLICATION_JSON_UTF8)).andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$['content'][0].['name']").value("John"));
}
@Test
public void whenGetRequestToSorteredUsersEndPoint_thenCorrectResponse() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/sortedusers").contentType(MediaType.APPLICATION_JSON_UTF8)).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.jsonPath("$['sort']['sorted']").value("true"));
}
@Test
public void whenGetRequestToFilteredUsersEndPoint_thenCorrectResponse() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/filteredusers").param("name", "John").contentType(MediaType.APPLICATION_JSON_UTF8)).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.jsonPath("$[0].name").value("John"));
}
}
@@ -0,0 +1,17 @@
package org.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 SpringContextIntegrationTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}
@@ -0,0 +1,17 @@
package org.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() {
}
}