Skip to content

Commit

Permalink
Adjust PolicyModifyCommandResponseMappingStrategies
Browse files Browse the repository at this point in the history
Use AdaptableToSignalMapper in PolicyModifyCommandResponseMappingStrategies and adjust all PolicyModifyCommands to validate HTTP status.

Signed-off-by: David Schwilk <david.schwilk@bosch.io>
  • Loading branch information
DerSchwilk committed Dec 10, 2021
1 parent b386630 commit fe05d4e
Show file tree
Hide file tree
Showing 20 changed files with 826 additions and 236 deletions.
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.Optional;
import java.util.function.Predicate;
Expand All @@ -27,6 +28,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 @@ -59,10 +61,9 @@ public final class CreatePolicyResponse extends AbstractCommandResponse<CreatePo

private static final CommandResponseJsonDeserializer<CreatePolicyResponse> JSON_DESERIALIZER =
CommandResponseJsonDeserializer.newInstance(TYPE,
HTTP_STATUS,
context -> {
final JsonObject jsonObject = context.getJsonObject();
return new CreatePolicyResponse(
return newInstance(
PolicyId.of(jsonObject.getValueOrThrow(PolicyCommandResponse.JsonFields.JSON_POLICY_ID)),
jsonObject.getValue(JSON_POLICY)
.map(JsonValue::asObject)
Expand All @@ -86,6 +87,31 @@ private CreatePolicyResponse(final PolicyId policyId,
this.policyCreated = policyCreated;
}

/**
* Returns a new instance of {@code CreatePolicyResponse} for the specified arguments.
*
* @param policyId the Policy ID of the created Policy
* @param policy the created Policy.
* @param httpStatus the status of the response.
* @param dittoHeaders the headers of the response.
* @return the {@code CreatePolicyResponse} instance.
* @throws NullPointerException if any argument is {@code null} except the {@code policy}.
* @throws IllegalArgumentException if {@code httpStatus} is not allowed for a {@code CreatePolicyResponse}.
* @since 2.3.0
*/
public static CreatePolicyResponse newInstance(final PolicyId policyId,
@Nullable final Policy policy,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders) {

return new CreatePolicyResponse(policyId,
policy,
CommandResponseHttpStatusValidator.validateHttpStatus(httpStatus,
Collections.singleton(HTTP_STATUS),
CreatePolicyResponse.class),
dittoHeaders);
}

/**
* Returns a new {@code CreatePolicyResponse} for a created Policy. This corresponds to the HTTP status
* {@link org.eclipse.ditto.base.model.common.HttpStatus#CREATED}.
Expand Down Expand Up @@ -170,7 +196,7 @@ protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder,

@Override
public CreatePolicyResponse setDittoHeaders(final DittoHeaders dittoHeaders) {
return of(policyId, policyCreated, dittoHeaders);
return newInstance(policyId, policyCreated, getHttpStatus(), dittoHeaders);
}

@Override
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.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
Expand Down Expand Up @@ -57,10 +59,9 @@ public final class DeletePolicyEntryResponse extends AbstractCommandResponse<Del

private static final CommandResponseJsonDeserializer<DeletePolicyEntryResponse> JSON_DESERIALIZER =
CommandResponseJsonDeserializer.newInstance(TYPE,
HTTP_STATUS,
context -> {
final JsonObject jsonObject = context.getJsonObject();
return new DeletePolicyEntryResponse(
return newInstance(
PolicyId.of(jsonObject.getValueOrThrow(PolicyCommandResponse.JsonFields.JSON_POLICY_ID)),
PoliciesModelFactory.newLabel(jsonObject.getValueOrThrow(JSON_LABEL)),
context.getDeserializedHttpStatus(),
Expand Down Expand Up @@ -94,7 +95,32 @@ public static DeletePolicyEntryResponse of(final PolicyId policyId,
final Label label,
final DittoHeaders dittoHeaders) {

return new DeletePolicyEntryResponse(policyId, label, HTTP_STATUS, dittoHeaders);
return newInstance(policyId, label, HTTP_STATUS, dittoHeaders);
}

/**
* Returns a new instance of {@code DeletePolicyEntryResponse} for the specified arguments.
*
* @param policyId the Policy ID of the deleted policy entry.
* @param label the Label of the deleted PolicyEntry.
* @param httpStatus the status of the response.
* @param dittoHeaders the headers of the response.
* @return the {@code DeletePolicyEntriesResponse} instance.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code httpStatus} is not allowed for a {@code DeletePolicyEntriesResponse}.
* @since 2.3.0
*/
public static DeletePolicyEntryResponse newInstance(final PolicyId policyId,
final Label label,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders) {

return new DeletePolicyEntryResponse(policyId,
label,
CommandResponseHttpStatusValidator.validateHttpStatus(httpStatus,
Collections.singleton(HTTP_STATUS),
DeletePolicyEntryResponse.class),
dittoHeaders);
}

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

@Override
public DeletePolicyEntryResponse setDittoHeaders(final DittoHeaders dittoHeaders) {
return of(policyId, label, dittoHeaders);
return newInstance(policyId, label, getHttpStatus(), dittoHeaders);
}

@Override
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 DeletePolicyResponse extends AbstractCommandResponse<DeletePo

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

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

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

/**
Expand Down Expand Up @@ -131,7 +154,7 @@ protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final Js

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

@Override
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 @@ -62,10 +64,9 @@ public final class DeleteResourceResponse extends AbstractCommandResponse<Delete

private static final CommandResponseJsonDeserializer<DeleteResourceResponse> JSON_DESERIALIZER =
CommandResponseJsonDeserializer.newInstance(TYPE,
HTTP_STATUS,
context -> {
final JsonObject jsonObject = context.getJsonObject();
return new DeleteResourceResponse(
return newInstance(
PolicyId.of(jsonObject.getValueOrThrow(PolicyCommandResponse.JsonFields.JSON_POLICY_ID)),
PoliciesModelFactory.newLabel(jsonObject.getValueOrThrow(JSON_LABEL)),
ResourceKey.newInstance(jsonObject.getValueOrThrow(JSON_RESOURCE_KEY)),
Expand Down Expand Up @@ -105,7 +106,35 @@ public static DeleteResourceResponse of(final PolicyId policyId,
final ResourceKey resourceKey,
final DittoHeaders dittoHeaders) {

return new DeleteResourceResponse(policyId, label, resourceKey, HttpStatus.NO_CONTENT, dittoHeaders);
return newInstance(policyId, label, resourceKey, HttpStatus.NO_CONTENT, dittoHeaders);
}

/**
* Returns a new instance of {@code DeleteResourceResponse} for the specified arguments.
*
* @param policyId the Policy ID of the deleted resource.
* @param label the Label of the PolicyEntry.
* @param resourceKey the ResourceKey of the deleted Resource.
* @param httpStatus the status of the response.
* @param dittoHeaders the headers of the response.
* @return the {@code DeleteResourceResponse} instance.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code httpStatus} is not allowed for a {@code DeleteResourceResponse}.
* @since 2.3.0
*/
public static DeleteResourceResponse newInstance(final PolicyId policyId,
final Label label,
final ResourceKey resourceKey,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders) {

return new DeleteResourceResponse(policyId,
label,
resourceKey,
CommandResponseHttpStatusValidator.validateHttpStatus(httpStatus,
Collections.singleton(HTTP_STATUS),
DeleteResourceResponse.class),
dittoHeaders);
}

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

@Override
public DeleteResourceResponse setDittoHeaders(final DittoHeaders dittoHeaders) {
return of(policyId, label, resourceKey, dittoHeaders);
return newInstance(policyId, label, resourceKey, getHttpStatus(), dittoHeaders);
}

@Override
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.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
Expand Down Expand Up @@ -61,10 +63,9 @@ public final class DeleteSubjectResponse extends AbstractCommandResponse<DeleteS

private static final CommandResponseJsonDeserializer<DeleteSubjectResponse> JSON_DESERIALIZER =
CommandResponseJsonDeserializer.newInstance(TYPE,
HTTP_STATUS,
context -> {
final JsonObject jsonObject = context.getJsonObject();
return new DeleteSubjectResponse(
return newInstance(
PolicyId.of(jsonObject.getValueOrThrow(PolicyCommandResponse.JsonFields.JSON_POLICY_ID)),
PoliciesModelFactory.newLabel(jsonObject.getValueOrThrow(JSON_LABEL)),
PoliciesModelFactory.newSubjectId(jsonObject.getValueOrThrow(JSON_SUBJECT_ID)),
Expand Down Expand Up @@ -104,7 +105,35 @@ public static DeleteSubjectResponse of(final PolicyId policyId,
final SubjectId subjectId,
final DittoHeaders dittoHeaders) {

return new DeleteSubjectResponse(policyId, label, subjectId, HTTP_STATUS, dittoHeaders);
return newInstance(policyId, label, subjectId, HTTP_STATUS, dittoHeaders);
}

/**
* Returns a new instance of {@code DeleteSubjectResponse} for the specified arguments.
*
* @param policyId the Policy ID of the deleted subject.
* @param label the Label of the PolicyEntry.
* @param subjectId the identifier of the deleted Subject.
* @param httpStatus the status of the response.
* @param dittoHeaders the headers of the response.
* @return the {@code DeleteSubjectResponse} instance.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code httpStatus} is not allowed for a {@code DeleteSubjectResponse}.
* @since 2.3.0
*/
public static DeleteSubjectResponse newInstance(final PolicyId policyId,
final Label label,
final SubjectId subjectId,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders) {

return new DeleteSubjectResponse(policyId,
label,
subjectId,
CommandResponseHttpStatusValidator.validateHttpStatus(httpStatus,
Collections.singleton(HTTP_STATUS),
DeleteSubjectResponse.class),
dittoHeaders);
}

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

@Override
public DeleteSubjectResponse setDittoHeaders(final DittoHeaders dittoHeaders) {
return of(policyId, label, subjectId, dittoHeaders);
return newInstance(policyId, label, subjectId, getHttpStatus(), dittoHeaders);
}

@Override
Expand Down

0 comments on commit fe05d4e

Please sign in to comment.