1
0

patch-with-getReference, no need for fetch anymore

This commit is contained in:
Marc O. Sandlus
2022-09-26 12:40:59 +02:00
committed by Michael Hoennig
parent d13f0cbcdf
commit 89415067ef
5 changed files with 36 additions and 46 deletions

View File

@@ -7,8 +7,6 @@ import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.openapitools.jackson.nullable.JsonNullable;
import java.util.NoSuchElementException;
import java.util.UUID;
import java.util.function.BiConsumer;
import java.util.stream.Stream;
@@ -73,29 +71,8 @@ public abstract class PatchUnitTestBase<R, E> {
@ParameterizedTest
@MethodSource("propertyTestCases")
void willThrowIfUUidCannotBeResolved(final Property<R, Object, E, Object> testCase) {
assumeThat(testCase.resolvesUuid).isTrue();
// given
final var givenEntity = newInitialEntity();
final var patchResource = newPatchResource();
final var givenPatchValue = UUID.fromString("11111111-1111-1111-1111-111111111111");
testCase.patchResourceWithExplicitValue(patchResource, givenPatchValue);
// when
final var exception = catchThrowableOfType(() -> {
createPatcher(givenEntity).apply(patchResource);
}, NoSuchElementException.class);
// then
assertThat(exception).isInstanceOf(NoSuchElementException.class)
.hasMessage("cannot find '" + testCase.name + "' uuid " + givenPatchValue);
}
@ParameterizedTest
@MethodSource("propertyTestCases")
void willThrowExceptionIfNotNullableUuidIsNull(final Property<R, Object, E, Object> testCase) {
assumeThat(testCase.resolvesUuid).isTrue();
void willThrowExceptionIfNotNullableValueIsNull(final Property<R, Object, E, Object> testCase) {
assumeThat(testCase instanceof JsonNullableProperty).isTrue();
assumeThat(testCase.nullable).isFalse();
// given
@@ -169,7 +146,6 @@ public abstract class PatchUnitTestBase<R, E> {
protected final BiConsumer<E, EV> entitySetter;
protected final EV expectedPatchValue;
protected boolean nullable = true;
private boolean resolvesUuid = false;
protected Property(
final String name,
@@ -198,11 +174,6 @@ public abstract class PatchUnitTestBase<R, E> {
return this;
}
public Property<R, RV, E, EV> resolvesUuid() {
resolvesUuid = true;
return this;
}
@SuppressWarnings("unchecked")
protected static <EV, RV> EV sameAs(final RV givenResourceValue) {
return (EV) givenResourceValue;

View File

@@ -4,16 +4,25 @@ import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactEntity;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePartnerPatchResource;
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonEntity;
import net.hostsharing.hsadminng.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.Optional;
import java.util.UUID;
import java.util.stream.Stream;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
import static org.mockito.Mockito.lenient;
@TestInstance(PER_CLASS)
@ExtendWith(MockitoExtension.class)
class HsOfficePartnerEntityPatcherUnitTest extends PatchUnitTestBase<
HsOfficePartnerPatchResource,
HsOfficePartnerEntity
@@ -37,7 +46,16 @@ class HsOfficePartnerEntityPatcherUnitTest extends PatchUnitTestBase<
private final HsOfficeContactEntity givenInitialContact = HsOfficeContactEntity.builder()
.uuid(INITIAL_CONTACT_UUID)
.build();
@Mock
private EntityManager em;
@BeforeEach
void initMocks() {
lenient().when(em.getReference(eq(HsOfficeContactEntity.class), any())).thenAnswer(invocation ->
HsOfficeContactEntity.builder().uuid(invocation.getArgument(1)).build());
lenient().when(em.getReference(eq(HsOfficePersonEntity.class), any())).thenAnswer(invocation ->
HsOfficePersonEntity.builder().uuid(invocation.getArgument(1)).build());
}
@Override
protected HsOfficePartnerEntity newInitialEntity() {
final var entity = new HsOfficePartnerEntity();
@@ -60,6 +78,7 @@ class HsOfficePartnerEntityPatcherUnitTest extends PatchUnitTestBase<
@Override
protected HsOfficePartnerEntityPatcher createPatcher(final HsOfficePartnerEntity partner) {
return new HsOfficePartnerEntityPatcher(
em,
partner,
uuid -> uuid == PATCHED_CONTACT_UUID
? Optional.of(newContact(uuid))
@@ -78,16 +97,14 @@ class HsOfficePartnerEntityPatcherUnitTest extends PatchUnitTestBase<
PATCHED_CONTACT_UUID,
HsOfficePartnerEntity::setContact,
newContact(PATCHED_CONTACT_UUID))
.notNullable()
.resolvesUuid(),
.notNullable(),
new JsonNullableProperty<>(
"person",
HsOfficePartnerPatchResource::setPersonUuid,
PATCHED_PERSON_UUID,
HsOfficePartnerEntity::setPerson,
newPerson(PATCHED_PERSON_UUID))
.notNullable()
.resolvesUuid(),
.notNullable(),
new JsonNullableProperty<>(
"registrationOffice",
HsOfficePartnerPatchResource::setRegistrationOffice,