Skip to content

Commit

Permalink
fix more unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Schneider <johannes.schneider@bosch.io>
  • Loading branch information
jokraehe committed Jun 21, 2022
1 parent 40a4c44 commit 1c4e43c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ public JsonPointer getPath() {
return result;
}

@Override
public JsonPointer getOriginalPath() {
return path;
}

@Override
public int compareTo(final MetadataHeaderKey other) {
checkNotNull(other, "other");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ static MetadataHeaderKey of(final JsonPointer path) {
*/
JsonPointer getPath();

/**
* Returns the original path of this key.
*
* @return the path.
*/
JsonPointer getOriginalPath();

/**
* Returns this key as string as it would appear in DittoHeaders.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package org.eclipse.ditto.things.service.persistence.actors.strategies.commands;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;

Expand Down Expand Up @@ -250,16 +250,16 @@ private boolean containsExactlySingleWildcard(final Set<JsonPointer> jsonPointer
private Set<JsonPointer> expandWildcardsInMetadataExpression(final Set<JsonPointer> metadataPointerWithWildcard,
final Thing entity, final C command, final String headerKey) {

final Set<JsonPointer> resolvedMetadataPointers = new HashSet<>();
final Set<JsonPointer> resolvedMetadataPointers = new LinkedHashSet<>();

metadataPointerWithWildcard.stream()
.filter(jsonPointer -> !checkIfContainsWildcard(jsonPointer))
.forEach(resolvedMetadataPointers::add);

metadataPointerWithWildcard.stream()
.filter(this::checkIfContainsWildcard)
.map(jsonPointer -> MetadataFieldsWildcardResolver.resolve(command, entity, jsonPointer, headerKey))
.forEach(resolvedMetadataPointers::addAll);
metadataPointerWithWildcard.forEach(jsonPointer -> {
if (checkIfContainsWildcard(jsonPointer)) {
final var resolved = MetadataFieldsWildcardResolver.resolve(command, entity, jsonPointer, headerKey);
resolvedMetadataPointers.addAll(resolved);
} else {
resolvedMetadataPointers.add(jsonPointer);
}
});

return resolvedMetadataPointers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -396,10 +397,14 @@ private static Set<JsonPointer> getReplacedWildcardPointersForPropertyKeysOnProp
private static Set<JsonPointer> getReplacedWildcardsPointersForLeafs(final List<String> featureIds,
final Thing thing,
final JsonKey metadataKey) {
final Set<JsonPointer> replacedWildcardsPointers = new HashSet<>();
final Set<JsonPointer> replacedWildcardsPointers = new LinkedHashSet<>();
replacedWildcardsPointers.add(JsonPointer.of("thingId/" + metadataKey));
replacedWildcardsPointers.add(JsonPointer.of("policyId/" + metadataKey));

replacedWildcardsPointers.addAll(
getReplacedWildcardPointersForAttributeIds(getAttributesLeafsFromThing(thing),
metadataKey.asPointer()));

featureIds.forEach(featureId -> {
replacedWildcardsPointers.addAll(
getReplacedWildcardPointersForPropertyKeysOnFeaturesLevel(
Expand All @@ -413,10 +418,6 @@ private static Set<JsonPointer> getReplacedWildcardsPointersForLeafs(final List<
}
);

replacedWildcardsPointers.addAll(
getReplacedWildcardPointersForAttributeIds(getAttributesLeafsFromThing(thing),
metadataKey.asPointer()));

return replacedWildcardsPointers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -115,7 +116,7 @@ static MetadataFromCommand of(final Command<?> command,
public Metadata get() {
return getInlineMetadata().orElseGet(() -> {
if (null != commandEntity) {
final Set<MetadataHeader> metadataHeaders = getMetadataHeadersToPut();
final SortedSet<MetadataHeader> metadataHeaders = getMetadataHeadersToPut();
if (!metadataHeaders.isEmpty()) {
final var expandedMetadataHeaders = metadataHeaders.stream()
.flatMap(this::expandWildcards)
Expand All @@ -131,7 +132,7 @@ private Optional<Metadata> getInlineMetadata() {
return command instanceof CreateThing createThing ? createThing.getThing().getMetadata() : Optional.empty();
}

private Set<MetadataHeader> getMetadataHeadersToPut() {
private SortedSet<MetadataHeader> getMetadataHeadersToPut() {
final DittoHeaders dittoHeaders = command.getDittoHeaders();
return dittoHeaders.getMetadataHeadersToPut();
}
Expand All @@ -140,7 +141,7 @@ private Stream<MetadataHeader> expandWildcards(final MetadataHeader metadataHead
if (containsWildcard(metadataHeader)) {
final var thing = command instanceof CreateThing createThing ? createThing.getThing() : existingThing;
final var jsonPointers = MetadataFieldsWildcardResolver.resolve(command, thing,
metadataHeader.getKey().getPath(), DittoHeaderDefinition.PUT_METADATA.getKey());
metadataHeader.getKey().getOriginalPath(), DittoHeaderDefinition.PUT_METADATA.getKey());
return jsonPointers.stream()
.map(MetadataHeaderKey::of)
.map(metadataHeaderKey -> MetadataHeader.of(metadataHeaderKey, metadataHeader.getValue()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1552,7 +1552,7 @@ public void retrieveFeaturePropertyAndAttributeMetadataWithGetMetadataWildcardHe
final Thing thing = createThingV2WithRandomIdAndMetadata();
final DittoHeaders dittoHeaders = dittoHeadersV2.toBuilder()
.putHeader(DittoHeaderDefinition.GET_METADATA.getKey(),
"features/*/properties/*/unit,attributes/*/edited")
"attributes/*/edited,features/*/properties/*/unit")
.build();
final ThingCommand<?> retrieveThingCommand = RetrieveThing.of(getIdOrThrow(thing), dittoHeaders);

Expand Down Expand Up @@ -1626,6 +1626,9 @@ public void retrieveLeafMetadataWithGetMetadataWildcardHeader() {
.setRevision(2L)
.build();
final Metadata expectedMetadata = Metadata.newBuilder()
.set("thingId", JsonObject.newBuilder()
.set("issuedBy", "the epic Ditto team")
.build())
.set("policyId", JsonObject.newBuilder()
.set("issuedBy", "the epic Ditto team")
.build())
Expand All @@ -1634,9 +1637,6 @@ public void retrieveLeafMetadataWithGetMetadataWildcardHeader() {
.set("issuedBy", "the epic Ditto team")
.build())
.build())
.set("thingId", JsonObject.newBuilder()
.set("issuedBy", "the epic Ditto team")
.build())
.set("features", JsonObject.newBuilder()
.set(FEATURE_ID, JsonObject.newBuilder()
.set("properties", JsonObject.newBuilder()
Expand Down

0 comments on commit 1c4e43c

Please sign in to comment.