1
0

PackageController+Repository with name search option

This commit is contained in:
Michael Hoennig
2022-08-04 09:26:01 +02:00
parent 212b1077c8
commit 8605d4c4f1
8 changed files with 53 additions and 31 deletions

View File

@ -30,7 +30,7 @@ class CustomerControllerRestTest {
void apiCustomersWillReturnAllCustomersFromRepositoryIfNoCriteriaGiven() throws Exception {
// given
when(customerRepositoryMock.findCustomerByOptionalPrefix(null)).thenReturn(asList(TestCustomer.xxx, TestCustomer.yyy));
when(customerRepositoryMock.findCustomerByOptionalPrefixLike(null)).thenReturn(asList(TestCustomer.xxx, TestCustomer.yyy));
// when
mockMvc.perform(MockMvcRequestBuilders
@ -49,7 +49,7 @@ class CustomerControllerRestTest {
void apiCustomersWillReturnMatchingCustomersFromRepositoryIfCriteriaGiven() throws Exception {
// given
when(customerRepositoryMock.findCustomerByOptionalPrefix("x")).thenReturn(asList(TestCustomer.xxx));
when(customerRepositoryMock.findCustomerByOptionalPrefixLike("x")).thenReturn(asList(TestCustomer.xxx));
// when
mockMvc.perform(MockMvcRequestBuilders

View File

@ -27,7 +27,7 @@ class CustomerControllerUnitTest {
void apiCustomersWillReturnCustomersFromRepository() throws Exception {
// given
when(customerRepositoryMock.findCustomerByOptionalPrefix(null)).thenReturn(asList(TestCustomer.xxx, TestCustomer.yyy));
when(customerRepositoryMock.findCustomerByOptionalPrefixLike(null)).thenReturn(asList(TestCustomer.xxx, TestCustomer.yyy));
// when
final var pacs = customerController.listCustomers("mike@hostsharing.net", null, null);
@ -42,7 +42,7 @@ class CustomerControllerUnitTest {
void findAllWithAssumedCustomerAdminRole() throws Exception {
// given
when(customerRepositoryMock.findCustomerByOptionalPrefix(null)).thenReturn(singletonList(TestCustomer.yyy));
when(customerRepositoryMock.findCustomerByOptionalPrefixLike(null)).thenReturn(singletonList(TestCustomer.yyy));
// when
final var pacs = customerController.listCustomers("mike@hostsharing.net", "customer#yyy.admin", null);

View File

@ -104,7 +104,7 @@ class CustomerRepositoryIntegrationTest {
currentUser("mike@hostsharing.net");
// when
final var result = customerRepository.findCustomerByOptionalPrefix(null);
final var result = customerRepository.findCustomerByOptionalPrefixLike(null);
// then
exactlyTheseCustomersAreReturned(result, "aaa", "aab", "aac");
@ -117,7 +117,7 @@ class CustomerRepositoryIntegrationTest {
assumedRoles("global#hostsharing.admin");
// when
final var result = customerRepository.findCustomerByOptionalPrefix(null);
final var result = customerRepository.findCustomerByOptionalPrefixLike(null);
then:
exactlyTheseCustomersAreReturned(result, "aaa", "aab", "aac");
@ -129,7 +129,7 @@ class CustomerRepositoryIntegrationTest {
currentUser("admin@aaa.example.com");
// when:
final var result = customerRepository.findCustomerByOptionalPrefix(null);
final var result = customerRepository.findCustomerByOptionalPrefixLike(null);
// then:
exactlyTheseCustomersAreReturned(result, "aaa");
@ -140,7 +140,7 @@ class CustomerRepositoryIntegrationTest {
currentUser("admin@aaa.example.com");
assumedRoles("package#aaa00.admin");
final var result = customerRepository.findCustomerByOptionalPrefix(null);
final var result = customerRepository.findCustomerByOptionalPrefixLike(null);
exactlyTheseCustomersAreReturned(result, "aaa");
}
@ -154,7 +154,7 @@ class CustomerRepositoryIntegrationTest {
// when
final var attempt = attempt(
em,
() -> customerRepository.findCustomerByOptionalPrefix(null));
() -> customerRepository.findCustomerByOptionalPrefixLike(null));
// then
attempt.assertExceptionWithRootCauseMessage(
@ -168,7 +168,7 @@ class CustomerRepositoryIntegrationTest {
final var attempt = attempt(
em,
() -> customerRepository.findCustomerByOptionalPrefix(null));
() -> customerRepository.findCustomerByOptionalPrefixLike(null));
attempt.assertExceptionWithRootCauseMessage(
JpaSystemException.class,
@ -183,7 +183,7 @@ class CustomerRepositoryIntegrationTest {
final var attempt = attempt(
em,
() -> customerRepository.findCustomerByOptionalPrefix(null));
() -> customerRepository.findCustomerByOptionalPrefixLike(null));
attempt.assertExceptionWithRootCauseMessage(
JpaSystemException.class,
@ -201,7 +201,7 @@ class CustomerRepositoryIntegrationTest {
currentUser("mike@hostsharing.net");
// when
final var result = customerRepository.findCustomerByOptionalPrefix("aab");
final var result = customerRepository.findCustomerByOptionalPrefixLike("aab");
// then
exactlyTheseCustomersAreReturned(result, "aab");
@ -213,7 +213,7 @@ class CustomerRepositoryIntegrationTest {
currentUser("admin@aaa.example.com");
// when:
final var result = customerRepository.findCustomerByOptionalPrefix("aab");
final var result = customerRepository.findCustomerByOptionalPrefixLike("aab");
// then:
exactlyTheseCustomersAreReturned(result);

View File

@ -9,7 +9,8 @@ import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static java.util.Arrays.asList;
import java.util.List;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.verify;
@ -28,11 +29,11 @@ class PackageControllerRestTest {
PackageRepository packageRepositoryMock;
@Test
void findAll() throws Exception {
void listPackagesWithoutNameParameter() throws Exception {
// given
final var givenPacs = asList(TestPackage.xxx00, TestPackage.xxx01, TestPackage.xxx02);
when(packageRepositoryMock.findAll()).thenReturn(givenPacs);
final var givenPacs = List.of(TestPackage.xxx00, TestPackage.xxx01, TestPackage.xxx02);
when(packageRepositoryMock.findAllByOptionalNameLike(null)).thenReturn(givenPacs);
// when
final var pacs = mockMvc.perform(MockMvcRequestBuilders
@ -52,4 +53,26 @@ class PackageControllerRestTest {
verify(contextMock).assumeRoles("customer#xxx.admin");
}
@Test
void listPackagesWithNameParameter() throws Exception {
// given
final var givenPacs = List.of(TestPackage.xxx01);
when(packageRepositoryMock.findAllByOptionalNameLike("xxx01")).thenReturn(givenPacs);
// when
final var pacs = mockMvc.perform(MockMvcRequestBuilders
.get("/api/packages?name=xxx01")
.header("current-user", "mike@hostsharing.net")
.header("assumed-roles", "customer#xxx.admin")
.accept(MediaType.APPLICATION_JSON))
// then
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].name", is("xxx01")));
verify(contextMock).setCurrentUser("mike@hostsharing.net");
verify(contextMock).assumeRoles("customer#xxx.admin");
}
}