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()) {
context.assumeRoles(assumedRoles);
}
return customerRepository.findCustomerByOptionalPrefix(prefix);
return customerRepository.findCustomerByOptionalPrefixLike(prefix);
}
@PostMapping(value = "/api/customers")
@ResponseStatus
@Transactional
public CustomerEntity addCustomer(
@RequestHeader(value = "current-user") String userName,

View File

@ -14,7 +14,7 @@ public interface CustomerRepository extends Repository<CustomerEntity, UUID> {
Optional<CustomerEntity> findByUuid(UUID id);
@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);

View File

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

View File

@ -1,9 +1,13 @@
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;
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 {
// 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");
}
}