Search in sources :

Example 1 with DittoHeaders

use of org.eclipse.ditto.base.model.headers.DittoHeaders in project ditto by eclipse.

the class SudoRetrieveThing method fromJson.

/**
 * Creates a new {@code SudoRetrieveThing} from a JSON object.
 *
 * @param jsonObject the JSON string of which a new SudoRetrieveThing is to be created.
 * @param dittoHeaders the optional command headers of the request.
 * @return the SudoRetrieveThing which was created from the given JSON object.
 * @throws NullPointerException if {@code jsonObject} is {@code null}.
 * @throws org.eclipse.ditto.json.JsonMissingFieldException if the passed in {@code jsonObject} was not in the
 * expected format.
 */
public static SudoRetrieveThing fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) {
    final String extractedThingId = jsonObject.getValueOrThrow(SudoCommand.JsonFields.JSON_THING_ID);
    final ThingId thingId = ThingId.of(extractedThingId);
    final JsonFieldSelector readFieldSelector = jsonObject.getValue(SudoCommand.JsonFields.SELECTED_FIELDS).map(str -> JsonFactory.newFieldSelector(str, JsonFactory.newParseOptionsBuilder().withoutUrlDecoding().build())).orElse(null);
    final boolean isUseOriginalSchemaVersion = jsonObject.getValue(JSON_USE_ORIGINAL_SCHEMA_VERSION).orElse(false);
    return new SudoRetrieveThing(thingId, readFieldSelector, dittoHeaders, isUseOriginalSchemaVersion);
}
Also used : FieldType(org.eclipse.ditto.base.model.json.FieldType) Predicate(java.util.function.Predicate) JsonSchemaVersion(org.eclipse.ditto.base.model.json.JsonSchemaVersion) ConditionChecker.checkNotNull(org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull) JsonFieldDefinition(org.eclipse.ditto.json.JsonFieldDefinition) JsonParsableCommand(org.eclipse.ditto.base.model.json.JsonParsableCommand) ThingId(org.eclipse.ditto.things.model.ThingId) JsonObjectBuilder(org.eclipse.ditto.json.JsonObjectBuilder) Objects(java.util.Objects) JsonObject(org.eclipse.ditto.json.JsonObject) DittoHeaders(org.eclipse.ditto.base.model.headers.DittoHeaders) AbstractCommand(org.eclipse.ditto.base.model.signals.commands.AbstractCommand) JsonFactory(org.eclipse.ditto.json.JsonFactory) EntityId(org.eclipse.ditto.base.model.entity.id.EntityId) SignalWithEntityId(org.eclipse.ditto.base.model.signals.SignalWithEntityId) JsonFieldSelector(org.eclipse.ditto.json.JsonFieldSelector) Optional(java.util.Optional) JsonField(org.eclipse.ditto.json.JsonField) AllValuesAreNonnullByDefault(org.eclipse.ditto.utils.jsr305.annotations.AllValuesAreNonnullByDefault) Immutable(javax.annotation.concurrent.Immutable) Nullable(javax.annotation.Nullable) JsonFieldSelector(org.eclipse.ditto.json.JsonFieldSelector) ThingId(org.eclipse.ditto.things.model.ThingId)

Example 2 with DittoHeaders

use of org.eclipse.ditto.base.model.headers.DittoHeaders in project ditto by eclipse.

the class SudoRetrieveThings method fromJson.

/**
 * Creates a new {@code SudoRetrieveThings} from a JSON object.
 *
 * @param jsonObject the JSON string of which a new SudoRetrieveThings is to be created.
 * @param dittoHeaders the optional command headers of the request.
 * @return the SudoRetrieveThings which was created from the given JSON object.
 * @throws NullPointerException if {@code jsonObject} is {@code null}.
 * @throws org.eclipse.ditto.json.JsonMissingFieldException if the passed in {@code jsonObject} was not in the
 * expected format.
 */
