From 2610ecd3112eaf6133474b500e22bdd91502e4ef Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Sat, 30 Jul 2022 15:25:44 +0200 Subject: [PATCH] add PackageControllerTest as WebMvcTest through the REST API --- .../hsadminng/hscustomer/CustomerEntity.java | 4 ++ .../hsadminng/hspackage/PackageEntity.java | 4 ++ .../hspackage/PackageControllerTest.java | 62 +++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 src/test/java/net/hostsharing/hsadminng/hspackage/PackageControllerTest.java diff --git a/src/main/java/net/hostsharing/hsadminng/hscustomer/CustomerEntity.java b/src/main/java/net/hostsharing/hsadminng/hscustomer/CustomerEntity.java index 05f0b6a3..8a5f99b3 100644 --- a/src/main/java/net/hostsharing/hsadminng/hscustomer/CustomerEntity.java +++ b/src/main/java/net/hostsharing/hsadminng/hscustomer/CustomerEntity.java @@ -1,6 +1,8 @@ package net.hostsharing.hsadminng.hscustomer; +import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.NoArgsConstructor; import javax.persistence.*; import java.util.UUID; @@ -8,6 +10,8 @@ import java.util.UUID; @Entity @Table(name = "customer_rv") @Getter +@NoArgsConstructor +@AllArgsConstructor public class CustomerEntity { private @Id UUID uuid; private String prefix; diff --git a/src/main/java/net/hostsharing/hsadminng/hspackage/PackageEntity.java b/src/main/java/net/hostsharing/hsadminng/hspackage/PackageEntity.java index 16feb7eb..0792e843 100644 --- a/src/main/java/net/hostsharing/hsadminng/hspackage/PackageEntity.java +++ b/src/main/java/net/hostsharing/hsadminng/hspackage/PackageEntity.java @@ -1,6 +1,8 @@ package net.hostsharing.hsadminng.hspackage; +import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.NoArgsConstructor; import net.hostsharing.hsadminng.hscustomer.CustomerEntity; import javax.persistence.*; @@ -9,6 +11,8 @@ import java.util.UUID; @Entity @Table(name = "package_rv") @Getter +@NoArgsConstructor +@AllArgsConstructor public class PackageEntity { private @Id UUID uuid; diff --git a/src/test/java/net/hostsharing/hsadminng/hspackage/PackageControllerTest.java b/src/test/java/net/hostsharing/hsadminng/hspackage/PackageControllerTest.java new file mode 100644 index 00000000..aae210ce --- /dev/null +++ b/src/test/java/net/hostsharing/hsadminng/hspackage/PackageControllerTest.java @@ -0,0 +1,62 @@ +package net.hostsharing.hsadminng.hspackage; + +import net.hostsharing.hsadminng.context.Context; +import net.hostsharing.hsadminng.hscustomer.CustomerEntity; +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 java.util.UUID; + +import static java.util.Arrays.asList; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@WebMvcTest(PackageController.class) +class PackageControllerTest { + + 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 + private Context contextMock; + @MockBean + private PackageRepository packageRepositoryMock; + + @Test + void findAll() throws Exception { + + // given + final var givenPacs = asList(pac00, pac01, pac02); + when(packageRepositoryMock.findAll()).thenReturn(givenPacs); + + // when + final var pacs = mockMvc.perform(MockMvcRequestBuilders + .get("/api/package") + .header("current-user", "mike@hostsharing.net") + .header("assumed-roles", "customer#xyz.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"))); + + verify(contextMock).setCurrentUser("mike@hostsharing.net"); + verify(contextMock).assumeRoles("customer#xyz.admin"); + } + +}