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

@ -29,11 +29,10 @@ public class CustomerController {
if (assumedRoles != null && !assumedRoles.isBlank()) { if (assumedRoles != null && !assumedRoles.isBlank()) {
context.assumeRoles(assumedRoles); context.assumeRoles(assumedRoles);
} }
return customerRepository.findCustomerByOptionalPrefix(prefix); return customerRepository.findCustomerByOptionalPrefixLike(prefix);
} }
@PostMapping(value = "/api/customers") @PostMapping(value = "/api/customers")
@ResponseStatus
@Transactional @Transactional
public CustomerEntity addCustomer( public CustomerEntity addCustomer(
@RequestHeader(value = "current-user") String userName, @RequestHeader(value = "current-user") String userName,

View File

@ -14,7 +14,7 @@ public interface CustomerRepository extends Repository<CustomerEntity, UUID> {
Optional<CustomerEntity> findByUuid(UUID id); Optional<CustomerEntity> findByUuid(UUID id);
@Query("SELECT c FROM CustomerEntity c WHERE :prefix is null or c.prefix like concat(:prefix, '%')") @Query("SELECT c FROM CustomerEntity c WHERE :prefix is null or c.prefix like concat(:prefix, '%')")
List<CustomerEntity> findCustomerByOptionalPrefix(@Param("prefix") String prefix); List<CustomerEntity> findCustomerByOptionalPrefixLike(@Param("prefix") String prefix);
CustomerEntity save(final CustomerEntity entity); CustomerEntity save(final CustomerEntity entity);

View File

@ -2,16 +2,12 @@ package net.hostsharing.hsadminng.hs.hspackage;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import java.util.List; import java.util.List;
@Controller @RestController
public class PackageController { public class PackageController {
@Autowired @Autowired
@ -20,18 +16,18 @@ public class PackageController {
@Autowired @Autowired
private PackageRepository packageRepository; private PackageRepository packageRepository;
@ResponseBody
@RequestMapping(value = "/api/packages", method = RequestMethod.GET) @RequestMapping(value = "/api/packages", method = RequestMethod.GET)
@Transactional @Transactional
public List<PackageEntity> listPackages( public List<PackageEntity> listPackages(
@RequestHeader(value = "current-user") String userName, @RequestHeader(value = "current-user") String userName,
@RequestHeader(value = "assumed-roles", required = false) String assumedRoles @RequestHeader(value = "assumed-roles", required = false) String assumedRoles,
@RequestParam(required = false) String name
) { ) {
context.setCurrentUser(userName); context.setCurrentUser(userName);
if (assumedRoles != null && !assumedRoles.isBlank()) { if (assumedRoles != null && !assumedRoles.isBlank()) {
context.assumeRoles(assumedRoles); context.assumeRoles(assumedRoles);
} }
return packageRepository.findAll(); return packageRepository.findAllByOptionalNameLike(name);
} }
} }

View File

@ -1,9 +1,13 @@
package net.hostsharing.hsadminng.hs.hspackage; package net.hostsharing.hsadminng.hs.hspackage;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import java.util.List;
import java.util.UUID; import java.util.UUID;
public interface PackageRepository extends JpaRepository<PackageEntity, UUID> { public interface PackageRepository extends Repository<PackageEntity, UUID> {
@Query("SELECT p FROM PackageEntity p WHERE :name is null or p.name like concat(:name, '%')")
List<PackageEntity> findAllByOptionalNameLike(final String name);
} }

View File

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

View File

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

View File

@ -104,7 +104,7 @@ class CustomerRepositoryIntegrationTest {
currentUser("mike@hostsharing.net"); currentUser("mike@hostsharing.net");
// when // when
final var result = customerRepository.findCustomerByOptionalPrefix(null); final var result = customerRepository.findCustomerByOptionalPrefixLike(null);
// then // then
exactlyTheseCustomersAreReturned(result, "aaa", "aab", "aac"); exactlyTheseCustomersAreReturned(result, "aaa", "aab", "aac");
@ -117,7 +117,7 @@ class CustomerRepositoryIntegrationTest {
assumedRoles("global#hostsharing.admin"); assumedRoles("global#hostsharing.admin");
// when // when
final var result = customerRepository.findCustomerByOptionalPrefix(null); final var result = customerRepository.findCustomerByOptionalPrefixLike(null);
then: then:
exactlyTheseCustomersAreReturned(result, "aaa", "aab", "aac"); exactlyTheseCustomersAreReturned(result, "aaa", "aab", "aac");
@ -129,7 +129,7 @@ class CustomerRepositoryIntegrationTest {
currentUser("admin@aaa.example.com"); currentUser("admin@aaa.example.com");
// when: // when:
final var result = customerRepository.findCustomerByOptionalPrefix(null); final var result = customerRepository.findCustomerByOptionalPrefixLike(null);
// then: // then:
exactlyTheseCustomersAreReturned(result, "aaa"); exactlyTheseCustomersAreReturned(result, "aaa");
@ -140,7 +140,7 @@ class CustomerRepositoryIntegrationTest {
currentUser("admin@aaa.example.com"); currentUser("admin@aaa.example.com");
assumedRoles("package#aaa00.admin"); assumedRoles("package#aaa00.admin");
final var result = customerRepository.findCustomerByOptionalPrefix(null); final var result = customerRepository.findCustomerByOptionalPrefixLike(null);
exactlyTheseCustomersAreReturned(result, "aaa"); exactlyTheseCustomersAreReturned(result, "aaa");
} }
@ -154,7 +154,7 @@ class CustomerRepositoryIntegrationTest {
// when // when
final var attempt = attempt( final var attempt = attempt(
em, em,
() -> customerRepository.findCustomerByOptionalPrefix(null)); () -> customerRepository.findCustomerByOptionalPrefixLike(null));
// then // then
attempt.assertExceptionWithRootCauseMessage( attempt.assertExceptionWithRootCauseMessage(
@ -168,7 +168,7 @@ class CustomerRepositoryIntegrationTest {
final var attempt = attempt( final var attempt = attempt(
em, em,
() -> customerRepository.findCustomerByOptionalPrefix(null)); () -> customerRepository.findCustomerByOptionalPrefixLike(null));
attempt.assertExceptionWithRootCauseMessage( attempt.assertExceptionWithRootCauseMessage(
JpaSystemException.class, JpaSystemException.class,
@ -183,7 +183,7 @@ class CustomerRepositoryIntegrationTest {
final var attempt = attempt( final var attempt = attempt(
em, em,
() -> customerRepository.findCustomerByOptionalPrefix(null)); () -> customerRepository.findCustomerByOptionalPrefixLike(null));
attempt.assertExceptionWithRootCauseMessage( attempt.assertExceptionWithRootCauseMessage(
JpaSystemException.class, JpaSystemException.class,
@ -201,7 +201,7 @@ class CustomerRepositoryIntegrationTest {
currentUser("mike@hostsharing.net"); currentUser("mike@hostsharing.net");
// when // when
final var result = customerRepository.findCustomerByOptionalPrefix("aab"); final var result = customerRepository.findCustomerByOptionalPrefixLike("aab");
// then // then
exactlyTheseCustomersAreReturned(result, "aab"); exactlyTheseCustomersAreReturned(result, "aab");
@ -213,7 +213,7 @@ class CustomerRepositoryIntegrationTest {
currentUser("admin@aaa.example.com"); currentUser("admin@aaa.example.com");
// when: // when:
final var result = customerRepository.findCustomerByOptionalPrefix("aab"); final var result = customerRepository.findCustomerByOptionalPrefixLike("aab");
// then: // then:
exactlyTheseCustomersAreReturned(result); 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.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 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.hasSize;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@ -28,11 +29,11 @@ class PackageControllerRestTest {
PackageRepository packageRepositoryMock; PackageRepository packageRepositoryMock;
@Test @Test
void findAll() throws Exception { void listPackagesWithoutNameParameter() throws Exception {
// given // given
final var givenPacs = asList(TestPackage.xxx00, TestPackage.xxx01, TestPackage.xxx02); final var givenPacs = List.of(TestPackage.xxx00, TestPackage.xxx01, TestPackage.xxx02);
when(packageRepositoryMock.findAll()).thenReturn(givenPacs); when(packageRepositoryMock.findAllByOptionalNameLike(null)).thenReturn(givenPacs);
// when // when
final var pacs = mockMvc.perform(MockMvcRequestBuilders final var pacs = mockMvc.perform(MockMvcRequestBuilders
@ -52,4 +53,26 @@ class PackageControllerRestTest {
verify(contextMock).assumeRoles("customer#xxx.admin"); 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");
}
} }