ModifyThing extends AbstractCommand<ModifyThing> implements ThingModifyCommand<ModifyThing> { @Deprecated public static ModifyThing of(final String thingId, final Thing thing, @Nullable final JsonObject initialPolicy, final DittoHeaders dittoHeaders) { return of(ThingId.of(thingId), thing, initialPolicy, dittoHeaders); } private  ModifyThing(final ThingId thingId, final Thing thing, @Nullable final JsonObject initialPolicy,
            @Nullable final String policyIdOrPlaceholder, final DittoHeaders dittoHeaders); @Deprecated static ModifyThing of(final String thingId, final Thing thing, @Nullable final JsonObject initialPolicy,
            final DittoHeaders dittoHeaders); static ModifyThing of(final ThingId thingId, final Thing thing, @Nullable final JsonObject initialPolicy,
            final DittoHeaders dittoHeaders); @Deprecated static ModifyThing withCopiedPolicy(final String thingId, final Thing thing,
            final String policyIdOrPlaceholder, final DittoHeaders dittoHeaders); static ModifyThing withCopiedPolicy(final ThingId thingId, final Thing thing,
            final String policyIdOrPlaceholder, final DittoHeaders dittoHeaders); @Deprecated static ModifyThing of(final String thingId, final Thing thing, @Nullable final JsonObject initialPolicy,
            @Nullable final String policyIdOrPlaceholder, final DittoHeaders dittoHeaders); static ModifyThing of(final ThingId thingId, final Thing thing, @Nullable final JsonObject initialPolicy,
            @Nullable final String policyIdOrPlaceholder, final DittoHeaders dittoHeaders); static ModifyThing fromJson(final String jsonString, final DittoHeaders dittoHeaders); static ModifyThing fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders); Thing getThing(); @Override ThingId getThingEntityId(); Optional<JsonObject> getInitialPolicy(); Optional<String> getPolicyIdOrPlaceholder(); @Override JsonPointer getResourcePath(); @Override Optional<JsonValue> getEntity(final JsonSchemaVersion schemaVersion); @Override Category getCategory(); @Override ModifyThing setDittoHeaders(final DittoHeaders dittoHeaders); @Override boolean changesAuthorization(); @Override int hashCode(); @SuppressWarnings("squid:MethodCyclomaticComplexity") @Override boolean equals(@Nullable final Object obj); @Override String toString(); static final String NAME; static final String TYPE; static final JsonFieldDefinition<JsonObject> JSON_INITIAL_POLICY; static final JsonFieldDefinition<String> JSON_POLICY_ID_OR_PLACEHOLDER; static final JsonFieldDefinition<String> JSON_COPY_POLICY_FROM; static final JsonFieldDefinition<JsonObject> JSON_INLINE_POLICY; }
@Test(expected = NullPointerException.class) public void tryToCreateInstanceWithNullThing() { ModifyThing.of(TestConstants.Thing.THING_ID, null, null, TestConstants.EMPTY_DITTO_HEADERS); }
@Test public void toJsonReturnsExpected() { final ModifyThing underTest = ModifyThing.of(TestConstants.Thing.THING_ID, TestConstants.Thing.THING, null, TestConstants.EMPTY_DITTO_HEADERS); final JsonObject actualJson = underTest.toJson(FieldType.regularOrSpecial()); assertThat(actualJson).isEqualTo(KNOWN_JSON); }
@Test public void toJsonWithInitialPolicyReturnsExpected() { final JsonObject expected = KNOWN_JSON.set(ModifyThing.JSON_INITIAL_POLICY, JsonObject.newBuilder().build()); final ModifyThing underTest = ModifyThing.of(TestConstants.Thing.THING_ID, TestConstants.Thing.THING, JsonObject.newBuilder().build(), TestConstants.EMPTY_DITTO_HEADERS); final JsonObject actualJson = underTest.toJson(FieldType.regularOrSpecial()); assertThat(actualJson).isEqualTo(expected); }
@Test(expected = AclNotAllowedException.class) public void ensuresNoACLInV2Command() { final DittoHeaders v2Headers = DittoHeaders.newBuilder().schemaVersion(JsonSchemaVersion.LATEST).build(); final Thing thingWithAcl = TestConstants.Thing.THING.toBuilder() .setPermissions(AuthorizationModelFactory.newAuthSubject("any"), AccessControlListModelFactory.allPermissions()) .removePolicyId() .build(); ModifyThing.of(TestConstants.Thing.THING_ID, thingWithAcl, null, v2Headers); }
@Test(expected = PolicyIdNotAllowedException.class) public void ensuresNoPolicyInV1Command() { final DittoHeaders v1Headers = DittoHeaders.newBuilder().schemaVersion(JsonSchemaVersion.V_1).build(); final Thing thingWithoutAclAndPolicy = TestConstants.Thing.THING.toBuilder() .removeAllPermissions() .removePolicyId() .build(); final JsonObject initialPolicy = JsonObject.newBuilder().build(); ModifyThing.of(TestConstants.Thing.THING_ID, thingWithoutAclAndPolicy, initialPolicy, v1Headers); }
@Test(expected = PolicyIdNotAllowedException.class) public void ensuresNoPolicyIdInV1Command() { final DittoHeaders v1Headers = DittoHeaders.newBuilder().schemaVersion(JsonSchemaVersion.V_1).build(); final Thing thingWithPolicyId = TestConstants.Thing.THING.toBuilder() .removeAllPermissions() .setPolicyId("any:policyId") .build(); ModifyThing.of(TestConstants.Thing.THING_ID, thingWithPolicyId, null, v1Headers); }
@Test public void initializeWithCopiedPolicyAndWithInitialPolicy() { final DittoHeaders v2Headers = DittoHeaders.newBuilder().schemaVersion(JsonSchemaVersion.LATEST).build(); final Thing thingWithoutAclAndPolicy = TestConstants.Thing.THING.toBuilder() .removeAllPermissions() .removePolicyId() .build(); final String thingReference = "{{ ref:things/my_namespace:my_thing/policyId }}"; assertThatThrownBy(() -> ModifyThing.of(TestConstants.Thing.THING_ID, thingWithoutAclAndPolicy, JsonObject.newBuilder().build(), thingReference, v2Headers)) .isInstanceOf(PoliciesConflictingException.class) .hasMessage(MessageFormat.format( "The Thing with ID ''{0}'' could not be created as it contained an inline " + "Policy as well as a policyID to copy.", TestConstants.Thing.THING_ID)); }