Skip to content

Commit

Permalink
Add fieldSelector and option to RetrievePolicy
Browse files Browse the repository at this point in the history
Signed-off-by: David Schwilk <david.schwilk@bosch.io>
  • Loading branch information
DerSchwilk committed Apr 6, 2022
1 parent 4ca55ac commit a0be370
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 14 deletions.
Expand Up @@ -222,7 +222,7 @@ public RetrieveThing retrieveThing(final ThingId thingId,
final Option<?>... options) {

return RetrieveThing.getBuilder(thingId,
buildDittoHeaders(EnumSet.of(CONDITION, LIVE_CHANNEL_CONDITION), options))
buildDittoHeaders(EnumSet.of(CONDITION, LIVE_CHANNEL_CONDITION), options))
.withSelectedFields(JsonFactory.newFieldSelector(fields))
.build();
}
Expand Down Expand Up @@ -298,16 +298,17 @@ public ModifyPolicy updatePolicy(final Policy policy, final Option<?>... options
return ModifyPolicy.of(policyId, policy, headers);
}

/**
* Builds a command to retrieve the policy with ID {@code policyId}.
*
* @param policyId the policy to retrieve.
* @return the {@link RetrievePolicy} command.
* @throws NullPointerException if the policyId is {@code null}.
* @since 1.1.0
*/
public RetrievePolicy retrievePolicy(final PolicyId policyId) {
return RetrievePolicy.of(policyId, buildDittoHeaders(Collections.emptySet()));
public RetrievePolicy retrievePolicy(final PolicyId policyId, final Option<?>... options) {
return RetrievePolicy.of(policyId, buildDittoHeaders(Collections.emptySet(), options));
}

public RetrievePolicy retrievePolicy(final PolicyId policyId,
final Iterable<JsonPointer> fields,
final Option<?>... options) {

return RetrievePolicy.of(policyId,
buildDittoHeaders(Collections.emptySet(), options),
JsonFactory.newFieldSelector(fields));
}

/**
Expand Down
Expand Up @@ -16,9 +16,11 @@
import java.util.concurrent.CompletionStage;

import org.eclipse.ditto.client.options.Option;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.things.model.Thing;

/**
* A {@code Policy} provides the basic functionality, which can be used to manage (i.e. create and delete)
Expand Down Expand Up @@ -153,12 +155,54 @@ public interface Policies {
CompletionStage<Void> delete(PolicyId policyId, Option<?>... options);

/**
* Gets the {@link org.eclipse.ditto.policies.model.Policy} specified by the given identifier.
* Gets the {@code Policy} specified by the given identifier.
*
* @param policyId the identifier of the Policy to be retrieved.
* @return CompletionStage providing the requested Policy or a specific
* {@link org.eclipse.ditto.base.model.exceptions.DittoRuntimeException} if the operation failed
* @throws IllegalArgumentException if {@code policyId} is {@code null}.
*/
CompletionStage<Policy> retrieve(PolicyId policyId);


/**
* Gets the {@code Policy} specified by the given identifier with the given options.
*
* @param options options that determine the behaviour of this method, see
* {@link org.eclipse.ditto.client.options.Options}.
* @return CompletionStage providing the requested {@link Thing} or a specific
* {@link org.eclipse.ditto.base.model.exceptions.DittoRuntimeException} if the operation failed.
* @throws NullPointerException if {@code options} is {@code null}.
* @throws IllegalArgumentException if {@code options} contains an option that is not allowed for retrieving
* a thing.
* @since 2.4.0
*/
CompletionStage<Policy> retrieve(PolicyId policyId, Option<?>... options);

/**
* Retrieve the {@code Policy} specified by the given identifier, containing the fields specified by
* the given {@code fieldSelector}.
*
* @param fieldSelector a field selector object allowing to select a subset of fields on the Policy to be retrieved.
* @return CompletionStage providing the requested {@link Policy} or a specific
* {@link org.eclipse.ditto.base.model.exceptions.DittoRuntimeException} if the operation failed
* @since 2.4.0
*/
CompletionStage<Policy> retrieve(PolicyId policyId, JsonFieldSelector fieldSelector);

/**
* Gets the {@code Policy} specified by the given identifier with the given options, containing the fields
* specified by the given {@code fieldSelector}.
*
* @param fieldSelector a field selector object allowing to select a subset of fields on the Policy to be retrieved.
* @param options options that determine the behaviour of this method, see
* {@link org.eclipse.ditto.client.options.Options}.
* @return CompletionStage providing the requested {@link Policy} or a specific
* {@link org.eclipse.ditto.base.model.exceptions.DittoRuntimeException} if the operation failed.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code options} contains an option that is not allowed for retrieving
* a policy.
* @since 2.4.0
*/
CompletionStage<Policy> retrieve(PolicyId policyId, JsonFieldSelector fieldSelector, Option<?>... options);
}
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.ditto.client.messaging.MessagingProvider;
import org.eclipse.ditto.client.options.Option;
import org.eclipse.ditto.client.policies.Policies;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.policies.model.PoliciesModelFactory;
Expand Down Expand Up @@ -176,14 +177,34 @@ public CompletionStage<Void> delete(final PolicyId policyId, final Option<?>...
}

