Skip to content

Commit

Permalink
Issue #106: Added method to get optional correlation ID from a specif…
Browse files Browse the repository at this point in the history
…ied Signal.

Signed-off-by: Juergen Fickel <juergen.fickel@bosch.io>
  • Loading branch information
Juergen Fickel committed Nov 11, 2021
1 parent d842488 commit ef4f8f0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class SignalInformationPoint {
private static final String CHANNEL_LIVE_VALUE = "live";

private SignalInformationPoint() {
throw new AssertionError("nope");
throw new AssertionError();
}

/**
Expand Down Expand Up @@ -167,4 +167,21 @@ public static Optional<EntityId> getEntityId(@Nullable final Signal<?> signal) {
return result;
}

/**
* Returns the optional correlation ID of the specified {@code Signal} argument's headers.
*
* @param signal the signal to get the optional correlation ID from.
* @return the optional correlation ID. The optional is empty if {@code signal} is {@code null}.
*/
public static Optional<String> getCorrelationId(@Nullable final Signal<?> signal) {
final Optional<String> result;
if (null != signal) {
final var signalDittoHeaders = signal.getDittoHeaders();
result = signalDittoHeaders.getCorrelationId();
} else {
result = Optional.empty();
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.eclipse.ditto.base.model.correlationid.TestNameCorrelationId;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.SignalWithEntityId;
import org.eclipse.ditto.internal.models.signal.common.SignalInterfaceImplementations;
import org.eclipse.ditto.messages.model.signals.commands.MessageCommand;
Expand Down Expand Up @@ -294,6 +295,27 @@ public void getEntityIdForSignalWithoutEntityIdReturnsEmptyOptional() {
assertThat(SignalInformationPoint.getEntityId(signal)).isEmpty();
}

@Test
public void getCorrelationIdFromNullSignalReturnsEmptyOptional() {
assertThat(SignalInformationPoint.getCorrelationId(null)).isEmpty();
}

@Test
public void getCorrelationIdFromSignalWithHeadersWithoutCorrelationIdReturnsEmptyCorrelationId() {
final Signal<?> signal = Mockito.mock(Signal.class);
Mockito.when(signal.getDittoHeaders()).thenReturn(DittoHeaders.empty());

assertThat(SignalInformationPoint.getCorrelationId(null)).isEmpty();
}

@Test
public void getCorrelationIdFromSignalWithCorrelationIdInHeadersReturnsThoseCorrelationId() {
final Signal<?> signal = Mockito.mock(Signal.class);
Mockito.when(signal.getDittoHeaders()).thenReturn(dittoHeaders);

assertThat(SignalInformationPoint.getCorrelationId(signal)).isEqualTo(dittoHeaders.getCorrelationId());
}

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

0 comments on commit ef4f8f0

Please sign in to comment.