Skip to content

Commit

Permalink
fix more unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Schneider <johannes.schneider@bosch.io>
  • Loading branch information
jokraehe committed Jun 20, 2022
1 parent 70bd5fd commit 40a4c44
Showing 1 changed file with 3 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,24 @@
import java.lang.ref.SoftReference;
import java.text.MessageFormat;

import org.assertj.core.api.JUnitSoftAssertions;
import org.eclipse.ditto.base.model.entity.metadata.Metadata;
import org.eclipse.ditto.base.model.exceptions.DittoHeaderInvalidException;
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.metadata.MetadataHeader;
import org.eclipse.ditto.base.model.headers.metadata.MetadataHeaderKey;
import org.eclipse.ditto.base.model.headers.metadata.MetadataHeaders;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.base.model.signals.commands.GlobalCommandRegistry;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.ThingTooLargeException;
import org.eclipse.ditto.things.model.ThingsModelFactory;
import org.eclipse.ditto.things.model.signals.commands.TestConstants;
import org.eclipse.ditto.things.model.signals.commands.ThingCommand;
import org.eclipse.ditto.things.model.signals.commands.exceptions.MetadataNotModifiableException;
import org.eclipse.ditto.things.model.signals.commands.exceptions.PoliciesConflictingException;
import org.junit.Rule;
import org.junit.Test;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.assertj.core.api.JUnitSoftAssertions;
import org.junit.Rule;
import org.junit.Test;


/**
Expand Down Expand Up @@ -179,81 +170,6 @@ public void initializeWithCopiedPolicyAndWithInitialPolicy() {
" well as a policyID to copy.", TestConstants.Thing.THING_ID));
}

@Test
public void initializeWithMetadata() {
final Thing thing = Thing.newBuilder()
.setGeneratedId()
.setAttribute(JsonPointer.of("/foo"), JsonValue.of("bar"))
.setMetadata(Metadata.newBuilder()
.set("/attributes/foo", 42)
.build())
.build();
final JsonObject commandJson = JsonFactory.newObjectBuilder()
.set(ThingCommand.JsonFields.TYPE, CreateThing.TYPE)
.set(CreateThing.JSON_THING, thing.toJson(FieldType.all()))
.build();

final CreateThing parsedCommand = CreateThing.fromJson(commandJson, DittoHeaders.empty());

final MetadataHeaders expectedMetadataHeaders = MetadataHeaders.newInstance();
final MetadataHeader expectedMetadataHeader = MetadataHeader.of(MetadataHeaderKey.parse("attributes"),
JsonObject.newBuilder()
.set("foo", 42)
.build());
expectedMetadataHeaders.add(expectedMetadataHeader);
final DittoHeaders expectedDittoHeaders = DittoHeaders.newBuilder()
.putHeader(DittoHeaderDefinition.PUT_METADATA.toString(), expectedMetadataHeaders.toJsonString())
.build();
final CreateThing expectedCommand = CreateThing.of(thing, null, expectedDittoHeaders);
softly.assertThat(parsedCommand).isEqualTo(expectedCommand);
}

@Test
public void initializeWithMetadataTwice() {
final Thing thing = Thing.newBuilder()
.setGeneratedId()
.setAttribute(JsonPointer.of("/foo"), JsonValue.of("bar"))
.setMetadata(Metadata.newBuilder()
.set("/attributes/foo", 42)
.build())
.build();
final JsonObject commandJson = JsonFactory.newObjectBuilder()
.set(ThingCommand.JsonFields.TYPE, CreateThing.TYPE)
.set(CreateThing.JSON_THING, thing.toJson(FieldType.all()))
.build();

final MetadataHeaders metadataHeaders = MetadataHeaders.newInstance();
final MetadataHeader metadataHeader = MetadataHeader.of(MetadataHeaderKey.parse("attributes"),
JsonObject.newBuilder()
.set("foo", 42)
.build());
metadataHeaders.add(metadataHeader);
final DittoHeaders dittoHeadersWithMetadata = DittoHeaders.newBuilder()
.putHeader(DittoHeaderDefinition.PUT_METADATA.toString(), metadataHeaders.toJsonString())
.build();

softly.assertThatThrownBy(() -> CreateThing.fromJson(commandJson, dittoHeadersWithMetadata))
.isInstanceOf(MetadataNotModifiableException.class);
}

@Test
public void initializeWithMetadataAndInvalidMetadataKey() {
final Thing thing = Thing.newBuilder()
.setGeneratedId()
.setAttribute(JsonPointer.of("/foo"), JsonValue.of("bar"))
.setMetadata(Metadata.newBuilder()
.set("*/*/foo", 42)
.build())
.build();
final JsonObject commandJson = JsonFactory.newObjectBuilder()
.set(ThingCommand.JsonFields.TYPE, CreateThing.TYPE)
.set(CreateThing.JSON_THING, thing.toJson(FieldType.all()))
.build();

softly.assertThatThrownBy(() -> CreateThing.fromJson(commandJson, TestConstants.DITTO_HEADERS))
.isInstanceOf(DittoHeaderInvalidException.class);
}

@Test
public void parseCreateThingCommand() {
final GlobalCommandRegistry commandRegistry = GlobalCommandRegistry.getInstance();
Expand Down

0 comments on commit 40a4c44

Please sign in to comment.