Skip to content

Commit

Permalink
Issue #106: Adjusted MergeThingResponse and ThingMergeCommandResponse…
Browse files Browse the repository at this point in the history
…MappingStrategies for safe mapping from Adaptable.

Signed-off-by: Juergen Fickel <juergen.fickel@bosch.io>
  • Loading branch information
Juergen Fickel committed Dec 13, 2021
1 parent f8f86c0 commit 6b207b6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
*/
package org.eclipse.ditto.protocol.mappingstrategies;

import java.util.HashMap;
import java.util.Collections;
import java.util.Map;

import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.protocol.Adaptable;
import org.eclipse.ditto.protocol.JsonifiableMapper;
import org.eclipse.ditto.protocol.Payload;
import org.eclipse.ditto.things.model.signals.commands.modify.MergeThingResponse;

/**
Expand All @@ -29,22 +30,24 @@ final class ThingMergeCommandResponseMappingStrategies
new ThingMergeCommandResponseMappingStrategies();

private ThingMergeCommandResponseMappingStrategies() {
super(new HashMap<>());
super(initMappingStrategies());
}

@Override
public JsonifiableMapper<MergeThingResponse> find(final String type) {
return ThingMergeCommandResponseMappingStrategies::mergeThing;
private static Map<String, JsonifiableMapper<MergeThingResponse>> initMappingStrategies() {
return Collections.singletonMap(MergeThingResponse.TYPE,
AdaptableToSignalMapper.of(MergeThingResponse.class,
context -> {
final Adaptable adaptable = context.getAdaptable();
final Payload payload = adaptable.getPayload();
return MergeThingResponse.newInstance(context.getThingId(),
payload.getPath(),
context.getHttpStatusOrThrow(),
context.getDittoHeaders());
}));
}

static ThingMergeCommandResponseMappingStrategies getInstance() {
return INSTANCE;
}

private static MergeThingResponse mergeThing(final Adaptable adaptable) {
return MergeThingResponse.of(thingIdFrom(adaptable),
JsonPointer.of(adaptable.getPayload().getPath().toString()),
dittoHeadersFrom(adaptable));
}

}
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 @@ -28,6 +29,7 @@
import org.eclipse.ditto.base.model.signals.FeatureToggle;
import org.eclipse.ditto.base.model.signals.UnsupportedSchemaVersionException;
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 @@ -57,10 +59,9 @@ public final class MergeThingResponse extends AbstractCommandResponse<MergeThing

private static final CommandResponseJsonDeserializer<MergeThingResponse> JSON_DESERIALIZER =
CommandResponseJsonDeserializer.newInstance(TYPE,
HTTP_STATUS,
context -> {
final JsonObject jsonObject = context.getJsonObject();
return new MergeThingResponse(
return newInstance(
ThingId.of(jsonObject.getValueOrThrow(ThingCommandResponse.JsonFields.JSON_THING_ID)),
JsonPointer.of(jsonObject.getValueOrThrow(JsonFields.JSON_PATH)),
context.getDeserializedHttpStatus(),
Expand Down Expand Up @@ -101,7 +102,31 @@ public static MergeThingResponse of(final ThingId thingId,
final JsonPointer path,
final DittoHeaders dittoHeaders) {

return new MergeThingResponse(thingId, path, HTTP_STATUS, dittoHeaders);
return newInstance(thingId, path, HTTP_STATUS, dittoHeaders);
}

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

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

/**
Expand Down Expand Up @@ -133,7 +158,7 @@ public JsonPointer getResourcePath() {

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

@Override
Expand Down

0 comments on commit 6b207b6

Please sign in to comment.