amend API to patch partner person directly into partner
This commit is contained in:
parent
839703ca48
commit
f645b97533
@ -157,32 +157,33 @@ public class HsOfficePartnerController implements HsOfficePartnersApi {
|
|||||||
context.define(currentSubject, assumedRoles);
|
context.define(currentSubject, assumedRoles);
|
||||||
|
|
||||||
final var current = rbacPartnerRepo.findByUuid(partnerUuid).orElseThrow();
|
final var current = rbacPartnerRepo.findByUuid(partnerUuid).orElseThrow();
|
||||||
final var previousPartnerRel = current.getPartnerRel();
|
final var previousPartnerPerson = current.getPartnerRel().getHolder();
|
||||||
|
|
||||||
new HsOfficePartnerEntityPatcher(em, current).apply(body);
|
new HsOfficePartnerEntityPatcher(em, current).apply(body);
|
||||||
|
|
||||||
final var saved = rbacPartnerRepo.save(current);
|
final var saved = rbacPartnerRepo.save(current);
|
||||||
optionallyCreateExPartnerRelation(saved, previousPartnerRel);
|
optionallyCreateExPartnerRelation(saved, previousPartnerPerson);
|
||||||
optionallyUpdateRelatedRelations(saved, previousPartnerRel);
|
optionallyUpdateRelatedRelations(saved, previousPartnerPerson);
|
||||||
|
|
||||||
final var mapped = mapper.map(saved, HsOfficePartnerResource.class, ENTITY_TO_RESOURCE_POSTMAPPER);
|
final var mapped = mapper.map(saved, HsOfficePartnerResource.class, ENTITY_TO_RESOURCE_POSTMAPPER);
|
||||||
return ResponseEntity.ok(mapped);
|
return ResponseEntity.ok(mapped);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void optionallyCreateExPartnerRelation(final HsOfficePartnerRbacEntity saved, final HsOfficeRelationRealEntity previousPartnerRel) {
|
private void optionallyCreateExPartnerRelation(final HsOfficePartnerRbacEntity saved, final HsOfficePersonRealEntity previousPartnerPerson) {
|
||||||
|
|
||||||
final var partnerRelHasChanged = !saved.getPartnerRel().getUuid().equals(previousPartnerRel.getUuid());
|
final var partnerPersonHasChanged = !saved.getPartnerRel().getHolder().getUuid().equals(previousPartnerPerson.getUuid());
|
||||||
if (partnerRelHasChanged) {
|
if (partnerPersonHasChanged) {
|
||||||
realRelationRepo.save(previousPartnerRel.toBuilder().uuid(null)
|
realRelationRepo.save(saved.getPartnerRel().toBuilder()
|
||||||
.type(EX_PARTNER).anchor(saved.getPartnerRel().getHolder())
|
.uuid(null)
|
||||||
|
.type(EX_PARTNER)
|
||||||
|
.anchor(saved.getPartnerRel().getHolder())
|
||||||
|
.holder(previousPartnerPerson)
|
||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void optionallyUpdateRelatedRelations(final HsOfficePartnerRbacEntity saved, final HsOfficeRelationRealEntity previousPartnerRel) {
|
private void optionallyUpdateRelatedRelations(final HsOfficePartnerRbacEntity saved, final HsOfficePersonRealEntity previousPartnerPerson) {
|
||||||
final var oldPartnerPerson = previousPartnerRel.getHolder();
|
final var partnerPersonHasChanged = !saved.getPartnerRel().getHolder().getUuid().equals(previousPartnerPerson.getUuid());
|
||||||
final var newPartnerPerson = saved.getPartnerRel().getHolder();
|
|
||||||
final var partnerPersonHasChanged = !newPartnerPerson.getUuid().equals(oldPartnerPerson.getUuid());
|
|
||||||
if (partnerPersonHasChanged) {
|
if (partnerPersonHasChanged) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
package net.hostsharing.hsadminng.hs.office.partner;
|
package net.hostsharing.hsadminng.hs.office.partner;
|
||||||
|
|
||||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePartnerPatchResource;
|
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePartnerPatchResource;
|
||||||
import net.hostsharing.hsadminng.hs.office.relation.HsOfficeRelationRealEntity;
|
import net.hostsharing.hsadminng.hs.office.relation.HsOfficeRelationEntityPatcher;
|
||||||
import net.hostsharing.hsadminng.mapper.EntityPatcher;
|
import net.hostsharing.hsadminng.mapper.EntityPatcher;
|
||||||
import net.hostsharing.hsadminng.mapper.OptionalFromJson;
|
|
||||||
|
|
||||||
import jakarta.persistence.EntityManager;
|
import jakarta.persistence.EntityManager;
|
||||||
|
|
||||||
@ -19,17 +18,13 @@ class HsOfficePartnerEntityPatcher implements EntityPatcher<HsOfficePartnerPatch
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void apply(final HsOfficePartnerPatchResource resource) {
|
public void apply(final HsOfficePartnerPatchResource resource) {
|
||||||
OptionalFromJson.of(resource.getPartnerRelUuid()).ifPresent(newValue -> {
|
|
||||||
verifyNotNull(newValue, "partnerRel");
|
|
||||||
entity.setPartnerRel(em.getReference(HsOfficeRelationRealEntity.class, newValue));
|
|
||||||
});
|
|
||||||
|
|
||||||
new HsOfficePartnerDetailsEntityPatcher(em, entity.getDetails()).apply(resource.getDetails());
|
if (resource.getPartnerRel() != null) {
|
||||||
|
new HsOfficeRelationEntityPatcher(em, entity.getPartnerRel()).apply(resource.getPartnerRel());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void verifyNotNull(final Object newValue, final String propertyName) {
|
if (resource.getDetails() != null) {
|
||||||
if (newValue == null) {
|
new HsOfficePartnerDetailsEntityPatcher(em, entity.getDetails()).apply(resource.getDetails());
|
||||||
throw new IllegalArgumentException("property '" + propertyName + "' must not be null");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -163,13 +163,13 @@ public class HsOfficeRelationController implements HsOfficeRelationsApi {
|
|||||||
final String currentSubject,
|
final String currentSubject,
|
||||||
final String assumedRoles,
|
final String assumedRoles,
|
||||||
final UUID relationUuid,
|
final UUID relationUuid,
|
||||||
final HsOfficeRelationPatchResource body) {
|
final HsOfficeRelationContactPatchResource body) {
|
||||||
|
|
||||||
context.define(currentSubject, assumedRoles);
|
context.define(currentSubject, assumedRoles);
|
||||||
|
|
||||||
final var current = rbacRelationRepo.findByUuid(relationUuid).orElseThrow();
|
final var current = rbacRelationRepo.findByUuid(relationUuid).orElseThrow();
|
||||||
|
|
||||||
new HsOfficeRelationEntityPatcher(em, current).apply(body);
|
new HsOfficeRelationEntityContactPatcher(em, current).apply(body);
|
||||||
|
|
||||||
final var saved = rbacRelationRepo.save(current);
|
final var saved = rbacRelationRepo.save(current);
|
||||||
final var mapped = mapper.map(saved, HsOfficeRelationResource.class);
|
final var mapped = mapper.map(saved, HsOfficeRelationResource.class);
|
||||||
|
@ -0,0 +1,34 @@
|
|||||||
|
package net.hostsharing.hsadminng.hs.office.relation;
|
||||||
|
|
||||||
|
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRealEntity;
|
||||||
|
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeRelationContactPatchResource;
|
||||||
|
import net.hostsharing.hsadminng.mapper.EntityPatcher;
|
||||||
|
import net.hostsharing.hsadminng.mapper.OptionalFromJson;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class HsOfficeRelationEntityContactPatcher implements EntityPatcher<HsOfficeRelationContactPatchResource> {
|
||||||
|
|
||||||
|
private final EntityManager em;
|
||||||
|
private final HsOfficeRelation entity;
|
||||||
|
|
||||||
|
public HsOfficeRelationEntityContactPatcher(final EntityManager em, final HsOfficeRelation entity) {
|
||||||
|
this.em = em;
|
||||||
|
this.entity = entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void apply(final HsOfficeRelationContactPatchResource resource) {
|
||||||
|
OptionalFromJson.of(resource.getContactUuid()).ifPresent(newValue -> {
|
||||||
|
verifyNotNull(newValue, "contact");
|
||||||
|
entity.setContact(em.getReference(HsOfficeContactRealEntity.class, newValue));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void verifyNotNull(final UUID newValue, final String propertyName) {
|
||||||
|
if (newValue == null) {
|
||||||
|
throw new IllegalArgumentException("property '" + propertyName + "' must not be null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,24 +2,33 @@ package net.hostsharing.hsadminng.hs.office.relation;
|
|||||||
|
|
||||||
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRealEntity;
|
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRealEntity;
|
||||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeRelationPatchResource;
|
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeRelationPatchResource;
|
||||||
|
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRealEntity;
|
||||||
import net.hostsharing.hsadminng.mapper.EntityPatcher;
|
import net.hostsharing.hsadminng.mapper.EntityPatcher;
|
||||||
import net.hostsharing.hsadminng.mapper.OptionalFromJson;
|
import net.hostsharing.hsadminng.mapper.OptionalFromJson;
|
||||||
|
|
||||||
import jakarta.persistence.EntityManager;
|
import jakarta.persistence.EntityManager;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
class HsOfficeRelationEntityPatcher implements EntityPatcher<HsOfficeRelationPatchResource> {
|
public class HsOfficeRelationEntityPatcher implements EntityPatcher<HsOfficeRelationPatchResource> {
|
||||||
|
|
||||||
private final EntityManager em;
|
private final EntityManager em;
|
||||||
private final HsOfficeRelation entity;
|
private final HsOfficeRelation entity;
|
||||||
|
|
||||||
HsOfficeRelationEntityPatcher(final EntityManager em, final HsOfficeRelation entity) {
|
public HsOfficeRelationEntityPatcher(final EntityManager em, final HsOfficeRelation entity) {
|
||||||
this.em = em;
|
this.em = em;
|
||||||
this.entity = entity;
|
this.entity = entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void apply(final HsOfficeRelationPatchResource resource) {
|
public void apply(final HsOfficeRelationPatchResource resource) {
|
||||||
|
OptionalFromJson.of(resource.getAnchorUuid()).ifPresent(newValue -> {
|
||||||
|
verifyNotNull(newValue, "contact");
|
||||||
|
entity.setAnchor(em.getReference(HsOfficePersonRealEntity.class, newValue));
|
||||||
|
});
|
||||||
|
OptionalFromJson.of(resource.getHolderUuid()).ifPresent(newValue -> {
|
||||||
|
verifyNotNull(newValue, "contact");
|
||||||
|
entity.setHolder(em.getReference(HsOfficePersonRealEntity.class, newValue));
|
||||||
|
});
|
||||||
OptionalFromJson.of(resource.getContactUuid()).ifPresent(newValue -> {
|
OptionalFromJson.of(resource.getContactUuid()).ifPresent(newValue -> {
|
||||||
verifyNotNull(newValue, "contact");
|
verifyNotNull(newValue, "contact");
|
||||||
entity.setContact(em.getReference(HsOfficeContactRealEntity.class, newValue));
|
entity.setContact(em.getReference(HsOfficeContactRealEntity.class, newValue));
|
||||||
|
@ -48,10 +48,8 @@ components:
|
|||||||
HsOfficePartnerPatch:
|
HsOfficePartnerPatch:
|
||||||
type: object
|
type: object
|
||||||
properties:
|
properties:
|
||||||
partnerRel.uuid:
|
partnerRel:
|
||||||
type: string
|
$ref: 'hs-office-relation-schemas.yaml#/components/schemas/HsOfficeRelationPatch'
|
||||||
format: uuid
|
|
||||||
nullable: true
|
|
||||||
details:
|
details:
|
||||||
$ref: '#/components/schemas/HsOfficePartnerDetailsPatch'
|
$ref: '#/components/schemas/HsOfficePartnerDetailsPatch'
|
||||||
|
|
||||||
|
@ -34,9 +34,25 @@ components:
|
|||||||
contact:
|
contact:
|
||||||
$ref: 'hs-office-contact-schemas.yaml#/components/schemas/HsOfficeContact'
|
$ref: 'hs-office-contact-schemas.yaml#/components/schemas/HsOfficeContact'
|
||||||
|
|
||||||
|
HsOfficeRelationContactPatch:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
contact.uuid:
|
||||||
|
type: string
|
||||||
|
format: uuid
|
||||||
|
nullable: true
|
||||||
|
|
||||||
HsOfficeRelationPatch:
|
HsOfficeRelationPatch:
|
||||||
type: object
|
type: object
|
||||||
properties:
|
properties:
|
||||||
|
anchor.uuid:
|
||||||
|
type: string
|
||||||
|
format: uuid
|
||||||
|
nullable: true
|
||||||
|
holder.uuid:
|
||||||
|
type: string
|
||||||
|
format: uuid
|
||||||
|
nullable: true
|
||||||
contact.uuid:
|
contact.uuid:
|
||||||
type: string
|
type: string
|
||||||
format: uuid
|
format: uuid
|
||||||
|
@ -44,7 +44,7 @@ patch:
|
|||||||
content:
|
content:
|
||||||
'application/json':
|
'application/json':
|
||||||
schema:
|
schema:
|
||||||
$ref: 'hs-office-relation-schemas.yaml#/components/schemas/HsOfficeRelationPatch'
|
$ref: 'hs-office-relation-schemas.yaml#/components/schemas/HsOfficeRelationContactPatch'
|
||||||
responses:
|
responses:
|
||||||
"200":
|
"200":
|
||||||
description: OK
|
description: OK
|
||||||
|
@ -316,7 +316,7 @@ class HsOfficePartnerControllerAcceptanceTest extends ContextBasedTestWithCleanu
|
|||||||
|
|
||||||
context.define("superuser-alex@hostsharing.net");
|
context.define("superuser-alex@hostsharing.net");
|
||||||
final var givenPartner = givenSomeTemporaryPartnerBessler(20011);
|
final var givenPartner = givenSomeTemporaryPartnerBessler(20011);
|
||||||
final var givenPartnerRel = givenSomeTemporaryPartnerRel("Winkler", "third contact");
|
final var newPartnerPerson = personRealRepo.findPersonByOptionalNameLike("Winkler").getFirst();
|
||||||
|
|
||||||
RestAssured // @formatter:off
|
RestAssured // @formatter:off
|
||||||
.given()
|
.given()
|
||||||
@ -325,7 +325,9 @@ class HsOfficePartnerControllerAcceptanceTest extends ContextBasedTestWithCleanu
|
|||||||
.body("""
|
.body("""
|
||||||
{
|
{
|
||||||
"partnerNumber": "P-20011",
|
"partnerNumber": "P-20011",
|
||||||
"partnerRel.uuid": "%s",
|
"partnerRel": {
|
||||||
|
"holder.uuid": "%s"
|
||||||
|
},
|
||||||
"details": {
|
"details": {
|
||||||
"registrationOffice": "Temp Registergericht Aurich",
|
"registrationOffice": "Temp Registergericht Aurich",
|
||||||
"registrationNumber": "222222",
|
"registrationNumber": "222222",
|
||||||
@ -334,7 +336,7 @@ class HsOfficePartnerControllerAcceptanceTest extends ContextBasedTestWithCleanu
|
|||||||
"dateOfDeath": "2022-01-12"
|
"dateOfDeath": "2022-01-12"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
""".formatted(givenPartnerRel.getUuid()))
|
""".formatted(newPartnerPerson.getUuid()))
|
||||||
.port(port)
|
.port(port)
|
||||||
.when()
|
.when()
|
||||||
.patch("http://localhost/api/hs/office/partners/" + givenPartner.getUuid())
|
.patch("http://localhost/api/hs/office/partners/" + givenPartner.getUuid())
|
||||||
@ -348,7 +350,7 @@ class HsOfficePartnerControllerAcceptanceTest extends ContextBasedTestWithCleanu
|
|||||||
"anchor": { "tradeName": "Hostsharing eG" },
|
"anchor": { "tradeName": "Hostsharing eG" },
|
||||||
"holder": { "familyName": "Winkler" },
|
"holder": { "familyName": "Winkler" },
|
||||||
"type": "PARTNER",
|
"type": "PARTNER",
|
||||||
"contact": { "caption": "third contact" }
|
"contact": { "caption": "fourth contact" }
|
||||||
},
|
},
|
||||||
"details": {
|
"details": {
|
||||||
"registrationOffice": "Temp Registergericht Aurich",
|
"registrationOffice": "Temp Registergericht Aurich",
|
||||||
@ -368,7 +370,7 @@ class HsOfficePartnerControllerAcceptanceTest extends ContextBasedTestWithCleanu
|
|||||||
.matches(partner -> {
|
.matches(partner -> {
|
||||||
assertThat(partner.getPartnerNumber()).isEqualTo(givenPartner.getPartnerNumber());
|
assertThat(partner.getPartnerNumber()).isEqualTo(givenPartner.getPartnerNumber());
|
||||||
assertThat(partner.getPartnerRel().getHolder().getFamilyName()).isEqualTo("Winkler");
|
assertThat(partner.getPartnerRel().getHolder().getFamilyName()).isEqualTo("Winkler");
|
||||||
assertThat(partner.getPartnerRel().getContact().getCaption()).isEqualTo("third contact");
|
assertThat(partner.getPartnerRel().getContact().getCaption()).isEqualTo("fourth contact");
|
||||||
assertThat(partner.getDetails().getRegistrationOffice()).isEqualTo("Temp Registergericht Aurich");
|
assertThat(partner.getDetails().getRegistrationOffice()).isEqualTo("Temp Registergericht Aurich");
|
||||||
assertThat(partner.getDetails().getRegistrationNumber()).isEqualTo("222222");
|
assertThat(partner.getDetails().getRegistrationNumber()).isEqualTo("222222");
|
||||||
assertThat(partner.getDetails().getBirthName()).isEqualTo("Maja Schmidt");
|
assertThat(partner.getDetails().getBirthName()).isEqualTo("Maja Schmidt");
|
||||||
@ -379,11 +381,11 @@ class HsOfficePartnerControllerAcceptanceTest extends ContextBasedTestWithCleanu
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void patchingThePartnerRelCreatesExPartnerRel() {
|
void patchingThePartnerPersonCreatesExPartnerRel() {
|
||||||
|
|
||||||
context.define("superuser-alex@hostsharing.net");
|
context.define("superuser-alex@hostsharing.net");
|
||||||
final var givenPartner = givenSomeTemporaryPartnerBessler(20011);
|
final var givenPartner = givenSomeTemporaryPartnerBessler(20011);
|
||||||
final var givenPartnerRel = givenSomeTemporaryPartnerRel("Winkler", "third contact");
|
final var newPartnerPerson = personRealRepo.findPersonByOptionalNameLike("Winkler").getFirst();
|
||||||
|
|
||||||
RestAssured // @formatter:off
|
RestAssured // @formatter:off
|
||||||
.given()
|
.given()
|
||||||
@ -391,9 +393,11 @@ class HsOfficePartnerControllerAcceptanceTest extends ContextBasedTestWithCleanu
|
|||||||
.contentType(ContentType.JSON)
|
.contentType(ContentType.JSON)
|
||||||
.body("""
|
.body("""
|
||||||
{
|
{
|
||||||
"partnerRel.uuid": "%s"
|
"partnerRel": {
|
||||||
|
"holder.uuid": "%s"
|
||||||
}
|
}
|
||||||
""".formatted(givenPartnerRel.getUuid()))
|
}
|
||||||
|
""".formatted(newPartnerPerson.getUuid()))
|
||||||
.port(port)
|
.port(port)
|
||||||
.when()
|
.when()
|
||||||
.patch("http://localhost/api/hs/office/partners/" + givenPartner.getUuid())
|
.patch("http://localhost/api/hs/office/partners/" + givenPartner.getUuid())
|
||||||
@ -405,8 +409,8 @@ class HsOfficePartnerControllerAcceptanceTest extends ContextBasedTestWithCleanu
|
|||||||
context.define("superuser-alex@hostsharing.net");
|
context.define("superuser-alex@hostsharing.net");
|
||||||
assertThat(partnerRepo.findByUuid(givenPartner.getUuid())).isPresent().get()
|
assertThat(partnerRepo.findByUuid(givenPartner.getUuid())).isPresent().get()
|
||||||
.matches(partner -> {
|
.matches(partner -> {
|
||||||
assertThat(partner.getPartnerRel().getHolder().getFamilyName()).isEqualTo("Winkler");
|
assertThat(partner.getPartnerRel().getHolder().getFamilyName()).isEqualTo("Winkler"); // updated
|
||||||
assertThat(partner.getPartnerRel().getContact().getCaption()).isEqualTo("third contact");
|
assertThat(partner.getPartnerRel().getContact().getCaption()).isEqualTo("fourth contact"); // unchanged
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,20 +1,24 @@
|
|||||||
package net.hostsharing.hsadminng.hs.office.partner;
|
package net.hostsharing.hsadminng.hs.office.partner;
|
||||||
|
|
||||||
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRealEntity;
|
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRealEntity;
|
||||||
|
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePartnerDetailsPatchResource;
|
||||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePartnerPatchResource;
|
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePartnerPatchResource;
|
||||||
|
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeRelationPatchResource;
|
||||||
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRealEntity;
|
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRealEntity;
|
||||||
import net.hostsharing.hsadminng.hs.office.relation.HsOfficeRelationRealEntity;
|
import net.hostsharing.hsadminng.hs.office.relation.HsOfficeRelationRealEntity;
|
||||||
import net.hostsharing.hsadminng.rbac.test.PatchUnitTestBase;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.TestInstance;
|
import org.junit.jupiter.api.TestInstance;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
import org.openapitools.jackson.nullable.JsonNullable;
|
||||||
|
|
||||||
import jakarta.persistence.EntityManager;
|
import jakarta.persistence.EntityManager;
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
|
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
@ -22,19 +26,17 @@ import static org.mockito.Mockito.lenient;
|
|||||||
|
|
||||||
@TestInstance(PER_CLASS)
|
@TestInstance(PER_CLASS)
|
||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
class HsOfficePartnerEntityPatcherUnitTest extends PatchUnitTestBase<
|
// This test class does not rerive from PatchUnitTestBase because it has no directly patchable properties.
|
||||||
HsOfficePartnerPatchResource,
|
// But the factory-structure is kept, so PatchUnitTestBase could easily be plugged back in if needed.
|
||||||
HsOfficePartnerRbacEntity
|
class HsOfficePartnerEntityPatcherUnitTest {
|
||||||
> {
|
|
||||||
|
|
||||||
private static final UUID INITIAL_PARTNER_UUID = UUID.randomUUID();
|
private static final UUID INITIAL_PARTNER_UUID = UUID.randomUUID();
|
||||||
private static final UUID INITIAL_CONTACT_UUID = UUID.randomUUID();
|
private static final UUID INITIAL_CONTACT_UUID = UUID.randomUUID();
|
||||||
private static final UUID INITIAL_PERSON_UUID = UUID.randomUUID();
|
private static final UUID INITIAL_PARTNER_PERSON_UUID = UUID.randomUUID();
|
||||||
private static final UUID INITIAL_DETAILS_UUID = UUID.randomUUID();
|
private static final UUID INITIAL_DETAILS_UUID = UUID.randomUUID();
|
||||||
private static final UUID PATCHED_PARTNER_ROLE_UUID = UUID.randomUUID();
|
|
||||||
|
|
||||||
private final HsOfficePersonRealEntity givenInitialPerson = HsOfficePersonRealEntity.builder()
|
private final HsOfficePersonRealEntity givenInitialPartnerPerson = HsOfficePersonRealEntity.builder()
|
||||||
.uuid(INITIAL_PERSON_UUID)
|
.uuid(INITIAL_PARTNER_PERSON_UUID)
|
||||||
.build();
|
.build();
|
||||||
private final HsOfficeContactRealEntity givenInitialContact = HsOfficeContactRealEntity.builder()
|
private final HsOfficeContactRealEntity givenInitialContact = HsOfficeContactRealEntity.builder()
|
||||||
.uuid(INITIAL_CONTACT_UUID)
|
.uuid(INITIAL_CONTACT_UUID)
|
||||||
@ -43,22 +45,72 @@ class HsOfficePartnerEntityPatcherUnitTest extends PatchUnitTestBase<
|
|||||||
private final HsOfficePartnerDetailsEntity givenInitialDetails = HsOfficePartnerDetailsEntity.builder()
|
private final HsOfficePartnerDetailsEntity givenInitialDetails = HsOfficePartnerDetailsEntity.builder()
|
||||||
.uuid(INITIAL_DETAILS_UUID)
|
.uuid(INITIAL_DETAILS_UUID)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private EntityManager em;
|
private EntityManager em;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void initMocks() {
|
void initMocks() {
|
||||||
lenient().when(em.getReference(eq(HsOfficeRelationRealEntity.class), any())).thenAnswer(invocation ->
|
lenient().when(em.getReference(eq(HsOfficePersonRealEntity.class), any())).thenAnswer(invocation ->
|
||||||
HsOfficeRelationRealEntity.builder().uuid(invocation.getArgument(1)).build());
|
HsOfficePersonRealEntity.builder().uuid(invocation.getArgument(1)).build());
|
||||||
|
lenient().when(em.getReference(eq(HsOfficeContactRealEntity.class), any())).thenAnswer(invocation ->
|
||||||
|
HsOfficeContactRealEntity.builder().uuid(invocation.getArgument(1)).build());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void patchPartnerPerson() {
|
||||||
|
// given
|
||||||
|
final var patchResource = newPatchResource();
|
||||||
|
final var newHolderUuid = UUID.randomUUID();
|
||||||
|
patchResource.setPartnerRel(new HsOfficeRelationPatchResource());
|
||||||
|
patchResource.getPartnerRel().setHolderUuid(JsonNullable.of(newHolderUuid));
|
||||||
|
final var entity = newInitialEntity();
|
||||||
|
|
||||||
|
// when
|
||||||
|
createPatcher(entity).apply(patchResource);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(entity.getPartnerRel().getHolder().getUuid()).isEqualTo(newHolderUuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void patchPartnerContact() {
|
||||||
|
// given
|
||||||
|
final var patchResource = newPatchResource();
|
||||||
|
final var newContactUuid = UUID.randomUUID();
|
||||||
|
patchResource.setPartnerRel(new HsOfficeRelationPatchResource());
|
||||||
|
patchResource.getPartnerRel().setContactUuid(JsonNullable.of(newContactUuid));
|
||||||
|
final var entity = newInitialEntity();
|
||||||
|
|
||||||
|
// when
|
||||||
|
createPatcher(entity).apply(patchResource);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(entity.getPartnerRel().getContact().getUuid()).isEqualTo(newContactUuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void patchPartnerDetails() {
|
||||||
|
// given
|
||||||
|
final var patchResource = newPatchResource();
|
||||||
|
final var newDateOfBirth = LocalDate.now();
|
||||||
|
patchResource.setDetails(new HsOfficePartnerDetailsPatchResource());
|
||||||
|
patchResource.getDetails().setDateOfDeath(JsonNullable.of(newDateOfBirth));
|
||||||
|
final var entity = newInitialEntity();
|
||||||
|
|
||||||
|
// when
|
||||||
|
createPatcher(entity).apply(patchResource);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(entity.getDetails().getDateOfDeath()).isEqualTo(newDateOfBirth);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected HsOfficePartnerRbacEntity newInitialEntity() {
|
protected HsOfficePartnerRbacEntity newInitialEntity() {
|
||||||
final var entity = HsOfficePartnerRbacEntity.builder()
|
final var entity = HsOfficePartnerRbacEntity.builder()
|
||||||
.uuid(INITIAL_PARTNER_UUID)
|
.uuid(INITIAL_PARTNER_UUID)
|
||||||
.partnerNumber(12345)
|
.partnerNumber(12345)
|
||||||
.partnerRel(HsOfficeRelationRealEntity.builder()
|
.partnerRel(HsOfficeRelationRealEntity.builder()
|
||||||
.holder(givenInitialPerson)
|
.holder(givenInitialPartnerPerson)
|
||||||
.contact(givenInitialContact)
|
.contact(givenInitialContact)
|
||||||
.build())
|
.build())
|
||||||
.details(givenInitialDetails)
|
.details(givenInitialDetails)
|
||||||
@ -66,32 +118,11 @@ class HsOfficePartnerEntityPatcherUnitTest extends PatchUnitTestBase<
|
|||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected HsOfficePartnerPatchResource newPatchResource() {
|
protected HsOfficePartnerPatchResource newPatchResource() {
|
||||||
return new HsOfficePartnerPatchResource();
|
return new HsOfficePartnerPatchResource();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected HsOfficePartnerEntityPatcher createPatcher(final HsOfficePartnerRbacEntity partner) {
|
protected HsOfficePartnerEntityPatcher createPatcher(final HsOfficePartnerRbacEntity partner) {
|
||||||
return new HsOfficePartnerEntityPatcher(em, partner);
|
return new HsOfficePartnerEntityPatcher(em, partner);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Stream<Property> propertyTestDescriptors() {
|
|
||||||
return Stream.of(
|
|
||||||
new JsonNullableProperty<>(
|
|
||||||
"partnerRel",
|
|
||||||
HsOfficePartnerPatchResource::setPartnerRelUuid,
|
|
||||||
PATCHED_PARTNER_ROLE_UUID,
|
|
||||||
HsOfficePartnerRbacEntity::setPartnerRel,
|
|
||||||
newPartnerRel(PATCHED_PARTNER_ROLE_UUID))
|
|
||||||
.notNullable()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static HsOfficeRelationRealEntity newPartnerRel(final UUID uuid) {
|
|
||||||
return HsOfficeRelationRealEntity.builder()
|
|
||||||
.uuid(uuid)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user