From d13f0cbcdf627e4b26ea155278e64048491cacd4 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Sun, 25 Sep 2022 11:37:46 +0200 Subject: [PATCH] refactor HsOfficeContactEntityPatcherUnitTest to PatchUnitTestBase --- .../{EntityPatch.java => EntityPatcher.java} | 2 +- .../contact/HsOfficeContactEntityPatch.java | 6 +- .../partner/HsOfficePartnerController.java | 3 +- ...java => HsOfficePartnerEntityPatcher.java} | 6 +- .../person/HsOfficePersonController.java | 3 +- ....java => HsOfficePersonEntityPatcher.java} | 6 +- .../hsadminng/PatchUnitTestBase.java | 2 +- .../HsOfficeContactEntityPatchUnitTest.java | 152 ------------------ .../HsOfficeContactEntityPatcherUnitTest.java | 66 ++++++++ ...HsOfficePartnerEntityPatcherUnitTest.java} | 6 +- ... HsOfficePersonEntityPatcherUnitTest.java} | 7 +- 11 files changed, 86 insertions(+), 173 deletions(-) rename src/main/java/net/hostsharing/hsadminng/{EntityPatch.java => EntityPatcher.java} (65%) rename src/main/java/net/hostsharing/hsadminng/hs/office/partner/{HsOfficePartnerEntityPatch.java => HsOfficePartnerEntityPatcher.java} (93%) rename src/main/java/net/hostsharing/hsadminng/hs/office/person/{HsOfficePersonEntityPatch.java => HsOfficePersonEntityPatcher.java} (82%) delete mode 100644 src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactEntityPatchUnitTest.java create mode 100644 src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactEntityPatcherUnitTest.java rename src/test/java/net/hostsharing/hsadminng/hs/office/partner/{HsOfficePartnerEntityPatchUnitTest.java => HsOfficePartnerEntityPatcherUnitTest.java} (95%) rename src/test/java/net/hostsharing/hsadminng/hs/office/person/{HsOfficePersonEntityPatchUnitTest.java => HsOfficePersonEntityPatcherUnitTest.java} (91%) diff --git a/src/main/java/net/hostsharing/hsadminng/EntityPatch.java b/src/main/java/net/hostsharing/hsadminng/EntityPatcher.java similarity index 65% rename from src/main/java/net/hostsharing/hsadminng/EntityPatch.java rename to src/main/java/net/hostsharing/hsadminng/EntityPatcher.java index a21c931d..5a0c0f44 100644 --- a/src/main/java/net/hostsharing/hsadminng/EntityPatch.java +++ b/src/main/java/net/hostsharing/hsadminng/EntityPatcher.java @@ -1,6 +1,6 @@ package net.hostsharing.hsadminng; -public interface EntityPatch { +public interface EntityPatcher { void apply(R resource); } diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactEntityPatch.java b/src/main/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactEntityPatch.java index d79a6345..863d1e1f 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactEntityPatch.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactEntityPatch.java @@ -1,9 +1,10 @@ package net.hostsharing.hsadminng.hs.office.contact; +import net.hostsharing.hsadminng.EntityPatcher; import net.hostsharing.hsadminng.OptionalFromJson; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeContactPatchResource; -class HsOfficeContactEntityPatch { +class HsOfficeContactEntityPatch implements EntityPatcher { private final HsOfficeContactEntity entity; @@ -11,7 +12,8 @@ class HsOfficeContactEntityPatch { this.entity = entity; } - void apply(final HsOfficeContactPatchResource resource) { + @Override + public void apply(final HsOfficeContactPatchResource resource) { OptionalFromJson.of(resource.getLabel()).ifPresent(entity::setLabel); OptionalFromJson.of(resource.getPostalAddress()).ifPresent(entity::setPostalAddress); OptionalFromJson.of(resource.getEmailAddresses()).ifPresent(entity::setEmailAddresses); diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerController.java index c56199c5..e17b1ed2 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerController.java @@ -1,7 +1,6 @@ package net.hostsharing.hsadminng.hs.office.partner; import net.hostsharing.hsadminng.Mapper; -import net.hostsharing.hsadminng.OptionalFromJson; import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRepository; import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficePartnersApi; @@ -125,7 +124,7 @@ public class HsOfficePartnerController implements HsOfficePartnersApi { final var current = partnerRepo.findByUuid(partnerUuid).orElseThrow(); - new HsOfficePartnerEntityPatch(current, contactRepo::findByUuid, personRepo::findByUuid).apply(body); + new HsOfficePartnerEntityPatcher(current, contactRepo::findByUuid, personRepo::findByUuid).apply(body); final var saved = partnerRepo.save(current); final var mapped = map(saved, HsOfficePartnerResource.class); diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerEntityPatch.java b/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerEntityPatcher.java similarity index 93% rename from src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerEntityPatch.java rename to src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerEntityPatcher.java index 9893807b..86590874 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerEntityPatch.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerEntityPatcher.java @@ -1,6 +1,6 @@ package net.hostsharing.hsadminng.hs.office.partner; -import net.hostsharing.hsadminng.EntityPatch; +import net.hostsharing.hsadminng.EntityPatcher; import net.hostsharing.hsadminng.OptionalFromJson; import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactEntity; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePartnerPatchResource; @@ -12,13 +12,13 @@ import java.util.UUID; import java.util.function.Function; import java.util.function.Supplier; -class HsOfficePartnerEntityPatch implements EntityPatch { +class HsOfficePartnerEntityPatcher implements EntityPatcher { private final HsOfficePartnerEntity entity; private final Function> fetchContact; private final Function> fetchPerson; - HsOfficePartnerEntityPatch( + HsOfficePartnerEntityPatcher( final HsOfficePartnerEntity entity, final Function> fetchContact, final Function> fetchPerson) { diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonController.java index a869e73e..9346afbd 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonController.java @@ -6,7 +6,6 @@ import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficePersonsA import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePersonInsertResource; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePersonPatchResource; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePersonResource; -import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonEntityPatch; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.transaction.annotation.Transactional; @@ -109,7 +108,7 @@ public class HsOfficePersonController implements HsOfficePersonsApi { final var current = personRepo.findByUuid(personUuid).orElseThrow(); - new HsOfficePersonEntityPatch(current).apply(body); + new HsOfficePersonEntityPatcher(current).apply(body); final var saved = personRepo.save(current); final var mapped = map(saved, HsOfficePersonResource.class); diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonEntityPatch.java b/src/main/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonEntityPatcher.java similarity index 82% rename from src/main/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonEntityPatch.java rename to src/main/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonEntityPatcher.java index 659011a2..01e200eb 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonEntityPatch.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonEntityPatcher.java @@ -3,15 +3,15 @@ package net.hostsharing.hsadminng.hs.office.person; import net.hostsharing.hsadminng.OptionalFromJson; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePersonPatchResource; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePersonTypeResource; -import net.hostsharing.hsadminng.EntityPatch; +import net.hostsharing.hsadminng.EntityPatcher; import java.util.Optional; -class HsOfficePersonEntityPatch implements EntityPatch { +class HsOfficePersonEntityPatcher implements EntityPatcher { private final HsOfficePersonEntity entity; - HsOfficePersonEntityPatch(final HsOfficePersonEntity entity) { + HsOfficePersonEntityPatcher(final HsOfficePersonEntity entity) { this.entity = entity; } diff --git a/src/test/java/net/hostsharing/hsadminng/PatchUnitTestBase.java b/src/test/java/net/hostsharing/hsadminng/PatchUnitTestBase.java index 6eaea7d2..ec05f6f3 100644 --- a/src/test/java/net/hostsharing/hsadminng/PatchUnitTestBase.java +++ b/src/test/java/net/hostsharing/hsadminng/PatchUnitTestBase.java @@ -153,7 +153,7 @@ public abstract class PatchUnitTestBase { protected abstract R newPatchResource(); - protected abstract EntityPatch createPatcher(final E entity); + protected abstract EntityPatcher createPatcher(final E entity); @SuppressWarnings("rawtypes") protected abstract Stream propertyTestDescriptors(); diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactEntityPatchUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactEntityPatchUnitTest.java deleted file mode 100644 index 607bc5fd..00000000 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactEntityPatchUnitTest.java +++ /dev/null @@ -1,152 +0,0 @@ -package net.hostsharing.hsadminng.hs.office.contact; - -import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeContactPatchResource; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.NullSource; -import org.junit.jupiter.params.provider.ValueSource; -import org.openapitools.jackson.nullable.JsonNullable; - -import java.util.UUID; - -import static org.assertj.core.api.Assertions.assertThat; - -// TODO: there must be an easier way to test such patch classes -class HsOfficeContactEntityPatchUnitTest { - - private static final UUID INITIAL_CONTACT_UUID = UUID.randomUUID(); - final HsOfficeContactEntity givenContact = new HsOfficeContactEntity(); - final HsOfficeContactPatchResource patchResource = new HsOfficeContactPatchResource(); - - private final HsOfficeContactEntityPatch hsOfficeContactEntityPatch = - new HsOfficeContactEntityPatch(givenContact); - - { - givenContact.setUuid(INITIAL_CONTACT_UUID); - givenContact.setLabel("initial label"); - givenContact.setEmailAddresses("initial@example.org"); - givenContact.setPostalAddress("initial postal address"); - givenContact.setPhoneNumbers("+01 100 123456789"); - } - - @Test - void willPatchAllProperties() { - // given - patchResource.setLabel(JsonNullable.of("patched label")); - patchResource.setEmailAddresses(JsonNullable.of("patched@example.org")); - patchResource.setPostalAddress(JsonNullable.of("patched postal address")); - patchResource.setPhoneNumbers(JsonNullable.of("+01 200 987654321")); - - // when - hsOfficeContactEntityPatch.apply(patchResource); - - // then - new HsOfficeContactEntityMatcher() - .withPatchedLabel("patched label") - .withPatchedEmailAddresses("patched@example.org") - .withPatchedPostalAddress("patched postal address") - .withPatchedPhoneNumbers("+01 200 987654321") - .matches(givenContact); - } - - @ParameterizedTest - @ValueSource(strings = { "patched label" }) - @NullSource - void willPatchOnlyLabelProperty(final String patchedValue) { - // given - patchResource.setLabel(JsonNullable.of(patchedValue)); - - // when - hsOfficeContactEntityPatch.apply(patchResource); - - // then - new HsOfficeContactEntityMatcher() - .withPatchedLabel(patchedValue) - .matches(givenContact); - } - - @ParameterizedTest - @ValueSource(strings = { "patched@example.org" }) - @NullSource - void willPatchOnlyEmailAddressesProperty(final String patchedValue) { - // given - patchResource.setEmailAddresses(JsonNullable.of(patchedValue)); - - // when - hsOfficeContactEntityPatch.apply(patchResource); - - // then - new HsOfficeContactEntityMatcher() - .withPatchedEmailAddresses(patchedValue) - .matches(givenContact); - } - - @ParameterizedTest - @ValueSource(strings = { "patched postal address" }) - @NullSource - void willPatchOnlyPostalAddressProperty(final String patchedValue) { - // given - patchResource.setPostalAddress(JsonNullable.of(patchedValue)); - - // when - hsOfficeContactEntityPatch.apply(patchResource); - - // then - new HsOfficeContactEntityMatcher() - .withPatchedPostalAddress(patchedValue) - .matches(givenContact); - } - - @ParameterizedTest - @ValueSource(strings = { "+01 200 987654321" }) - @NullSource - void willPatchOnlyPhoneNumbersProperty(final String patchedValue) { - // given - patchResource.setPhoneNumbers(JsonNullable.of(patchedValue)); - - // when - hsOfficeContactEntityPatch.apply(patchResource); - - // then - new HsOfficeContactEntityMatcher() - .withPatchedPhoneNumbers(patchedValue) - .matches(givenContact); - } - - private static class HsOfficeContactEntityMatcher { - - private String expectedLabel = "initial label"; - private String expectedEmailAddresses = "initial@example.org"; - private String expectedPostalAddress = "initial postal address"; - - private String expectedPhoneNumbers = "+01 100 123456789"; - - HsOfficeContactEntityMatcher withPatchedLabel(final String patchedLabel) { - expectedLabel = patchedLabel; - return this; - } - - HsOfficeContactEntityMatcher withPatchedEmailAddresses(final String patchedEmailAddresses) { - expectedEmailAddresses = patchedEmailAddresses; - return this; - } - - HsOfficeContactEntityMatcher withPatchedPostalAddress(final String patchedPostalAddress) { - expectedPostalAddress = patchedPostalAddress; - return this; - } - - HsOfficeContactEntityMatcher withPatchedPhoneNumbers(final String patchedPhoneNumbers) { - expectedPhoneNumbers = patchedPhoneNumbers; - return this; - } - - void matches(final HsOfficeContactEntity givenContact) { - - assertThat(givenContact.getLabel()).isEqualTo(expectedLabel); - assertThat(givenContact.getEmailAddresses()).isEqualTo(expectedEmailAddresses); - assertThat(givenContact.getPostalAddress()).isEqualTo(expectedPostalAddress); - assertThat(givenContact.getPhoneNumbers()).isEqualTo(expectedPhoneNumbers); - } - } -} diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactEntityPatcherUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactEntityPatcherUnitTest.java new file mode 100644 index 00000000..624db76c --- /dev/null +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactEntityPatcherUnitTest.java @@ -0,0 +1,66 @@ +package net.hostsharing.hsadminng.hs.office.contact; + +import net.hostsharing.hsadminng.PatchUnitTestBase; +import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeContactPatchResource; +import org.junit.jupiter.api.TestInstance; + +import java.util.UUID; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; + +@TestInstance(PER_CLASS) +class HsOfficeContactEntityPatcherUnitTest extends PatchUnitTestBase< + HsOfficeContactPatchResource, + HsOfficeContactEntity + > { + + private static final UUID INITIAL_CONTACT_UUID = UUID.randomUUID(); + + @Override + protected HsOfficeContactEntity newInitialEntity() { + final var entity = new HsOfficeContactEntity(); + entity.setUuid(INITIAL_CONTACT_UUID); + entity.setLabel("initial label"); + entity.setEmailAddresses("initial@example.org"); + entity.setPhoneNumbers("initial postal address"); + entity.setPostalAddress("+01 100 123456789"); + return entity; + } + + @Override + protected HsOfficeContactPatchResource newPatchResource() { + return new HsOfficeContactPatchResource(); + } + + @Override + protected HsOfficeContactEntityPatch createPatcher(final HsOfficeContactEntity entity) { + return new HsOfficeContactEntityPatch(entity); + } + + @Override + protected Stream propertyTestDescriptors() { + return Stream.of( + new JsonNullableProperty<>( + "label", + HsOfficeContactPatchResource::setLabel, + "patched label", + HsOfficeContactEntity::setLabel), + new JsonNullableProperty<>( + "emailAddresses", + HsOfficeContactPatchResource::setEmailAddresses, + "patched trade name", + HsOfficeContactEntity::setEmailAddresses), + new JsonNullableProperty<>( + "phoneNumbers", + HsOfficeContactPatchResource::setPhoneNumbers, + "patched family name", + HsOfficeContactEntity::setPhoneNumbers), + new JsonNullableProperty<>( + "patched given name", + HsOfficeContactPatchResource::setPostalAddress, + "patched given name", + HsOfficeContactEntity::setPostalAddress) + ); + } +} diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerEntityPatchUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerEntityPatcherUnitTest.java similarity index 95% rename from src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerEntityPatchUnitTest.java rename to src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerEntityPatcherUnitTest.java index a1460c76..0e8d2bae 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerEntityPatchUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerEntityPatcherUnitTest.java @@ -14,7 +14,7 @@ import java.util.stream.Stream; import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; @TestInstance(PER_CLASS) -class HsOfficePartnerEntityPatchUnitTest extends PatchUnitTestBase< +class HsOfficePartnerEntityPatcherUnitTest extends PatchUnitTestBase< HsOfficePartnerPatchResource, HsOfficePartnerEntity > { @@ -58,8 +58,8 @@ class HsOfficePartnerEntityPatchUnitTest extends PatchUnitTestBase< } @Override - protected HsOfficePartnerEntityPatch createPatcher(final HsOfficePartnerEntity partner) { - return new HsOfficePartnerEntityPatch( + protected HsOfficePartnerEntityPatcher createPatcher(final HsOfficePartnerEntity partner) { + return new HsOfficePartnerEntityPatcher( partner, uuid -> uuid == PATCHED_CONTACT_UUID ? Optional.of(newContact(uuid)) diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonEntityPatchUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonEntityPatcherUnitTest.java similarity index 91% rename from src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonEntityPatchUnitTest.java rename to src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonEntityPatcherUnitTest.java index bf3d9c83..7a227789 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonEntityPatchUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonEntityPatcherUnitTest.java @@ -11,7 +11,7 @@ import java.util.stream.Stream; import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; @TestInstance(PER_CLASS) -class HsOfficePersonEntityPatchUnitTest extends PatchUnitTestBase< +class HsOfficePersonEntityPatcherUnitTest extends PatchUnitTestBase< HsOfficePersonPatchResource, HsOfficePersonEntity > { @@ -35,8 +35,8 @@ class HsOfficePersonEntityPatchUnitTest extends PatchUnitTestBase< } @Override - protected HsOfficePersonEntityPatch createPatcher(final HsOfficePersonEntity entity) { - return new HsOfficePersonEntityPatch(entity); + protected HsOfficePersonEntityPatcher createPatcher(final HsOfficePersonEntity entity) { + return new HsOfficePersonEntityPatcher(entity); } @Override @@ -66,5 +66,4 @@ class HsOfficePersonEntityPatchUnitTest extends PatchUnitTestBase< HsOfficePersonEntity::setGivenName) ); } - }