Skip to content

Commit

Permalink
Fix API-breaking changes by adding deprecated methods for old behavior
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Fendt <Florian.Fendt@bosch.io>
  • Loading branch information
ffendt committed Mar 19, 2020
1 parent 1fdd858 commit 3b90cad
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 44 deletions.
7 changes: 0 additions & 7 deletions signals/commands/policies/pom.xml
Expand Up @@ -73,13 +73,6 @@
<excludes>
<!-- Don't add excludes here before checking with the whole Ditto team -->
<!--<exclude></exclude>-->
<!-- TODO temp. exclude - try to stay compatible later -->
<exclude>org.eclipse.ditto.signals.commands.policies.modify.ModifySubjectResponse#modified(org.eclipse.ditto.model.policies.PolicyId,org.eclipse.ditto.model.policies.Label,org.eclipse.ditto.model.base.headers.DittoHeaders)</exclude>
<exclude>org.eclipse.ditto.signals.commands.policies.modify.ModifyPolicyEntryResponse#modified(org.eclipse.ditto.model.policies.PolicyId,org.eclipse.ditto.model.base.headers.DittoHeaders)</exclude>
<exclude>org.eclipse.ditto.signals.commands.policies.modify.ModifyResourceResponse#modified(org.eclipse.ditto.model.policies.PolicyId,org.eclipse.ditto.model.policies.Label,org.eclipse.ditto.model.base.headers.DittoHeaders)</exclude>
<exclude>org.eclipse.ditto.signals.commands.policies.modify.ModifyPolicyEntryResponse#modified(java.lang.String,org.eclipse.ditto.model.base.headers.DittoHeaders)</exclude>
<exclude>org.eclipse.ditto.signals.commands.policies.modify.ModifyResourceResponse#modified(java.lang.String,org.eclipse.ditto.model.policies.Label,org.eclipse.ditto.model.base.headers.DittoHeaders)</exclude>
<exclude>org.eclipse.ditto.signals.commands.policies.modify.ModifySubjectResponse#modified(java.lang.String,org.eclipse.ditto.model.policies.Label,org.eclipse.ditto.model.base.headers.DittoHeaders)</exclude>
</excludes>
</parameter>
</configuration>
Expand Down
Expand Up @@ -39,6 +39,7 @@
import org.eclipse.ditto.model.policies.PolicyId;
import org.eclipse.ditto.signals.commands.base.AbstractCommandResponse;
import org.eclipse.ditto.signals.commands.base.CommandResponseJsonDeserializer;
import org.eclipse.ditto.signals.commands.policies.PolicyCommandResponse;

/**
* Response to a {@link ModifyPolicyEntry} command.
Expand All @@ -60,20 +61,19 @@ public final class ModifyPolicyEntryResponse extends AbstractCommandResponse<Mod
JsonFactory.newJsonValueFieldDefinition("policyEntry", FieldType.REGULAR, JsonSchemaVersion.V_2);

private final PolicyId policyId;
private final Label label;
@Nullable private final Label label;
@Nullable private final PolicyEntry policyEntryCreated;

private ModifyPolicyEntryResponse(final PolicyId policyId,
final HttpStatusCode statusCode,
@Nullable final PolicyEntry policyEntryCreated,
final Label label,
@Nullable final Label label,
final DittoHeaders dittoHeaders) {

super(TYPE, statusCode, dittoHeaders);
this.policyId = checkNotNull(policyId, "Policy ID");
this.policyEntryCreated = policyEntryCreated;
this.label = checkNotNull(label, "label");
;
this.label = label;
}

/**
Expand Down Expand Up @@ -125,6 +125,37 @@ public static ModifyPolicyEntryResponse created(final PolicyId policyId, final P
* instead.
*/
@Deprecated
public static ModifyPolicyEntryResponse modified(final String policyId, final DittoHeaders dittoHeaders) {
return modified(PolicyId.of(policyId), null, dittoHeaders);
}

