From b3b70aaaf4d27b9d61e1ab247ccda1a336b1223b Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Sat, 22 Oct 2022 10:40:59 +0200 Subject: [PATCH] add HsOfficeMembershipEntityPatcher --- .../HsOfficeMembershipController.java | 38 +++---- .../membership/HsOfficeMembershipEntity.java | 5 + .../HsOfficeMembershipEntityPatcher.java | 45 ++++++++ .../HsOfficePartnerDetailsEntityPatcher.java | 2 - .../HsOfficeSepaMandateController.java | 9 +- .../hsadminng/mapper/PostgresDateRange.java | 18 +++ .../hs-office-membership-schemas.yaml | 6 + .../bankaccount/TestHsOfficeBankAccount.java | 2 +- .../office/contact/TestHsOfficeContact.java | 2 +- ...OfficeCoopAssetsTransactionEntityTest.java | 4 +- ...OfficeCoopSharesTransactionEntityTest.java | 4 +- .../office/debitor/TestHsOfficeDebitor.java | 10 +- ...iceMembershipControllerAcceptanceTest.java | 10 +- ...OfficeMembershipEntityPatcherUnitTest.java | 106 ++++++++++++++++++ .../HsOfficeMembershipEntityUnitTest.java | 17 ++- .../office/membership/TestHsMembership.java | 6 +- .../office/partner/TestHsOfficePartner.java | 2 +- .../HsOfficeSepaMandateEntityTest.java | 4 +- 18 files changed, 231 insertions(+), 59 deletions(-) create mode 100644 src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipEntityPatcher.java create mode 100644 src/main/java/net/hostsharing/hsadminng/mapper/PostgresDateRange.java create mode 100644 src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipEntityPatcherUnitTest.java diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipController.java index 73255336..827d6222 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipController.java @@ -1,12 +1,12 @@ package net.hostsharing.hsadminng.hs.office.membership; -import com.vladmihalcea.hibernate.type.range.Range; -import net.hostsharing.hsadminng.mapper.Mapper; import net.hostsharing.hsadminng.context.Context; +import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity; import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficeMembershipsApi; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipInsertResource; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipPatchResource; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipResource; +import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerEntity; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.transaction.annotation.Transactional; @@ -15,13 +15,13 @@ import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBui import javax.persistence.EntityManager; import javax.validation.Valid; -import java.time.LocalDate; import java.util.List; -import java.util.Optional; import java.util.UUID; import java.util.function.BiConsumer; import static net.hostsharing.hsadminng.mapper.Mapper.map; +import static net.hostsharing.hsadminng.mapper.Mapper.mapList; +import static net.hostsharing.hsadminng.mapper.PostgresDateRange.toPostgresDateRange; @RestController @@ -48,7 +48,7 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi { final var entities = membershipRepo.findMembershipsByOptionalPartnerUuidAndOptionalMemberNumber(partnerUuid, memberNumber); - final var resources = Mapper.mapList(entities, HsOfficeMembershipResource.class, + final var resources = mapList(entities, HsOfficeMembershipResource.class, SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER); return ResponseEntity.ok(resources); } @@ -62,7 +62,7 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi { context.define(currentUser, assumedRoles); - final var entityToSave = map(body, HsOfficeMembershipEntity.class, SEPA_MANDATE_RESOURCE_TO_ENTITY_POSTMAPPER); + final var entityToSave = mapX(body, HsOfficeMembershipEntity.class); entityToSave.setUuid(UUID.randomUUID()); final var saved = membershipRepo.save(entityToSave); @@ -122,25 +122,13 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi { final var current = membershipRepo.findByUuid(membershipUuid).orElseThrow(); - current.setValidity(toPostgresDateRange(current.getValidity().lower(), body.getValidTo())); -// current.setReasonForTermination(HsOfficeReasonForTermination.valueOf(body.getReasonForTermination().name())); - current.setReasonForTermination( - Optional.ofNullable(body.getReasonForTermination()).map(Enum::name).map(HsOfficeReasonForTermination::valueOf).orElse(current.getReasonForTermination()) - ); + new HsOfficeMembershipEntityPatcher(em, current).apply(body); final var saved = membershipRepo.save(current); final var mapped = map(saved, HsOfficeMembershipResource.class, SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER); return ResponseEntity.ok(mapped); } - private static Range toPostgresDateRange( - final LocalDate validFrom, - final LocalDate validTo) { - return validTo != null - ? Range.closedOpen(validFrom, validTo.plusDays(1)) - : Range.closedInfinite(validFrom); - } - final BiConsumer SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> { resource.setValidFrom(entity.getValidity().lower()); if (entity.getValidity().hasUpperBound()) { @@ -148,7 +136,15 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi { } }; - final BiConsumer SEPA_MANDATE_RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> { + private HsOfficeMembershipEntity mapX( + final HsOfficeMembershipInsertResource resource, + final Class entityClass) { + final var entity = new HsOfficeMembershipEntity(); + entity.setPartner(em.getReference(HsOfficePartnerEntity.class, resource.getPartnerUuid())); + entity.setMainDebitor(em.getReference(HsOfficeDebitorEntity.class, resource.getMainDebitorUuid())); + entity.setMemberNumber(resource.getMemberNumber()); entity.setValidity(toPostgresDateRange(resource.getValidFrom(), resource.getValidTo())); - }; + entity.setReasonForTermination(map(resource.getReasonForTermination(), HsOfficeReasonForTermination.class)); + return entity; + } } diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipEntity.java b/src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipEntity.java index 59cd81a4..0cee1196 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipEntity.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipEntity.java @@ -18,6 +18,7 @@ import javax.persistence.*; import java.time.LocalDate; import java.util.UUID; +import static net.hostsharing.hsadminng.mapper.PostgresDateRange.toPostgresDateRange; import static net.hostsharing.hsadminng.stringify.Stringify.stringify; @Entity @@ -69,6 +70,10 @@ public class HsOfficeMembershipEntity implements Stringifyable { @Type(type = "pgsql_enum") private HsOfficeReasonForTermination reasonForTermination; + public void setValidTo(final LocalDate validTo) { + validity = toPostgresDateRange(getValidity().lower(), validTo); + } + @Override public String toString() { return stringify.apply(this); diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipEntityPatcher.java b/src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipEntityPatcher.java new file mode 100644 index 00000000..8696fa86 --- /dev/null +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipEntityPatcher.java @@ -0,0 +1,45 @@ +package net.hostsharing.hsadminng.hs.office.membership; + +import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity; +import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipPatchResource; +import net.hostsharing.hsadminng.mapper.EntityPatcher; +import net.hostsharing.hsadminng.mapper.OptionalFromJson; + +import javax.persistence.EntityManager; +import java.util.Optional; +import java.util.UUID; + +import static net.hostsharing.hsadminng.mapper.Mapper.map; + +public class HsOfficeMembershipEntityPatcher implements EntityPatcher { + + private final EntityManager em; + private final HsOfficeMembershipEntity entity; + + public HsOfficeMembershipEntityPatcher( + final EntityManager em, + final HsOfficeMembershipEntity entity) { + this.em = em; + this.entity = entity; + } + + @Override + public void apply(final HsOfficeMembershipPatchResource resource) { + OptionalFromJson.of(resource.getMainDebitorUuid()) + .ifPresent(newValue -> { + verifyNotNull(newValue, "debitor"); + entity.setMainDebitor(em.getReference(HsOfficeDebitorEntity.class, newValue)); + }); + OptionalFromJson.of(resource.getValidTo()).ifPresent( + entity::setValidTo); + Optional.ofNullable(resource.getReasonForTermination()) + .map(v -> map(v, HsOfficeReasonForTermination.class)) + .ifPresent(entity::setReasonForTermination); + } + + private void verifyNotNull(final UUID newValue, final String propertyName) { + if (newValue == null) { + throw new IllegalArgumentException("property '" + propertyName + "' must not be null"); + } + } +} diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerDetailsEntityPatcher.java b/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerDetailsEntityPatcher.java index d26f4556..aead11e5 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerDetailsEntityPatcher.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerDetailsEntityPatcher.java @@ -8,13 +8,11 @@ import javax.persistence.EntityManager; class HsOfficePartnerDetailsEntityPatcher implements EntityPatcher { - private final EntityManager em; private final HsOfficePartnerDetailsEntity entity; HsOfficePartnerDetailsEntityPatcher( final EntityManager em, final HsOfficePartnerDetailsEntity entity) { - this.em = em; this.entity = entity; } diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java index 9b1a1f57..ba36bd00 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java @@ -21,6 +21,7 @@ import java.util.UUID; import java.util.function.BiConsumer; import static net.hostsharing.hsadminng.mapper.Mapper.map; +import static net.hostsharing.hsadminng.mapper.PostgresDateRange.toPostgresDateRange; @RestController @@ -126,14 +127,6 @@ public class HsOfficeSepaMandateController implements HsOfficeSepaMandatesApi { return ResponseEntity.ok(mapped); } - private static Range toPostgresDateRange( - final LocalDate validFrom, - final LocalDate validTo) { - return validTo != null - ? Range.closedOpen(validFrom, validTo.plusDays(1)) - : Range.closedInfinite(validFrom); - } - final BiConsumer SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> { resource.setValidFrom(entity.getValidity().lower()); if (entity.getValidity().hasUpperBound()) { diff --git a/src/main/java/net/hostsharing/hsadminng/mapper/PostgresDateRange.java b/src/main/java/net/hostsharing/hsadminng/mapper/PostgresDateRange.java new file mode 100644 index 00000000..30b4fab7 --- /dev/null +++ b/src/main/java/net/hostsharing/hsadminng/mapper/PostgresDateRange.java @@ -0,0 +1,18 @@ +package net.hostsharing.hsadminng.mapper; + +import com.vladmihalcea.hibernate.type.range.Range; +import lombok.experimental.UtilityClass; + +import java.time.LocalDate; + +@UtilityClass +public class PostgresDateRange { + + public static Range toPostgresDateRange( + final LocalDate validFrom, + final LocalDate validTo) { + return validTo != null + ? Range.closedOpen(validFrom, validTo.plusDays(1)) + : Range.closedInfinite(validFrom); + } +} 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 fa4451d0..fd27412a 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 @@ -37,10 +37,16 @@ components: HsOfficeMembershipPatch: type: object properties: + mainDebitorUuid: + type: string + format: uuid + nullable: true validTo: type: string format: date + nullable: true reasonForTermination: + nullable: true $ref: '#/components/schemas/HsOfficeReasonForTermination' additionalProperties: false diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/TestHsOfficeBankAccount.java b/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/TestHsOfficeBankAccount.java index 3ce8601d..5b5c43ec 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/TestHsOfficeBankAccount.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/TestHsOfficeBankAccount.java @@ -4,7 +4,7 @@ import java.util.UUID; public class TestHsOfficeBankAccount { - public static final HsOfficeBankAccountEntity someBankAccount = + public static final HsOfficeBankAccountEntity TEST_BANK_ACCOUNT = hsOfficeBankAccount("some bankaccount", "DE67500105173931168623", "INGDDEFFXXX"); static public HsOfficeBankAccountEntity hsOfficeBankAccount(final String holder, final String iban, final String bic) { diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/contact/TestHsOfficeContact.java b/src/test/java/net/hostsharing/hsadminng/hs/office/contact/TestHsOfficeContact.java index a30fc502..02c33305 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/contact/TestHsOfficeContact.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/contact/TestHsOfficeContact.java @@ -4,7 +4,7 @@ import java.util.UUID; public class TestHsOfficeContact { - public static final HsOfficeContactEntity someContact = hsOfficeContact("some contact", "some-contact@example.com"); + public static final HsOfficeContactEntity TEST_CONTACT = hsOfficeContact("some contact", "some-contact@example.com"); static public HsOfficeContactEntity hsOfficeContact(final String label, final String emailAddr) { return HsOfficeContactEntity.builder() diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntityTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntityTest.java index 391fd193..b8c220d4 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntityTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntityTest.java @@ -5,13 +5,13 @@ import org.junit.jupiter.api.Test; import java.math.BigDecimal; import java.time.LocalDate; -import static net.hostsharing.hsadminng.hs.office.membership.TestHsMembership.testMembership; +import static net.hostsharing.hsadminng.hs.office.membership.TestHsMembership.TEST_MEMBERSHIP; import static org.assertj.core.api.Assertions.assertThat; class HsOfficeCoopAssetsTransactionEntityTest { final HsOfficeCoopAssetsTransactionEntity givenCoopAssetTransaction = HsOfficeCoopAssetsTransactionEntity.builder() - .membership(testMembership) + .membership(TEST_MEMBERSHIP) .reference("some-ref") .valueDate(LocalDate.parse("2020-01-01")) .transactionType(HsOfficeCoopAssetsTransactionType.DEPOSIT) diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionEntityTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionEntityTest.java index 63171f53..a12e30c9 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionEntityTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionEntityTest.java @@ -4,13 +4,13 @@ import org.junit.jupiter.api.Test; import java.time.LocalDate; -import static net.hostsharing.hsadminng.hs.office.membership.TestHsMembership.testMembership; +import static net.hostsharing.hsadminng.hs.office.membership.TestHsMembership.TEST_MEMBERSHIP; import static org.assertj.core.api.Assertions.assertThat; class HsOfficeCoopSharesTransactionEntityTest { final HsOfficeCoopSharesTransactionEntity givenCoopSharesTransaction = HsOfficeCoopSharesTransactionEntity.builder() - .membership(testMembership) + .membership(TEST_MEMBERSHIP) .reference("some-ref") .valueDate(LocalDate.parse("2020-01-01")) .transactionType(HsOfficeCoopSharesTransactionType.SUBSCRIPTION) diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/TestHsOfficeDebitor.java b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/TestHsOfficeDebitor.java index 11537d6b..d36524c4 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/TestHsOfficeDebitor.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/TestHsOfficeDebitor.java @@ -4,16 +4,16 @@ import lombok.experimental.UtilityClass; import java.util.UUID; -import static net.hostsharing.hsadminng.hs.office.contact.TestHsOfficeContact.someContact; -import static net.hostsharing.hsadminng.hs.office.partner.TestHsOfficePartner.testPartner; +import static net.hostsharing.hsadminng.hs.office.contact.TestHsOfficeContact.TEST_CONTACT; +import static net.hostsharing.hsadminng.hs.office.partner.TestHsOfficePartner.TEST_PARTNER; @UtilityClass public class TestHsOfficeDebitor { - public static final HsOfficeDebitorEntity testDebitor = HsOfficeDebitorEntity.builder() + public static final HsOfficeDebitorEntity TEST_DEBITOR = HsOfficeDebitorEntity.builder() .uuid(UUID.randomUUID()) .debitorNumber(10001) - .partner(testPartner) - .billingContact(someContact) + .partner(TEST_PARTNER) + .billingContact(TEST_CONTACT) .build(); } 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 6ed38c64..dd854262 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 @@ -3,11 +3,11 @@ package net.hostsharing.hsadminng.hs.office.membership; import com.vladmihalcea.hibernate.type.range.Range; import io.restassured.RestAssured; import io.restassured.http.ContentType; -import net.hostsharing.test.Accepts; import net.hostsharing.hsadminng.HsadminNgApplication; import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorRepository; import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerRepository; +import net.hostsharing.test.Accepts; import net.hostsharing.test.JpaAttempt; import org.json.JSONException; import org.junit.jupiter.api.AfterEach; @@ -300,7 +300,7 @@ class HsOfficeMembershipControllerAcceptanceTest { final var givenMembership = givenSomeTemporaryMembershipBessler(); final var givenNewMainDebitor = debitorRepo.findDebitorByDebitorNumber(10003).get(0); - final var location = RestAssured // @formatter:off + RestAssured // @formatter:off .given() .header("current-user", "superuser-alex@hostsharing.net") .contentType(ContentType.JSON) @@ -317,8 +317,7 @@ class HsOfficeMembershipControllerAcceptanceTest { .contentType(ContentType.JSON) .body("uuid", isUuidValid()) .body("partner.person.tradeName", is(givenMembership.getPartner().getPerson().getTradeName())) - // TODO.impl: implement patching the mainDebitor - // .body("mainDebitor.debitorNumber", is(10003)) + .body("mainDebitor.debitorNumber", is(10003)) .body("memberNumber", is(givenMembership.getMemberNumber())) .body("validFrom", is("2022-11-01")) .body("validTo", nullValue()) @@ -329,8 +328,7 @@ class HsOfficeMembershipControllerAcceptanceTest { assertThat(membershipRepo.findByUuid(givenMembership.getUuid())).isPresent().get() .matches(mandate -> { assertThat(mandate.getPartner().toShortString()).isEqualTo("First GmbH"); - // TODO.impl: implement patching the mainDebitor - // assertThat(mandate.getMainDebitor().toString()).isEqualTo(givenMembership.getMainDebitor().toString()); + assertThat(mandate.getMainDebitor().toString()).isEqualTo(givenMembership.getMainDebitor().toString()); assertThat(mandate.getMemberNumber()).isEqualTo(givenMembership.getMemberNumber()); assertThat(mandate.getValidity().asString()).isEqualTo("[2022-11-01,)"); assertThat(mandate.getReasonForTermination()).isEqualTo(NONE); diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipEntityPatcherUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipEntityPatcherUnitTest.java new file mode 100644 index 00000000..6befee4c --- /dev/null +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipEntityPatcherUnitTest.java @@ -0,0 +1,106 @@ +package net.hostsharing.hsadminng.hs.office.membership; + +import com.vladmihalcea.hibernate.type.range.Range; +import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity; +import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipPatchResource; +import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeReasonForTerminationResource; +import net.hostsharing.test.PatchUnitTestBase; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import javax.persistence.EntityManager; +import java.time.LocalDate; +import java.util.UUID; +import java.util.stream.Stream; + +import static net.hostsharing.hsadminng.hs.office.debitor.TestHsOfficeDebitor.TEST_DEBITOR; +import static net.hostsharing.hsadminng.hs.office.partner.TestHsOfficePartner.TEST_PARTNER; +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.lenient; + +@TestInstance(PER_CLASS) +@ExtendWith(MockitoExtension.class) +class HsOfficeMembershipEntityPatcherUnitTest extends PatchUnitTestBase< + HsOfficeMembershipPatchResource, + HsOfficeMembershipEntity + > { + + private static final UUID INITIAL_MEMBERSHIP_UUID = UUID.randomUUID(); + private static final UUID INITIAL_MAIN_DEBITOR_UUID = UUID.randomUUID(); + private static final UUID PATCHED_MAIN_DEBITOR_UUID = UUID.randomUUID(); + private static final LocalDate GIVEN_VALID_FROM = LocalDate.parse("2020-04-15"); + private static final LocalDate PATCHED_VALID_TO = LocalDate.parse("2022-12-31"); + + @Mock + private EntityManager em; + + @BeforeEach + void initMocks() { + lenient().when(em.getReference(eq(HsOfficeDebitorEntity.class), any())).thenAnswer(invocation -> + HsOfficeDebitorEntity.builder().uuid(invocation.getArgument(1)).build()); + lenient().when(em.getReference(eq(HsOfficeMembershipEntity.class), any())).thenAnswer(invocation -> + HsOfficeMembershipEntity.builder().uuid(invocation.getArgument(1)).build()); + } + + @Override + protected HsOfficeMembershipEntity newInitialEntity() { + final var entity = new HsOfficeMembershipEntity(); + entity.setUuid(INITIAL_MEMBERSHIP_UUID); + entity.setMainDebitor(TEST_DEBITOR); + entity.setPartner(TEST_PARTNER); + entity.setValidity(Range.closedInfinite(GIVEN_VALID_FROM)); + return entity; + } + + @Override + protected HsOfficeMembershipPatchResource newPatchResource() { + return new HsOfficeMembershipPatchResource(); + } + + @Override + protected HsOfficeMembershipEntityPatcher createPatcher(final HsOfficeMembershipEntity Membership) { + return new HsOfficeMembershipEntityPatcher(em, Membership); + } + + @Override + protected Stream propertyTestDescriptors() { + return Stream.of( + new JsonNullableProperty<>( + "debitor", + HsOfficeMembershipPatchResource::setMainDebitorUuid, + PATCHED_MAIN_DEBITOR_UUID, + HsOfficeMembershipEntity::setMainDebitor, + newDebitor(PATCHED_MAIN_DEBITOR_UUID)) + .notNullable(), + new JsonNullableProperty<>( + "valid", + HsOfficeMembershipPatchResource::setValidTo, + PATCHED_VALID_TO, + HsOfficeMembershipEntity::setValidTo), + new SimpleProperty<>( + "reasonForTermination", + HsOfficeMembershipPatchResource::setReasonForTermination, + HsOfficeReasonForTerminationResource.CANCELLATION, + HsOfficeMembershipEntity::setReasonForTermination, + HsOfficeReasonForTermination.CANCELLATION) + .notNullable() + ); + } + + private static HsOfficeDebitorEntity newDebitor(final UUID uuid) { + final var newDebitor = new HsOfficeDebitorEntity(); + newDebitor.setUuid(uuid); + return newDebitor; + } + + private HsOfficeMembershipEntity newMembership(final UUID uuid) { + final var newMembership = new HsOfficeMembershipEntity(); + newMembership.setUuid(uuid); + return newMembership; + } +} diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipEntityUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipEntityUnitTest.java index 52ce7b1c..2c07d140 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipEntityUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipEntityUnitTest.java @@ -8,17 +8,18 @@ import java.lang.reflect.InvocationTargetException; import java.time.LocalDate; import java.util.Arrays; -import static net.hostsharing.hsadminng.hs.office.debitor.TestHsOfficeDebitor.testDebitor; -import static net.hostsharing.hsadminng.hs.office.partner.TestHsOfficePartner.testPartner; +import static net.hostsharing.hsadminng.hs.office.debitor.TestHsOfficeDebitor.TEST_DEBITOR; +import static net.hostsharing.hsadminng.hs.office.partner.TestHsOfficePartner.TEST_PARTNER; import static org.assertj.core.api.Assertions.assertThat; class HsOfficeMembershipEntityUnitTest { + public static final LocalDate GIVEN_VALID_FROM = LocalDate.parse("2020-01-01"); final HsOfficeMembershipEntity givenMembership = HsOfficeMembershipEntity.builder() .memberNumber(10001) - .partner(testPartner) - .mainDebitor(testDebitor) - .validity(Range.closedInfinite(LocalDate.parse("2020-01-01"))) + .partner(TEST_PARTNER) + .mainDebitor(TEST_DEBITOR) + .validity(Range.closedInfinite(GIVEN_VALID_FROM)) .build(); @Test @@ -52,6 +53,12 @@ class HsOfficeMembershipEntityUnitTest { assertThat(givenMembership.getReasonForTermination()).isEqualTo(HsOfficeReasonForTermination.CANCELLATION); } + @Test + void settingValidToKeepsValidFrom() { + givenMembership.setValidTo(LocalDate.parse("2024-12-31")); + assertThat(givenMembership.getValidity().lower()).isEqualTo(GIVEN_VALID_FROM); + } + private static void invokePrePersist(final HsOfficeMembershipEntity membershipEntity) throws IllegalAccessException, InvocationTargetException { final var prePersistMethod = Arrays.stream(HsOfficeMembershipEntity.class.getDeclaredMethods()) diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/TestHsMembership.java b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/TestHsMembership.java index 5e26ac81..4377eaf8 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/TestHsMembership.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/TestHsMembership.java @@ -5,14 +5,14 @@ import com.vladmihalcea.hibernate.type.range.Range; import java.time.LocalDate; import java.util.UUID; -import static net.hostsharing.hsadminng.hs.office.partner.TestHsOfficePartner.testPartner; +import static net.hostsharing.hsadminng.hs.office.partner.TestHsOfficePartner.TEST_PARTNER; public class TestHsMembership { - public static final HsOfficeMembershipEntity testMembership = + public static final HsOfficeMembershipEntity TEST_MEMBERSHIP = HsOfficeMembershipEntity.builder() .uuid(UUID.randomUUID()) - .partner(testPartner) + .partner(TEST_PARTNER) .memberNumber(300001) .validity(Range.closedInfinite(LocalDate.parse("2020-01-01"))) .build(); diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/partner/TestHsOfficePartner.java b/src/test/java/net/hostsharing/hsadminng/hs/office/partner/TestHsOfficePartner.java index 96f5a5d4..5f447665 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/partner/TestHsOfficePartner.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/partner/TestHsOfficePartner.java @@ -9,7 +9,7 @@ import static net.hostsharing.hsadminng.hs.office.person.HsOfficePersonType.LEGA public class TestHsOfficePartner { - public static final HsOfficePartnerEntity testPartner = HsOfficePartnerWithLegalPerson("Test Ltd."); + public static final HsOfficePartnerEntity TEST_PARTNER = HsOfficePartnerWithLegalPerson("Test Ltd."); static public HsOfficePartnerEntity HsOfficePartnerWithLegalPerson(final String tradeName) { return HsOfficePartnerEntity.builder() diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateEntityTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateEntityTest.java index c7259cdc..4ef2aed2 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateEntityTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateEntityTest.java @@ -6,13 +6,13 @@ import org.junit.jupiter.api.Test; import java.time.LocalDate; -import static net.hostsharing.hsadminng.hs.office.debitor.TestHsOfficeDebitor.testDebitor; +import static net.hostsharing.hsadminng.hs.office.debitor.TestHsOfficeDebitor.TEST_DEBITOR; import static org.assertj.core.api.Assertions.assertThat; class HsOfficeSepaMandateEntityTest { final HsOfficeSepaMandateEntity givenSepaMandate = HsOfficeSepaMandateEntity.builder() - .debitor(testDebitor) + .debitor(TEST_DEBITOR) .reference("some-ref") .validity(Range.closedOpen(LocalDate.parse("2020-01-01"), LocalDate.parse("2031-01-01"))) .bankAccount(HsOfficeBankAccountEntity.builder().iban("some label").build())