introduce TestPackage.java

This commit is contained in:
Michael Hoennig 2022-07-30 18:37:32 +02:00
parent 1463807b89
commit 53d3d68021
3 changed files with 27 additions and 14 deletions

View File

@ -4,11 +4,11 @@ import static java.util.UUID.randomUUID;
public class TestCustomer { public class TestCustomer {
static final CustomerEntity xxx = customer("xxx", 10001, "xxx@example.com"); public static final CustomerEntity xxx = hsCustomer("xxx", 10001, "xxx@example.com");
static final CustomerEntity yyy = customer("yyy", 10002, "yyy@example.com"); static final CustomerEntity yyy = hsCustomer("yyy", 10002, "yyy@example.com");
static public CustomerEntity customer(final String prefix, final int reference, final String adminName) { static public CustomerEntity hsCustomer(final String prefix, final int reference, final String adminName) {
return new CustomerEntity(randomUUID(), prefix, reference, adminName); return new CustomerEntity(randomUUID(), prefix, reference, adminName);
} }
} }

View File

@ -1,7 +1,7 @@
package net.hostsharing.hsadminng.hspackage; package net.hostsharing.hsadminng.hspackage;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.hscustomer.CustomerEntity; import net.hostsharing.hsadminng.hscustomer.TestCustomer;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
@ -23,10 +23,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@WebMvcTest(PackageController.class) @WebMvcTest(PackageController.class)
class PackageControllerRestTest { class PackageControllerRestTest {
final CustomerEntity cust = new CustomerEntity(UUID.randomUUID(), "xyz", 10001, "xyz@example.com");
final PackageEntity pac00 = new PackageEntity(UUID.randomUUID(), "xyz00", cust);
final PackageEntity pac01 = new PackageEntity(UUID.randomUUID(), "xyz01", cust);
final PackageEntity pac02 = new PackageEntity(UUID.randomUUID(), "xyz02", cust);
@Autowired @Autowired
MockMvc mockMvc; MockMvc mockMvc;
@MockBean @MockBean
@ -38,25 +34,25 @@ class PackageControllerRestTest {
void findAll() throws Exception { void findAll() throws Exception {
// given // given
final var givenPacs = asList(pac00, pac01, pac02); final var givenPacs = asList(TestPackage.xxx00, TestPackage.xxx01, TestPackage.xxx02);
when(packageRepositoryMock.findAll()).thenReturn(givenPacs); when(packageRepositoryMock.findAll()).thenReturn(givenPacs);
// when // when
final var pacs = mockMvc.perform(MockMvcRequestBuilders final var pacs = mockMvc.perform(MockMvcRequestBuilders
.get("/api/packages") .get("/api/packages")
.header("current-user", "mike@hostsharing.net") .header("current-user", "mike@hostsharing.net")
.header("assumed-roles", "customer#xyz.admin") .header("assumed-roles", "customer#xxx.admin")
.accept(MediaType.APPLICATION_JSON)) .accept(MediaType.APPLICATION_JSON))
// then // then
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(3))) .andExpect(jsonPath("$", hasSize(3)))
.andExpect(jsonPath("$[0].name", is("xyz00"))) .andExpect(jsonPath("$[0].name", is("xxx00")))
.andExpect(jsonPath("$[1].uuid", is(pac01.getUuid().toString()))) .andExpect(jsonPath("$[1].uuid", is(TestPackage.xxx01.getUuid().toString())))
.andExpect(jsonPath("$[2].customer.prefix", is("xyz"))); .andExpect(jsonPath("$[2].customer.prefix", is("xxx")));
verify(contextMock).setCurrentUser("mike@hostsharing.net"); verify(contextMock).setCurrentUser("mike@hostsharing.net");
verify(contextMock).assumeRoles("customer#xyz.admin"); verify(contextMock).assumeRoles("customer#xxx.admin");
} }
} }

View File

@ -0,0 +1,17 @@
package net.hostsharing.hsadminng.hspackage;
import net.hostsharing.hsadminng.hscustomer.CustomerEntity;
import net.hostsharing.hsadminng.hscustomer.TestCustomer;
import static java.util.UUID.randomUUID;
public class TestPackage {
public static final PackageEntity xxx00 = hsPackage(TestCustomer.xxx, "xxx00");
public static final PackageEntity xxx01 = hsPackage(TestCustomer.xxx, "xxx01");
public static final PackageEntity xxx02 = hsPackage(TestCustomer.xxx, "xxx02");
public static PackageEntity hsPackage(final CustomerEntity customer, final String name) {
return new PackageEntity(randomUUID(), name, customer);
}
}