Skip to content

Commit

Permalink
[eclipse-ditto#926] Remove unnecessary field subjectId from Activate-…
Browse files Browse the repository at this point in the history
… and DeactivatePolicyTokenIntegrationResponse.

Signed-off-by: Yufei Cai <yufei.cai@bosch.io>
  • Loading branch information
yufei-cai committed Jan 12, 2021
1 parent 37b75c9 commit b7ce02b
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 70 deletions.
Expand Up @@ -105,7 +105,7 @@ protected Result<PolicyEvent<?>> doApply(final Context<PolicyId> context,
SubjectsModifiedPartially.of(policyId, activatedSubjects, nextRevision, getEventTimestamp(),
dittoHeaders);
final ActivatePolicyTokenIntegrationResponse rawResponse =
ActivatePolicyTokenIntegrationResponse.of(policyId, command.getSubjectId(), dittoHeaders);
ActivatePolicyTokenIntegrationResponse.of(policyId, dittoHeaders);
// do not append ETag - activated subjects do not support ETags.
return ResultFactory.newMutationResult(command, event, rawResponse);
} else {
Expand Down
Expand Up @@ -98,7 +98,7 @@ protected Result<PolicyEvent<?>> doApply(final Context<PolicyId> context,
SubjectsDeletedPartially.of(policyId, deactivatedSubjectsIds, nextRevision, getEventTimestamp(),
dittoHeaders);
final DeactivatePolicyTokenIntegrationResponse rawResponse =
DeactivatePolicyTokenIntegrationResponse.of(policyId, command.getSubjectId(), dittoHeaders);
DeactivatePolicyTokenIntegrationResponse.of(policyId, dittoHeaders);
return ResultFactory.newMutationResult(command, event, rawResponse);
}

Expand Down
Expand Up @@ -82,7 +82,7 @@ public void activateTokenIntegration() {
ActivatePolicyTokenIntegrationResponse.class,
response -> assertThat(response)
.isEqualTo(
ActivatePolicyTokenIntegrationResponse.of(context.getState(), subjectId, dittoHeaders)));
ActivatePolicyTokenIntegrationResponse.of(context.getState(), dittoHeaders)));
}

@Test
Expand Down
Expand Up @@ -91,7 +91,7 @@ public void deactivateTokenIntegration() {
DeactivatePolicyTokenIntegrationResponse.class,
response -> assertThat(response)
.isEqualTo(
DeactivatePolicyTokenIntegrationResponse.of(context.getState(), subjectId, dittoHeaders)));
DeactivatePolicyTokenIntegrationResponse.of(context.getState(), dittoHeaders)));
}

@Test
Expand All @@ -108,7 +108,7 @@ public void deactivateNonexistentSubject() {
DeactivatePolicyTokenIntegrationResponse.class,
response -> assertThat(response)
.isEqualTo(
DeactivatePolicyTokenIntegrationResponse.of(context.getState(), subjectId, dittoHeaders)));
DeactivatePolicyTokenIntegrationResponse.of(context.getState(), dittoHeaders)));
}

@Test
Expand Down
Expand Up @@ -20,19 +20,15 @@
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.model.base.common.HttpStatusCode;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.json.FieldType;
import org.eclipse.ditto.model.base.json.JsonParsableCommandResponse;
import org.eclipse.ditto.model.base.json.JsonSchemaVersion;
import org.eclipse.ditto.model.policies.PolicyId;
import org.eclipse.ditto.model.policies.SubjectId;
import org.eclipse.ditto.signals.commands.base.AbstractCommandResponse;
import org.eclipse.ditto.signals.commands.policies.PolicyCommandResponse;

Expand All @@ -57,33 +53,23 @@ public final class ActivatePolicyTokenIntegrationResponse
*/
public static final HttpStatusCode STATUS = HttpStatusCode.OK;

static final JsonFieldDefinition<String> JSON_SUBJECT_ID =
JsonFactory.newStringFieldDefinition("subjectId", FieldType.REGULAR, JsonSchemaVersion.V_2);

private final PolicyId policyId;
private final SubjectId subjectId;

