From 8eab3b7cd55ba45cd589f7f5e5aef95be190270c Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Fri, 21 Oct 2022 18:43:15 +0200 Subject: [PATCH] add HsOfficeMembershipControllerRestTest and make mainDebitorUuid mandatory --- .../hs-office-membership-schemas.yaml | 2 +- ...iceMembershipControllerAcceptanceTest.java | 13 -- .../HsOfficeMembershipControllerRestTest.java | 123 ++++++++++++++++++ 3 files changed, 124 insertions(+), 14 deletions(-) create mode 100644 src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerRestTest.java diff --git a/src/main/resources/api-definition/hs-office/hs-office-membership-schemas.yaml b/src/main/resources/api-definition/hs-office/hs-office-membership-schemas.yaml index e8d6c065..fa4451d0 100644 --- a/src/main/resources/api-definition/hs-office/hs-office-membership-schemas.yaml +++ b/src/main/resources/api-definition/hs-office/hs-office-membership-schemas.yaml @@ -70,6 +70,6 @@ components: $ref: '#/components/schemas/HsOfficeReasonForTermination' required: - partnerUuid - - debitorUuid + - mainDebitorUuid - validFrom additionalProperties: false diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerAcceptanceTest.java index b2a9f2aa..6ed38c64 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerAcceptanceTest.java @@ -154,19 +154,6 @@ class HsOfficeMembershipControllerAcceptanceTest { assertThat(newUserUuid).isNotNull(); assertThat(membershipRepo.findByUuid(newUserUuid)).isPresent(); } - - // TODO.test: move validation tests to a ...WebMvcTest - @Test - void globalAdmin_canNotAddMembershipWhenDebitorUuidIsMissing() { - } - - @Test - void globalAdmin_canNotAddMembership_ifPartnerDoesNotExist() { - } - - @Test - void globalAdmin_canNotAddMembership_ifPersonDoesNotExist() { - } } @Nested diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerRestTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerRestTest.java new file mode 100644 index 00000000..53e1d5ac --- /dev/null +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerRestTest.java @@ -0,0 +1,123 @@ +package net.hostsharing.hsadminng.hs.office.membership; + +import net.hostsharing.hsadminng.context.Context; +import net.hostsharing.hsadminng.hs.office.coopassets.HsOfficeCoopAssetsTransactionRepository; +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.orm.jpa.JpaObjectRetrievalFailureException; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import javax.persistence.EntityManager; +import javax.persistence.EntityNotFoundException; +import java.util.UUID; + +import static org.hamcrest.Matchers.is; +import static org.mockito.ArgumentMatchers.any; +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(HsOfficeMembershipController.class) +public class HsOfficeMembershipControllerRestTest { + + @Autowired + MockMvc mockMvc; + + @MockBean + Context contextMock; + + @MockBean + EntityManager em; + + @MockBean + HsOfficeCoopAssetsTransactionRepository coopAssetsTransactionRepo; + + @MockBean + HsOfficeMembershipRepository membershipRepo; + + @Test + void respondBadRequest_ifPartnerUuidIsMissing() throws Exception { + + // when + mockMvc.perform(MockMvcRequestBuilders + .post("/api/hs/office/memberships") + .header("current-user", "superuser-alex@hostsharing.net") + .contentType(MediaType.APPLICATION_JSON) + .content(""" + { + "partnerUuid": null, + "mainDebitorUuid": "%s", + "memberNumber": 20001, + "validFrom": "2022-10-13" + } + """.formatted(UUID.randomUUID())) + .accept(MediaType.APPLICATION_JSON)) + + // then + .andExpect(status().is4xxClientError()) + .andExpect(jsonPath("status", is(400))) + .andExpect(jsonPath("error", is("Bad Request"))) + .andExpect(jsonPath("message", is("[partnerUuid must not be null but is \"null\"]"))); + } + + @Test + void respondBadRequest_ifDebitorUuidIsMissing() throws Exception { + + // when + mockMvc.perform(MockMvcRequestBuilders + .post("/api/hs/office/memberships") + .header("current-user", "superuser-alex@hostsharing.net") + .contentType(MediaType.APPLICATION_JSON) + .content(""" + { + "partnerUuid": "%s", + "mainDebitorUuid": null, + "memberNumber": 20001, + "validFrom": "2022-10-13" + } + """.formatted(UUID.randomUUID())) + .accept(MediaType.APPLICATION_JSON)) + + // then + .andExpect(status().is4xxClientError()) + .andExpect(jsonPath("status", is(400))) + .andExpect(jsonPath("error", is("Bad Request"))) + .andExpect(jsonPath("message", is("[mainDebitorUuid must not be null but is \"null\"]"))); + } + + @Test + void respondBadRequest_ifAnyGivenUuidCannotBeResolved() throws Exception { + + // given + when(membershipRepo.save(any())).thenThrow( + new JpaObjectRetrievalFailureException( + new EntityNotFoundException( + // same would happen with HsOfficePartnerEntity which could not be resolved + "Unable to find net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity with id ..."))); + + // when + mockMvc.perform(MockMvcRequestBuilders + .post("/api/hs/office/memberships") + .header("current-user", "superuser-alex@hostsharing.net") + .contentType(MediaType.APPLICATION_JSON) + .content(""" + { + "partnerUuid": "%s", + "mainDebitorUuid": "%s", + "memberNumber": 20001, + "validFrom": "2022-10-13" + } + """.formatted(UUID.randomUUID(), UUID.randomUUID())) + .accept(MediaType.APPLICATION_JSON)) + + // then + .andExpect(status().is4xxClientError()) + .andExpect(jsonPath("status", is(400))) + .andExpect(jsonPath("error", is("Bad Request"))) + .andExpect(jsonPath("message", is("Unable to find Debitor with uuid ..."))); + } +}