Skip to content

Commit

Permalink
filter for incorrect element types in jsonArray of feature definitions;
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Maute <stefan.maute@bosch.io>
  • Loading branch information
Stefan Maute committed Feb 25, 2022
1 parent 8abf9d5 commit ebd78dc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.eclipse.ditto.gateway.service.endpoints.routes.RouteBaseProperties;
import org.eclipse.ditto.gateway.service.util.config.endpoints.MessageConfig;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonKey;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.ThingsModelFactory;
import org.eclipse.ditto.things.model.signals.commands.modify.DeleteFeature;
Expand Down Expand Up @@ -59,11 +58,6 @@ final class FeaturesRoute extends AbstractRoute {
static final String PATH_DESIRED_PROPERTIES = "desiredProperties";
static final String PATH_DEFINITION = "definition";

static final JsonKey FEATURE_JSON_KEY = JsonKey.of(PATH_FEATURES);
static final JsonKey PROPERTIES_JSON_KEY = JsonKey.of(PATH_PROPERTIES);
static final JsonKey DESIRED_PROPERTIES_JSON_KEY = JsonKey.of(PATH_DESIRED_PROPERTIES);
static final JsonKey DEFINITION_JSON_KEY = JsonKey.of(PATH_DEFINITION);

private final MessagesRoute messagesRoute;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public static ImmutableFeatureDefinition fromJson(final JsonArray featureDefinit
throw new FeatureDefinitionEmptyException();
}

final List<JsonValue> notStringValues = featureDefinitionEntriesAsJsonArray.stream()
.filter(jsonValue -> !jsonValue.isString())
.collect(Collectors.toList());
if (!notStringValues.isEmpty()) {
throw DefinitionIdentifierInvalidException.newBuilder(notStringValues.get(0).formatAsString())
.description("The provided identifier is not a string.")
.build();
}

final List<DefinitionIdentifier> identifiersFromJson = featureDefinitionEntriesAsJsonArray.stream()
.filter(JsonValue::isString)
.map(JsonValue::asString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonFactory;
import org.junit.Ignore;
import org.junit.Test;

import nl.jqno.equalsverifier.EqualsVerifier;
Expand Down Expand Up @@ -183,4 +182,17 @@ public void fromJsonOfEmptyArrayFailsWithException() {
.withNoCause();
}

@Test
public void fromJsonWithIncorrectTypeFailsWithException() {
final JsonArray jsonArray = JsonFactory.newArrayBuilder()
.add(1.5)
.add(FIRST_IDENTIFIER.toString())
.build();

assertThatExceptionOfType(DefinitionIdentifierInvalidException.class)
.isThrownBy(() -> ImmutableFeatureDefinition.fromJson(jsonArray))
.withMessage("Definition identifier <1.5> is invalid!")
.withNoCause();
}

}

0 comments on commit ebd78dc

Please sign in to comment.