Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed filtering desired properties signals. #1599 #1602

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@
import org.eclipse.ditto.protocol.TopicPath;
import org.eclipse.ditto.protocol.adapter.DittoProtocolAdapter;
import org.eclipse.ditto.things.model.Attributes;
import org.eclipse.ditto.things.model.FeatureProperties;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.modify.ModifyThing;
import org.eclipse.ditto.things.model.signals.events.FeatureDesiredPropertiesModified;
import org.eclipse.ditto.things.model.signals.events.ThingModified;
import org.eclipse.ditto.things.model.signals.events.ThingModifiedEvent;
import org.mockito.Mockito;
Expand Down Expand Up @@ -326,6 +328,14 @@ public static final class Things {

}

public static final class Feature {

public static final String FEATURE_ID = "Feature";
public static final FeatureProperties FEATURE_DESIRED_PROPERTIES = FeatureProperties.newBuilder()
.set("property", "test").build();

}

public static final class Authorization {

public static final String SUBJECT_ID = "some:subject";
Expand Down Expand Up @@ -986,6 +996,12 @@ public static ThingModifiedEvent<?> thingModified(final Collection<Authorization
TestConstants.INSTANT, dittoHeaders, TestConstants.METADATA);
}

public static ThingModifiedEvent<?> featureDesiredPropertiesModified(Collection<AuthorizationSubject> readSubjects) {
DittoHeaders dittoHeaders = DittoHeaders.newBuilder().readGrantedSubjects(readSubjects).build();
return FeatureDesiredPropertiesModified.of(Things.THING_ID, Feature.FEATURE_ID,
Feature.FEATURE_DESIRED_PROPERTIES, 1, null, dittoHeaders, null);
}

public static MessageCommand<?, ?> sendThingMessage(final Collection<AuthorizationSubject> readSubjects) {
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.readGrantedSubjects(readSubjects)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.Collections;
import java.util.List;

import org.assertj.core.api.Assertions;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.connectivity.service.messaging.monitoring.ConnectionMonitorRegistry;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
Expand Down Expand Up @@ -253,4 +255,21 @@ public void applySignalFilterWithNamespacesAndRqlFilter() {
targetD); // THEN: only targetA and targetD should be in the filtered targets
}

/**
* Test that target filtering works also for desired properties events. Issue #1599
*/
@Test
public void applySignalFilterOnFeatureDesiredPropertiesModified() {
Target target = ConnectivityModelFactory.newTargetBuilder().address("address")
.authorizationContext(newAuthContext(DittoAuthorizationContextType.UNSPECIFIED, AUTHORIZED))
.topics(ConnectivityModelFactory.newFilteredTopicBuilder(TWIN_EVENTS)
.withFilter("like(resource:path,'/features/" + TestConstants.Feature.FEATURE_ID + "*')")
.build()).build();
Connection connection = TestConstants.createConnection(CONNECTION_ID, target);
SignalFilter signalFilter = new SignalFilter(connection, connectionMonitorRegistry);
Signal<?> signal = TestConstants.featureDesiredPropertiesModified(Collections.singletonList(AUTHORIZED));

List<Target> filteredTargets = signalFilter.filter(signal);
Assertions.assertThat(filteredTargets).hasSize(1).contains(target);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,25 @@ private static Map<Class<?>, BiFunction<ThingEvent<?>, ThingBuilder.FromScratch,
((FeaturePropertyModified) te).getPropertyValue()).build());
mappers.put(FeaturePropertyDeleted.class, (te, tb) -> tb.build());

