Skip to content

Commit

Permalink
Issue #559: Widened type of condition argument to CharSequence. Adjus…
Browse files Browse the repository at this point in the history
…ted Javadoc as well.

Signed-off-by: Juergen Fickel <juergen.fickel@bosch.io>
  • Loading branch information
Juergen Fickel committed Sep 14, 2021
1 parent 987afbf commit 0eae873
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 83 deletions.
32 changes: 19 additions & 13 deletions java/src/main/java/org/eclipse/ditto/client/options/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@

import java.util.Arrays;

import org.eclipse.ditto.base.model.common.ConditionChecker;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.client.management.CommonManagement;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.things.model.ThingId;

/**
* This utility class allows to create {@link Option}s with custom values the Ditto Client is aware of.
* This utility class allows creating {@link Option}s with custom values the Ditto Client is aware of.
*
* @since 1.0.0
*/
Expand All @@ -35,7 +36,7 @@ private Options() {
* Creates an option for specifying additional/custom DittoHeaders to send along together with any command/message
* accepting options.
* <p>
* DittoHeader passed in here will be overwritten by more specific {@link Options}, when specified.
* DittoHeader passed in here will be overwritten by more specific {@code Options}, when specified.
* </p>
*
* @param dittoHeaders the additional DittoHeaders to send along with an operation.
Expand All @@ -47,7 +48,8 @@ public static Option<DittoHeaders> headers(final DittoHeaders dittoHeaders) {
}

/**
* The Modify class provides static factory methods for creating Options which are related to modifying operations.
* The {@code Modify} class provides static factory methods for creating Options which are related to modifying
* operations.
*
* @since 1.0.0
*/
Expand Down Expand Up @@ -115,7 +117,6 @@ public static Option<PolicyId> copyPolicy(final PolicyId copyPolicyFrom) {
return DefaultOption.newInstance(OptionName.Modify.COPY_POLICY, copyPolicyFrom);
}


/**
* Creates an option for specifying whether the created policy should be copied from an already existing thing
* <p>
Expand All @@ -125,7 +126,7 @@ public static Option<PolicyId> copyPolicy(final PolicyId copyPolicyFrom) {
* If this Option is not specified, it does not matter whether the object exists.
* </p>
*
* @param thingToCopyPolicyFrom existing thing from which the policy are used
* @param thingToCopyPolicyFrom existing thing from which the policy are used.
* @return the new option.
* @since 1.1.0
*/
Expand All @@ -134,26 +135,30 @@ public static Option<ThingId> copyPolicyFromThing(final ThingId thingToCopyPolic
}

/**
* Creates an option for specifying whether the request should be applied to the thing based on the given condition
* Creates an option for specifying whether the command should be applied based on the given condition.
* <p>
* The returned option has the name {@link OptionName.Modify#CONDITION} and the given {@code Condition} value.
* The returned option has the name {@link OptionName.Modify#CONDITION} and the given argument value.
* </p>
* <p>
* If this Option is not specified, the request will be applied no matter what.
* If this {@code Option} is not specified, the command will be applied no matter what.
* </p>
*
* @param condition existing thing from which the policy are used
* @param condition the RQL condition that determines whether the command will be applied.
* @return the new option.
* @throws NullPointerException if {@code condition} is {@code null}.
* @throws IllegalArgumentException if {@code condition} is empty.
* @since 2.1.0
*/
public static Option<String> condition(final String condition) {
return DefaultOption.newInstance(OptionName.Modify.CONDITION, condition);
public static Option<String> condition(final CharSequence condition) {
ConditionChecker.argumentNotEmpty(condition, "condition");
return DefaultOption.newInstance(OptionName.Modify.CONDITION, condition.toString());
}

}

/**
* The Consumption class provides static factory methods for creating Options which are related to {@link
* CommonManagement#startConsumption() startConsumption()}.
* The Consumption class provides static factory methods for creating Options which are related to
* {@link CommonManagement#startConsumption() startConsumption()}.
*
* @since 1.0.0
*/
Expand Down Expand Up @@ -210,6 +215,7 @@ public static Option<CharSequence> filter(final CharSequence rqlExpression) {
public static Option<JsonFieldSelector> extraFields(final JsonFieldSelector jsonFieldSelector) {
return DefaultOption.newInstance(OptionName.Consumption.EXTRA_FIELDS, jsonFieldSelector);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.eclipse.ditto.client;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.ditto.client.TestConstants.Thing.THING_ID;
import static org.eclipse.ditto.client.assertions.ClientAssertions.assertThat;

Expand All @@ -20,7 +21,6 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.assertj.core.api.Assertions;
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.client.internal.AbstractDittoClientThingsTest;
Expand Down Expand Up @@ -62,7 +62,7 @@ public final class DittoClientAttributesTest extends AbstractDittoClientThingsTe
private static final JsonPointer ATTRIBUTE_KEY_REALLY_NEW = JsonFactory.newPointer("reallyNew");
private static final JsonPointer ATTRIBUTE_KEY_OLD = JsonFactory.newPointer("old");
private static final String ATTRIBUTE_VALUE = "value";
private static final JsonObject ATTRIBUTES = JsonFactory.newObjectBuilder()
private static final JsonObject ATTRIBUTES = JsonFactory.newObjectBuilder()
.set(ATTRIBUTE_KEY_NEW, 42)
.set(ATTRIBUTE_KEY_REALLY_NEW, true)
.build();
Expand Down Expand Up @@ -94,8 +94,7 @@ public void testAddStringAttributeWithExistsOptionTrue() {

@Test
public void testMergeStringAttributeWithExistsOptionFalse() {
final JsonPointer absolutePath =
Thing.JsonFields.ATTRIBUTES.getPointer().append(ATTRIBUTE_KEY_NEW);
final JsonPointer absolutePath = Thing.JsonFields.ATTRIBUTES.getPointer().append(ATTRIBUTE_KEY_NEW);

assertEventualCompletion(
getManagement()
Expand All @@ -104,9 +103,7 @@ public void testMergeStringAttributeWithExistsOptionFalse() {
);
final Signal<?> command = expectMsgClass(MergeThing.class);
assertOnlyIfNoneMatchHeader(command);
reply(MergeThingResponse.of(THING_ID,
absolutePath,
command.getDittoHeaders()));
reply(MergeThingResponse.of(THING_ID, absolutePath, command.getDittoHeaders()));
}

@Test
Expand All @@ -121,9 +118,7 @@ public void testMergeStringAttributeWithExistsOptionTrue() {
);
final Signal<?> command = expectMsgClass(MergeThing.class);
assertOnlyIfMatchHeader(command);
reply(MergeThingResponse.of(THING_ID,
absolutePath,
command.getDittoHeaders()));
reply(MergeThingResponse.of(THING_ID, absolutePath, command.getDittoHeaders()));
}

@Test
Expand Down Expand Up @@ -151,8 +146,7 @@ public void testAddObjectAttribute() {

@Test
public void testMergeAttribute() {
final JsonPointer absolutePath =
Thing.JsonFields.ATTRIBUTES.getPointer().append(ATTRIBUTE_KEY_NEW);
final JsonPointer absolutePath = Thing.JsonFields.ATTRIBUTES.getPointer().append(ATTRIBUTE_KEY_NEW);
final JsonObject value = JsonFactory.newObject("{\"id\": 42, \"name\": " +
"\"someName\"}");

Expand Down Expand Up @@ -244,13 +238,18 @@ public void testReceiveAttributeModifiedEvent() throws Exception {
.build();

final Message<ThingEvent> attributeModified = MessagesModelFactory.<ThingEvent>newMessageBuilder(messageHeaders)
.payload(AttributeModified.of(THING_ID, ATTRIBUTE_KEY_NEW, JsonFactory.newValue("value"), 1,
Instant.now(), headersWithChannel(), null))
.payload(AttributeModified.of(THING_ID,
ATTRIBUTE_KEY_NEW,
JsonFactory.newValue("value"),
1,
Instant.now(),
headersWithChannel(),
null))
.build();

messaging.receiveEvent(attributeModified);

Assertions.assertThat(latch.await(TIMEOUT, TIME_UNIT)).isTrue();
assertThat(latch.await(TIMEOUT, TIME_UNIT)).isTrue();
}

@Test
Expand All @@ -273,13 +272,18 @@ public void testReceiveAttributeModifiedEventWithActionAdded() throws Exception
MessageHeaders.newBuilder(MessageDirection.FROM, THING_ID, AttributeCreated.TYPE).build();

final Message<ThingEvent> attributeCreated = MessagesModelFactory.<ThingEvent>newMessageBuilder(messageHeaders)
.payload(AttributeCreated.of(THING_ID, ATTRIBUTE_KEY_REALLY_NEW, JsonFactory.newValue("value"), 1,
Instant.now(), headersWithChannel(), null))
.payload(AttributeCreated.of(THING_ID,
ATTRIBUTE_KEY_REALLY_NEW,
JsonFactory.newValue("value"),
1,
Instant.now(),
headersWithChannel(),
null))
.build();

messaging.receiveEvent(attributeCreated);

Assertions.assertThat(latch.await(TIMEOUT, TIME_UNIT)).isTrue();
assertThat(latch.await(TIMEOUT, TIME_UNIT)).isTrue();
}

@Test(expected = IllegalArgumentException.class)
Expand Down Expand Up @@ -325,8 +329,7 @@ public void testPutAttributeWithConditionOption() {
final ModifyAttribute command = expectMsgClass(ModifyAttribute.class);
reply(ModifyAttributeResponse.created(THING_ID, ATTRIBUTE_KEY_NEW, JsonValue.of(ATTRIBUTE_VALUE),
command.getDittoHeaders()));
Assertions.assertThat(command.getDittoHeaders())
.containsEntry(DittoHeaderDefinition.CONDITION.getKey(), CONDITION.toString());
assertThat(command.getDittoHeaders()).containsEntry(DittoHeaderDefinition.CONDITION.getKey(), CONDITION);
}

@Test
Expand All @@ -337,10 +340,8 @@ public void testMergeAttributeWithConditionOption() {
.mergeAttribute(ATTRIBUTE_KEY_NEW, ATTRIBUTE_VALUE, Options.Modify.condition(CONDITION))
);
final MergeThing command = expectMsgClass(MergeThing.class);
reply(MergeThingResponse.of(THING_ID, JsonFactory.newPointer("/attributes"),
command.getDittoHeaders()));
Assertions.assertThat(command.getDittoHeaders())
.containsEntry(DittoHeaderDefinition.CONDITION.getKey(), CONDITION.toString());
reply(MergeThingResponse.of(THING_ID, JsonFactory.newPointer("/attributes"), command.getDittoHeaders()));
assertThat(command.getDittoHeaders()).containsEntry(DittoHeaderDefinition.CONDITION.getKey(), CONDITION);
}

@Test
Expand All @@ -351,10 +352,8 @@ public void testMergeAttributesWithConditionOption() {
.mergeAttributes(ATTRIBUTES, Options.Modify.condition(CONDITION))
);
final MergeThing command = expectMsgClass(MergeThing.class);
reply(MergeThingResponse.of(THING_ID, JsonFactory.newPointer("/attributes"),
command.getDittoHeaders()));
Assertions.assertThat(command.getDittoHeaders())
.containsEntry(DittoHeaderDefinition.CONDITION.getKey(), CONDITION.toString());
reply(MergeThingResponse.of(THING_ID, JsonFactory.newPointer("/attributes"), command.getDittoHeaders()));
assertThat(command.getDittoHeaders()).containsEntry(DittoHeaderDefinition.CONDITION.getKey(), CONDITION);
}

@Test
Expand All @@ -365,10 +364,8 @@ public void testDeleteAttributeWithConditionOption() {
.deleteAttribute(ATTRIBUTE_KEY_NEW, Options.Modify.condition(CONDITION))
);
final DeleteAttribute command = expectMsgClass(DeleteAttribute.class);
reply(DeleteAttributeResponse.of(THING_ID, ATTRIBUTE_KEY_NEW,
command.getDittoHeaders()));
Assertions.assertThat(command.getDittoHeaders())
.containsEntry(DittoHeaderDefinition.CONDITION.getKey(), CONDITION.toString());
reply(DeleteAttributeResponse.of(THING_ID, ATTRIBUTE_KEY_NEW, command.getDittoHeaders()));
assertThat(command.getDittoHeaders()).containsEntry(DittoHeaderDefinition.CONDITION.getKey(), CONDITION);
}

@Test
Expand All @@ -380,8 +377,7 @@ public void testDeleteAttributesWithConditionOption() {
);
final DeleteAttributes command = expectMsgClass(DeleteAttributes.class);
reply(DeleteAttributesResponse.of(THING_ID, command.getDittoHeaders()));
Assertions.assertThat(command.getDittoHeaders())
.containsEntry(DittoHeaderDefinition.CONDITION.getKey(), CONDITION.toString());
assertThat(command.getDittoHeaders()).containsEntry(DittoHeaderDefinition.CONDITION.getKey(), CONDITION);
}

}
Loading

0 comments on commit 0eae873

Please sign in to comment.