use of org.eclipse.ditto.base.model.signals.commands.CommandResponse in project ditto by eclipse.
the class SudoRetrieveThingResponseTest method checkSudoCommandResponseRegistryWorks.
@Test
public void checkSudoCommandResponseRegistryWorks() {
final SudoRetrieveThingResponse sudoRetrieveThingResponse = SudoRetrieveThingResponse.fromJson(KNOWN_JSON.toString(), EMPTY_DITTO_HEADERS);
final CommandResponse commandResponse = GlobalCommandResponseRegistry.getInstance().parse(KNOWN_JSON, EMPTY_DITTO_HEADERS);
assertThat(sudoRetrieveThingResponse).isEqualTo(commandResponse);
}
use of org.eclipse.ditto.base.model.signals.commands.CommandResponse in project ditto by eclipse.
the class MessageMappingProcessorActorHeaderInteractionTest method run.
@Test
public void run() {
new TestKit(actorSystem) {
{
final TestProbe collectorProbe = TestProbe.apply("collector", actorSystem);
final ActorRef outboundMappingProcessorActor = createOutboundMappingProcessorActor(this);
final ActorRef inboundMappingProcessorActor = createInboundMappingProcessorActor(this, outboundMappingProcessorActor);
final ModifyThing modifyThing = getModifyThing();
final Optional<HttpStatus> expectedStatusCode = getExpectedOutcome();
final boolean isBadRequest = expectedStatusCode.filter(HttpStatus.BAD_REQUEST::equals).isPresent();
final boolean settleImmediately = modifyThing.getDittoHeaders().getAcknowledgementRequests().isEmpty();
inboundMappingProcessorActor.tell(new InboundMappingSink.ExternalMessageWithSender(toExternalMessage(modifyThing), collectorProbe.ref()), ActorRef.noSender());
// transport-layer settlement based on requested-acks alone
if (settleImmediately && !isBadRequest) {
collectorProbe.expectMsg(FiniteDuration.apply(20l, TimeUnit.SECONDS), ResponseCollectorActor.setCount(0));
} else if (isBadRequest) {
// bad requests should settle immediately because no command is forwarded
collectorProbe.expectMsgClass(DittoHeaderInvalidException.class);
collectorProbe.expectMsg(ResponseCollectorActor.setCount(0));
} else {
// no immediate settlement; response collector actor is asked to wait for the response.
collectorProbe.expectMsg(ResponseCollectorActor.setCount(1));
}
if (!isBadRequest) {
// command forwarded for non-bad requests.
final ModifyThing forwardedModifyThing = expectMsgClass(ModifyThing.class);
// send a response always - MessageMappingProcessorActor should drop it if not wanted.
final Object response = getModifyThingResponse(forwardedModifyThing);
reply(response);
// if an acknowledgement is requested, expect response collector to receive it and trigger settlement.
if (!settleImmediately) {
collectorProbe.expectMsg(response);
}
}
if (expectedStatusCode.isPresent()) {
// check published response for expected status
final BaseClientActor.PublishMappedMessage publish = expectMsgClass(BaseClientActor.PublishMappedMessage.class);
final HttpStatus publishedStatusCode = ((CommandResponse<?>) publish.getOutboundSignal().getSource()).getHttpStatus();
assertThat(publishedStatusCode).isEqualTo(expectedStatusCode.get());
} else {
// check that no response is published
expectNoMessage(Duration.ofMillis(250L));
}
}
};
}
use of org.eclipse.ditto.base.model.signals.commands.CommandResponse in project ditto by eclipse.
the class ModifyPolicyResponseTest method parseModifyPolicyCommandResponse.
@Test
public void parseModifyPolicyCommandResponse() {
final ModifyPolicyResponse commandResponse = ModifyPolicyResponse.created(TestConstants.Policy.POLICY_ID, TestConstants.Policy.POLICY, TestConstants.DITTO_HEADERS);
final JsonObject jsonObject = commandResponse.toJson(FieldType.regularOrSpecial());
final CommandResponse parsedCommandResponse = GlobalCommandResponseRegistry.getInstance().parse(jsonObject, TestConstants.DITTO_HEADERS);
assertThat(parsedCommandResponse).isEqualTo(commandResponse);
}
use of org.eclipse.ditto.base.model.signals.commands.CommandResponse in project ditto by eclipse.
the class AggregatedDevOpsCommandResponseTest method testAggregatedPiggybackCommandResponseFromListOfCommandResponsesSerialization.
@Test
public void testAggregatedPiggybackCommandResponseFromListOfCommandResponsesSerialization() {
final ChangeLogLevelResponse changeLogLevelResponseForThings1 = ChangeLogLevelResponse.of("things", "1", true, DittoHeaders.empty());
final ChangeLogLevelResponse changeLogLevelResponseForThings2 = ChangeLogLevelResponse.of("things", "2", true, DittoHeaders.empty());
final ChangeLogLevelResponse changeLogLevelResponseForGateway1 = ChangeLogLevelResponse.of("gateway", "1", true, DittoHeaders.empty());
final AggregatedDevOpsCommandResponse aggregatedDevOpsCommandResponse = AggregatedDevOpsCommandResponse.of(Arrays.asList(changeLogLevelResponseForThings1, changeLogLevelResponseForThings2, changeLogLevelResponseForGateway1), RESPONSES_TYPE, HttpStatus.OK, DittoHeaders.empty());
final JsonObject responseToJson = aggregatedDevOpsCommandResponse.toJson();
final CommandResponse parsedCommandResponse = underTest.parse(responseToJson, DittoHeaders.empty());
assertThat(parsedCommandResponse).isEqualTo(aggregatedDevOpsCommandResponse);
assertThat(parsedCommandResponse.toJson()).isEqualTo(responseToJson);
}
use of org.eclipse.ditto.base.model.signals.commands.CommandResponse in project ditto by eclipse.
the class SendingTest method monitorAcknowledgementSendFailureInCaseOfUnhandledException.
@Test
public void monitorAcknowledgementSendFailureInCaseOfUnhandledException() {
final var source = Mockito.mock(WithThingId.class, withSettings().extraInterfaces(Signal.class, DittoHeadersSettable.class));
final var thingId = ThingId.generateRandom();
Mockito.when(source.getEntityId()).thenReturn(thingId);
Mockito.when(((DittoHeadersSettable<?>) source).getDittoHeaders()).thenReturn(dittoHeaders);
Mockito.when(mappedOutboundSignal.getSource()).thenReturn((Signal) source);
Mockito.when(autoAckTarget.getIssuedAcknowledgementLabel()).thenReturn(Optional.of(ACKNOWLEDGEMENT_LABEL));
final var thrownException = new IllegalStateException("Test");
final var acknowledgementPayload = JsonObject.newBuilder().set("message", "Encountered <IllegalStateException>.").set("description", "Test").build();
final var acknowledgementStatus = HttpStatus.INTERNAL_SERVER_ERROR;
final var expectedException = MessageSendingFailedException.newBuilder().httpStatus(acknowledgementStatus).message("Received negative acknowledgement for label <" + ACKNOWLEDGEMENT_LABEL + ">.").description("Payload: " + acknowledgementPayload).build();
final CompletableFuture<SendResult> failedFuture = new CompletableFuture<>();
failedFuture.completeExceptionally(thrownException);
final Sending underTest = new Sending(sendingContext, failedFuture, connectionIdResolver, logger);
final Optional<CompletionStage<CommandResponse>> result = underTest.monitorAndAcknowledge(exceptionConverter);
Mockito.verify(publishedMonitor).exception(externalMessage, thrownException);
Mockito.verify(acknowledgedMonitor).failure(externalMessage, expectedException);
assertThat(result).hasValueSatisfying(resultFuture -> assertThat(resultFuture).isCompletedWithValueMatching(response -> {
final SoftAssertions softly = new SoftAssertions();
softly.assertThat(response).isInstanceOf(Acknowledgement.class);
final Acknowledgement ack = (Acknowledgement) response;
softly.assertThat(ack.getLabel().toString()).isEqualTo(ACKNOWLEDGEMENT_LABEL.toString());
softly.assertThat(ack.getEntity()).contains(acknowledgementPayload);
softly.assertThat(ack.getEntityId().toString()).isEqualTo(thingId.toString());
softly.assertThat(ack.getHttpStatus()).isEqualTo(acknowledgementStatus);
softly.assertAll();
return true;
}));
}
Aggregations