Skip to content

Commit

Permalink
Issue #106: Harmonized behaviour of 'HttpPushRoundTripSignalsValidato…
Browse files Browse the repository at this point in the history
…r' with 'AbstractHttpRequestActor'.

* Both classes now create an equal 'LogEntry'.
* Instead of 'MessageSendingFailedException' an 'UnsupportedSignalException' is thrown because it is more appropriate in this context.

Signed-off-by: Juergen Fickel <juergen.fickel@bosch.io>
  • Loading branch information
Juergen Fickel committed Nov 12, 2021
1 parent 878f960 commit c48363c
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import javax.annotation.concurrent.NotThreadSafe;

import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.signals.UnsupportedSignalException;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.base.model.signals.commands.CommandResponse;
import org.eclipse.ditto.connectivity.model.MessageSendingFailedException;
import org.eclipse.ditto.connectivity.api.messaging.monitoring.logs.LogEntryFactory;
import org.eclipse.ditto.connectivity.model.LogEntry;
import org.eclipse.ditto.connectivity.service.messaging.monitoring.logs.ConnectionLogger;
import org.eclipse.ditto.internal.models.signal.correlation.CommandAndCommandResponseMatchingValidator;

Expand All @@ -37,10 +39,9 @@
* </ul>
* </p>
* <p>
* If any of the above evaluates to {@code false} a {@link MessageSendingFailedException} is thrown with a detail
* If any of the above evaluates to {@code false} a {@link UnsupportedSignalException} is thrown with a detail
* message describing the cause.
* Furthermore the exception gets logged for the command response via
* {@link ConnectionLogger#failure(org.eclipse.ditto.base.model.signals.Signal, org.eclipse.ditto.base.model.exceptions.DittoRuntimeException)}.
* Furthermore the exception gets logged for the command response via {@link ConnectionLogger#logEntry(LogEntry)}.
* </p>
*/
@NotThreadSafe
Expand All @@ -62,14 +63,23 @@ static HttpPushRoundTripSignalsValidator newInstance(final ConnectionLogger conn
public void accept(final Command<?> command, final CommandResponse<?> commandResponse) {
final var validationResult = validator.apply(command, commandResponse);
if (!validationResult.isSuccess()) {
final var messageSendingFailedException = MessageSendingFailedException.newBuilder()
.httpStatus(HttpStatus.BAD_REQUEST)
.message(validationResult.getDetailMessageOrThrow())
.dittoHeaders(command.getDittoHeaders())
.build();
connectionLogger.failure(commandResponse, messageSendingFailedException);
throw messageSendingFailedException;
final var detailMessage = validationResult.getDetailMessageOrThrow();
connectionLogger.logEntry(LogEntryFactory.getLogEntryForFailedCommandResponseRoundTrip(command,
commandResponse,
detailMessage));
throw newUnsupportedSignalException(command, commandResponse, detailMessage);
}
}

private static UnsupportedSignalException newUnsupportedSignalException(final Command<?> command,
final CommandResponse<?> commandResponse,
final String detailMessage) {

return UnsupportedSignalException.newBuilder(commandResponse.getType())
.httpStatus(HttpStatus.UNPROCESSABLE_ENTITY)
.message(detailMessage)
.dittoHeaders(command.getDittoHeaders())
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ public void sendingLiveResponseWithWrongCorrelationIdDoesNotWork() {

final var acknowledgements = expectMsgClass(Duration.ofSeconds(5), Acknowledgements.class);
assertThat((CharSequence) acknowledgements.getEntityId()).isEqualTo(TestConstants.Things.THING_ID);
assertThat(acknowledgements.getHttpStatus()).isEqualTo(HttpStatus.BAD_REQUEST);
assertThat(acknowledgements.getHttpStatus()).isEqualTo(HttpStatus.UNPROCESSABLE_ENTITY);
assertThat(acknowledgements.getDittoHeaders().getCorrelationId())
.contains(TestConstants.CORRELATION_ID);
assertThat(acknowledgements.getSize()).isOne();
Expand Down Expand Up @@ -614,15 +614,14 @@ public void sendingLiveResponseToDifferentThingIdDoesNotWork() {

final var acknowledgements = expectMsgClass(Acknowledgements.class);
assertThat((CharSequence) acknowledgements.getEntityId()).isEqualTo(TestConstants.Things.THING_ID);
assertThat(acknowledgements.getHttpStatus()).isEqualTo(HttpStatus.BAD_REQUEST);
assertThat(acknowledgements.getHttpStatus()).isEqualTo(HttpStatus.UNPROCESSABLE_ENTITY);
assertThat(acknowledgements.getDittoHeaders().getCorrelationId())
.contains(TestConstants.CORRELATION_ID);
assertThat(acknowledgements.getSize()).isOne();
assertThat(acknowledgements.getAcknowledgement(DittoAcknowledgementLabel.LIVE_RESPONSE))
.hasValueSatisfying(ack -> assertThat(ack.toJsonString())
.contains("Entity ID of live response <namespace:wrongthing> differs from entity ID of" +
" command <ditto:thing>."));

}};
}

Expand Down Expand Up @@ -695,7 +694,7 @@ public void sendingWrongResponseTypeDoesNotWork() {

final var acknowledgements = expectMsgClass(Acknowledgements.class);
assertThat((CharSequence) acknowledgements.getEntityId()).isEqualTo(TestConstants.Things.THING_ID);
assertThat(acknowledgements.getHttpStatus()).isEqualTo(HttpStatus.BAD_REQUEST);
assertThat(acknowledgements.getHttpStatus()).isEqualTo(HttpStatus.UNPROCESSABLE_ENTITY);
assertThat(acknowledgements.getDittoHeaders().getCorrelationId())
.contains(TestConstants.CORRELATION_ID);
assertThat(acknowledgements.getSize()).isOne();
Expand Down

0 comments on commit c48363c

Please sign in to comment.