Skip to content

Commit

Permalink
[#1228]: Mainly code formatting and adjustment of one test case.
Browse files Browse the repository at this point in the history
Signed-off-by: Juergen Fickel <juergen.fickel@bosch.io>
  • Loading branch information
Juergen Fickel committed Dec 21, 2021
1 parent 1138198 commit f8ffff8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eclipse.ditto.base.model.headers.entitytag.EntityTagMatchers;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.internal.models.signal.SignalInformationPoint;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
Expand Down Expand Up @@ -392,23 +393,20 @@ public void enforceConditionAndLiveChannelCondition() {
@Test
public void enforceLiveChannelConditionOnModifyCommandFails() {
final PolicyId policyId = PolicyId.of("policy:id");
final JsonObject thing = newThingWithAttributeWithPolicyId(policyId)
.toBuilder()
.build();

final JsonObject policy = PoliciesModelFactory.newPolicyBuilder(policyId)
final Policy policy = PoliciesModelFactory.newPolicyBuilder(policyId)
.setRevision(1L)
.forLabel("authorize-self")
.setSubject(GOOGLE, TestSetup.SUBJECT_ID)
.setGrantedPermissions(PoliciesResourceType.thingResource(JsonPointer.empty()),
Permissions.newInstance(Permission.READ, Permission.WRITE))
.build()
.toJson(FieldType.all());
.build();
final JsonObject policyJsonObject = policy.toJson(FieldType.all());

final SudoRetrieveThingResponse sudoRetrieveThingResponse =
SudoRetrieveThingResponse.of(thing, DittoHeaders.empty());
SudoRetrieveThingResponse.of(newThingWithAttributeWithPolicyId(policyId), DittoHeaders.empty());
final SudoRetrievePolicyResponse sudoRetrievePolicyResponse =
SudoRetrievePolicyResponse.of(policyId, policy, DittoHeaders.empty());
SudoRetrievePolicyResponse.of(policyId, policyJsonObject, DittoHeaders.empty());

new TestKit(system) {{
mockEntitiesActorInstance.setReply(TestSetup.THING_SUDO, sudoRetrieveThingResponse);
Expand All @@ -417,18 +415,20 @@ public void enforceLiveChannelConditionOnModifyCommandFails() {
final ActorRef underTest = newEnforcerActor(getRef());

// WHEN: Live channel condition is set on an unreadable feature
final DittoHeaders dittoHeaders = headers().toBuilder()
final DittoHeaders dittoHeadersWithLiveChannelCondition = DittoHeaders.newBuilder(headers())
.liveChannelCondition("exists(thingId)")
.build();

final ThingCommand<?> modifyCommand = getModifyCommand(dittoHeaders);
final ThingCommand<?> modifyCommand = getModifyCommand(dittoHeadersWithLiveChannelCondition);
mockEntitiesActorInstance.setReply(modifyCommand);
underTest.tell(modifyCommand, getRef());

// THEN: The command is rejected
final DittoRuntimeException response = TestSetup.fishForMsgClass(this, DittoRuntimeException.class);
assertThat(response.getErrorCode()).isEqualTo(LiveChannelConditionNotAllowedException.ERROR_CODE);
assertThat(response.getHttpStatus()).isEqualTo(HttpStatus.METHOD_NOT_ALLOWED);
final LiveChannelConditionNotAllowedException response =
TestSetup.fishForMsgClass(this, LiveChannelConditionNotAllowedException.class);

assertThat(SignalInformationPoint.getCorrelationId(response))
.isEqualTo(SignalInformationPoint.getCorrelationId(modifyCommand));
}};
}

Expand Down Expand Up @@ -869,9 +869,9 @@ private ActorRef newEnforcerActor(final ActorRef testActorRef, final ActorRef co

private DittoHeaders headers() {
return DittoHeaders.newBuilder()
.authorizationContext(
AuthorizationContext.newInstance(DittoAuthorizationContextType.UNSPECIFIED, TestSetup.SUBJECT,
AuthorizationSubject.newInstance(String.format("%s:%s", GOOGLE, TestSetup.SUBJECT_ID))))
.authorizationContext(AuthorizationContext.newInstance(DittoAuthorizationContextType.UNSPECIFIED,
TestSetup.SUBJECT,
AuthorizationSubject.newInstance(String.format("%s:%s", GOOGLE, TestSetup.SUBJECT_ID))))
.correlationId(testName.getMethodName())
.schemaVersion(JsonSchemaVersion.V_2)
.build();
Expand Down Expand Up @@ -901,7 +901,7 @@ private ThingCommand<?> getModifyCommand() {
return getModifyCommand(headers());
}

private ThingCommand<?> getModifyCommand(final DittoHeaders dittoHeaders) {
private static ThingCommand<?> getModifyCommand(final DittoHeaders dittoHeaders) {
return ModifyFeature.of(TestSetup.THING_ID, Feature.newBuilder().withId("x").build(), dittoHeaders);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,12 @@ public static Optional<EntityId> getEntityId(@Nullable final Signal<?> signal) {
}

/**
* Returns the optional correlation ID of the specified {@code Signal} argument's headers.
* Returns the optional correlation ID of the specified argument's headers.
*
* @param signal the signal to get the optional correlation ID from.
* @return the optional correlation ID. The optional is empty if {@code signal} is {@code null}.
*/
public static Optional<String> getCorrelationId(@Nullable final Signal<?> signal) {
public static Optional<String> getCorrelationId(@Nullable final WithDittoHeaders signal) {
final Optional<String> result;
if (null != signal) {
final var signalDittoHeaders = signal.getDittoHeaders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private LiveChannelConditionNotAllowedException(final DittoHeaders dittoHeaders,
@Nullable final String description,
@Nullable final Throwable cause,
@Nullable final URI href) {

super(ERROR_CODE, HttpStatus.METHOD_NOT_ALLOWED, dittoHeaders, message, description, cause, href);
}

Expand All @@ -79,6 +80,7 @@ public static Builder newBuilder() {
*/
public static LiveChannelConditionNotAllowedException fromJson(final JsonObject jsonObject,
final DittoHeaders dittoHeaders) {

return DittoRuntimeException.fromJson(jsonObject, dittoHeaders, new Builder());
}

Expand All @@ -97,8 +99,7 @@ public DittoRuntimeException setDittoHeaders(final DittoHeaders dittoHeaders) {
* A mutable builder with a fluent API for a {@link LiveChannelConditionNotAllowedException}.
*/
@NotThreadSafe
public static final class Builder
extends DittoRuntimeExceptionBuilder<LiveChannelConditionNotAllowedException> {
public static final class Builder extends DittoRuntimeExceptionBuilder<LiveChannelConditionNotAllowedException> {

private Builder() {
this(DEFAULT_DESCRIPTION);
Expand All @@ -117,8 +118,10 @@ protected LiveChannelConditionNotAllowedException doBuild(final DittoHeaders dit
@Nullable final String description,
@Nullable final Throwable cause,
@Nullable final URI href) {

return new LiveChannelConditionNotAllowedException(dittoHeaders, message, description, cause, href);
}

}

}

0 comments on commit f8ffff8

Please sign in to comment.