add CustomerControllerRestTest and -UnitTest
This commit is contained in:
parent
2610ecd311
commit
fe4fef2752
@ -47,13 +47,13 @@ If you have at least Docker, the Java JDK and Gradle installed in appropriate ve
|
|||||||
# the following command should return a JSON array with just all customers:
|
# the following command should return a JSON array with just all customers:
|
||||||
curl \
|
curl \
|
||||||
-H 'current-user: mike@hostsharing.net' \
|
-H 'current-user: mike@hostsharing.net' \
|
||||||
http://localhost:8080/api/customer
|
http://localhost:8080/api/customers
|
||||||
|
|
||||||
# the following command should return a JSON array with just all packages visible for the admin of the customer aab:
|
# the following command should return a JSON array with just all packages visible for the admin of the customer aab:
|
||||||
curl \
|
curl \
|
||||||
-H 'current-user: mike@hostsharing.net' \
|
-H 'current-user: mike@hostsharing.net' \
|
||||||
-H 'assumed-roles: customer#aab.admin' \
|
-H 'assumed-roles: customer#aab.admin' \
|
||||||
http://localhost:8080/api/package
|
http://localhost:8080/api/packages
|
||||||
|
|
||||||
The latter `curl` command actually goes through the database server.
|
The latter `curl` command actually goes through the database server.
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ public class CustomerController {
|
|||||||
private CustomerRepository customerRepository;
|
private CustomerRepository customerRepository;
|
||||||
|
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@RequestMapping(value = "/api/customer", method = RequestMethod.GET)
|
@RequestMapping(value = "/api/customers", method = RequestMethod.GET)
|
||||||
@Transactional
|
@Transactional
|
||||||
public List<CustomerEntity> listCustomers(
|
public List<CustomerEntity> listCustomers(
|
||||||
@RequestHeader(value = "current-user") String userName,
|
@RequestHeader(value = "current-user") String userName,
|
||||||
|
@ -0,0 +1,49 @@
|
|||||||
|
package net.hostsharing.hsadminng.hscustomer;
|
||||||
|
|
||||||
|
import net.hostsharing.hsadminng.context.Context;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
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 static java.util.Collections.singletonList;
|
||||||
|
import static java.util.UUID.randomUUID;
|
||||||
|
import static org.hamcrest.Matchers.hasSize;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
@WebMvcTest(CustomerController.class)
|
||||||
|
class CustomerControllerRestTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
MockMvc mockMvc;
|
||||||
|
@MockBean
|
||||||
|
Context contextMock;
|
||||||
|
@MockBean
|
||||||
|
CustomerRepository customerRepositoryMock;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void apiCustomersWillReturnCustomersFromRepository() throws Exception {
|
||||||
|
|
||||||
|
// given
|
||||||
|
when(customerRepositoryMock.findAll()).thenReturn(asList(TestCustomer.xxx, TestCustomer.yyy));
|
||||||
|
|
||||||
|
// when
|
||||||
|
final var pacs = mockMvc.perform(MockMvcRequestBuilders
|
||||||
|
.get("/api/customers")
|
||||||
|
.header("current-user", "mike@hostsharing.net")
|
||||||
|
.accept(MediaType.APPLICATION_JSON))
|
||||||
|
|
||||||
|
// then
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$", hasSize(2)))
|
||||||
|
.andExpect(jsonPath("$[0].prefix", is(TestCustomer.xxx.getPrefix())))
|
||||||
|
.andExpect(jsonPath("$[1].reference", is(TestCustomer.yyy.getReference())));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
package net.hostsharing.hsadminng.hscustomer;
|
||||||
|
|
||||||
|
import net.hostsharing.hsadminng.context.Context;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
import static java.util.Arrays.asList;
|
||||||
|
import static java.util.Collections.singletonList;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class CustomerControllerUnitTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
Context contextMock;
|
||||||
|
@Mock
|
||||||
|
CustomerRepository customerRepositoryMock;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
CustomerController customerController;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void apiCustomersWillReturnCustomersFromRepository() throws Exception {
|
||||||
|
|
||||||
|
// given
|
||||||
|
when(customerRepositoryMock.findAll()).thenReturn(asList(TestCustomer.xxx, TestCustomer.yyy));
|
||||||
|
|
||||||
|
// when
|
||||||
|
final var pacs = customerController.listCustomers("mike@hostsharing.net", null);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(pacs).hasSize(2);
|
||||||
|
verify(contextMock).setCurrentUser("mike@hostsharing.net");
|
||||||
|
verify(contextMock, never()).assumeRoles(any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findAllWithAssumedCustomerAdminRole() throws Exception {
|
||||||
|
|
||||||
|
// given
|
||||||
|
when(customerRepositoryMock.findAll()).thenReturn(singletonList(TestCustomer.yyy));
|
||||||
|
|
||||||
|
// when
|
||||||
|
final var pacs = customerController.listCustomers("mike@hostsharing.net", "customer#yyy.admin");
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(pacs).hasSize(1);
|
||||||
|
verify(contextMock).setCurrentUser("mike@hostsharing.net");
|
||||||
|
verify(contextMock).assumeRoles("customer#yyy.admin");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package net.hostsharing.hsadminng.hscustomer;
|
||||||
|
|
||||||
|
import static java.util.UUID.randomUUID;
|
||||||
|
|
||||||
|
public class TestCustomer {
|
||||||
|
|
||||||
|
static final CustomerEntity xxx = customer("xxx", 10001, "xxx@example.com");
|
||||||
|
static final CustomerEntity yyy = customer("yyy", 10002, "yyy@example.com");
|
||||||
|
|
||||||
|
|
||||||
|
static public CustomerEntity customer(final String prefix, final int reference, final String adminName) {
|
||||||
|
return new CustomerEntity(randomUUID(), prefix, reference, adminName);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user