Search in sources :

Example 1 with CommandResponse

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);
}
Also used : CommandResponse(org.eclipse.ditto.base.model.signals.commands.CommandResponse) Test(org.junit.Test)

Example 2 with 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));
            }
        }
    };
}
Also used : HttpStatus(org.eclipse.ditto.base.model.common.HttpStatus) ActorRef(akka.actor.ActorRef) TestKit(akka.testkit.javadsl.TestKit) ModifyThing(org.eclipse.ditto.things.model.signals.commands.modify.ModifyThing) CommandResponse(org.eclipse.ditto.base.model.signals.commands.CommandResponse) DittoHeaderInvalidException(org.eclipse.ditto.base.model.exceptions.DittoHeaderInvalidException) TestProbe(akka.testkit.TestProbe) Test(org.junit.Test)

Example 3 with CommandResponse

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);
}
Also used : JsonObject(org.eclipse.ditto.json.JsonObject) PolicyCommandResponse(org.eclipse.ditto.policies.model.signals.commands.PolicyCommandResponse) CommandResponse(org.eclipse.ditto.base.model.signals.commands.CommandResponse) Test(org.junit.Test)

Example 4 with 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);
}
Also used : JsonObject(org.eclipse.ditto.json.JsonObject) CommandResponse(org.eclipse.ditto.base.model.signals.commands.CommandResponse) Test(org.junit.Test)

Example 5 with CommandResponse

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;
    }));
}
Also used : SoftAssertions(org.assertj.core.api.SoftAssertions) DittoAcknowledgementLabel(org.eclipse.ditto.base.model.acks.DittoAcknowledgementLabel) Acknowledgement(org.eclipse.ditto.base.model.signals.acks.Acknowledgement) Signal(org.eclipse.ditto.base.model.signals.Signal) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) AcknowledgementLabel(org.eclipse.ditto.base.model.acks.AcknowledgementLabel) ConnectionMonitor(org.eclipse.ditto.connectivity.service.messaging.monitoring.ConnectionMonitor) CompletableFuture(java.util.concurrent.CompletableFuture) HttpStatus(org.eclipse.ditto.base.model.common.HttpStatus) OutboundSignal(org.eclipse.ditto.connectivity.api.OutboundSignal) DittoHeadersSettable(org.eclipse.ditto.base.model.headers.DittoHeadersSettable) Target(org.eclipse.ditto.connectivity.model.Target) DittoRuntimeException(org.eclipse.ditto.base.model.exceptions.DittoRuntimeException) JsonObject(org.eclipse.ditto.json.JsonObject) DittoHeaders(org.eclipse.ditto.base.model.headers.DittoHeaders) ThreadSafeDittoLoggingAdapter(org.eclipse.ditto.internal.utils.akka.logging.ThreadSafeDittoLoggingAdapter) TestName(org.junit.rules.TestName) CommandResponse(org.eclipse.ditto.base.model.signals.commands.CommandResponse) GenericTarget(org.eclipse.ditto.connectivity.model.GenericTarget) Before(org.junit.Before) Assertions.assertThatNullPointerException(org.assertj.core.api.Assertions.assertThatNullPointerException) Test(org.junit.Test) ThingId(org.eclipse.ditto.things.model.ThingId) ExpressionResolver(org.eclipse.ditto.placeholders.ExpressionResolver) Mockito(org.mockito.Mockito) WithThingId(org.eclipse.ditto.things.model.WithThingId) CompletionStage(java.util.concurrent.CompletionStage) Rule(org.junit.Rule) WithDittoHeaders(org.eclipse.ditto.base.model.headers.WithDittoHeaders) Optional(java.util.Optional) Mockito.withSettings(org.mockito.Mockito.withSettings) MessageSendingFailedException(org.eclipse.ditto.connectivity.model.MessageSendingFailedException) ExternalMessage(org.eclipse.ditto.connectivity.api.ExternalMessage) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.eq(org.mockito.Mockito.eq) SoftAssertions(org.assertj.core.api.SoftAssertions) Acknowledgement(org.eclipse.ditto.base.model.signals.acks.Acknowledgement) Signal(org.eclipse.ditto.base.model.signals.Signal) OutboundSignal(org.eclipse.ditto.connectivity.api.OutboundSignal) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionStage(java.util.concurrent.CompletionStage) DittoHeadersSettable(org.eclipse.ditto.base.model.headers.DittoHeadersSettable) Test(org.junit.Test)

Aggregations

CommandResponse (org.eclipse.ditto.base.model.signals.commands.CommandResponse)12 Test (org.junit.Test)9 JsonObject (org.eclipse.ditto.json.JsonObject)6 ActorRef (akka.actor.ActorRef)4 DittoRuntimeException (org.eclipse.ditto.base.model.exceptions.DittoRuntimeException)4 Duration (java.time.Duration)3 DittoHeaders (org.eclipse.ditto.base.model.headers.DittoHeaders)3 NotUsed (akka.NotUsed)2 Patterns (akka.pattern.Patterns)2 Sink (akka.stream.javadsl.Sink)2 List (java.util.List)2 Optional (java.util.Optional)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletionStage (java.util.concurrent.CompletionStage)2 Nullable (javax.annotation.Nullable)2 ConditionChecker.checkNotNull (org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull)2 HttpStatus (org.eclipse.ditto.base.model.common.HttpStatus)2 Jsonifiable (org.eclipse.ditto.base.model.json.Jsonifiable)2 DittoMetrics (org.eclipse.ditto.internal.utils.metrics.DittoMetrics)2 StartedTimer (org.eclipse.ditto.internal.utils.metrics.instruments.timer.StartedTimer)2