Skip to content

Commit

Permalink
Add first test for live command response handling of http-push
Browse files Browse the repository at this point in the history
Signed-off-by: Joel Bartelheimer <joel.bartelheimer@bosch.io>
  • Loading branch information
jbartelh committed Oct 8, 2021
1 parent 6a91202 commit d5b5dc2
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
package org.eclipse.ditto.connectivity.service.messaging.httppush;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.ditto.connectivity.service.messaging.httppush.HttpTestDittoProtocolHelper.signalToJsonString;
import static org.eclipse.ditto.connectivity.service.messaging.httppush.HttpTestDittoProtocolHelper.toMultiMapped;
import static org.mockito.Mockito.mock;

import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -67,6 +69,8 @@
import org.eclipse.ditto.protocol.ProtocolFactory;
import org.eclipse.ditto.protocol.adapter.DittoProtocolAdapter;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThing;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThingResponse;
import org.junit.Test;

import akka.actor.ActorRef;
Expand Down Expand Up @@ -423,6 +427,59 @@ public void testMessageCommandHttpPushCreatesCommandResponseFromProtocolMessage(
}};
}

@Test
public void testLiveCommandHttpPushCreatesLiveCommandResponseFromProtocolMessage() {
// Arrange
final var testCorrelationId = TestConstants.CORRELATION_ID.concat(".liveCommandHttpPush");
final var contentType = DittoConstants.DITTO_PROTOCOL_CONTENT_TYPE;
final var thingId = TestConstants.Things.THING_ID;

final var commandResponseDittoHeaders = DittoHeaders.newBuilder()
.contentType(contentType)
.channel("live")
.correlationId(testCorrelationId)
.build();

final var retrieveThingMockResponse = RetrieveThingResponse.of(thingId,
TestConstants.Things.THING,
null,
null,
commandResponseDittoHeaders);

httpPushFactory = mockHttpPushFactory(contentType, retrieveThingMockResponse.getHttpStatus(),
signalToJsonString(retrieveThingMockResponse));

final var target = ConnectivityModelFactory.newTargetBuilder()
.address(getOutboundAddress())
.originalAddress(getOutboundAddress())
.authorizationContext(TestConstants.Authorization.AUTHORIZATION_CONTEXT)
.headerMapping(TestConstants.HEADER_MAPPING)
.topics(Topic.LIVE_COMMANDS)
.build();

final var testKit = new TestKit(actorSystem);
final var publisherActor = testKit.childActorOf(getPublisherActorProps());
publisherCreated(testKit, publisherActor);

final var commandDittoHeaders = commandResponseDittoHeaders.toBuilder()
.responseRequired(true)
.build();

final Signal<?> command = RetrieveThing.of(thingId, commandDittoHeaders);

// Act
publisherActor.tell(toMultiMapped(command, target, testKit.getRef()), testKit.getRef());

// Assert
final var responseSignal = testKit.expectMsgClass(Signal.class);
assertThat(responseSignal).isInstanceOfSatisfying(RetrieveThingResponse.class, retrieveThingResponse -> {
assertThat((CharSequence) retrieveThingResponse.getEntityId()).isEqualTo(thingId);
assertThat(retrieveThingResponse.getHttpStatus()).isEqualTo(retrieveThingMockResponse.getHttpStatus());
assertThat(retrieveThingResponse.getDittoHeaders().getCorrelationId()).contains(testCorrelationId);
assertThat(retrieveThingResponse.getThing()).isEqualTo(TestConstants.Things.THING);
});
}

@Test
public void sendingLiveResponseWithWrongCorrelationIdDoesNotWork() {
new TestKit(actorSystem) {{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.service.messaging.httppush;

import java.util.Collections;
import java.util.function.Function;

import org.eclipse.ditto.base.model.json.Jsonifiable;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.connectivity.api.ExternalMessageFactory;
import org.eclipse.ditto.connectivity.api.OutboundSignal;
import org.eclipse.ditto.connectivity.api.OutboundSignalFactory;
import org.eclipse.ditto.connectivity.model.Target;
import org.eclipse.ditto.protocol.Adaptable;
import org.eclipse.ditto.protocol.ProtocolFactory;
import org.eclipse.ditto.protocol.adapter.DittoProtocolAdapter;

import akka.actor.ActorRef;

public final class HttpTestDittoProtocolHelper {

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

private static final Function<Signal<?>, Adaptable> TO_ADAPTABLE = DittoProtocolAdapter.newInstance()::toAdaptable;

public static String signalToJsonString(final Signal<?> signal) {
return TO_ADAPTABLE
.andThen(ProtocolFactory::wrapAsJsonifiableAdaptable)
.andThen(Jsonifiable::toJson)
.andThen(Object::toString)
.apply(signal);
}

public static OutboundSignal.MultiMapped toMultiMapped(final Signal<?> signal,
final Target target,
final ActorRef sender) {

final var outboundSignal =
OutboundSignalFactory.newOutboundSignal(signal, Collections.singletonList(target));

final var externalMessage =
ExternalMessageFactory.newExternalMessageBuilder(Collections.emptyMap())
.withText("payload")
.build();

return TO_ADAPTABLE
.andThen(a -> OutboundSignalFactory.newMappedOutboundSignal(outboundSignal, a, externalMessage))
.andThen(Collections::singletonList)
.andThen(ls -> OutboundSignalFactory.newMultiMappedOutboundSignal(ls, sender))
.apply(signal);
}
}

0 comments on commit d5b5dc2

Please sign in to comment.