Skip to content

Commit

Permalink
Added unit test
Browse files Browse the repository at this point in the history
Signed-off-by: David Joos <david.joos@bosch-si.com>
  • Loading branch information
David Joos committed Apr 20, 2020
1 parent c6565ba commit 5a68af1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import javax.annotation.Nullable;

import org.assertj.core.api.ThrowableAssertAlternative;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.headers.entitytag.EntityTag;
Expand Down Expand Up @@ -66,7 +67,7 @@ public void doesNotThrowExceptionsIfAllChecksSucceed() {
final String ifMatchHeaderValue = "\"rev:1\"";
final String ifNoneMatchHeaderValue = "\"rev:2\"";
final EntityTag actualEntityTag = EntityTag.fromString("\"rev:1\"");
final Command commandMock = createCommandMock(MODIFY, ifMatchHeaderValue, ifNoneMatchHeaderValue);
final Command commandMock = createCommandMock(MODIFY, ifMatchHeaderValue, ifNoneMatchHeaderValue, null);

SUT.checkConditionalHeaders(commandMock, actualEntityTag);
}
Expand Down Expand Up @@ -126,26 +127,60 @@ public void throwsThingPreconditionNotModifiedExceptionWhenIfNoneMatchFailsAndCa
final String expectedMessage =
format(IF_NONE_MATCH_NOT_MODIFIED_MESSAGE_PATTERN, ifNoneMatchHeaderValue, actualEntityTag);

assertNotModified(ifMatchHeaderValue, ifNoneMatchHeaderValue, actualEntityTag, expectedMessage);
assertNotModified(ifMatchHeaderValue, ifNoneMatchHeaderValue, actualEntityTag, expectedMessage, null);
}

@Test
public void assertNotThrowingNotModifiedWhenSelectedFieldsContainsPolicy() {
final String ifMatchHeaderValue = "\"rev:1\"";
final String ifNoneMatchHeaderValue = "\"rev:1\"";
final EntityTag actualEntityTag = EntityTag.fromString("\"rev:1\"");

final Command commandMockWithPolicy =
createCommandMock(QUERY, ifMatchHeaderValue, ifNoneMatchHeaderValue, null);
when(commandMockWithPolicy.toJson()).thenReturn(JsonObject.of("{\"selectedFields\":\"_policy\"}"));

SUT.checkConditionalHeaders(commandMockWithPolicy, actualEntityTag);
}

@Test
public void assertThrowingNotModifiedWhenSelectedFieldDoesNotContainPolicy() {
final String ifMatchHeaderValue = "\"rev:1\"";
final String ifNoneMatchHeaderValue = "\"rev:1\"";
final EntityTag actualEntityTag = EntityTag.fromString("\"rev:1\"");
final String expectedMessage =
format(IF_NONE_MATCH_NOT_MODIFIED_MESSAGE_PATTERN, ifNoneMatchHeaderValue, actualEntityTag);

JsonObject selectedFields = JsonObject.of("{\"selectedFields\":\"policyId\"}");

assertNotModified(ifMatchHeaderValue, ifNoneMatchHeaderValue, actualEntityTag, expectedMessage, selectedFields);
}

private Command createCommandMock(final Category commandCategory, final String ifMatchHeaderValue,
final String ifNoneMatchHeaderValue) {
final String ifNoneMatchHeaderValue, final @Nullable JsonObject selectedFields) {
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.ifMatch(EntityTagMatchers.fromCommaSeparatedString(ifMatchHeaderValue))
.ifNoneMatch(EntityTagMatchers.fromCommaSeparatedString(ifNoneMatchHeaderValue))
.build();
final Command commandMock = mock(Command.class);
when(commandMock.getDittoHeaders()).thenReturn(dittoHeaders);
when(commandMock.getCategory()).thenReturn(commandCategory);

if (Optional.ofNullable(selectedFields).isPresent()) {
when(commandMock.toJson()).thenReturn(Optional.of(selectedFields).get());
} else {
when(commandMock.toJson()).thenReturn(JsonObject.empty());
}

return commandMock;
}

private void assertSkipForCommandCategory(final Category commandCategory) {
final String ifMatchHeaderValue = "\"rev:2\"";
final String ifNoneMatchHeaderValue = "\"rev:1\"";
final EntityTag actualEntityTag = null;
final Command commandMock = createCommandMock(commandCategory, ifMatchHeaderValue, ifNoneMatchHeaderValue);
final Command commandMock =
createCommandMock(commandCategory, ifMatchHeaderValue, ifNoneMatchHeaderValue, null);

SUT.checkConditionalHeaders(commandMock, actualEntityTag);
}
Expand All @@ -160,7 +195,8 @@ private void assertETagHeaderInDre(final DittoRuntimeException dre, final Entity
private void assertPreconditionFailed(final String ifMatchHeaderValue,
final String ifNoneMatchHeaderValue, @Nullable final EntityTag actualEntityTag,
final Category commandCategory, final String expectedMessage) {
final Command commandMock = createCommandMock(commandCategory, ifMatchHeaderValue, ifNoneMatchHeaderValue);
final Command commandMock =
createCommandMock(commandCategory, ifMatchHeaderValue, ifNoneMatchHeaderValue, null);

final ThrowableAssertAlternative<ThingPreconditionFailedException> assertion =
assertThatExceptionOfType(ThingPreconditionFailedException.class)
Expand All @@ -175,8 +211,9 @@ private void assertPreconditionFailed(final String ifMatchHeaderValue,
}

private void assertNotModified(final String ifMatchHeaderValue,
final String ifNoneMatchHeaderValue, @Nullable final EntityTag actualEntityTag, final String expectedMessage) {
final Command commandMock = createCommandMock(QUERY, ifMatchHeaderValue, ifNoneMatchHeaderValue);
final String ifNoneMatchHeaderValue, @Nullable final EntityTag actualEntityTag,
final String expectedMessage, final @Nullable JsonObject selectedFields) {
final Command commandMock = createCommandMock(QUERY, ifMatchHeaderValue, ifNoneMatchHeaderValue, selectedFields);

final ThrowableAssertAlternative<ThingPreconditionNotModifiedException> assertion =
assertThatExceptionOfType(ThingPreconditionNotModifiedException.class)
Expand Down
10 changes: 10 additions & 0 deletions services/utils/conditional-headers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@
<groupId>org.eclipse.ditto</groupId>
<artifactId>ditto-signals-commands-base</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.ditto</groupId>
<artifactId>ditto-signals-commands-things</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.ditto</groupId>
<artifactId>ditto-signals-commands-things</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

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

import org.eclipse.ditto.signals.commands.base.Command;
import org.eclipse.ditto.signals.commands.things.ThingCommand;
import org.junit.Test;

/**
Expand All @@ -36,4 +38,5 @@ public void creationFailsWithNullValidationSettings() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> ConditionalHeadersValidator.of(null));
}

}

0 comments on commit 5a68af1

Please sign in to comment.