Skip to content

Commit

Permalink
made "topicPath" in IllegalAdaptableException non-optional
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch.io>
  • Loading branch information
thjaeckle committed Dec 22, 2021
1 parent e25c34e commit 7bdb628
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static LogEntry getLogEntryForIllegalCommandResponseAdaptable(
}

private static Optional<EntityId> getEntityId(final IllegalAdaptableException illegalAdaptableException) {
return illegalAdaptableException.getTopicPath().flatMap(LogEntryFactory::getEntityIdFromTopicPath);
return getEntityIdFromTopicPath(illegalAdaptableException.getTopicPath());
}

private static Optional<EntityId> getEntityIdFromTopicPath(final TopicPath topicPath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,9 @@ private Optional<Signal<?>> handleIllegalAdaptableExceptionForLiveResponse(
) {
final Optional<Signal<?>> result;
countAndLogIllegalAdaptableExceptionForLiveResponse(illegalAdaptableException, inboundMessage);
final Optional<TopicPath> topicPath = illegalAdaptableException.getTopicPath();
if (isResponseRequired(illegalAdaptableException) && topicPath.isPresent()) {
result = Optional.of(toErrorResponseFunction.apply(illegalAdaptableException, topicPath.get()));
if (isResponseRequired(illegalAdaptableException)) {
result = Optional.of(toErrorResponseFunction.apply(illegalAdaptableException,
illegalAdaptableException.getTopicPath()));
} else {
result = Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Another perfect fit for that feature is when the device (or the device connectiv
connection status of the device in the Ditto managed twin.
When e.g. using [Eclipse Hono](https://www.eclipse.org/hono/) in combination with Ditto, the
[ConnectionStatus](connectivity-mapping.html#connectionstatus-mapper) mapper can be configured in a Ditto
[managed connection](basic-connections.html) which will automatically updates a [feature](basic-thing.html#features) in
[managed connection](basic-connections.html) which will automatically update a [feature](basic-thing.html#features) in
the twin based on Hono's [device notifications](https://www.eclipse.org/hono/docs/concepts/device-notifications/)
reflecting a `"readySince"` and `"readyUntil"` timestamp:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public final class IllegalAdaptableException extends DittoRuntimeException {

private static final long serialVersionUID = 1552465013185426687L;

@Nullable private final transient TopicPath topicPath;
private final transient TopicPath topicPath;
@Nullable private final transient CharSequence signalType;

private IllegalAdaptableException(final String errorCode,
Expand Down Expand Up @@ -114,7 +114,6 @@ public static IllegalAdaptableException newInstance(final String message,
final Adaptable adaptable) {

return newBuilder(message, adaptable)
.withTopicPath(adaptable.getTopicPath())
.withDescription(description)
.build();
}
Expand All @@ -130,21 +129,21 @@ public static IllegalAdaptableException newInstance(final String message,
*/
public static Builder newBuilder(final String message, final Adaptable adaptable) {
ConditionChecker.checkNotNull(adaptable, "adaptable");
return newBuilder(message, adaptable.getDittoHeaders())
.withTopicPath(adaptable.getTopicPath());
return newBuilder(message, adaptable.getTopicPath(), adaptable.getDittoHeaders());
}

/**
* Returns a mutable builder with a fluent API for constructing an instance of {@code IllegalAdaptableException}.
*
* @param message the detail message of the exception.
* @param topicPath the {@code TopicPath} of the {@code Adaptable} which caused the exception to be built.
* @param dittoHeaders the {@code DittoHeaders} to use for the exception.
* @return the builder.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code message} is blank.
*/
public static Builder newBuilder(final String message, final DittoHeaders dittoHeaders) {
return new Builder(message, dittoHeaders);
public static Builder newBuilder(final String message, final TopicPath topicPath, final DittoHeaders dittoHeaders) {
return new Builder(message, topicPath, dittoHeaders);
}

/**
Expand All @@ -165,8 +164,8 @@ public static IllegalAdaptableException fromJson(final JsonObject jsonObject, fi
deserializeErrorCode(jsonObject),
deserializeHttpStatus(jsonObject),
dittoHeaders,
newBuilder(jsonObject.getValueOrThrow(JsonFields.MESSAGE), dittoHeaders)
.withTopicPath(deserializeTopicPath(jsonObject).orElse(null))
newBuilder(jsonObject.getValueOrThrow(JsonFields.MESSAGE), deserializeTopicPath(jsonObject),
dittoHeaders)
.withSignalType(deserializeSignalType(jsonObject).orElse(null))
.withDescription(jsonObject.getValue(JsonFields.DESCRIPTION).orElse(null))
.withHref(deserializeHref(jsonObject).orElse(null))
Expand Down Expand Up @@ -210,18 +209,9 @@ private static HttpStatus deserializeHttpStatus(final JsonObject jsonObject) {
}
}

private static Optional<TopicPath> deserializeTopicPath(final JsonObject jsonObject) {
final Optional<TopicPath> result;
final JsonFieldDefinition<String> fieldDefinition = JSON_FIELD_TOPIC_PATH;
final Optional<String> deserializedTopicPathOptional = jsonObject.getValue(fieldDefinition);
if (deserializedTopicPathOptional.isPresent() && isBlank(deserializedTopicPathOptional.get())) {
throw new JsonParseException(
MessageFormat.format("Value of field <{0}> must not be blank.", fieldDefinition.getPointer())
);
} else {
result = deserializedTopicPathOptional.map(ProtocolFactory::newTopicPath);
}
return result;
private static TopicPath deserializeTopicPath(final JsonObject jsonObject) {
final String deserializedTopicPathOptional = jsonObject.getValueOrThrow(JSON_FIELD_TOPIC_PATH);
return ProtocolFactory.newTopicPath(deserializedTopicPathOptional);
}

private static Optional<String> deserializeSignalType(final JsonObject jsonObject) {
Expand Down Expand Up @@ -257,12 +247,12 @@ private static Optional<URI> deserializeHref(final JsonObject jsonObject) {
}

/**
* Returns the{@code TopicPath} if available.
* Returns the {@code TopicPath} of the illegal {@code Adaptable}.
*
* @return the optional topic path.
* @return the topic path.
*/
public Optional<TopicPath> getTopicPath() {
return Optional.ofNullable(topicPath);
public TopicPath getTopicPath() {
return topicPath;
}

/**
Expand All @@ -282,8 +272,7 @@ public DittoRuntimeException setDittoHeaders(final DittoHeaders dittoHeaders) {
getErrorCode(),
getHttpStatus(),
dittoHeaders,
newBuilder(getMessage(), dittoHeaders)
.withTopicPath(topicPath)
newBuilder(getMessage(), topicPath, dittoHeaders)
.withSignalType(signalType)
.withDescription(getDescription().orElse(null))
.withCause(getCause())
Expand All @@ -293,9 +282,7 @@ public DittoRuntimeException setDittoHeaders(final DittoHeaders dittoHeaders) {

@Override
protected void appendToJson(final JsonObjectBuilder jsonObjectBuilder, final Predicate<JsonField> predicate) {
if (null != topicPath) {
jsonObjectBuilder.set(JSON_FIELD_TOPIC_PATH, topicPath.getPath());
}
jsonObjectBuilder.set(JSON_FIELD_TOPIC_PATH, topicPath.getPath());
if (null != signalType) {
jsonObjectBuilder.set(JSON_FIELD_SIGNAL_TYPE, signalType.toString());
}
Expand Down Expand Up @@ -330,14 +317,15 @@ public static final class Builder {

private final String message;
private final DittoHeaders dittoHeaders;
private final TopicPath topicPath;
@Nullable private CharSequence signalType;
@Nullable private TopicPath topicPath;
@Nullable private String description;
@Nullable private Throwable cause;
@Nullable private URI href;

private Builder(final String message, final DittoHeaders dittoHeaders) {
private Builder(final String message, final TopicPath topicPath, final DittoHeaders dittoHeaders) {
this.message = checkCharSequenceArgumentNotBlank(message, "message");
this.topicPath = ConditionChecker.checkNotNull(topicPath, "topicPath");
this.dittoHeaders = ConditionChecker.checkNotNull(dittoHeaders, "dittoHeaders");
}

Expand Down Expand Up @@ -366,17 +354,6 @@ public Builder withSignalType(@Nullable final CharSequence signalType) {
return this;
}

/**
* Sets the specified argument as {@code TopicPath}.
*
* @param topicPath the topic path.
* @return this builder instance for method chaining.
*/
public Builder withTopicPath(@Nullable final TopicPath topicPath) {
this.topicPath = topicPath;
return this;
}

/**
* Sets the specified string argument as description.
* The description provides further information about the cause or possible solution for the failure.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.ditto.base.model.correlationid.TestNameCorrelationId;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.protocol.Adaptable;
import org.eclipse.ditto.protocol.TopicPath;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.signals.commands.modify.CreateThing;
import org.eclipse.ditto.things.model.signals.commands.modify.CreateThingResponse;
Expand Down Expand Up @@ -130,6 +131,7 @@ public void mapAdaptableCreatesExpectedMappingContext() {
public void mapAdaptableThrowsIllegalAdaptableExceptionIfMappingFunctionThrowsDittoRuntimeException() {
final Adaptable adaptable = Mockito.mock(Adaptable.class);
Mockito.when(adaptable.getDittoHeaders()).thenReturn(dittoHeaders);
Mockito.when(adaptable.getTopicPath()).thenReturn(Mockito.mock(TopicPath.class));
final IllegalAdaptableException cause =
IllegalAdaptableException.newInstance("This is a message.", "This is a description.", adaptable);
Mockito.when(mappingFunction.apply(Mockito.any(MappingContext.class))).thenThrow(cause);
Expand All @@ -153,6 +155,7 @@ public void mapAdaptableThrowsIllegalAdaptableExceptionIfMappingFunctionThrowsDi
public void mapAdaptableThrowsIllegalAdaptableExceptionIfMappingFunctionThrowsRuntimeException() {
final Adaptable adaptable = Mockito.mock(Adaptable.class);
Mockito.when(adaptable.getDittoHeaders()).thenReturn(dittoHeaders);
Mockito.when(adaptable.getTopicPath()).thenReturn(Mockito.mock(TopicPath.class));
final IllegalArgumentException illegalArgumentException = new IllegalArgumentException("This is a message.");
Mockito.when(mappingFunction.apply(Mockito.any(MappingContext.class))).thenThrow(illegalArgumentException);
final String targetSignalType = CreateThingResponse.TYPE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void getMessageReturnsExpected() {
public void getTopicPathReturnsExpected() {
final IllegalAdaptableException underTest = IllegalAdaptableException.newInstance(MESSAGE, adaptable);

assertThat(underTest.getTopicPath()).contains(adaptable.getTopicPath());
assertThat(underTest.getTopicPath()).isEqualTo(adaptable.getTopicPath());
}

@Test
Expand Down Expand Up @@ -179,7 +179,7 @@ public void newInstanceViaBuilderReturnsExpected() {
.as("HTTP status")
.isEqualTo(IllegalAdaptableException.HTTP_STATUS);
softly.assertThat(underTest.getMessage()).as("message").isEqualTo(MESSAGE);
softly.assertThat(underTest.getTopicPath()).as("topic path").contains(adaptable.getTopicPath());
softly.assertThat(underTest.getTopicPath()).as("topic path").isEqualTo(adaptable.getTopicPath());
softly.assertThat(underTest.getDittoHeaders()).as("Ditto headers").isEqualTo(dittoHeaders);
softly.assertThat(underTest.getSignalType()).as("signal type").contains(SIGNAL_TYPE);
softly.assertThat(underTest.getDescription()).as("description").contains(description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ public final class MappingContextTest {
@Mock
private Adaptable adaptable;

@Mock
private TopicPath topicPath;
private DittoHeaders dittoHeaders;

@Before
public void before() {
dittoHeaders = DittoHeaders.newBuilder().correlationId(testNameCorrelationId.getCorrelationId()).build();
Mockito.when(adaptable.getDittoHeaders()).thenReturn(dittoHeaders);
Mockito.when(adaptable.getTopicPath()).thenReturn(topicPath);
}

@Test
Expand Down

0 comments on commit 7bdb628

Please sign in to comment.