Skip to content

Commit

Permalink
Issue #559: Extended SignalInformationPoint.
Browse files Browse the repository at this point in the history
It is now possible check whether a Signal provides an EntityId and to optionally obtain the EntityId from a Signal.

Signed-off-by: Juergen Fickel <juergen.fickel@bosch.io>
  • Loading branch information
Juergen Fickel committed Oct 29, 2021
1 parent 32c5b1f commit 353f798
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
*/
package org.eclipse.ditto.internal.models.signal;

import java.util.Optional;

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import org.eclipse.ditto.base.model.entity.id.EntityId;
import org.eclipse.ditto.base.model.entity.id.WithEntityId;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.WithType;
Expand Down Expand Up @@ -129,4 +133,38 @@ public static boolean isChannelLive(@Nullable final WithDittoHeaders signal) {
return result;
}

/**
* Indicates whether the specified signal argument provides an entity ID.
*
* @param signal the signal to be checked.
* @return {@code true} if {@code signal} provides an entity ID because it implements {@link WithEntityId}.
* {@code false} else.
*/
public static boolean isWithEntityId(@Nullable final Signal<?> signal) {
final boolean result;
if (null != signal) {
result = WithEntityId.class.isAssignableFrom(signal.getClass());
} else {
result = false;
}
return result;
}

/**
* Returns the {@link EntityId} for the specified signal argument.
*
* @param signal the signal to get the entity ID from.
* @return an {@code Optional} containing the signal's entity ID if it provides one, an empty {@code Optional} else.
* @see #isWithEntityId(Signal)
*/
public static Optional<EntityId> getEntityId(@Nullable final Signal<?> signal) {
final Optional<EntityId> result;
if (isWithEntityId(signal)) {
result = Optional.of(((WithEntityId) signal).getEntityId());
} else {
result = Optional.empty();
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
import org.eclipse.ditto.internal.models.signal.common.SignalInterfaceImplementations;
import org.eclipse.ditto.messages.model.signals.commands.MessageCommand;
import org.eclipse.ditto.messages.model.signals.commands.MessageCommandResponse;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.ThingCommand;
import org.eclipse.ditto.things.model.signals.commands.ThingCommandResponse;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThing;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThings;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
Expand Down Expand Up @@ -253,6 +256,44 @@ public void isChannelLiveForSignalWithoutChannelHeaderReturnsTrue() {
assertThat(SignalInformationPoint.isChannelLive(signalMock)).isFalse();
}

@Test
public void isWithEntityIdForNullSignalReturnsFalse() {
assertThat(SignalInformationPoint.isWithEntityId(null)).isFalse();
}

@Test
public void isWithEntityIdForSignalImplementingWithEntityIdReturnsTrue() {
final var signal = RetrieveThing.of(ThingId.generateRandom(), dittoHeaders);

assertThat(SignalInformationPoint.isWithEntityId(signal)).isTrue();
}

@Test
public void isWithEntityIdForSignalNotImplementingWithEntityIdReturnsFalse() {
final var signal = RetrieveThings.getBuilder(ThingId.generateRandom()).dittoHeaders(dittoHeaders).build();

assertThat(SignalInformationPoint.isWithEntityId(signal)).isFalse();
}

@Test
public void getEntityIdForNullSignalReturnsEmptyOptional() {
assertThat(SignalInformationPoint.getEntityId(null)).isEmpty();
}

@Test
public void getEntityIdForSignalWithEntityIdReturnsOptionalWithEntityId() {
final var signal = RetrieveThing.of(ThingId.generateRandom(), dittoHeaders);

assertThat(SignalInformationPoint.getEntityId(signal)).hasValue(signal.getEntityId());
}

@Test
public void getEntityIdForSignalWithoutEntityIdReturnsEmptyOptional() {
final var signal = RetrieveThings.getBuilder(ThingId.generateRandom()).dittoHeaders(dittoHeaders).build();

assertThat(SignalInformationPoint.getEntityId(signal)).isEmpty();
}

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

0 comments on commit 353f798

Please sign in to comment.