CreateThing extends AbstractCommand<CreateThing> implements ThingModifyCommand<CreateThing> { public static CreateThing of(final Thing newThing, @Nullable final JsonObject initialPolicy, final DittoHeaders dittoHeaders) { checkNotNull(newThing, "new Thing"); return new CreateThing(newThing, initialPolicy, dittoHeaders); } private  CreateThing(final Thing thing, @Nullable final JsonObject initialPolicy, final DittoHeaders dittoHeaders); private  CreateThing(final Thing thing, final String policyIdOrPlaceholder, final DittoHeaders dittoHeaders); static CreateThing of(final Thing newThing, @Nullable final JsonObject initialPolicy,
            final DittoHeaders dittoHeaders); static CreateThing withCopiedPolicy(final Thing newThing, final String policyIdOrPlaceholder,
            final DittoHeaders dittoHeaders); static CreateThing of(final Thing newThing, @Nullable final JsonObject initialPolicy,
            @Nullable final String policyIdOrPlaceholder, final DittoHeaders dittoHeaders); static CreateThing fromJson(final String jsonString, final DittoHeaders dittoHeaders); static CreateThing fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders); Thing getThing(); Optional<JsonObject> getInitialPolicy(); Optional<String> getPolicyIdOrPlaceholder(); @Override ThingId getThingEntityId(); @Override Optional<JsonValue> getEntity(final JsonSchemaVersion schemaVersion); @Override JsonPointer getResourcePath(); @Override Category getCategory(); @Override CreateThing 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_THING; static final JsonFieldDefinition<String> JSON_POLICY_ID_OR_PLACEHOLDER; static final JsonFieldDefinition<String> JSON_COPY_POLICY_FROM; static final JsonFieldDefinition<JsonObject> JSON_INITIAL_POLICY; static final JsonFieldDefinition<JsonObject> JSON_INLINE_POLICY; }
@Test(expected = NullPointerException.class) public void tryToCreateInstanceWithNullThing() { CreateThing.of(null, null, TestConstants.EMPTY_DITTO_HEADERS); }
@Test public void createInstanceWithValidThingId() { final Thing thing = ThingsModelFactory.newThingBuilder() .setLifecycle(TestConstants.Thing.LIFECYCLE) .setPolicyId(TestConstants.Thing.POLICY_ID) .setId(ThingId.of("test.ns", "foo-bar")) .build(); final CreateThing createThing = CreateThing.of(thing, null, TestConstants.EMPTY_DITTO_HEADERS); softly.assertThat(createThing).isNotNull(); }
@Test public void toJsonReturnsExpected() { final CreateThing underTest = CreateThing.of(TestConstants.Thing.THING, null, TestConstants.EMPTY_DITTO_HEADERS); final JsonObject actualJson = underTest.toJson(FieldType.regularOrSpecial()); softly.assertThat(actualJson).isEqualTo(KNOWN_JSON); }
@Test public void initializeWithCopiedPolicyAndWithInitialPolicy() { final String thingReference = "{{ ref:things/my_namespace:my_thing/policyId }}"; softly.assertThatThrownBy(() -> CreateThing.of(TestConstants.Thing.THING, JsonObject.newBuilder().build(), thingReference, TestConstants.EMPTY_DITTO_HEADERS)) .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)); }
@Test public void parseCreateThingCommand() { final GlobalCommandRegistry commandRegistry = GlobalCommandRegistry.getInstance(); final CreateThing command = CreateThing.of(TestConstants.Thing.THING, null, TestConstants.DITTO_HEADERS); final JsonObject jsonObject = command.toJson(FieldType.regularOrSpecial()); final Command parsedCommand = commandRegistry.parse(jsonObject, TestConstants.DITTO_HEADERS); softly.assertThat(parsedCommand).isEqualTo(command); }