/**
* Creates a response to a {@code ModifyPolicyEntry} command.
*
* @param policyId the Policy ID of the modified policy entry.
* @param dittoHeaders the headers of the preceding command.
* @return the response.
* @throws NullPointerException if any argument is {@code null}.
* @deprecated since 1.1.0, use {@link #modified(PolicyId, Label, DittoHeaders)} instead.
*/
@Deprecated
public static ModifyPolicyEntryResponse modified(final PolicyId policyId, final DittoHeaders dittoHeaders) {
return modified(policyId, null, dittoHeaders);
}
/**
* Creates a response to a {@code ModifyPolicyEntry} command.
*
* @param policyId the Policy ID of the modified policy entry.
* @param label the label of the modified policy entry.
* @param dittoHeaders the headers of the preceding command.
* @return the response.
* @throws NullPointerException if any argument is {@code null}.
* @deprecated Policy Id is now Typed. Use
* {@link #modified(org.eclipse.ditto.model.policies.PolicyId, org.eclipse.ditto.model.policies.Label,
* org.eclipse.ditto.model.base.headers.DittoHeaders)}
* instead.
*/
@Deprecated
public static ModifyPolicyEntryResponse modified(final String policyId, final Label label,
final DittoHeaders dittoHeaders) {
return modified(PolicyId.of(policyId), label, dittoHeaders);
Expand All @@ -134,6 +165,7 @@ public static ModifyPolicyEntryResponse modified(final String policyId, final La
* Creates a response to a {@code ModifyPolicyEntry} command.
*
* @param policyId the Policy ID of the modified policy entry.
* @param label the label of the modified policy entry.
* @param dittoHeaders the headers of the preceding command.
* @return the response.
* @throws NullPointerException if any argument is {@code null}.
Expand Down Expand Up @@ -170,19 +202,20 @@ public static ModifyPolicyEntryResponse fromJson(final String jsonString, final
*/
public static ModifyPolicyEntryResponse fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) {
return new CommandResponseJsonDeserializer<ModifyPolicyEntryResponse>(TYPE, jsonObject)
.deserialize((statusCode) -> {
.deserialize(statusCode -> {
final String extractedPolicyId =
jsonObject.getValueOrThrow(PolicyModifyCommandResponse.JsonFields.JSON_POLICY_ID);
jsonObject.getValueOrThrow(PolicyCommandResponse.JsonFields.JSON_POLICY_ID);
final PolicyId policyId = PolicyId.of(extractedPolicyId);
final Label readLabel = Label.of(jsonObject.getValueOrThrow(JSON_LABEL));
final Optional<String> readLabel = jsonObject.getValue(JSON_LABEL);

final PolicyEntry extractedPolicyEntryCreated = jsonObject.getValue(JSON_POLICY_ENTRY)
.filter(JsonValue::isObject)
.map(JsonValue::asObject)
.map(obj -> PoliciesModelFactory.newPolicyEntry(readLabel, obj))
.flatMap(obj -> readLabel.map(label -> PoliciesModelFactory.newPolicyEntry(label, obj)))
.orElse(null);

return new ModifyPolicyEntryResponse(policyId, statusCode, extractedPolicyEntryCreated, readLabel,
return new ModifyPolicyEntryResponse(policyId, statusCode, extractedPolicyEntryCreated,
readLabel.map(Label::of).orElse(null),
dittoHeaders);
});
}
Expand All @@ -208,14 +241,9 @@ public Optional<JsonValue> getEntity(final JsonSchemaVersion schemaVersion) {

@Override
public JsonPointer getResourcePath() {

final Label label;
if (policyEntryCreated == null) {
label = this.label;
} else {
label = policyEntryCreated.getLabel();
if (null == label) {
return JsonPointer.empty();
}

final String path = "/entries/" + label;
return JsonPointer.of(path);
}
Expand All @@ -225,9 +253,11 @@ protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final Js
final Predicate<JsonField> thePredicate) {

final Predicate<JsonField> predicate = schemaVersion.and(thePredicate);
jsonObjectBuilder.set(PolicyModifyCommandResponse.JsonFields.JSON_POLICY_ID, String.valueOf(policyId),
jsonObjectBuilder.set(PolicyCommandResponse.JsonFields.JSON_POLICY_ID, String.valueOf(policyId),
predicate);
jsonObjectBuilder.set(JSON_LABEL, label.toString(), predicate);
if (null != label) {
jsonObjectBuilder.set(JSON_LABEL, label.toString(), predicate);
}
if (null != policyEntryCreated) {
jsonObjectBuilder.set(JSON_POLICY_ENTRY, policyEntryCreated.toJson(schemaVersion, thePredicate), predicate);
}
Expand Down
Expand Up @@ -40,6 +40,7 @@
import org.eclipse.ditto.model.policies.ResourceKey;
import org.eclipse.ditto.signals.commands.base.AbstractCommandResponse;
import org.eclipse.ditto.signals.commands.base.CommandResponseJsonDeserializer;
import org.eclipse.ditto.signals.commands.policies.PolicyCommandResponse;

/**
* Response to a {@link ModifyResource} command.
Expand All @@ -65,20 +66,20 @@ public final class ModifyResourceResponse extends AbstractCommandResponse<Modify

private final PolicyId policyId;
private final Label label;
private final ResourceKey resourceKey;
@Nullable private final ResourceKey resourceKey;
@Nullable private final Resource resourceCreated;

private ModifyResourceResponse(final PolicyId policyId,
final Label label,
final ResourceKey resourceKey,
@Nullable final ResourceKey resourceKey,
@Nullable final Resource resourceCreated,
final HttpStatusCode statusCode,
final DittoHeaders dittoHeaders) {

super(TYPE, statusCode, dittoHeaders);
this.policyId = checkNotNull(policyId, "Policy ID");
this.label = checkNotNull(label, "Label");
this.resourceKey = checkNotNull(resourceKey, "resourceKey");
this.resourceKey = resourceKey;
this.resourceCreated = resourceCreated;
}

Expand Down Expand Up @@ -132,6 +133,38 @@ public static ModifyResourceResponse created(final PolicyId policyId,
* @return the response.
* @throws NullPointerException if any argument is {@code null}.
* @deprecated Policy ID is now typed. Use
* {@link #modified(org.eclipse.ditto.model.policies.PolicyId, org.eclipse.ditto.model.policies.Label, org.eclipse.ditto.model.policies.ResourceKey, org.eclipse.ditto.model.base.headers.DittoHeaders)}
* instead.
*/
@Deprecated
public static ModifyResourceResponse modified(final String policyId, final Label label, final DittoHeaders dittoHeaders) {
return modified(PolicyId.of(policyId), label, null, dittoHeaders);
}

/**
* Creates a response to a {@code ModifyResource} command.
*
* @param policyId the Policy ID of the modified resource.
* @param label the Label of the PolicyEntry.
* @param dittoHeaders the headers of the preceding command.
* @return the response.
* @throws NullPointerException if any argument is {@code null}.
* @deprecated since 1.1.0, use {@link #modified(PolicyId, Label, ResourceKey, DittoHeaders)} instead.
*/
@Deprecated
public static ModifyResourceResponse modified(final PolicyId policyId, final Label label, final DittoHeaders dittoHeaders) {
return modified(PolicyId.of(policyId), label, null, dittoHeaders);
}
/**
* Creates a response to a {@code ModifyResource} command.
*
* @param policyId the Policy ID of the modified resource.
* @param label the Label of the PolicyEntry.
* @param resourceKey the resource key of the modified resource
* @param dittoHeaders the headers of the preceding command.
* @return the response.
* @throws NullPointerException if any argument is {@code null}.
* @deprecated Policy ID is now typed. Use
*
*
*
Expand Down Expand Up @@ -189,21 +222,22 @@ public static ModifyResourceResponse fromJson(final String jsonString, final Dit
public static ModifyResourceResponse fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) {
return new CommandResponseJsonDeserializer<ModifyResourceResponse>(TYPE, jsonObject).deserialize(statusCode -> {
final String extractedPolicyId =
jsonObject.getValueOrThrow(PolicyModifyCommandResponse.JsonFields.JSON_POLICY_ID);
jsonObject.getValueOrThrow(PolicyCommandResponse.JsonFields.JSON_POLICY_ID);
final PolicyId policyId = PolicyId.of(extractedPolicyId);

final String stringLabel = jsonObject.getValueOrThrow(JSON_LABEL);
final Label label = PoliciesModelFactory.newLabel(stringLabel);

final ResourceKey extractedResourceKey =
ResourceKey.newInstance(jsonObject.getValueOrThrow(JSON_RESOURCE_KEY));
final Optional<ResourceKey> extractedResourceKey = jsonObject.getValue(JSON_RESOURCE_KEY)
.map(ResourceKey::newInstance);

final Resource extractedResourceCreated = jsonObject.getValue(JSON_RESOURCE)
.map(JsonValue::asObject)
.map(obj -> PoliciesModelFactory.newResource(extractedResourceKey, obj))
.flatMap(obj -> extractedResourceKey.map(resourceKey -> PoliciesModelFactory.newResource(resourceKey, obj)))
.orElse(null);

return new ModifyResourceResponse(policyId, label, extractedResourceKey, extractedResourceCreated,
return new ModifyResourceResponse(policyId, label,
extractedResourceKey.map(ResourceKey::newInstance).orElse(null), extractedResourceCreated,
statusCode, dittoHeaders);
});
}
Expand Down Expand Up @@ -247,10 +281,12 @@ protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final Js
final Predicate<JsonField> thePredicate) {

final Predicate<JsonField> predicate = schemaVersion.and(thePredicate);
jsonObjectBuilder.set(PolicyModifyCommandResponse.JsonFields.JSON_POLICY_ID, String.valueOf(policyId),
jsonObjectBuilder.set(PolicyCommandResponse.JsonFields.JSON_POLICY_ID, String.valueOf(policyId),
predicate);
jsonObjectBuilder.set(JSON_LABEL, label.toString(), predicate);
jsonObjectBuilder.set(JSON_RESOURCE_KEY, resourceKey.toString(), predicate);
if (null != resourceKey) {
jsonObjectBuilder.set(JSON_RESOURCE_KEY, resourceKey.toString(), predicate);
}
if (null != resourceCreated) {
jsonObjectBuilder.set(JSON_RESOURCE, resourceCreated.toJson(schemaVersion, thePredicate), predicate);
}
Expand Down

0 comments on commit 3b90cad

Please sign in to comment.