@Override
public CompletionStage<Policy> retrieve(PolicyId policyId) {
public CompletionStage<Policy> retrieve(final PolicyId policyId) {
final RetrievePolicy command = outgoingMessageFactory.retrievePolicy(policyId);
return askPolicyCommand(command, RetrievePolicyResponse.class, response -> {
final Policy policyFromResponse = response.getPolicy();
return appendRevisionFromHeadersIfNeeded(policyFromResponse, response.getDittoHeaders());
});
}

@Override
public CompletionStage<Policy> retrieve(final PolicyId policyId, final Option<?>... options) {
final RetrievePolicy command = outgoingMessageFactory.retrievePolicy(policyId, options);
return askPolicyCommand(command, RetrievePolicyResponse.class, RetrievePolicyResponse::getPolicy);
}

@Override
public CompletionStage<Policy> retrieve(final PolicyId policyId, final JsonFieldSelector fieldSelector) {
final RetrievePolicy command = outgoingMessageFactory.retrievePolicy(policyId, fieldSelector);
return askPolicyCommand(command, RetrievePolicyResponse.class, RetrievePolicyResponse::getPolicy);
}

@Override
public CompletionStage<Policy> retrieve(final PolicyId policyId, final JsonFieldSelector fieldSelector,
final Option<?>... options) {

final RetrievePolicy command = outgoingMessageFactory.retrievePolicy(policyId, fieldSelector, options);
return askPolicyCommand(command, RetrievePolicyResponse.class, RetrievePolicyResponse::getPolicy);
}

private static void assertThatPolicyHasId(final Policy policy) {
if (!policy.getEntityId().isPresent()) {
final String msgPattern = "Mandatory field <{0}> is missing!";
Expand Down
Expand Up @@ -16,6 +16,8 @@
import static org.eclipse.ditto.client.TestConstants.Policy.POLICY;
import static org.eclipse.ditto.client.TestConstants.Policy.POLICY_ID;
import static org.eclipse.ditto.client.TestConstants.Policy.POLICY_JSON_OBJECT;
import static org.eclipse.ditto.client.TestConstants.Policy.POLICY_REVISION_ONLY_JSON_OBJECT;
import static org.eclipse.ditto.client.TestConstants.Policy.REVISION_ONLY_POLICY;
import static org.eclipse.ditto.client.assertions.ClientAssertions.assertThat;

import java.util.Arrays;
Expand All @@ -27,10 +29,11 @@
import java.util.function.Function;

import org.assertj.core.api.Assertions;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.client.internal.AbstractDittoClientTest;
import org.eclipse.ditto.client.options.Options;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonMissingFieldException;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.policies.model.signals.commands.PolicyCommand;
import org.eclipse.ditto.policies.model.signals.commands.PolicyCommandResponse;
Expand Down Expand Up @@ -181,6 +184,54 @@ public void testRetrievePolicy() throws Exception {
Assertions.assertThat(retrievePolicyResponse).isCompletedWithValue(POLICY);
}

@Test
public void testRetrievePolicyWithOptions() {
final String correlationId = "abc";
client.policies()
.retrieve(POLICY_ID, Options.headers(DittoHeaders.newBuilder().correlationId(correlationId).build()))
.toCompletableFuture();
final RetrievePolicy command = expectMsgClass(RetrievePolicy.class);
Assertions.assertThat(command.getDittoHeaders().getCorrelationId()).contains(correlationId);
}

@Test
public void testRetrievePolicyWithInvalidOptions() {
Assertions.assertThatIllegalArgumentException().isThrownBy(() -> client.policies()
.retrieve(POLICY_ID, Options.condition("exists(entry)")));
}

@Test
public void testRetrievePolicyWithFieldSelector() throws Exception {
final CompletableFuture<Policy> retrievePolicyResponse = client.policies()
.retrieve(POLICY_ID, JsonFieldSelector.newInstance("_revision"))
.toCompletableFuture();
reply(RetrievePolicyResponse.of(POLICY_ID, POLICY_REVISION_ONLY_JSON_OBJECT,
expectMsgClass(RetrievePolicy.class).getDittoHeaders()));
retrievePolicyResponse.get(TIMEOUT, TIME_UNIT);
Assertions.assertThat(retrievePolicyResponse).isCompletedWithValue(REVISION_ONLY_POLICY);
}

@Test
public void testRetrievePolicyWithFieldSelectorAndOptions() throws Exception {
final String correlationId = "abc";
final CompletableFuture<Policy> retrievePolicyResponse = client.policies()
.retrieve(POLICY_ID, JsonFieldSelector.newInstance("_revision"),
Options.headers(DittoHeaders.newBuilder().correlationId(correlationId).build()))
.toCompletableFuture();
final RetrievePolicy command = expectMsgClass(RetrievePolicy.class);
Assertions.assertThat(command.getDittoHeaders().getCorrelationId()).contains(correlationId);
reply(RetrievePolicyResponse.of(POLICY_ID, POLICY_REVISION_ONLY_JSON_OBJECT,
command.getDittoHeaders()));
retrievePolicyResponse.get(TIMEOUT, TIME_UNIT);
Assertions.assertThat(retrievePolicyResponse).isCompletedWithValue(REVISION_ONLY_POLICY);
}

@Test
public void testRetrievePolicyWithFieldSelectorAndInvalidOptions() {
Assertions.assertThatIllegalArgumentException().isThrownBy(() -> client.policies()
.retrieve(POLICY_ID, JsonFieldSelector.newInstance("_revision"), Options.condition("exists(entry)")));
}

@Test
public void testRetrievePolicyFails() {
assertEventualCompletion(client.policies().retrieve(POLICY_ID).handle((response, error) -> {
Expand Down
Expand Up @@ -581,6 +581,15 @@ public void retrieveThingWithConditionOption() {
assertThat(retrieveThing.getDittoHeaders()).containsEntry(DittoHeaderDefinition.CONDITION.getKey(), CONDITION);
}

@Test
public void retrieveThingWithExtraFieldsOption() {
assertEventualCompletion(getManagement().forId(THING_ID)
.retrieve(Options.Consumption.extraFields(JsonFieldSelector.newInstance("_revision"))));
final RetrieveThing retrieveThing = expectMsgClass(RetrieveThing.class);
reply(RetrieveThingResponse.of(THING_ID, TestConstants.Thing.THING_V2.toJson(), retrieveThing.getDittoHeaders()));
assertThat(retrieveThing.getSelectedFields()).contains(JsonFieldSelector.newInstance("_revision"));
}

@Test
public void retrieveThingWithFieldSelectorAndConditionOption() {
final JsonFieldSelector jsonFieldSelector = JsonFieldSelector.newInstance("attributes/manufacturer");
Expand Down
Expand Up @@ -217,6 +217,12 @@ public static final class Policy {
public static final org.eclipse.ditto.policies.model.Policy POLICY =
PoliciesModelFactory.newPolicy(POLICY_JSON_OBJECT);

public static final JsonObject POLICY_REVISION_ONLY_JSON_OBJECT = JsonObject.of("{\n" +
" \"_revision\": " + 1 + "\n" + "}");

public static final org.eclipse.ditto.policies.model.Policy REVISION_ONLY_POLICY =
PoliciesModelFactory.newPolicy(POLICY_REVISION_ONLY_JSON_OBJECT);

}

}

0 comments on commit a0be370

Please sign in to comment.