Skip to content

Commit

Permalink
Fix unit tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Yufei Cai <yufei.cai@bosch.io>
  • Loading branch information
yufei-cai committed Mar 30, 2022
1 parent e5eaf1c commit 57695e4
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@
*/
package org.eclipse.ditto.thingsearch.service.persistence.write.mapping;

import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_INTERNAL;
import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_INTERNAL_KEY;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.bson.BsonArray;
Expand All @@ -32,8 +28,6 @@
import org.bson.BsonValue;
import org.eclipse.ditto.json.JsonPointer;

import akka.japi.Pair;

/**
* Diff between 2 BSON arrays. Only used for the flattened key-value array because Ditto has no API for array
* operations.
Expand All @@ -51,32 +45,6 @@ static BsonValue diff(final JsonPointer key,
return diff(key, minuend, subtrahend, (v, j) -> j);
}

static BsonDiff diffInternalArray(final BsonArray minuend, final BsonArray subtrahend) {
final BsonSizeVisitor bsonSizeVisitor = new BsonSizeVisitor();
final int replacementSize = bsonSizeVisitor.eval(subtrahend);
if (minuend.equals(subtrahend)) {
return BsonDiff.empty(replacementSize);
}
final JsonPointer internalArrayKey = JsonPointer.of(FIELD_INTERNAL);
final Map<BsonValue, Integer> kMap = IntStream.range(0, subtrahend.size())
.boxed()
.collect(Collectors.toMap(
i -> subtrahend.get(i).asDocument().get(FIELD_INTERNAL_KEY),
Function.identity(),
(x, y) -> x
));
final BiFunction<BsonDocument, Integer, Integer> kMapGet =
// use 0 as default value to re-use root grant/revoke arrays where possible
(doc, j) -> kMap.getOrDefault(doc.get(FIELD_INTERNAL_KEY), 0);
final BsonValue difference = diff(internalArrayKey, minuend, subtrahend, kMapGet);
return new BsonDiff(
replacementSize,
bsonSizeVisitor.eval(difference),
Stream.of(Pair.create(internalArrayKey, difference)),
Stream.empty()
);
}

private static BsonValue diff(final JsonPointer key,
final BsonArray minuend,
final BsonArray subtrahend,
Expand Down Expand Up @@ -279,8 +247,7 @@ public BsonValue toAggregatedBsonValue() {

@Override
public boolean addElementToGroup(final Element element) {
if (element instanceof Pointer) {
final var pointer = (Pointer) element;
if (element instanceof final Pointer pointer) {
if (end + 1 == pointer.index) {
end = pointer.index;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
package org.eclipse.ditto.thingsearch.service.persistence.write.mapping;

import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_INTERNAL;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -95,17 +93,7 @@ public static BsonDiff minus(final BsonDocument minuend,
* @return the difference.
*/
public static BsonDiff minusThingDocs(final BsonDocument minuend, final BsonDocument subtrahend) {
// compute the internal array diff especially to find similar elements by internal key
final var minuendInternal = minuend.getArray(FIELD_INTERNAL);
final var subtrahendInternal = subtrahend.getArray(FIELD_INTERNAL);
final var diffInternal = BsonArrayDiff.diffInternalArray(minuendInternal, subtrahendInternal);
// compute the rest of the diff without the internal array
final var minuendWithoutInternal = minuend.clone();
final var subtrahendWithoutInternal = subtrahend.clone();
minuendWithoutInternal.remove(FIELD_INTERNAL);
subtrahendWithoutInternal.remove(FIELD_INTERNAL);
final var diffWithoutInternal = minus(minuendWithoutInternal, subtrahendWithoutInternal, true);
return diffWithoutInternal.concat(diffInternal);
return minus(minuend, subtrahend, true);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
/**
* Tests incremental update.
*/
public class BsonDiffVisitorIT {
public final class BsonDiffVisitorIT {

@ClassRule
public static final MongoDbResource MONGO_RESOURCE = new MongoDbResource();
Expand Down Expand Up @@ -103,7 +103,6 @@ public void shutdown() {
public void testAggregationUpdate() {
final var collection = client.getCollection("test");

final int maxArraySize = 99;
final Metadata metadata =
Metadata.of(ThingId.of("solar.system:pluto"), 23L, PolicyId.of("solar.system:pluto"), 45L, null, null);

Expand Down Expand Up @@ -138,7 +137,6 @@ public void testAggregationUpdate() {
public void testEnforcerChange() {
final var collection = client.getCollection("test");

final int maxArraySize = 99;
final Metadata metadata =
Metadata.of(ThingId.of("solar.system:pluto"), 23L, PolicyId.of("solar.system:pluto"), 45L, null, null);

Expand Down Expand Up @@ -173,7 +171,6 @@ public void testEnforcerChange() {
public void testEnforcerAndThingChange() {
final var collection = client.getCollection("test");

final int maxArraySize = 99;
final Metadata metadata =
Metadata.of(ThingId.of("solar.system:pluto"), 23L, PolicyId.of("solar.system:pluto"), 45L, null, null);

Expand Down Expand Up @@ -208,7 +205,6 @@ public void testEnforcerAndThingChange() {
public void testArrayConcat() {
final var collection = client.getCollection("test");

final int maxArraySize = 99;
final Metadata metadata =
Metadata.of(ThingId.of("solar.system:pluto"), 23L, PolicyId.of("solar.system:pluto"), 45L, null, null);

Expand Down Expand Up @@ -245,7 +241,6 @@ public void testArrayConcat() {
public void testSetEmptyObject() {
final var collection = client.getCollection("test");

final int maxArraySize = 99;
final Metadata metadata =
Metadata.of(ThingId.of("solar.system:pluto"), 23L, PolicyId.of("solar.system:pluto"), 45L, null, null);

Expand Down Expand Up @@ -282,7 +277,6 @@ public void testSetEmptyObject() {
public void testStringExpressionInUpdate() {
final var collection = client.getCollection("test");

final int maxArraySize = 99;
final Metadata metadata =
Metadata.of(ThingId.of("solar.system:pluto"), 23L, PolicyId.of("solar.system:pluto"), 45L, null, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public void computeThingCacheValueFromThingEvents() {
assertThat(document.getValue("policyId")).contains(JsonValue.of(policyId));
assertThat(document.getValue("_revision")).contains(JsonValue.of(6));
assertThat(document.getValue("__policyRev")).contains(JsonValue.of(1));
assertThat(document.getValue("s/attributes")).contains(JsonObject.of("{\"x\":5,\"y\":6,\"z\":7}"));
assertThat(document.getValue("t/attributes")).contains(JsonObject.of("{\"x\":5,\"y\":6,\"z\":7}"));

// THEN: thing is computed in the cache
thingsProbe.expectNoMessage(FiniteDuration.Zero());
Expand Down Expand Up @@ -520,7 +520,7 @@ public void noInitialCreatedOrDeletedEvent() {
assertThat(document.getValue("policyId")).contains(JsonValue.of(policyId));
assertThat(document.getValue("_revision")).contains(JsonValue.of(6));
assertThat(document.getValue("__policyRev")).contains(JsonValue.of(1));
assertThat(document.getValue("s/attributes")).contains(JsonObject.of("{\"x\":5,\"y\":6,\"z\":7}"));
assertThat(document.getValue("t/attributes")).contains(JsonObject.of("{\"x\":5,\"y\":6,\"z\":7}"));

// THEN: thing is computed in the cache
thingsProbe.expectNoMessage(FiniteDuration.Zero());
Expand Down Expand Up @@ -587,7 +587,7 @@ public void onlyApplyRelevantEvents() {
assertThat(document.getValue("policyId")).contains(JsonValue.of(policyId));
assertThat(document.getValue("_revision")).contains(JsonValue.of(6));
assertThat(document.getValue("__policyRev")).contains(JsonValue.of(1));
assertThat(document.getValue("s/attributes")).contains(JsonObject.of("{\"x\":5,\"y\":6,\"z\":7}"));
assertThat(document.getValue("t/attributes")).contains(JsonObject.of("{\"x\":5,\"y\":6,\"z\":7}"));

// THEN: thing is computed in the cache
thingsProbe.expectNoMessage(FiniteDuration.Zero());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,21 +361,17 @@ public void forceUpdateAfterInitialStart() throws InterruptedException {
recoveryCompleteConsumer));
final var underTest = childActorOf(props, THING_ID.toString());

final long request = probe.expectRequest();
probe.expectRequest();
final var existingIndexDocument = codec.decode(new BsonDocumentReader(existingIndexBsonDocument),
decoderContext);
probe.sendNext(existingIndexDocument);

// wait until Actor was recovered:
assertThat(recoveryCompleteLatch.await(5L, TimeUnit.SECONDS)).isTrue();

final var document = new BsonDocument()
.append("_revision", new BsonInt64(1235))
.append("d", new BsonArray())
.append("s", new BsonDocument().append("Lorem ipsum", new BsonString(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, " +
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
)));
final var document = new BsonDocument();
document.putAll(existingIndexBsonDocument);
document.put(PersistenceConstants.FIELD_REVISION, new BsonInt64(1235L));
final var writeModel = ThingWriteModel.of(Metadata.of(THING_ID, 1235L, policyId, 1L, null), document);

// WHEN: updater is requested to compute incremental update against the next update
Expand Down

0 comments on commit 57695e4

Please sign in to comment.