public static SudoRetrieveThings fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) {
    final List<ThingId> extractedThingIds = jsonObject.getValueOrThrow(JSON_THING_IDS).stream().filter(JsonValue::isString).map(JsonValue::asString).map(ThingId::of).collect(Collectors.toList());
    final JsonFieldSelector extractedFieldSelector = jsonObject.getValue(SudoCommand.JsonFields.SELECTED_FIELDS).map(str -> JsonFactory.newFieldSelector(str, JsonFactory.newParseOptionsBuilder().withoutUrlDecoding().build())).orElse(null);
    return SudoRetrieveThings.of(extractedThingIds, extractedFieldSelector, dittoHeaders);
}
Also used : FieldType(org.eclipse.ditto.base.model.json.FieldType) ArrayList(java.util.ArrayList) JsonObject(org.eclipse.ditto.json.JsonObject) DittoHeaders(org.eclipse.ditto.base.model.headers.DittoHeaders) Objects.requireNonNull(java.util.Objects.requireNonNull) JsonArray(org.eclipse.ditto.json.JsonArray) AllValuesAreNonnullByDefault(org.eclipse.ditto.utils.jsr305.annotations.AllValuesAreNonnullByDefault) Nullable(javax.annotation.Nullable) JsonValue(org.eclipse.ditto.json.JsonValue) Predicate(java.util.function.Predicate) JsonSchemaVersion(org.eclipse.ditto.base.model.json.JsonSchemaVersion) JsonFieldDefinition(org.eclipse.ditto.json.JsonFieldDefinition) JsonParsableCommand(org.eclipse.ditto.base.model.json.JsonParsableCommand) ThingId(org.eclipse.ditto.things.model.ThingId) Collectors(java.util.stream.Collectors) JsonObjectBuilder(org.eclipse.ditto.json.JsonObjectBuilder) Objects(java.util.Objects) List(java.util.List) JsonFactory(org.eclipse.ditto.json.JsonFactory) AbstractCommand(org.eclipse.ditto.base.model.signals.commands.AbstractCommand) JsonFieldSelector(org.eclipse.ditto.json.JsonFieldSelector) Optional(java.util.Optional) JsonField(org.eclipse.ditto.json.JsonField) Collections(java.util.Collections) Immutable(javax.annotation.concurrent.Immutable) JsonCollectors(org.eclipse.ditto.json.JsonCollectors) JsonFieldSelector(org.eclipse.ditto.json.JsonFieldSelector) ThingId(org.eclipse.ditto.things.model.ThingId) JsonValue(org.eclipse.ditto.json.JsonValue)

Example 3 with DittoHeaders

use of org.eclipse.ditto.base.model.headers.DittoHeaders in project ditto by eclipse.

the class ThingLiveCommandAckRequestSetterTest method addsLiveResponseAckLabelToAlreadyRequiredAckLabels.

@Test
public void addsLiveResponseAckLabelToAlreadyRequiredAckLabels() {
    final AcknowledgementRequest ackRequest1 = AcknowledgementRequest.of(AcknowledgementLabel.of("FOO"));
    final AcknowledgementRequest ackRequest2 = AcknowledgementRequest.of(AcknowledgementLabel.of("BAR"));
    final DittoHeaders dittoHeaders = DittoHeaders.newBuilder().channel("live").acknowledgementRequest(ackRequest1, ackRequest2).randomCorrelationId().responseRequired(true).build();
    final CreateThing command = CreateThing.of(Thing.newBuilder().build(), null, dittoHeaders);
    final ThingLiveCommandAckRequestSetter underTest = ThingLiveCommandAckRequestSetter.getInstance();
    final DittoHeaders expectedHeaders = dittoHeaders.toBuilder().acknowledgementRequest(ackRequest1, ackRequest2, AcknowledgementRequest.of(DittoAcknowledgementLabel.LIVE_RESPONSE)).build();
    final CreateThing expectedCommand = command.setDittoHeaders(expectedHeaders);
    assertThat(underTest.apply(command)).isEqualTo(expectedCommand);
}
Also used : DittoHeaders(org.eclipse.ditto.base.model.headers.DittoHeaders) AcknowledgementRequest(org.eclipse.ditto.base.model.acks.AcknowledgementRequest) CreateThing(org.eclipse.ditto.things.model.signals.commands.modify.CreateThing) Test(org.junit.Test)

