1
0

refactor HsOfficePartnerEntityPatchUnitTest to PatchUnitTestBase

This commit is contained in:
Michael Hoennig
2022-09-24 12:43:42 +02:00
parent d08e60f8dc
commit 3e6da45302
4 changed files with 312 additions and 272 deletions

View File

@ -0,0 +1,6 @@
package net.hostsharing.hsadminng.hs.office.partner;
public interface EntityPatch<R> {
void apply(R resource);
}

View File

@ -9,8 +9,9 @@ import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Supplier;
class HsOfficePartnerEntityPatch {
class HsOfficePartnerEntityPatch implements EntityPatch<HsOfficePartnerPatchResource> {
private final HsOfficePartnerEntity entity;
private final Function<UUID, Optional<HsOfficeContactEntity>> fetchContact;
@ -25,16 +26,17 @@ class HsOfficePartnerEntityPatch {
this.fetchPerson = fetchPerson;
}
void apply(final HsOfficePartnerPatchResource resource) {
@Override
public void apply(final HsOfficePartnerPatchResource resource) {
OptionalFromJson.of(resource.getContactUuid()).ifPresent(newValue -> {
entity.setContact(fetchContact.apply(newValue).orElseThrow(
() -> new NoSuchElementException("cannot find contact uuid " + newValue)
));
verifyNotNull(newValue, "contact");
entity.setContact(fetchContact.apply(newValue)
.orElseThrow(noSuchElementException("contact", newValue)));
});
OptionalFromJson.of(resource.getPersonUuid()).ifPresent(newValue -> {
entity.setPerson(fetchPerson.apply(newValue).orElseThrow(
() -> new NoSuchElementException("cannot find person uuid " + newValue)
));
verifyNotNull(newValue, "person");
entity.setPerson(fetchPerson.apply(newValue)
.orElseThrow(noSuchElementException("person", newValue)));
});
OptionalFromJson.of(resource.getRegistrationOffice()).ifPresent(entity::setRegistrationOffice);
OptionalFromJson.of(resource.getRegistrationNumber()).ifPresent(entity::setRegistrationNumber);
@ -42,4 +44,14 @@ class HsOfficePartnerEntityPatch {
OptionalFromJson.of(resource.getBirthName()).ifPresent(entity::setBirthName);
OptionalFromJson.of(resource.getDateOfDeath()).ifPresent(entity::setDateOfDeath);
}
private Supplier<RuntimeException> noSuchElementException(final String propertyName, final UUID newValue) {
return () -> new NoSuchElementException("cannot find '" + propertyName + "' uuid " + newValue);
}
private void verifyNotNull(final UUID newValue, final String propertyName) {
if (newValue == null) {
throw new IllegalArgumentException("property '" + propertyName + "' must not be null");
}
}
}