add HsOfficeMembershipEntityPatcher

This commit is contained in:
Michael Hoennig 2022-10-22 10:40:59 +02:00
parent 8eab3b7cd5
commit b3b70aaaf4
18 changed files with 231 additions and 59 deletions

View File

@ -1,12 +1,12 @@
package net.hostsharing.hsadminng.hs.office.membership; 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.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.api.HsOfficeMembershipsApi;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipInsertResource; 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.HsOfficeMembershipPatchResource;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipResource; 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.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional; 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.persistence.EntityManager;
import javax.validation.Valid; import javax.validation.Valid;
import java.time.LocalDate;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import static net.hostsharing.hsadminng.mapper.Mapper.map; 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 @RestController
@ -48,7 +48,7 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi {
final var entities = final var entities =
membershipRepo.findMembershipsByOptionalPartnerUuidAndOptionalMemberNumber(partnerUuid, memberNumber); 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); SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.ok(resources); return ResponseEntity.ok(resources);
} }
@ -62,7 +62,7 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi {
context.define(currentUser, assumedRoles); 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()); entityToSave.setUuid(UUID.randomUUID());
final var saved = membershipRepo.save(entityToSave); final var saved = membershipRepo.save(entityToSave);
@ -122,25 +122,13 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi {
final var current = membershipRepo.findByUuid(membershipUuid).orElseThrow(); final var current = membershipRepo.findByUuid(membershipUuid).orElseThrow();
current.setValidity(toPostgresDateRange(current.getValidity().lower(), body.getValidTo())); new HsOfficeMembershipEntityPatcher(em, current).apply(body);
// current.setReasonForTermination(HsOfficeReasonForTermination.valueOf(body.getReasonForTermination().name()));
current.setReasonForTermination(
Optional.ofNullable(body.getReasonForTermination()).map(Enum::name).map(HsOfficeReasonForTermination::valueOf).orElse(current.getReasonForTermination())
);
final var saved = membershipRepo.save(current); final var saved = membershipRepo.save(current);
final var mapped = map(saved, HsOfficeMembershipResource.class, SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER); final var mapped = map(saved, HsOfficeMembershipResource.class, SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.ok(mapped); return ResponseEntity.ok(mapped);
} }
private static Range<LocalDate> toPostgresDateRange(
final LocalDate validFrom,
final LocalDate validTo) {
return validTo != null
? Range.closedOpen(validFrom, validTo.plusDays(1))
: Range.closedInfinite(validFrom);
}
final BiConsumer<HsOfficeMembershipEntity, HsOfficeMembershipResource> SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> { final BiConsumer<HsOfficeMembershipEntity, HsOfficeMembershipResource> SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> {
resource.setValidFrom(entity.getValidity().lower()); resource.setValidFrom(entity.getValidity().lower());
if (entity.getValidity().hasUpperBound()) { if (entity.getValidity().hasUpperBound()) {
@ -148,7 +136,15 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi {
} }
}; };
final BiConsumer<HsOfficeMembershipInsertResource, HsOfficeMembershipEntity> SEPA_MANDATE_RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> { private HsOfficeMembershipEntity mapX(
final HsOfficeMembershipInsertResource resource,
final Class<HsOfficeMembershipEntity> 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.setValidity(toPostgresDateRange(resource.getValidFrom(), resource.getValidTo()));
}; entity.setReasonForTermination(map(resource.getReasonForTermination(), HsOfficeReasonForTermination.class));
return entity;
}
} }

View File