Example 4 with DittoHeaders

use of org.eclipse.ditto.base.model.headers.DittoHeaders in project ditto by eclipse.

the class ThingLiveCommandAckRequestSetterTest method notAddingLiveResponseAckLabelToExplicitlyEmptyRequiredAckLabels.

@Test
public void notAddingLiveResponseAckLabelToExplicitlyEmptyRequiredAckLabels() {
    final DittoHeaders dittoHeaders = DittoHeaders.newBuilder().channel("live").acknowledgementRequests(Collections.emptyList()).randomCorrelationId().responseRequired(true).build();
    final CreateThing command = CreateThing.of(Thing.newBuilder().build(), null, dittoHeaders);
    final ThingLiveCommandAckRequestSetter underTest = ThingLiveCommandAckRequestSetter.getInstance();
    final DittoHeaders expectedHeaders = dittoHeaders.toBuilder().acknowledgementRequests(Collections.emptyList()).build();
    final CreateThing expectedCommand = command.setDittoHeaders(expectedHeaders);
    assertThat(underTest.apply(command)).isEqualTo(expectedCommand);
}
Also used : DittoHeaders(org.eclipse.ditto.base.model.headers.DittoHeaders) CreateThing(org.eclipse.ditto.things.model.signals.commands.modify.CreateThing) Test(org.junit.Test)

Example 5 with DittoHeaders

use of org.eclipse.ditto.base.model.headers.DittoHeaders in project ditto by eclipse.

the class ThingLiveCommandAckRequestSetterTest method addLiveResponseAckLabelByDefault.

@Test
public void addLiveResponseAckLabelByDefault() {
    final DittoHeaders dittoHeaders = DittoHeaders.newBuilder().channel("live").randomCorrelationId().build();
    final CreateThing command = CreateThing.of(Thing.newBuilder().build(), null, dittoHeaders);
    final CreateThing expected = command.setDittoHeaders(DittoHeaders.newBuilder(dittoHeaders).acknowledgementRequest(AcknowledgementRequest.of(DittoAcknowledgementLabel.LIVE_RESPONSE)).responseRequired(true).build());
    final ThingLiveCommandAckRequestSetter underTest = ThingLiveCommandAckRequestSetter.getInstance();
    assertThat(underTest.apply(command)).isEqualTo(expected);
}
Also used : DittoHeaders(org.eclipse.ditto.base.model.headers.DittoHeaders) CreateThing(org.eclipse.ditto.things.model.signals.commands.modify.CreateThing) Test(org.junit.Test)

Aggregations

DittoHeaders (org.eclipse.ditto.base.model.headers.DittoHeaders)469 Test (org.junit.Test)259 ThingId (org.eclipse.ditto.things.model.ThingId)113 JsonObject (org.eclipse.ditto.json.JsonObject)112 WithDittoHeaders (org.eclipse.ditto.base.model.headers.WithDittoHeaders)92 PolicyId (org.eclipse.ditto.policies.model.PolicyId)86 ActorRef (akka.actor.ActorRef)69 Nullable (javax.annotation.Nullable)63 Thing (org.eclipse.ditto.things.model.Thing)61 TestKit (akka.testkit.javadsl.TestKit)59 Instant (java.time.Instant)59 Optional (java.util.Optional)59 JsonValue (org.eclipse.ditto.json.JsonValue)59 Policy (org.eclipse.ditto.policies.model.Policy)53 JsonPointer (org.eclipse.ditto.json.JsonPointer)52 DittoRuntimeException (org.eclipse.ditto.base.model.exceptions.DittoRuntimeException)46 SubjectId (org.eclipse.ditto.policies.model.SubjectId)45 CommandStrategy (org.eclipse.ditto.internal.utils.persistentactors.commands.CommandStrategy)40 Collectors (java.util.stream.Collectors)38 JsonSchemaVersion (org.eclipse.ditto.base.model.json.JsonSchemaVersion)38