mappers.put(FeatureDesiredPropertiesCreated.class, (te, tb) -> tb.setFeature(Feature.newBuilder()
.desiredProperties(((FeatureDesiredPropertiesCreated) te).getDesiredProperties())
.withId(((FeatureDesiredPropertiesCreated) te).getFeatureId())
.build()).build());
mappers.put(FeatureDesiredPropertiesModified.class, (te, tb) -> tb.setFeature(Feature.newBuilder()
.desiredProperties(((FeatureDesiredPropertiesModified) te).getDesiredProperties())
.withId(((FeatureDesiredPropertiesModified) te).getFeatureId())
.build()).build());
mappers.put(FeatureDesiredPropertiesDeleted.class, (te, tb) -> tb.build());
mappers.put(FeatureDesiredPropertyCreated.class, (te, tb) ->
tb.setFeatureDesiredProperty(((FeatureDesiredPropertyCreated) te).getFeatureId(),
((FeatureDesiredPropertyCreated) te).getDesiredPropertyPointer(),
((FeatureDesiredPropertyCreated) te).getDesiredPropertyValue()).build());
mappers.put(FeatureDesiredPropertyModified.class, (te, tb) ->
tb.setFeatureDesiredProperty(((FeatureDesiredPropertyModified) te).getFeatureId(),
((FeatureDesiredPropertyModified) te).getDesiredPropertyPointer(),
((FeatureDesiredPropertyModified) te).getDesiredPropertyValue()).build());
mappers.put(FeatureDesiredPropertyDeleted.class, (te, tb) -> tb.build());

return mappers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.things.model.Attributes;
import org.eclipse.ditto.things.model.Feature;
import org.eclipse.ditto.things.model.FeatureProperties;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingRevision;
import org.eclipse.ditto.things.model.*;
import org.junit.Test;

