From 53d3d680218e936a6b8b404a68fff04cc94e1f22 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Sat, 30 Jul 2022 18:37:32 +0200 Subject: [PATCH] introduce TestPackage.java --- .../hsadminng/hscustomer/TestCustomer.java | 6 +++--- .../hspackage/PackageControllerRestTest.java | 18 +++++++----------- .../hsadminng/hspackage/TestPackage.java | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 14 deletions(-) create mode 100644 src/test/java/net/hostsharing/hsadminng/hspackage/TestPackage.java diff --git a/src/test/java/net/hostsharing/hsadminng/hscustomer/TestCustomer.java b/src/test/java/net/hostsharing/hsadminng/hscustomer/TestCustomer.java index ab887848..f90b7e42 100644 --- a/src/test/java/net/hostsharing/hsadminng/hscustomer/TestCustomer.java +++ b/src/test/java/net/hostsharing/hsadminng/hscustomer/TestCustomer.java @@ -4,11 +4,11 @@ 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"); + public static final CustomerEntity xxx = hsCustomer("xxx", 10001, "xxx@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); } } diff --git a/src/test/java/net/hostsharing/hsadminng/hspackage/PackageControllerRestTest.java b/src/test/java/net/hostsharing/hsadminng/hspackage/PackageControllerRestTest.java index bb5957be..1100080c 100644 --- a/src/test/java/net/hostsharing/hsadminng/hspackage/PackageControllerRestTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hspackage/PackageControllerRestTest.java @@ -1,7 +1,7 @@ package net.hostsharing.hsadminng.hspackage; 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.springframework.beans.factory.annotation.Autowired; 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) 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 MockMvc mockMvc; @MockBean @@ -38,25 +34,25 @@ class PackageControllerRestTest { void findAll() throws Exception { // given - final var givenPacs = asList(pac00, pac01, pac02); + final var givenPacs = asList(TestPackage.xxx00, TestPackage.xxx01, TestPackage.xxx02); when(packageRepositoryMock.findAll()).thenReturn(givenPacs); // when final var pacs = mockMvc.perform(MockMvcRequestBuilders .get("/api/packages") .header("current-user", "mike@hostsharing.net") - .header("assumed-roles", "customer#xyz.admin") + .header("assumed-roles", "customer#xxx.admin") .accept(MediaType.APPLICATION_JSON)) // then .andExpect(status().isOk()) .andExpect(jsonPath("$", hasSize(3))) - .andExpect(jsonPath("$[0].name", is("xyz00"))) - .andExpect(jsonPath("$[1].uuid", is(pac01.getUuid().toString()))) - .andExpect(jsonPath("$[2].customer.prefix", is("xyz"))); + .andExpect(jsonPath("$[0].name", is("xxx00"))) + .andExpect(jsonPath("$[1].uuid", is(TestPackage.xxx01.getUuid().toString()))) + .andExpect(jsonPath("$[2].customer.prefix", is("xxx"))); verify(contextMock).setCurrentUser("mike@hostsharing.net"); - verify(contextMock).assumeRoles("customer#xyz.admin"); + verify(contextMock).assumeRoles("customer#xxx.admin"); } } diff --git a/src/test/java/net/hostsharing/hsadminng/hspackage/TestPackage.java b/src/test/java/net/hostsharing/hsadminng/hspackage/TestPackage.java new file mode 100644 index 00000000..37651767 --- /dev/null +++ b/src/test/java/net/hostsharing/hsadminng/hspackage/TestPackage.java @@ -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); + } +}