Skip to content

Commit

Permalink
Issue #106: Added methods to 'SignalInformationPoint'.
Browse files Browse the repository at this point in the history
Signed-off-by: Juergen Fickel <juergen.fickel@bosch.io>
  • Loading branch information
Juergen Fickel committed Nov 15, 2021
1 parent 330613d commit a6aecc6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.WithType;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.base.model.signals.commands.CommandResponse;
import org.eclipse.ditto.messages.model.signals.commands.MessageCommand;
import org.eclipse.ditto.messages.model.signals.commands.MessageCommandResponse;
import org.eclipse.ditto.things.model.signals.commands.ThingCommand;
Expand All @@ -45,12 +47,21 @@ private SignalInformationPoint() {
* Indicates whether the specified signal argument is a live command.
*
* @param signal the signal to be checked.
* @return {@code true} if {@code signal} is a live command, i.e. either a message command or a thing command with
* channel {@value CHANNEL_LIVE_VALUE} in its headers.
* {@code false} if {@code signal} is not a live command.
* @return {@code true} if {@code signal} is an instance of {@link Command} with channel
* {@value CHANNEL_LIVE_VALUE} in its headers, {@code false} else.
*/
public static boolean isLiveCommand(@Nullable final Signal<?> signal) {
return isMessageCommand(signal) || isThingCommand(signal) && isChannelLive(signal);
return isMessageCommand(signal) || isCommand(signal) && isChannelLive(signal);
}

/**
* Indicates whether the specified signal argument is an instance of {@code Command}.
*
* @param signal the signal to be checked.
* @return {@code true} if {@code signal} is an instance of {@link Command}, {@code false} else.
*/
public static boolean isCommand(@Nullable final Signal<?> signal) {
return signal instanceof Command;
}

/**
Expand Down Expand Up @@ -88,12 +99,22 @@ public static boolean isThingCommand(@Nullable final Signal<?> signal) {
* Indicates whether the specified signal is a live command response.
*
* @param signal the signal to be checked.
* @return {@code true} if {@code signal} is a live command response, i.e. either a message command response
* or a thing command response with channel {@value CHANNEL_LIVE_VALUE} in its headers.
* @return {@code true} if {@code signal} is a live command response, i.e. an instance of {@link CommandResponse}
* with channel {@value CHANNEL_LIVE_VALUE} in its headers.
* {@code false} if {@code signal} is not a live command response.
*/
public static boolean isLiveCommandResponse(@Nullable final Signal<?> signal) {
return isMessageCommandResponse(signal) || isThingCommandResponse(signal) && isChannelLive(signal);
return isMessageCommandResponse(signal) || isCommandResponse(signal) && isChannelLive(signal);
}

/**
* Indicates whether the specified signal argument is an instance of {@code CommandResponse}.
*
* @param signal the signal to be checked.
* @return {@code true} if {@code signal} is an instance of {@link CommandResponse}, {@code false} else.
*/
public static boolean isCommandResponse(@Nullable final Signal<?> signal) {
return signal instanceof CommandResponse;
}

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

import java.util.stream.Stream;

import org.assertj.core.api.JUnitSoftAssertions;
import org.eclipse.ditto.base.model.correlationid.TestNameCorrelationId;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
Expand Down Expand Up @@ -316,6 +318,48 @@ public void getCorrelationIdFromSignalWithCorrelationIdInHeadersReturnsThoseCorr
assertThat(SignalInformationPoint.getCorrelationId(signal)).isEqualTo(dittoHeaders.getCorrelationId());
}

@Test
public void isCommandForCommandReturnsFalse() {
Stream.concat(thingCommands.stream(), messageCommands.stream())
.forEach(command -> softly.assertThat(SignalInformationPoint.isCommand(command))
.as(command.getType())
.isTrue());
}

@Test
public void isCommandForNullReturnsFalse() {
assertThat(SignalInformationPoint.isCommand(null)).isFalse();
}

@Test
public void isCommandForCommandResponseReturnsFalse() {
Stream.concat(thingCommandResponses.stream(), messageCommandResponses.stream())
.forEach(commandResponse -> softly.assertThat(SignalInformationPoint.isCommand(commandResponse))
.as(commandResponse.getType())
.isFalse());
}

@Test
public void isCommandResponseForCommandResponseReturnsFalse() {
Stream.concat(thingCommandResponses.stream(), messageCommandResponses.stream())
.forEach(commandResponse -> softly.assertThat(SignalInformationPoint.isCommandResponse(commandResponse))
.as(commandResponse.getType())
.isTrue());
}

@Test
public void isCommandResponseForNullReturnsFalse() {
assertThat(SignalInformationPoint.isCommandResponse(null)).isFalse();
}

@Test
public void isCommandForCommandResponseResponseReturnsFalse() {
Stream.concat(thingCommands.stream(), messageCommands.stream())
.forEach(command -> softly.assertThat(SignalInformationPoint.isCommandResponse(command))
.as(command.getType())
.isFalse());
}

private static String getSimpleClassName(final Object o) {
final var oClass = o.getClass();
return oClass.getSimpleName();
Expand Down

0 comments on commit a6aecc6

Please sign in to comment.