From 0bab27d72386a50ac08ef1aa0f9ddfe57ffffc40 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Tue, 20 Sep 2022 16:32:56 +0200 Subject: [PATCH] add missing HsOfficeContactEntityPatchUnitTest --- .../hs-office-contacts-with-uuid.yaml | 2 +- .../HsOfficeContactEntityPatchUnitTest.java | 152 ++++++++++++++++++ 2 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactEntityPatchUnitTest.java diff --git a/src/main/resources/api-definition/hs-office/hs-office-contacts-with-uuid.yaml b/src/main/resources/api-definition/hs-office/hs-office-contacts-with-uuid.yaml index b61848f3..60f74fa5 100644 --- a/src/main/resources/api-definition/hs-office/hs-office-contacts-with-uuid.yaml +++ b/src/main/resources/api-definition/hs-office/hs-office-contacts-with-uuid.yaml @@ -29,7 +29,7 @@ get: patch: tags: - hs-office-contacts - description: 'Updates a single business contact by its uuid, if permitted for the current subject.' + description: 'Updates a single contact by its uuid, if permitted for the current subject.' operationId: patchContact parameters: - $ref: './auth.yaml#/components/parameters/currentUser' 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 new file mode 100644 index 00000000..607bc5fd --- /dev/null +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactEntityPatchUnitTest.java @@ -0,0 +1,152 @@ +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); + } + } +}