Skip to content

Commit

Permalink
Merge pull request #622 from FgForrest/621-failed-to-remove-all-refer…
Browse files Browse the repository at this point in the history
…ences

fix(#621): Failed to remove all references
  • Loading branch information
novoj authored Jun 25, 2024
2 parents d7d59c7 + 63a0595 commit 305111d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ public void addOrReplaceReferenceMutations(@Nonnull ReferenceBuilder referenceBu
final AttributeKey attributeKey = attributeMutation.getAttributeKey();
if (attributeMutation instanceof UpsertAttributeMutation upsertAttributeMutation) {
final boolean attributeSameAsPreviousOne = this.baseEntity.getReference(referenceAttributeMutation.getReferenceKey())
.flatMap(ref -> ref.getAttributeValue(attributeKey))
.flatMap(ref -> ref.getAttributeSchema(attributeKey.attributeName()).isPresent() ? ref.getAttributeValue(attributeKey) : Optional.empty())
.map(attribute -> Objects.equals(attribute.value(), upsertAttributeMutation.getAttributeValue()))
.orElse(false);
return !attributeSameAsPreviousOne;
Expand Down Expand Up @@ -758,21 +758,23 @@ public EntityBuilder removeReference(@Nonnull String referenceName, int referenc
);
final ReferenceKey referenceKey = new ReferenceKey(referenceName, referencedPrimaryKey);
Assert.isTrue(getReference(referenceName, referencedPrimaryKey).isPresent(), "There's no reference of type " + referenceName + " and primary key " + referencedPrimaryKey + "!");

// remove possibly added / updated reference mutation
this.referenceMutations.remove(referenceKey);

final Optional<ReferenceContract> theReference = baseEntity.getReferenceWithoutSchemaCheck(referenceKey)
.filter(referencePredicate);
Assert.isTrue(
theReference.isPresent(),
"Reference to " + referenceName + " and primary key " + referenceKey +
" is not present on the entity " + baseEntity.getType() + " and id " +
baseEntity.getPrimaryKey() + "!"
);
this.referenceMutations.put(
referenceKey,
Collections.singletonList(
new RemoveReferenceMutation(referenceKey)
)
);
this.removedReferences.add(referenceKey);
if (theReference.isPresent()) {
// if the reference was part of the previous entity version we build upon, remove it as-well
this.referenceMutations.put(
referenceKey,
Collections.singletonList(
new RemoveReferenceMutation(referenceKey)
)
);
this.removedReferences.add(referenceKey);
}

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* | __/\ V /| | || (_| | |_| | |_) |
* \___| \_/ |_|\__\__,_|____/|____/
*
* Copyright (c) 2023
* Copyright (c) 2023-2024
*
* Licensed under the Business Source License, Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -43,6 +43,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import javax.annotation.Nonnull;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Currency;
Expand Down Expand Up @@ -130,24 +131,7 @@ void shouldRemoveParent() {

@Test
void shouldDefineReferenceGroup() {
builder.setReference(BRAND_TYPE, BRAND_TYPE, Cardinality.ZERO_OR_ONE, 1, whichIs -> whichIs.setGroup("Whatever", 8));

final EntityMutation entityMutation = builder.toMutation().orElseThrow();
final Collection<? extends LocalMutation<?, ?>> localMutations = entityMutation.getLocalMutations();
assertEquals(2, localMutations.size());

final SealedEntitySchema sealedEntitySchema = new EntitySchemaDecorator(() -> CATALOG_SCHEMA, (EntitySchema) initialEntity.getSchema());
final EntitySchemaMutation[] schemaMutations = EntityMutation.verifyOrEvolveSchema(
CATALOG_SCHEMA,
sealedEntitySchema,
localMutations
).orElseThrow();

final EntitySchemaContract updatedSchema = sealedEntitySchema
.withMutations(schemaMutations)
.toInstance();

final Entity updatedEntity = entityMutation.mutate(updatedSchema, initialEntity);
final Entity updatedEntity = setupEntityWithBrand();
final ReferenceContract reference = updatedEntity.getReference(BRAND_TYPE, 1).orElseThrow();
assertEquals(new GroupEntityReference("Whatever", 8, 1, false), reference.getGroup().orElse(null));
}
Expand Down Expand Up @@ -265,4 +249,63 @@ void shouldReturnOriginalEntityInstanceWhenNothingHasChanged() {
assertSame(initialEntity, newEntity);
}

@Test
void shouldRemoveAddedReference() {
final Entity entityWithBrand = setupEntityWithBrand();

final SealedEntity updatedInstance = new ExistingEntityBuilder(entityWithBrand)
.setReference(
BRAND_TYPE, BRAND_TYPE, Cardinality.ZERO_OR_MORE, 2,
whichIs -> whichIs.setAttribute("newAttribute", "someValue")
)
.removeReference(BRAND_TYPE, 2)
.toInstance();

assertEquals(1, updatedInstance.getReferences(BRAND_TYPE).size());
}

@Test
void shouldRemoveExistingReferenceAndAddAgain() {
final Entity entityWithBrand = setupEntityWithBrand();

final SealedEntity updatedInstance = new ExistingEntityBuilder(entityWithBrand)
.removeReference(BRAND_TYPE, 1)
.setReference(
BRAND_TYPE, 1,
whichIs -> whichIs
.setGroup("Whatever", 8)
.setAttribute("newAttribute", "someValue")
)
.toInstance();

assertEquals(1, updatedInstance.getReferences(BRAND_TYPE).size());

updatedInstance.getReference(BRAND_TYPE, 1).ifPresent(reference -> {
assertEquals("Whatever", reference.getGroup().map(GroupEntityReference::getType).orElse(null));
assertEquals(8, reference.getGroup().map(GroupEntityReference::getPrimaryKey).orElse(null));
assertEquals("someValue", reference.getAttribute("newAttribute"));
});
}

@Nonnull
private Entity setupEntityWithBrand() {
builder.setReference(BRAND_TYPE, BRAND_TYPE, Cardinality.ZERO_OR_ONE, 1, whichIs -> whichIs.setGroup("Whatever", 8));

final EntityMutation entityMutation = builder.toMutation().orElseThrow();
final Collection<? extends LocalMutation<?, ?>> localMutations = entityMutation.getLocalMutations();
assertEquals(2, localMutations.size());

final SealedEntitySchema sealedEntitySchema = new EntitySchemaDecorator(() -> CATALOG_SCHEMA, (EntitySchema) initialEntity.getSchema());
final EntitySchemaMutation[] schemaMutations = EntityMutation.verifyOrEvolveSchema(
CATALOG_SCHEMA,
sealedEntitySchema,
localMutations
).orElseThrow();

final EntitySchemaContract updatedSchema = sealedEntitySchema
.withMutations(schemaMutations)
.toInstance();

return entityMutation.mutate(updatedSchema, initialEntity);
}
}

0 comments on commit 305111d

Please sign in to comment.