Skip to content

Commit

Permalink
Fix updater errors due to string values starting with '$'.
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 Sep 21, 2021
1 parent 1c18471 commit 2cb92b9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/
final class BsonDiffVisitor implements BsonValueVisitor<Function<BsonValue, BsonDiff>> {

private static final String DOLLAR = "$";
private static final String LITERAL = "$literal";

private final BsonSizeVisitor bsonSizeVisitor = new BsonSizeVisitor();
Expand All @@ -40,6 +41,9 @@ public Function<BsonValue, BsonDiff> primitive(final JsonPointer key, final Bson
return oldValue -> {
if (value.equals(oldValue)) {
return BsonDiff.empty(replacementSize);
} else if (value.isString() && value.asString().getValue().startsWith(DOLLAR)) {
final var literalValue = new BsonDocument().append(LITERAL, value);
return BsonDiff.set(replacementSize, key, literalValue);
} else {
return BsonDiff.set(replacementSize, key, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,41 @@ public void testEmptyUpdate() {
.isEqualTo(thingDoc);
}

@Test
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);

final JsonObject prevThing = getThing1();
final JsonObject nextThing = getThing5(); // Thing1 with string field updated to begin with '$' and end with '.'

final BsonDocument prevThingDoc =
EnforcedThingMapper.toBsonDocument(prevThing, enforcer, maxArraySize, metadata);

final BsonDocument nextThingDoc =
EnforcedThingMapper.toBsonDocument(nextThing, enforcer, maxArraySize, metadata);

final BsonDiff diff = BsonDiff.minusThingDocs(nextThingDoc, prevThingDoc);

final List<BsonDocument> updateDoc = diff.consumeAndExport();

assertThat(updateDoc.toString().length())
.describedAs("Incremental update should be less than 1/8 as large as replacement")
.isLessThan(nextThingDoc.toString().length() / 8);

run(collection.insertOne(toDocument(prevThingDoc)));
run(collection.updateOne(new Document(), updateDoc));

final BsonDocument incrementalUpdateResult = toBsonDocument(run(collection.find()).get(0));

assertThat(incrementalUpdateResult)
.describedAs("Incremental update result")
.isEqualTo(nextThingDoc);
}

private <T> List<T> run(final Publisher<T> publisher) {
return Source.fromPublisher(publisher).runWith(Sink.seq(), system).toCompletableFuture().join();
}
Expand Down Expand Up @@ -322,4 +357,21 @@ private static JsonObject getThing4() {
" }\n" +
"}");
}

private static JsonObject getThing5() {
return JsonFactory.newObject("{\n" +
" \"thingId\":\"solar.system:pluto\"," +
" \"_namespace\":\"solar.system\"," +
" \"a\": [ {\"b\": \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"}, true ],\n" +
" \"d\": {\n" +
" \"e\": {\n" +
" \"f\": \"g\",\n" +
" \"h\": \"$lorem ipsum dolor sit amet.\"\n" +
" },\n" +
" \"j\": true,\n" +
" \"k\": 6.0,\n" +
" \"l\": 123456789012\n" +
" }\n" +
"}");
}
}

0 comments on commit 2cb92b9

Please sign in to comment.