private ActivatePolicyTokenIntegrationResponse(final PolicyId policyId, final SubjectId subjectId,
final DittoHeaders dittoHeaders) {

private ActivatePolicyTokenIntegrationResponse(final PolicyId policyId, final DittoHeaders dittoHeaders) {
super(TYPE, STATUS, dittoHeaders);
this.policyId = checkNotNull(policyId, "policyId");
this.subjectId = checkNotNull(subjectId, "subjectId");
}

/**
* Creates a response to an {@code ActivatePolicyTokenIntegration} command.
*
* @param policyId the policy ID.
* @param subjectId the added subject ID.
* @param dittoHeaders the headers of the preceding command.
* @return the response.
* @throws NullPointerException if any argument is {@code null}.
*/
public static ActivatePolicyTokenIntegrationResponse of(final PolicyId policyId,
final SubjectId subjectId,
final DittoHeaders dittoHeaders) {
return new ActivatePolicyTokenIntegrationResponse(policyId, subjectId, dittoHeaders);
public static ActivatePolicyTokenIntegrationResponse of(final PolicyId policyId, final DittoHeaders dittoHeaders) {
return new ActivatePolicyTokenIntegrationResponse(policyId, dittoHeaders);
}

/**
Expand All @@ -101,8 +87,7 @@ public static ActivatePolicyTokenIntegrationResponse fromJson(final JsonObject j
final DittoHeaders dittoHeaders) {
final PolicyId policyId =
PolicyId.of(jsonObject.getValueOrThrow(PolicyCommandResponse.JsonFields.JSON_POLICY_ID));
final SubjectId subjectId = SubjectId.newInstance(jsonObject.getValueOrThrow(JSON_SUBJECT_ID));
return new ActivatePolicyTokenIntegrationResponse(policyId, subjectId, dittoHeaders);
return new ActivatePolicyTokenIntegrationResponse(policyId, dittoHeaders);
}

@Override
Expand All @@ -121,12 +106,11 @@ protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final Js

final Predicate<JsonField> predicate = schemaVersion.and(thePredicate);
jsonObjectBuilder.set(PolicyCommandResponse.JsonFields.JSON_POLICY_ID, policyId.toString(), predicate);
jsonObjectBuilder.set(JSON_SUBJECT_ID, subjectId.toString(), predicate);
}

@Override
public ActivatePolicyTokenIntegrationResponse setDittoHeaders(final DittoHeaders dittoHeaders) {
return new ActivatePolicyTokenIntegrationResponse(policyId, subjectId, dittoHeaders);
return new ActivatePolicyTokenIntegrationResponse(policyId, dittoHeaders);
}

@Override
Expand All @@ -144,21 +128,19 @@ public boolean equals(@Nullable final Object o) {
}
final ActivatePolicyTokenIntegrationResponse that = (ActivatePolicyTokenIntegrationResponse) o;
return Objects.equals(policyId, that.policyId) &&
Objects.equals(subjectId, that.subjectId) &&
super.equals(o);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), policyId, subjectId);
return Objects.hash(super.hashCode(), policyId);
}

@Override
public String toString() {
return getClass().getSimpleName() +
" [" + super.toString() +
", policyId=" + policyId +
", subjectId=" + subjectId +
"]";
}

Expand Down
Expand Up @@ -64,7 +64,6 @@ public final class ActivateTokenIntegrationResponse
static final JsonFieldDefinition<String> JSON_SUBJECT_ID =
JsonFactory.newStringFieldDefinition("subjectId", FieldType.REGULAR, JsonSchemaVersion.V_2);

// TODO: determine the content of the response.
private final PolicyId policyId;
private final Label label;
private final SubjectId subjectId;
Expand Down
Expand Up @@ -20,19 +20,15 @@
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.model.base.common.HttpStatusCode;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.json.FieldType;
import org.eclipse.ditto.model.base.json.JsonParsableCommandResponse;
import org.eclipse.ditto.model.base.json.JsonSchemaVersion;
import org.eclipse.ditto.model.policies.PolicyId;
import org.eclipse.ditto.model.policies.SubjectId;
import org.eclipse.ditto.signals.commands.base.AbstractCommandResponse;
import org.eclipse.ditto.signals.commands.policies.PolicyCommandResponse;

Expand All @@ -57,33 +53,26 @@ public final class DeactivatePolicyTokenIntegrationResponse
*/
public static final HttpStatusCode STATUS = HttpStatusCode.OK;

static final JsonFieldDefinition<String> JSON_SUBJECT_ID =
JsonFactory.newStringFieldDefinition("subjectId", FieldType.REGULAR, JsonSchemaVersion.V_2);

private final PolicyId policyId;
private final SubjectId subjectId;

private DeactivatePolicyTokenIntegrationResponse(final PolicyId policyId, final SubjectId subjectId,
private DeactivatePolicyTokenIntegrationResponse(final PolicyId policyId,
final DittoHeaders dittoHeaders) {

super(TYPE, STATUS, dittoHeaders);
this.policyId = checkNotNull(policyId, "policyId");
this.subjectId = checkNotNull(subjectId, "subjectId");
}

/**
* Creates a response to an {@code DeactivatePolicyTokenIntegration} command.
*
* @param policyId the policy ID.
* @param subjectId the added subject ID.
* @param dittoHeaders the headers of the preceding command.
* @return the response.
* @throws NullPointerException if any argument is {@code null}.
*/
public static DeactivatePolicyTokenIntegrationResponse of(final PolicyId policyId,
final SubjectId subjectId,
final DittoHeaders dittoHeaders) {
return new DeactivatePolicyTokenIntegrationResponse(policyId, subjectId, dittoHeaders);
return new DeactivatePolicyTokenIntegrationResponse(policyId, dittoHeaders);
}

/**
Expand All @@ -101,8 +90,7 @@ public static DeactivatePolicyTokenIntegrationResponse fromJson(final JsonObject
final DittoHeaders dittoHeaders) {
final PolicyId policyId =
PolicyId.of(jsonObject.getValueOrThrow(PolicyCommandResponse.JsonFields.JSON_POLICY_ID));
final SubjectId subjectId = SubjectId.newInstance(jsonObject.getValueOrThrow(JSON_SUBJECT_ID));
return new DeactivatePolicyTokenIntegrationResponse(policyId, subjectId, dittoHeaders);
return new DeactivatePolicyTokenIntegrationResponse(policyId, dittoHeaders);
}

@Override
Expand All @@ -121,12 +109,11 @@ protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final Js

final Predicate<JsonField> predicate = schemaVersion.and(thePredicate);
jsonObjectBuilder.set(PolicyCommandResponse.JsonFields.JSON_POLICY_ID, policyId.toString(), predicate);
jsonObjectBuilder.set(JSON_SUBJECT_ID, subjectId.toString(), predicate);
}

@Override
public DeactivatePolicyTokenIntegrationResponse setDittoHeaders(final DittoHeaders dittoHeaders) {
return new DeactivatePolicyTokenIntegrationResponse(policyId, subjectId, dittoHeaders);
return new DeactivatePolicyTokenIntegrationResponse(policyId, dittoHeaders);
}

@Override
Expand All @@ -144,21 +131,19 @@ public boolean equals(@Nullable final Object o) {
}
final DeactivatePolicyTokenIntegrationResponse that = (DeactivatePolicyTokenIntegrationResponse) o;
return Objects.equals(policyId, that.policyId) &&
Objects.equals(subjectId, that.subjectId) &&
super.equals(o);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), policyId, subjectId);
return Objects.hash(super.hashCode(), policyId);
}

@Override
public String toString() {
return getClass().getSimpleName() +
" [" + super.toString() +
", policyId=" + policyId +
", subjectId=" + subjectId +
"]";
}

Expand Down
Expand Up @@ -37,7 +37,6 @@ public final class ActivatePolicyTokenIntegrationResponseTest {
.set(PolicyCommandResponse.JsonFields.TYPE, ActivatePolicyTokenIntegrationResponse.TYPE)
.set(PolicyCommandResponse.JsonFields.STATUS, ActivatePolicyTokenIntegrationResponse.STATUS.toInt())
.set(PolicyCommandResponse.JsonFields.JSON_POLICY_ID, TestConstants.Policy.POLICY_ID.toString())
.set(ActivatePolicyTokenIntegrationResponse.JSON_SUBJECT_ID, TestConstants.Policy.SUBJECT_ID.toString())
.build();

@Test
Expand All @@ -56,19 +55,13 @@ public void testHashCodeAndEquals() {

@Test(expected = NullPointerException.class)
public void tryToCreateInstanceWithNullPolicyId() {
ActivatePolicyTokenIntegrationResponse.of(null, TestConstants.Policy.SUBJECT_ID, TestConstants.EMPTY_DITTO_HEADERS);
}


@Test(expected = NullPointerException.class)
public void tryToCreateInstanceWithNullSubject() {
ActivatePolicyTokenIntegrationResponse.of(TestConstants.Policy.POLICY_ID, null, TestConstants.EMPTY_DITTO_HEADERS);
ActivatePolicyTokenIntegrationResponse.of(null, TestConstants.EMPTY_DITTO_HEADERS);
}

@Test
public void toJsonReturnsExpected() {
final ActivatePolicyTokenIntegrationResponse underTest =
ActivatePolicyTokenIntegrationResponse.of(TestConstants.Policy.POLICY_ID, TestConstants.Policy.SUBJECT_ID,
ActivatePolicyTokenIntegrationResponse.of(TestConstants.Policy.POLICY_ID,
TestConstants.EMPTY_DITTO_HEADERS);
final JsonObject actualJson = underTest.toJson(FieldType.regularOrSpecial());

Expand All @@ -81,7 +74,7 @@ public void createInstanceFromValidJson() {
ActivatePolicyTokenIntegrationResponse.fromJson(KNOWN_JSON, TestConstants.EMPTY_DITTO_HEADERS);

final ActivatePolicyTokenIntegrationResponse expectedCommand =
ActivatePolicyTokenIntegrationResponse.of(TestConstants.Policy.POLICY_ID, TestConstants.Policy.SUBJECT_ID,
ActivatePolicyTokenIntegrationResponse.of(TestConstants.Policy.POLICY_ID,
TestConstants.EMPTY_DITTO_HEADERS);
assertThat(underTest).isEqualTo(expectedCommand);
}
Expand Down
Expand Up @@ -37,7 +37,6 @@ public final class DeactivatePolicyTokenIntegrationResponseTest {
.set(PolicyCommandResponse.JsonFields.TYPE, DeactivatePolicyTokenIntegrationResponse.TYPE)
.set(PolicyCommandResponse.JsonFields.STATUS, DeactivatePolicyTokenIntegrationResponse.STATUS.toInt())
.set(PolicyCommandResponse.JsonFields.JSON_POLICY_ID, TestConstants.Policy.POLICY_ID.toString())
.set(DeactivatePolicyTokenIntegrationResponse.JSON_SUBJECT_ID, TestConstants.Policy.SUBJECT_ID.toString())
.build();

@Test
Expand All @@ -56,20 +55,14 @@ public void testHashCodeAndEquals() {

@Test(expected = NullPointerException.class)
public void tryToCreateInstanceWithNullPolicyId() {
DeactivatePolicyTokenIntegrationResponse.of(null, TestConstants.Policy.SUBJECT_ID, TestConstants.EMPTY_DITTO_HEADERS);
}


@Test(expected = NullPointerException.class)
public void tryToCreateInstanceWithNullSubject() {
DeactivatePolicyTokenIntegrationResponse.of(TestConstants.Policy.POLICY_ID, null, TestConstants.EMPTY_DITTO_HEADERS);
DeactivatePolicyTokenIntegrationResponse.of(null, TestConstants.EMPTY_DITTO_HEADERS);
}

@Test
public void toJsonReturnsExpected() {
final DeactivatePolicyTokenIntegrationResponse underTest =
DeactivatePolicyTokenIntegrationResponse.of(TestConstants.Policy.POLICY_ID,
TestConstants.Policy.SUBJECT_ID, TestConstants.EMPTY_DITTO_HEADERS);
TestConstants.EMPTY_DITTO_HEADERS);
final JsonObject actualJson = underTest.toJson(FieldType.regularOrSpecial());

assertThat(actualJson).isEqualTo(KNOWN_JSON);
Expand All @@ -82,7 +75,7 @@ public void createInstanceFromValidJson() {

final DeactivatePolicyTokenIntegrationResponse expectedCommand =
DeactivatePolicyTokenIntegrationResponse.of(TestConstants.Policy.POLICY_ID,
TestConstants.Policy.SUBJECT_ID, TestConstants.EMPTY_DITTO_HEADERS);
TestConstants.EMPTY_DITTO_HEADERS);
assertThat(underTest).isEqualTo(expectedCommand);
}

Expand Down

0 comments on commit b7ce02b

Please sign in to comment.