Skip to content

Commit

Permalink
eclipse-ditto#2072: add unit test for caching signal enrichment with …
Browse files Browse the repository at this point in the history
…pre-defined extra fields
  • Loading branch information
thjaeckle committed Jan 14, 2025
1 parent 0412a6b commit c5cb75a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,11 @@ private CompletionStage<JsonObject> doSmartUpdateCachedObject(final SignalEnrich
}

private static <T> T getLast(final List<T> list) {
return list.get(list.size() - 1);
return list.getLast();
}

private static <T> T getFirst(final List<T> list) {
return list.get(0);
return list.getFirst();
}

private CompletionStage<JsonObject> handleNextExpectedThingEvents(final SignalEnrichmentCacheKey cacheKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
*/
abstract class AbstractCachingSignalEnrichmentFacadeTest extends AbstractSignalEnrichmentFacadeTest {

private static final String ISSUER_PREFIX = "test:";
protected static final String ISSUER_PREFIX = "test:";
private static final String CACHE_CONFIG_KEY = "my-cache";
private static final String CACHE_CONFIG = CACHE_CONFIG_KEY + """
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,26 @@
*/
package org.eclipse.ditto.internal.models.signalenrichment;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.CompletionStage;

import org.apache.pekko.testkit.javadsl.TestKit;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.auth.AuthorizationSubject;
import org.eclipse.ditto.base.model.auth.DittoAuthorizationContextType;
import org.eclipse.ditto.base.model.entity.metadata.MetadataModelFactory;
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.signals.DittoTestSystem;
import org.eclipse.ditto.internal.utils.cache.config.CacheConfig;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.events.AttributeModified;
import org.junit.Test;

/**
* Unit tests for {@link DittoCachingSignalEnrichmentFacade}.
Expand All @@ -27,7 +44,37 @@ public final class DittoCachingSignalEnrichmentFacadeTest extends AbstractCachin
"attributes": {"x": 5},
"features": {"y": {"properties": {"z": true}}},
"_metadata": {"attributes": {"x": {"type": "x attribute"}}}
}""");
}"""
);

private static final JsonObject EXPECTED_THING_JSON_PRE_DEFINED_EXTRA = JsonObject.of("""
{
"definition": "some:cool:definition",
"attributes": {"x": 5, "pre": {"bar": [1,2,3]}, "pre2": {"some": 41, "secret": true}}
}"""
);

private static final AttributeModified THING_EVENT_PRE_DEFINED_EXTRA_FIELDS = AttributeModified.of(
ThingId.generateRandom("org.eclipse.test"),
JsonPointer.of("x"),
JsonValue.of(42),
4L,
Instant.EPOCH,
DittoHeaders.newBuilder()
.putHeader(DittoHeaderDefinition.PRE_DEFINED_EXTRA_FIELDS.getKey(),
"[\"/definition\",\"/attributes/pre\",\"/attributes/pre2\"]")
.putHeader(DittoHeaderDefinition.PRE_DEFINED_EXTRA_FIELDS_READ_GRANT_OBJECT.getKey(),
"{\"/definition\":[\"test:user\"],\"/attributes/pre\":[\"test:user\"]}")
.putHeader(DittoHeaderDefinition.PRE_DEFINED_EXTRA_FIELDS_OBJECT.getKey(),
"{\"definition\":\"some:cool:definition\",\"attributes\":{\"pre\":{\"bar\": [1,2,3]}}}")
.build(),
MetadataModelFactory.newMetadataBuilder()
.set("type", "x attribute")
.build());

private static final JsonFieldSelector SELECTOR_PRE_DEFINED_EXTRA_FIELDS =
JsonFieldSelector.newInstance("definition", "attributes/pre", "attributes/pre2");


@Override
protected CachingSignalEnrichmentFacade createCachingSignalEnrichmentFacade(final TestKit kit,
Expand All @@ -44,5 +91,32 @@ protected JsonObject getExpectedThingJson() {
return EXPECTED_THING_JSON;
}

@Test
public void enrichedEventWithPreDefinedExtraFieldsDoesNotLeadToCacheLookup() {
DittoTestSystem.run(this, kit -> {
final SignalEnrichmentFacade underTest =
createSignalEnrichmentFacadeUnderTest(kit, Duration.ofSeconds(10L));
final ThingId thingId = ThingId.generateRandom();
final String userId = ISSUER_PREFIX + "user";
final DittoHeaders headers = DittoHeaders.newBuilder()
.authorizationContext(AuthorizationContext.newInstance(DittoAuthorizationContextType.UNSPECIFIED,
AuthorizationSubject.newInstance(userId)))
.randomCorrelationId()
.build();
final CompletionStage<JsonObject> askResult =
underTest.retrievePartialThing(thingId, SELECTOR_PRE_DEFINED_EXTRA_FIELDS, headers,
THING_EVENT_PRE_DEFINED_EXTRA_FIELDS);

// THEN: no cache lookup should be done
kit.expectNoMessage(Duration.ofSeconds(1));
askResult.toCompletableFuture().join();
// AND: the resulting thing JSON includes the with the updated value:
final JsonObject expectedThingJson = EXPECTED_THING_JSON_PRE_DEFINED_EXTRA.toBuilder()
.remove("attributes/x") // x was not asked for in extra fields
.remove("attributes/pre2") // we don't have the read grant for this field
.build();
softly.assertThat(askResult).isCompletedWithValue(expectedThingJson);
});
}

}

0 comments on commit c5cb75a

Please sign in to comment.