Skip to content

Commit

Permalink
fix 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 29, 2022
1 parent 7b81d05 commit a882d06
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,21 @@
*/
package org.eclipse.ditto.things.service.persistence.actors.strategies.commands;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.ditto.things.model.TestConstants.Thing.THING_V2;
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;

import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.metadata.MetadataHeaderKey;
import org.eclipse.ditto.internal.utils.persistentactors.commands.CommandStrategy;
import org.eclipse.ditto.things.model.TestConstants;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.internal.utils.persistentactors.commands.CommandStrategy;
import org.eclipse.ditto.things.model.signals.commands.modify.DeleteFeature;
import org.eclipse.ditto.things.model.signals.commands.modify.DeleteFeatureResponse;
import org.eclipse.ditto.things.model.signals.events.FeatureDeleted;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

/**
Expand Down Expand Up @@ -61,14 +57,10 @@ public void assertImmutability() {
public void successfullyDeleteFeatureFromThing() {
final CommandStrategy.Context<ThingId> context = getDefaultContext();
final DeleteFeature command = DeleteFeature.of(context.getState(), featureId, DittoHeaders.newBuilder()
// metadata is ignored
.putMetadata(MetadataHeaderKey.of(JsonPointer.of("sn")), JsonValue.of(2))
.build());

final FeatureDeleted event = assertModificationResult(underTest, THING_V2, command, FeatureDeleted.class,
DeleteFeatureResponse.of(context.getState(), command.getFeatureId(), command.getDittoHeaders()));

assertThat(event.getMetadata()).isEmpty();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;

import java.util.Optional;

import org.eclipse.ditto.base.model.entity.Entity;
import org.eclipse.ditto.base.model.entity.metadata.Metadata;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.metadata.MetadataHeaderKey;
Expand All @@ -38,6 +35,7 @@
import org.eclipse.ditto.things.model.signals.commands.modify.MergeThing;
import org.eclipse.ditto.things.model.signals.commands.modify.ModifyFeature;
import org.eclipse.ditto.things.model.signals.commands.modify.ThingModifyCommand;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -57,7 +55,6 @@ public final class MetadataFromCommandTest {
private static Thing thingWithoutMetadata;

@Mock private ThingModifyCommand command;
@Mock private Entity<?> entity;

@BeforeClass
public static void setUpClass() {
Expand Down Expand Up @@ -96,40 +93,34 @@ public void assertImmutability() {
@Test
public void tryToGetInstanceWithNullCommand() {
assertThatNullPointerException()
.isThrownBy(() -> MetadataFromCommand.of(null, null, entity.getMetadata().orElse(null)))
.isThrownBy(() -> MetadataFromCommand.of(null, null, null))
.withMessage("The command must not be null!")
.withNoCause();
}

@Test
public void getMetadataWhenEventHasNoEntityAndEntityHasNullExistingMetadata() {
Mockito.when(command.getEntity(Mockito.any())).thenReturn(Optional.empty());
Mockito.when(command.getResourcePath()).thenReturn(JsonPointer.empty());
Mockito.when(entity.getMetadata()).thenReturn(Optional.empty());
final MetadataFromCommand underTest = MetadataFromCommand.of(command, null, entity.getMetadata().orElse(null));
final MetadataFromCommand underTest = MetadataFromCommand.of(command, null, null);

assertThat(underTest.get()).isNull();
}

@Test
public void entityHasNoMetadataAndEventDittoHeadersHaveNoMetadata() {
Mockito.when(command.getEntity(Mockito.any())).thenReturn(Optional.of(thingWithoutMetadata.toJson()));
Mockito.when(command.getResourcePath()).thenReturn(JsonPointer.empty());
Mockito.when(command.getDittoHeaders()).thenReturn(DittoHeaders.empty());
Mockito.when(entity.getMetadata()).thenReturn(Optional.empty());
final MetadataFromCommand underTest = MetadataFromCommand.of(command, null, entity.getMetadata().orElse(null));
final MetadataFromCommand underTest = MetadataFromCommand.of(command, null, null);

assertThat(underTest.get()).isNull();
}

@Test
public void entityMetadataButEventDittoHeadersHaveNoMetadata() {
final Metadata existingMetadata = Metadata.newBuilder().set("/scruplusFine", JsonValue.of("^6,00.32")).build();
Mockito.when(command.getEntity(Mockito.any())).thenReturn(Optional.of(thingWithoutMetadata.toJson()));
Mockito.when(command.getResourcePath()).thenReturn(JsonPointer.empty());
Mockito.when(command.getDittoHeaders()).thenReturn(DittoHeaders.empty());
Mockito.when(entity.getMetadata()).thenReturn(Optional.of(existingMetadata));
final MetadataFromCommand underTest = MetadataFromCommand.of(command, null, entity.getMetadata().orElse(null));
final MetadataFromCommand underTest = MetadataFromCommand.of(command, null, existingMetadata);

assertThat(underTest.get()).isEqualTo(existingMetadata);
}
Expand Down

0 comments on commit a882d06

Please sign in to comment.