Skip to content

Commit

Permalink
Issue #106: Began to prepare command responses for safe mapping from …
Browse files Browse the repository at this point in the history
…Adaptable. (WIP)

Signed-off-by: Juergen Fickel <juergen.fickel@bosch.io>
  • Loading branch information
Juergen Fickel committed Dec 8, 2021
1 parent 8353748 commit 3f25ee5
Show file tree
Hide file tree
Showing 11 changed files with 533 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;

import java.text.MessageFormat;
import java.util.Collections;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
Expand All @@ -28,6 +29,7 @@
import org.eclipse.ditto.base.model.json.JsonParsableCommandResponse;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.signals.commands.AbstractCommandResponse;
import org.eclipse.ditto.base.model.signals.commands.CommandResponseHttpStatusValidator;
import org.eclipse.ditto.base.model.signals.commands.CommandResponseJsonDeserializer;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
Expand Down Expand Up @@ -60,10 +62,9 @@ public final class CreateThingResponse extends AbstractCommandResponse<CreateThi

private static final CommandResponseJsonDeserializer<CreateThingResponse> JSON_DESERIALIZER =
CommandResponseJsonDeserializer.newInstance(TYPE,
HTTP_STATUS,
context -> {
final JsonObject jsonObject = context.getJsonObject();
return new CreateThingResponse(
return newInstance(
jsonObject.getValue(JSON_THING)
.map(JsonValue::asObject)
.map(ThingsModelFactory::newThing)
Expand Down Expand Up @@ -96,7 +97,29 @@ private CreateThingResponse(final Thing createdThing,
* @throws NullPointerException if any argument is {@code null}.
*/
public static CreateThingResponse of(final Thing thing, final DittoHeaders dittoHeaders) {
return new CreateThingResponse(thing, HTTP_STATUS, dittoHeaders);
return newInstance(thing, HTTP_STATUS, dittoHeaders);
}

/**
* Returns a new instance of {@code CreateThingResponse} for the specified arguments.
*
* @param createdThing the created thing.
* @param httpStatus the status of the response.
* @param dittoHeaders the headers of the response.
* @return the {@code CreateThingResponse} instance.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code httpStatus} is not allowed for a {@code CreateThingResponse}.
* @since 2.3.0
*/
public static CreateThingResponse newInstance(final Thing createdThing,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders) {

return new CreateThingResponse(createdThing,
CommandResponseHttpStatusValidator.validateHttpStatus(httpStatus,
Collections.singleton(HTTP_STATUS),
CreateThingResponse.class),
dittoHeaders);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;

import java.util.Collections;
import java.util.Objects;
import java.util.function.Predicate;

Expand All @@ -26,6 +27,7 @@
import org.eclipse.ditto.base.model.json.JsonParsableCommandResponse;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.signals.commands.AbstractCommandResponse;
import org.eclipse.ditto.base.model.signals.commands.CommandResponseHttpStatusValidator;
import org.eclipse.ditto.base.model.signals.commands.CommandResponseJsonDeserializer;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
Expand Down Expand Up @@ -54,24 +56,29 @@ public final class DeleteAttributeResponse extends AbstractCommandResponse<Delet
static final JsonFieldDefinition<String> JSON_ATTRIBUTE =
JsonFieldDefinition.ofString("attribute", FieldType.REGULAR, JsonSchemaVersion.V_2);

private static final HttpStatus HTTP_STATUS = HttpStatus.NO_CONTENT;

private static final CommandResponseJsonDeserializer<DeleteAttributeResponse> JSON_DESERIALIZER =
CommandResponseJsonDeserializer.newInstance(TYPE,
HttpStatus.NO_CONTENT,
context -> {
final JsonObject jsonObject = context.getJsonObject();
return of(ThingId.of(jsonObject.getValueOrThrow(ThingCommandResponse.JsonFields.JSON_THING_ID)),
return newInstance(
ThingId.of(jsonObject.getValueOrThrow(ThingCommandResponse.JsonFields.JSON_THING_ID)),
JsonFactory.newPointer(jsonObject.getValueOrThrow(JSON_ATTRIBUTE)),
context.getDittoHeaders());
context.getDeserializedHttpStatus(),
context.getDittoHeaders()
);
});

private final ThingId thingId;
private final JsonPointer attributePointer;

private DeleteAttributeResponse(final ThingId thingId,
final JsonPointer attributePointer,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders) {

super(TYPE, HttpStatus.NO_CONTENT, dittoHeaders);
super(TYPE, httpStatus, dittoHeaders);
this.thingId = checkNotNull(thingId, "thingId");
this.attributePointer = checkAttributePointer(attributePointer, dittoHeaders);
}
Expand Down Expand Up @@ -105,7 +112,32 @@ public static DeleteAttributeResponse of(final ThingId thingId,
final JsonPointer attributePointer,
final DittoHeaders dittoHeaders) {

return new DeleteAttributeResponse(thingId, attributePointer, dittoHeaders);
return newInstance(thingId, attributePointer, HTTP_STATUS, dittoHeaders);
}

/**
* Returns a new instance of {@code DeleteAttributeResponse} for the specified arguments.
*
* @param thingId the ID of the thing the attributes were deleted from.
* @param attributePointer the JSON pointer of the deleted attribute.
* @param httpStatus the status of the response.
* @param dittoHeaders the headers of the response.
* @return the {@code DeleteAttributeResponse} instance.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code httpStatus} is not allowed for a {@code DeleteAttributeResponse}.
* @since 2.3.0
*/
public static DeleteAttributeResponse newInstance(final ThingId thingId,
final JsonPointer attributePointer,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders) {

return new DeleteAttributeResponse(thingId,
attributePointer,
CommandResponseHttpStatusValidator.validateHttpStatus(httpStatus,
Collections.singleton(HTTP_STATUS),
DeleteAttributeResponse.class),
dittoHeaders);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.eclipse.ditto.things.model.signals.commands.modify;

import java.util.Collections;
import java.util.Objects;
import java.util.function.Predicate;

Expand All @@ -24,6 +25,7 @@
import org.eclipse.ditto.base.model.json.JsonParsableCommandResponse;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.signals.commands.AbstractCommandResponse;
import org.eclipse.ditto.base.model.signals.commands.CommandResponseHttpStatusValidator;
import org.eclipse.ditto.base.model.signals.commands.CommandResponseJsonDeserializer;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonObject;
Expand All @@ -49,10 +51,9 @@ public final class DeleteAttributesResponse extends AbstractCommandResponse<Dele

private static final CommandResponseJsonDeserializer<DeleteAttributesResponse> JSON_DESERIALIZER =
CommandResponseJsonDeserializer.newInstance(TYPE,
HTTP_STATUS,
context -> {
final JsonObject jsonObject = context.getJsonObject();
return new DeleteAttributesResponse(
return newInstance(
ThingId.of(jsonObject.getValueOrThrow(ThingCommandResponse.JsonFields.JSON_THING_ID)),
context.getDeserializedHttpStatus(),
context.getDittoHeaders()
Expand All @@ -78,7 +79,29 @@ private DeleteAttributesResponse(final ThingId thingId,
* @throws NullPointerException if {@code dittoHeaders} is {@code null}.
*/
public static DeleteAttributesResponse of(final ThingId thingId, final DittoHeaders dittoHeaders) {
return new DeleteAttributesResponse(thingId, HTTP_STATUS, dittoHeaders);
return newInstance(thingId, HTTP_STATUS, dittoHeaders);
}

/**
* Returns a new instance of {@code DeleteAttributesResponse} for the specified arguments.
*
* @param thingId the ID of the thing the attributes were deleted from.
* @param httpStatus the status of the response.
* @param dittoHeaders the headers of the response.
* @return the {@code DeleteAttributesResponse} instance.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code httpStatus} is not allowed for a {@code DeleteAttributesResponse}.
* @since 2.3.0
*/
public static DeleteAttributesResponse newInstance(final ThingId thingId,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders) {

return new DeleteAttributesResponse(thingId,
CommandResponseHttpStatusValidator.validateHttpStatus(httpStatus,
Collections.singleton(HTTP_STATUS),
DeleteAttributesResponse.class),
dittoHeaders);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@

import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;

import java.util.Collections;
import java.util.Objects;
import java.util.function.Predicate;

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import org.eclipse.ditto.base.model.common.ConditionChecker;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.base.model.json.JsonParsableCommandResponse;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.signals.commands.AbstractCommandResponse;
import org.eclipse.ditto.base.model.signals.commands.CommandResponseHttpStatusValidator;
import org.eclipse.ditto.base.model.signals.commands.CommandResponseJsonDeserializer;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
Expand Down Expand Up @@ -55,10 +58,9 @@ public final class DeleteFeatureResponse extends AbstractCommandResponse<DeleteF

private static final CommandResponseJsonDeserializer<DeleteFeatureResponse> JSON_DESERIALIZER =
CommandResponseJsonDeserializer.newInstance(TYPE,
HTTP_STATUS,
context -> {
final JsonObject jsonObject = context.getJsonObject();
return new DeleteFeatureResponse(
return newInstance(
ThingId.of(jsonObject.getValueOrThrow(ThingCommandResponse.JsonFields.JSON_THING_ID)),
jsonObject.getValueOrThrow(JSON_FEATURE_ID),
context.getDeserializedHttpStatus(),
Expand All @@ -70,13 +72,15 @@ public final class DeleteFeatureResponse extends AbstractCommandResponse<DeleteF
private final String featureId;

private DeleteFeatureResponse(final ThingId thingId,
final String featureId,
final CharSequence featureId,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders) {

super(TYPE, httpStatus, dittoHeaders);
this.thingId = checkNotNull(thingId, "thingId");
this.featureId = checkNotNull(featureId, "featureId");
this.featureId = ConditionChecker.checkArgument(checkNotNull(featureId, "featureId").toString(),
featureIdArgument -> !featureIdArgument.trim().isEmpty(),
() -> "The featureId must neither be empty nor blank.");
}

/**
Expand All @@ -92,7 +96,7 @@ public static DeleteFeatureResponse of(final ThingId thingId,
final String featureId,
final DittoHeaders dittoHeaders) {

return new DeleteFeatureResponse(thingId, featureId, HTTP_STATUS, dittoHeaders);
return newInstance(thingId, featureId, HTTP_STATUS, dittoHeaders);
}

/**
Expand Down Expand Up @@ -124,6 +128,32 @@ public static DeleteFeatureResponse fromJson(final JsonObject jsonObject, final
return JSON_DESERIALIZER.deserialize(jsonObject, dittoHeaders);
}

/**
* Returns a new instance of {@code DeleteFeatureResponse} for the specified arguments.
*
* @param thingId the ID of the thing the feature was deleted from.
* @param featureId ID of the deleted feature.
* @param httpStatus the status of the response.
* @param dittoHeaders the headers of the response.
* @return the {@code DeleteFeatureResponse} instance.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code featureId} is empty or blank or if {@code httpStatus} is not allowed
* for a {@code DeleteFeatureResponse}.
* @since 2.3.0
*/
public static DeleteFeatureResponse newInstance(final ThingId thingId,
final CharSequence featureId,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders) {

return new DeleteFeatureResponse(thingId,
featureId,
CommandResponseHttpStatusValidator.validateHttpStatus(httpStatus,
Collections.singleton(HTTP_STATUS),
DeleteFeatureResponse.class),
dittoHeaders);
}

@Override
public ThingId getEntityId() {
return thingId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;

import java.util.Collections;
import java.util.Objects;
import java.util.function.Predicate;

Expand All @@ -25,6 +26,7 @@
import org.eclipse.ditto.base.model.json.JsonParsableCommandResponse;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.signals.commands.AbstractCommandResponse;
import org.eclipse.ditto.base.model.signals.commands.CommandResponseHttpStatusValidator;
import org.eclipse.ditto.base.model.signals.commands.CommandResponseJsonDeserializer;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonObject;
Expand All @@ -50,10 +52,9 @@ public final class DeleteFeaturesResponse extends AbstractCommandResponse<Delete

private static final CommandResponseJsonDeserializer<DeleteFeaturesResponse> JSON_DESERIALIZER =
CommandResponseJsonDeserializer.newInstance(TYPE,
HTTP_STATUS,
context -> {
final JsonObject jsonObject = context.getJsonObject();
return new DeleteFeaturesResponse(
return newInstance(
ThingId.of(jsonObject.getValueOrThrow(ThingCommandResponse.JsonFields.JSON_THING_ID)),
context.getDeserializedHttpStatus(),
context.getDittoHeaders()
Expand All @@ -79,7 +80,29 @@ private DeleteFeaturesResponse(final ThingId thingId,
* @throws NullPointerException if {@code dittoHeaders} is {@code null}.
*/
public static DeleteFeaturesResponse of(final ThingId thingId, final DittoHeaders dittoHeaders) {
return new DeleteFeaturesResponse(thingId, HTTP_STATUS, dittoHeaders);
return newInstance(thingId, HTTP_STATUS, dittoHeaders);
}

/**
* Returns a new instance of {@code DeleteFeaturesResponse} for the specified arguments.
*
* @param thingId the ID of the thing the features were deleted from.
* @param httpStatus the status of the response.
* @param dittoHeaders the headers of the response.
* @return the {@code DeleteFeaturesResponse} instance.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code httpStatus} is not allowed for a {@code DeleteFeaturesResponse}.
* @since 2.3.0
*/
public static DeleteFeaturesResponse newInstance(final ThingId thingId,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders) {

return new DeleteFeaturesResponse(thingId,
CommandResponseHttpStatusValidator.validateHttpStatus(httpStatus,
Collections.singleton(HTTP_STATUS),
DeleteFeaturesResponse.class),
dittoHeaders);
}

/**
Expand Down

0 comments on commit 3f25ee5

Please sign in to comment.