/**
Expand All @@ -39,6 +35,8 @@ public final class ThingEventToThingConverterTest {
private static final String ATTR_KEY_CITY = "city";
private static final String KNOWN_CITY = "Immenstaad am Bodensee";

private static final JsonPointer DESIRED_PROPERTY_POINTER = JsonPointer.of("target_year_1");

@Test
public void ensureMergeWithExtraFieldsMergesCorrectly() {
final long revision = 23L;
Expand Down Expand Up @@ -124,4 +122,104 @@ public void ensureThingMergedConvertsAsExpected() {
assertThat(thing.getRevision()).contains(ThingRevision.newInstance(revision));
assertThat(thing.getAttributes()).contains(Attributes.newBuilder().set("foo", "bar").build());
}

@Test
public void testThingEventToThingConvertsFeatureDesiredPropertiesCreated() {
long revision = 23L;
FeatureDesiredPropertiesCreated desiredPropertiesCreated = FeatureDesiredPropertiesCreated.of(
TestConstants.Thing.THING_ID, TestConstants.Feature.FLUX_CAPACITOR_ID,
TestConstants.Feature.FLUX_CAPACITOR_PROPERTIES, revision,null, DittoHeaders.empty(), null);

Optional<Thing> thingOpt = ThingEventToThingConverter.thingEventToThing(desiredPropertiesCreated);
assertThat(thingOpt).isPresent();
Thing thing = thingOpt.get();
assertThat(thing.getEntityId()).contains(TestConstants.Thing.THING_ID);
assertThat(thing.getRevision()).contains(ThingRevision.newInstance(revision));
FeatureProperties desiredProperties = getDesiredProperties(thing);
assertThat(desiredProperties).isEqualTo(TestConstants.Feature.FLUX_CAPACITOR_PROPERTIES);
}

private FeatureProperties getDesiredProperties(Thing thing) {
Optional<Features> featuresOpt = thing.getFeatures();
assertThat(featuresOpt).isPresent();
Optional<Feature> featureOpt = featuresOpt.get().getFeature(TestConstants.Feature.FLUX_CAPACITOR_ID);
assertThat(featureOpt).isPresent();
Optional<FeatureProperties> desiredPropertiesOpt = featureOpt.get().getDesiredProperties();
assertThat(desiredPropertiesOpt).isPresent();
return desiredPropertiesOpt.get();
}

@Test
public void testThingEventToThingConvertsFeatureDesiredPropertiesModified() {
long revision = 23L;
FeatureDesiredPropertiesModified desiredPropertiesModified = FeatureDesiredPropertiesModified.of(
TestConstants.Thing.THING_ID, TestConstants.Feature.FLUX_CAPACITOR_ID,
TestConstants.Feature.FLUX_CAPACITOR_PROPERTIES, revision,null, DittoHeaders.empty(), null);

Optional<Thing> thingOpt = ThingEventToThingConverter.thingEventToThing(desiredPropertiesModified);
assertThat(thingOpt).isPresent();
Thing thing = thingOpt.get();
assertThat(thing.getEntityId()).contains(TestConstants.Thing.THING_ID);
assertThat(thing.getRevision()).contains(ThingRevision.newInstance(revision));
FeatureProperties desiredProperties = getDesiredProperties(thing);
assertThat(desiredProperties).isEqualTo(TestConstants.Feature.FLUX_CAPACITOR_PROPERTIES);
}

@Test
public void testThingEventToThingConvertsFeatureDesiredPropertiesDeleted() {
long revision = 23L;
FeatureDesiredPropertiesDeleted desiredPropertiesDeleted = FeatureDesiredPropertiesDeleted.of(
TestConstants.Thing.THING_ID, TestConstants.Feature.FLUX_CAPACITOR_ID, revision,null,
DittoHeaders.empty(), null);
Optional<Thing> thingOpt = ThingEventToThingConverter.thingEventToThing(desiredPropertiesDeleted);
assertThat(thingOpt).isPresent();
Thing thing = thingOpt.get();
assertThat(thing.getEntityId()).contains(TestConstants.Thing.THING_ID);
assertThat(thing.getRevision()).contains(ThingRevision.newInstance(revision));
}

@Test
public void testThingEventToThingConvertsFeatureDesiredPropertyCreated() {
long revision = 23L;
JsonValue value = JsonValue.of(1955);
FeatureDesiredPropertyCreated desiredPropertyCreated = FeatureDesiredPropertyCreated.of(
TestConstants.Thing.THING_ID, TestConstants.Feature.FLUX_CAPACITOR_ID, DESIRED_PROPERTY_POINTER, value,
revision, null, DittoHeaders.empty(), null);
Optional<Thing> thingOpt = ThingEventToThingConverter.thingEventToThing(desiredPropertyCreated);
assertThat(thingOpt).isPresent();
Thing thing = thingOpt.get();
assertThat(thing.getEntityId()).contains(TestConstants.Thing.THING_ID);
assertThat(thing.getRevision()).contains(ThingRevision.newInstance(revision));
FeatureProperties desiredProperties = getDesiredProperties(thing);
assertThat(desiredProperties).isEqualTo(FeatureProperties.newBuilder().set("target_year_1", 1955).build());
}

@Test
public void testThingEventToThingConvertsFeatureDesiredPropertyModified() {
long revision = 23L;
JsonValue value = JsonValue.of(1955);
FeatureDesiredPropertyModified desiredPropertyModified = FeatureDesiredPropertyModified.of(
TestConstants.Thing.THING_ID, TestConstants.Feature.FLUX_CAPACITOR_ID, DESIRED_PROPERTY_POINTER, value,
revision, null, DittoHeaders.empty(), null);
Optional<Thing> thingOpt = ThingEventToThingConverter.thingEventToThing(desiredPropertyModified);
assertThat(thingOpt).isPresent();
Thing thing = thingOpt.get();
assertThat(thing.getEntityId()).contains(TestConstants.Thing.THING_ID);
assertThat(thing.getRevision()).contains(ThingRevision.newInstance(revision));
FeatureProperties desiredProperties = getDesiredProperties(thing);
assertThat(desiredProperties).isEqualTo(FeatureProperties.newBuilder().set("target_year_1", 1955).build());
}

@Test
public void testThingEventToThingConvertsFeatureDesiredPropertyDeleted() {
long revision = 23L;
FeatureDesiredPropertyDeleted desiredPropertyDeleted = FeatureDesiredPropertyDeleted.of(
TestConstants.Thing.THING_ID, TestConstants.Feature.FLUX_CAPACITOR_ID, DESIRED_PROPERTY_POINTER, revision,
null, DittoHeaders.empty(), null);
Optional<Thing> thingOpt = ThingEventToThingConverter.thingEventToThing(desiredPropertyDeleted);
assertThat(thingOpt).isPresent();
Thing thing = thingOpt.get();
assertThat(thing.getEntityId()).contains(TestConstants.Thing.THING_ID);
assertThat(thing.getRevision()).contains(ThingRevision.newInstance(revision));
}
}