Skip to content

Commit

Permalink
Issue #106: Adjusted thing modifying command responses and ThingModif…
Browse files Browse the repository at this point in the history
…yCommandResponseMappingStrategies for safe mapping from Adaptable.

Signed-off-by: Juergen Fickel <juergen.fickel@bosch.io>
  • Loading branch information
Juergen Fickel committed Dec 10, 2021
1 parent 6cf438f commit 4af3160
Show file tree
Hide file tree
Showing 25 changed files with 891 additions and 406 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder,

@Override
public CreateThingResponse setDittoHeaders(final DittoHeaders dittoHeaders) {
return of(createdThing, dittoHeaders);
return newInstance(createdThing, getHttpStatus(), dittoHeaders);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder,

@Override
public DeleteAttributeResponse setDittoHeaders(final DittoHeaders dittoHeaders) {
return of(thingId, attributePointer, dittoHeaders);
return newInstance(thingId, attributePointer, getHttpStatus(), dittoHeaders);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder,

@Override
public DeleteAttributesResponse setDittoHeaders(final DittoHeaders dittoHeaders) {
return of(thingId, dittoHeaders);
return newInstance(thingId, getHttpStatus(), dittoHeaders);
}

@Override
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 DeleteFeatureDefinitionResponse extends AbstractCommandRespon

private static final CommandResponseJsonDeserializer<DeleteFeatureDefinitionResponse> JSON_DESERIALIZER =
CommandResponseJsonDeserializer.newInstance(TYPE,
HTTP_STATUS,
context -> {
final JsonObject jsonObject = context.getJsonObject();
return new DeleteFeatureDefinitionResponse(
return newInstance(
ThingId.of(jsonObject.getValueOrThrow(ThingCommandResponse.JsonFields.JSON_THING_ID)),
jsonObject.getValueOrThrow(JSON_FEATURE_ID),
context.getDeserializedHttpStatus(),
Expand All @@ -77,7 +79,9 @@ private DeleteFeatureDefinitionResponse(final ThingId thingId,

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

/**
Expand All @@ -93,7 +97,33 @@ public static DeleteFeatureDefinitionResponse of(final ThingId thingId,
final String featureId,
final DittoHeaders dittoHeaders) {

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

/**
* Returns a new instance of {@code DeleteFeatureDefinitionResponse} for the specified arguments.
*
* @param thingId the ID of the thing the feature belongs to.
* @param featureId ID of the feature the definition was deleted from.
* @param httpStatus the status of the response.
* @param dittoHeaders the headers of the response.
* @return the {@code DeleteFeatureDefinitionResponse} 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 DeleteFeatureDefinitionResponse}.
* @since 2.3.0
*/
public static DeleteFeatureDefinitionResponse newInstance(final ThingId thingId,
final String featureId,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders) {

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

/**
Expand Down Expand Up @@ -174,7 +204,7 @@ protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder,

@Override
public DeleteFeatureDefinitionResponse setDittoHeaders(final DittoHeaders dittoHeaders) {
return of(thingId, featureId, dittoHeaders);
return newInstance(thingId, featureId, getHttpStatus(), dittoHeaders);
}

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

import static org.eclipse.ditto.base.model.common.ConditionChecker.argumentNotEmpty;
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 @@ -59,10 +61,9 @@ public final class DeleteFeatureDesiredPropertiesResponse

private static final CommandResponseJsonDeserializer<DeleteFeatureDesiredPropertiesResponse> JSON_DESERIALIZER =
CommandResponseJsonDeserializer.newInstance(TYPE,
HTTP_STATUS,
context -> {
final JsonObject jsonObject = context.getJsonObject();
return new DeleteFeatureDesiredPropertiesResponse(
return newInstance(
ThingId.of(jsonObject.getValueOrThrow(ThingCommandResponse.JsonFields.JSON_THING_ID)),
jsonObject.getValueOrThrow(JSON_FEATURE_ID),
context.getDeserializedHttpStatus(),
Expand All @@ -80,7 +81,9 @@ private DeleteFeatureDesiredPropertiesResponse(final ThingId thingId,

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

/**
Expand All @@ -96,7 +99,33 @@ public static DeleteFeatureDesiredPropertiesResponse of(final ThingId thingId,
final CharSequence featureId,
final DittoHeaders dittoHeaders) {

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

/**
* Returns a new instance of {@code DeleteFeatureDesiredPropertiesResponse} for the specified arguments.
*
* @param thingId the ID of the thing the desired feature properties were deleted from.
* @param featureId ID of the feature of which the desired properties were deleted.
* @param httpStatus the status of the response.
* @param dittoHeaders the headers of the response.
* @return the {@code DeleteFeatureDesiredPropertiesResponse} 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 DeleteFeatureDesiredPropertiesResponse}.
* @since 2.3.0
*/
public static DeleteFeatureDesiredPropertiesResponse newInstance(final ThingId thingId,
final CharSequence featureId,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders) {

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

/**
Expand Down Expand Up @@ -173,7 +202,7 @@ protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder,

@Override
public DeleteFeatureDesiredPropertiesResponse setDittoHeaders(final DittoHeaders dittoHeaders) {
return of(thingId, featureId, dittoHeaders);
return newInstance(thingId, featureId, getHttpStatus(), dittoHeaders);
}

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

import static org.eclipse.ditto.base.model.common.ConditionChecker.argumentNotEmpty;
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 @@ -63,10 +65,9 @@ public final class DeleteFeatureDesiredPropertyResponse

private static final CommandResponseJsonDeserializer<DeleteFeatureDesiredPropertyResponse> JSON_DESERIALIZER =
CommandResponseJsonDeserializer.newInstance(TYPE,
HTTP_STATUS,
context -> {
final JsonObject jsonObject = context.getJsonObject();
return new DeleteFeatureDesiredPropertyResponse(
return newInstance(
ThingId.of(jsonObject.getValueOrThrow(ThingCommandResponse.JsonFields.JSON_THING_ID)),
jsonObject.getValueOrThrow(JSON_FEATURE_ID),
JsonPointer.of(jsonObject.getValueOrThrow(JSON_DESIRED_PROPERTY)),
Expand All @@ -87,7 +88,9 @@ private DeleteFeatureDesiredPropertyResponse(final ThingId thingId,

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

Expand All @@ -113,10 +116,35 @@ public static DeleteFeatureDesiredPropertyResponse of(final ThingId thingId,
final JsonPointer desiredPropertyPointer,
final DittoHeaders dittoHeaders) {

return newInstance(thingId, featureId, desiredPropertyPointer, HTTP_STATUS, dittoHeaders);
}

/**
* Returns a new instance of {@code DeleteFeatureDesiredPropertyResponse} for the specified arguments.
*
* @param thingId the ID of the thing of which the desired feature property was deleted from.
* @param featureId ID of the feature of which the desired property was deleted.
* @param desiredPropertyPointer the JSON pointer of the deleted desired property.
* @param httpStatus the status of the response.
* @param dittoHeaders the headers of the response.
* @return the {@code DeleteFeatureDesiredPropertyResponse} 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 DeleteFeatureDesiredPropertyResponse}.
* @since 2.3.0
*/
public static DeleteFeatureDesiredPropertyResponse newInstance(final ThingId thingId,
final CharSequence featureId,
final JsonPointer desiredPropertyPointer,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders) {

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

Expand Down Expand Up @@ -208,7 +236,7 @@ protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder,

@Override
public DeleteFeatureDesiredPropertyResponse setDittoHeaders(final DittoHeaders dittoHeaders) {
return of(thingId, featureId, desiredPropertyPointer, dittoHeaders);
return newInstance(thingId, featureId, desiredPropertyPointer, getHttpStatus(), dittoHeaders);
}

@Override
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 DeleteFeaturePropertiesResponse extends AbstractCommandRespon

private static final CommandResponseJsonDeserializer<DeleteFeaturePropertiesResponse> JSON_DESERIALIZER =
CommandResponseJsonDeserializer.newInstance(TYPE,
HTTP_STATUS,
context -> {
final JsonObject jsonObject = context.getJsonObject();
return new DeleteFeaturePropertiesResponse(
return newInstance(
ThingId.of(jsonObject.getValueOrThrow(ThingCommandResponse.JsonFields.JSON_THING_ID)),
jsonObject.getValueOrThrow(JSON_FEATURE_ID),
context.getDeserializedHttpStatus(),
Expand All @@ -76,7 +78,9 @@ private DeleteFeaturePropertiesResponse(final ThingId thingId,

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

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

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

/**
* Returns a new instance of {@code DeleteFeaturePropertiesResponse} for the specified arguments.
*
* @param thingId the ID of the thing the feature properties were deleted from.
* @param featureId ID of feature the properties were deleted from.
* @param httpStatus the status of the response.
* @param dittoHeaders the headers of the response.
* @return the {@code DeleteFeaturePropertiesResponse} 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 DeleteFeaturePropertiesResponse}.
* @since 2.3.0
*/
public static DeleteFeaturePropertiesResponse newInstance(final ThingId thingId,
final String featureId,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders) {

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

/**
Expand Down Expand Up @@ -157,7 +187,7 @@ protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder,

@Override
public DeleteFeaturePropertiesResponse setDittoHeaders(final DittoHeaders dittoHeaders) {
return of(thingId, featureId, dittoHeaders);
return newInstance(thingId, featureId, getHttpStatus(), dittoHeaders);
}

@Override
Expand Down

0 comments on commit 4af3160

Please sign in to comment.