@ -18,6 +18,7 @@ import javax.persistence.*;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.UUID; import java.util.UUID;
import static net.hostsharing.hsadminng.mapper.PostgresDateRange.toPostgresDateRange;
import static net.hostsharing.hsadminng.stringify.Stringify.stringify; import static net.hostsharing.hsadminng.stringify.Stringify.stringify;
@Entity @Entity
@ -69,6 +70,10 @@ public class HsOfficeMembershipEntity implements Stringifyable {
@Type(type = "pgsql_enum") @Type(type = "pgsql_enum")
private HsOfficeReasonForTermination reasonForTermination; private HsOfficeReasonForTermination reasonForTermination;
public void setValidTo(final LocalDate validTo) {
validity = toPostgresDateRange(getValidity().lower(), validTo);
}
@Override @Override
public String toString() { public String toString() {
return stringify.apply(this); return stringify.apply(this);

View File

@ -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<HsOfficeMembershipPatchResource> {
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");
}
}
}

View File

@ -8,13 +8,11 @@ import javax.persistence.EntityManager;
class HsOfficePartnerDetailsEntityPatcher implements EntityPatcher<HsOfficePartnerDetailsPatchResource> { class HsOfficePartnerDetailsEntityPatcher implements EntityPatcher<HsOfficePartnerDetailsPatchResource> {
private final EntityManager em;
private final HsOfficePartnerDetailsEntity entity; private final HsOfficePartnerDetailsEntity entity;
HsOfficePartnerDetailsEntityPatcher( HsOfficePartnerDetailsEntityPatcher(
final EntityManager em, final EntityManager em,
final HsOfficePartnerDetailsEntity entity) { final HsOfficePartnerDetailsEntity entity) {
this.em = em;
this.entity = entity; this.entity = entity;
} }

View File

@ -21,6 +21,7 @@ import java.util.UUID;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import static net.hostsharing.hsadminng.mapper.Mapper.map; import static net.hostsharing.hsadminng.mapper.Mapper.map;
import static net.hostsharing.hsadminng.mapper.PostgresDateRange.toPostgresDateRange;
@RestController @RestController
@ -126,14 +127,6 @@ public class HsOfficeSepaMandateController implements HsOfficeSepaMandatesApi {
return ResponseEntity.ok(mapped); return ResponseEntity.ok(mapped);
} }
private static Range<LocalDate> toPostgresDateRange(
final LocalDate validFrom,
final LocalDate validTo) {
return validTo != null
? Range.closedOpen(validFrom, validTo.plusDays(1))
: Range.closedInfinite(validFrom);
}
final BiConsumer<HsOfficeSepaMandateEntity, HsOfficeSepaMandateResource> SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> { final BiConsumer<HsOfficeSepaMandateEntity, HsOfficeSepaMandateResource> SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> {
resource.setValidFrom(entity.getValidity().lower()); resource.setValidFrom(entity.getValidity().lower());
if (entity.getValidity().hasUpperBound()) { if (entity.getValidity().hasUpperBound()) {

View File

@ -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<LocalDate> toPostgresDateRange(
final LocalDate validFrom,
final LocalDate validTo) {
return validTo != null
? Range.closedOpen(validFrom, validTo.plusDays(1))
: Range.closedInfinite(validFrom);
}
}

View File

@ -37,10 +37,16 @@ components:
HsOfficeMembershipPatch: HsOfficeMembershipPatch:
type: object type: object
properties: properties:
mainDebitorUuid:
type: string
format: uuid
nullable: true
validTo: validTo:
type: string type: string
format: date format: date
nullable: true
reasonForTermination: reasonForTermination:
nullable: true
$ref: '#/components/schemas/HsOfficeReasonForTermination' $ref: '#/components/schemas/HsOfficeReasonForTermination'
additionalProperties: false additionalProperties: false

View File

@ -4,7 +4,7 @@ import java.util.UUID;
public class TestHsOfficeBankAccount { public class TestHsOfficeBankAccount {
public static final HsOfficeBankAccountEntity someBankAccount = public static final HsOfficeBankAccountEntity TEST_BANK_ACCOUNT =
hsOfficeBankAccount("some bankaccount", "DE67500105173931168623", "INGDDEFFXXX"); hsOfficeBankAccount("some bankaccount", "DE67500105173931168623", "INGDDEFFXXX");
static public HsOfficeBankAccountEntity hsOfficeBankAccount(final String holder, final String iban, final String bic) { static public HsOfficeBankAccountEntity hsOfficeBankAccount(final String holder, final String iban, final String bic) {

View File

@ -4,7 +4,7 @@ import java.util.UUID;
public class TestHsOfficeContact { 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) { static public HsOfficeContactEntity hsOfficeContact(final String label, final String emailAddr) {
return HsOfficeContactEntity.builder() return HsOfficeContactEntity.builder()

View File

@ -5,13 +5,13 @@ import org.junit.jupiter.api.Test;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.LocalDate; 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; import static org.assertj.core.api.Assertions.assertThat;
class HsOfficeCoopAssetsTransactionEntityTest { class HsOfficeCoopAssetsTransactionEntityTest {
final HsOfficeCoopAssetsTransactionEntity givenCoopAssetTransaction = HsOfficeCoopAssetsTransactionEntity.builder() final HsOfficeCoopAssetsTransactionEntity givenCoopAssetTransaction = HsOfficeCoopAssetsTransactionEntity.builder()
.membership(testMembership) .membership(TEST_MEMBERSHIP)
.reference("some-ref") .reference("some-ref")
.valueDate(LocalDate.parse("2020-01-01")) .valueDate(LocalDate.parse("2020-01-01"))
.transactionType(HsOfficeCoopAssetsTransactionType.DEPOSIT) .transactionType(HsOfficeCoopAssetsTransactionType.DEPOSIT)

View File

@ -4,13 +4,13 @@ import org.junit.jupiter.api.Test;
import java.time.LocalDate; 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; import static org.assertj.core.api.Assertions.assertThat;
class HsOfficeCoopSharesTransactionEntityTest { class HsOfficeCoopSharesTransactionEntityTest {
final HsOfficeCoopSharesTransactionEntity givenCoopSharesTransaction = HsOfficeCoopSharesTransactionEntity.builder() final HsOfficeCoopSharesTransactionEntity givenCoopSharesTransaction = HsOfficeCoopSharesTransactionEntity.builder()
.membership(testMembership) .membership(TEST_MEMBERSHIP)
.reference("some-ref") .reference("some-ref")
.valueDate(LocalDate.parse("2020-01-01")) .valueDate(LocalDate.parse("2020-01-01"))
.transactionType(HsOfficeCoopSharesTransactionType.SUBSCRIPTION) .transactionType(HsOfficeCoopSharesTransactionType.SUBSCRIPTION)

View File

@ -4,16 +4,16 @@ import lombok.experimental.UtilityClass;
import java.util.UUID; import java.util.UUID;
import static net.hostsharing.hsadminng.hs.office.contact.TestHsOfficeContact.someContact; import static net.hostsharing.hsadminng.hs.office.contact.TestHsOfficeContact.TEST_CONTACT;
import static net.hostsharing.hsadminng.hs.office.partner.TestHsOfficePartner.testPartner; import static net.hostsharing.hsadminng.hs.office.partner.TestHsOfficePartner.TEST_PARTNER;
@UtilityClass @UtilityClass
public class TestHsOfficeDebitor { public class TestHsOfficeDebitor {
public static final HsOfficeDebitorEntity testDebitor = HsOfficeDebitorEntity.builder() public static final HsOfficeDebitorEntity TEST_DEBITOR = HsOfficeDebitorEntity.builder()
.uuid(UUID.randomUUID()) .uuid(UUID.randomUUID())
.debitorNumber(10001) .debitorNumber(10001)
.partner(testPartner) .partner(TEST_PARTNER)
.billingContact(someContact) .billingContact(TEST_CONTACT)
.build(); .build();
} }

View File

@ -3,11 +3,11 @@ package net.hostsharing.hsadminng.hs.office.membership;
import com.vladmihalcea.hibernate.type.range.Range; import com.vladmihalcea.hibernate.type.range.Range;
import io.restassured.RestAssured; import io.restassured.RestAssured;
import io.restassured.http.ContentType; import io.restassured.http.ContentType;
import net.hostsharing.test.Accepts;
import net.hostsharing.hsadminng.HsadminNgApplication; import net.hostsharing.hsadminng.HsadminNgApplication;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorRepository; import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorRepository;
import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerRepository; import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerRepository;
import net.hostsharing.test.Accepts;
import net.hostsharing.test.JpaAttempt; import net.hostsharing.test.JpaAttempt;
import org.json.JSONException; import org.json.JSONException;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
@ -300,7 +300,7 @@ class HsOfficeMembershipControllerAcceptanceTest {
final var givenMembership = givenSomeTemporaryMembershipBessler(); final var givenMembership = givenSomeTemporaryMembershipBessler();
final var givenNewMainDebitor = debitorRepo.findDebitorByDebitorNumber(10003).get(0); final var givenNewMainDebitor = debitorRepo.findDebitorByDebitorNumber(10003).get(0);
final var location = RestAssured // @formatter:off RestAssured // @formatter:off
.given() .given()
.header("current-user", "superuser-alex@hostsharing.net") .header("current-user", "superuser-alex@hostsharing.net")
.contentType(ContentType.JSON) .contentType(ContentType.JSON)
@ -317,8 +317,7 @@ class HsOfficeMembershipControllerAcceptanceTest {
.contentType(ContentType.JSON) .contentType(ContentType.JSON)
.body("uuid", isUuidValid()) .body("uuid", isUuidValid())
.body("partner.person.tradeName", is(givenMembership.getPartner().getPerson().getTradeName())) .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("memberNumber", is(givenMembership.getMemberNumber()))
.body("validFrom", is("2022-11-01")) .body("validFrom", is("2022-11-01"))
.body("validTo", nullValue()) .body("validTo", nullValue())
@ -329,8 +328,7 @@ class HsOfficeMembershipControllerAcceptanceTest {
assertThat(membershipRepo.findByUuid(givenMembership.getUuid())).isPresent().get() assertThat(membershipRepo.findByUuid(givenMembership.getUuid())).isPresent().get()
.matches(mandate -> { .matches(mandate -> {
assertThat(mandate.getPartner().toShortString()).isEqualTo("First GmbH"); 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.getMemberNumber()).isEqualTo(givenMembership.getMemberNumber());
assertThat(mandate.getValidity().asString()).isEqualTo("[2022-11-01,)"); assertThat(mandate.getValidity().asString()).isEqualTo("[2022-11-01,)");
assertThat(mandate.getReasonForTermination()).isEqualTo(NONE); assertThat(mandate.getReasonForTermination()).isEqualTo(NONE);

View File

@ -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<Property> 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;
}
}

View File

@ -8,17 +8,18 @@ import java.lang.reflect.InvocationTargetException;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Arrays; import java.util.Arrays;
import static net.hostsharing.hsadminng.hs.office.debitor.TestHsOfficeDebitor.testDebitor; import static net.hostsharing.hsadminng.hs.office.debitor.TestHsOfficeDebitor.TEST_DEBITOR;
import static net.hostsharing.hsadminng.hs.office.partner.TestHsOfficePartner.testPartner; import static net.hostsharing.hsadminng.hs.office.partner.TestHsOfficePartner.TEST_PARTNER;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
class HsOfficeMembershipEntityUnitTest { class HsOfficeMembershipEntityUnitTest {
public static final LocalDate GIVEN_VALID_FROM = LocalDate.parse("2020-01-01");
final HsOfficeMembershipEntity givenMembership = HsOfficeMembershipEntity.builder() final HsOfficeMembershipEntity givenMembership = HsOfficeMembershipEntity.builder()
.memberNumber(10001) .memberNumber(10001)
.partner(testPartner) .partner(TEST_PARTNER)
.mainDebitor(testDebitor) .mainDebitor(TEST_DEBITOR)
.validity(Range.closedInfinite(LocalDate.parse("2020-01-01"))) .validity(Range.closedInfinite(GIVEN_VALID_FROM))
.build(); .build();
@Test @Test
@ -52,6 +53,12 @@ class HsOfficeMembershipEntityUnitTest {
assertThat(givenMembership.getReasonForTermination()).isEqualTo(HsOfficeReasonForTermination.CANCELLATION); 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) private static void invokePrePersist(final HsOfficeMembershipEntity membershipEntity)
throws IllegalAccessException, InvocationTargetException { throws IllegalAccessException, InvocationTargetException {
final var prePersistMethod = Arrays.stream(HsOfficeMembershipEntity.class.getDeclaredMethods()) final var prePersistMethod = Arrays.stream(HsOfficeMembershipEntity.class.getDeclaredMethods())

View File

@ -5,14 +5,14 @@ import com.vladmihalcea.hibernate.type.range.Range;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.UUID; 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 class TestHsMembership {
public static final HsOfficeMembershipEntity testMembership = public static final HsOfficeMembershipEntity TEST_MEMBERSHIP =
HsOfficeMembershipEntity.builder() HsOfficeMembershipEntity.builder()
.uuid(UUID.randomUUID()) .uuid(UUID.randomUUID())
.partner(testPartner) .partner(TEST_PARTNER)
.memberNumber(300001) .memberNumber(300001)
.validity(Range.closedInfinite(LocalDate.parse("2020-01-01"))) .validity(Range.closedInfinite(LocalDate.parse("2020-01-01")))
.build(); .build();

View File

@ -9,7 +9,7 @@ import static net.hostsharing.hsadminng.hs.office.person.HsOfficePersonType.LEGA
public class TestHsOfficePartner { 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) { static public HsOfficePartnerEntity HsOfficePartnerWithLegalPerson(final String tradeName) {
return HsOfficePartnerEntity.builder() return HsOfficePartnerEntity.builder()

View File

@ -6,13 +6,13 @@ import org.junit.jupiter.api.Test;
import java.time.LocalDate; 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; import static org.assertj.core.api.Assertions.assertThat;
class HsOfficeSepaMandateEntityTest { class HsOfficeSepaMandateEntityTest {
final HsOfficeSepaMandateEntity givenSepaMandate = HsOfficeSepaMandateEntity.builder() final HsOfficeSepaMandateEntity givenSepaMandate = HsOfficeSepaMandateEntity.builder()
.debitor(testDebitor) .debitor(TEST_DEBITOR)
.reference("some-ref") .reference("some-ref")
.validity(Range.closedOpen(LocalDate.parse("2020-01-01"), LocalDate.parse("2031-01-01"))) .validity(Range.closedOpen(LocalDate.parse("2020-01-01"), LocalDate.parse("2031-01-01")))
.bankAccount(HsOfficeBankAccountEntity.builder().iban("some label").build()) .bankAccount(HsOfficeBankAccountEntity.builder().iban("some label").build())