From c6fbf8bee15dffdd335c1ae9964010269521b5db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20J=C3=A4ckle?= Date: Thu, 14 Sep 2023 21:13:17 +0200 Subject: [PATCH] #1727 added unit test, using "thing-json:" placeholder in HTTP path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Jäckle --- .../httppush/HttpPublisherActorTest.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/connectivity/service/src/test/java/org/eclipse/ditto/connectivity/service/messaging/httppush/HttpPublisherActorTest.java b/connectivity/service/src/test/java/org/eclipse/ditto/connectivity/service/messaging/httppush/HttpPublisherActorTest.java index 4040777d42..e564c99c8f 100644 --- a/connectivity/service/src/test/java/org/eclipse/ditto/connectivity/service/messaging/httppush/HttpPublisherActorTest.java +++ b/connectivity/service/src/test/java/org/eclipse/ditto/connectivity/service/messaging/httppush/HttpPublisherActorTest.java @@ -58,6 +58,7 @@ import org.eclipse.ditto.json.JsonArray; import org.eclipse.ditto.json.JsonFactory; import org.eclipse.ditto.json.JsonObject; +import org.eclipse.ditto.json.JsonPointer; import org.eclipse.ditto.json.JsonValue; import org.eclipse.ditto.messages.model.Message; import org.eclipse.ditto.messages.model.MessageDirection; @@ -67,6 +68,8 @@ import org.eclipse.ditto.messages.model.signals.commands.SendThingMessageResponse; import org.eclipse.ditto.protocol.ProtocolFactory; import org.eclipse.ditto.protocol.adapter.DittoProtocolAdapter; +import org.eclipse.ditto.things.model.Thing; +import org.eclipse.ditto.things.model.ThingFieldSelector; 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; @@ -859,6 +862,64 @@ public void testAwsRequestSigning() throws Exception { }}; } + @Test + public void testSendMessageUsingThingJsonPlaceholderOnExtraAttributes() throws Exception { + new TestKit(actorSystem) {{ + httpPushFactory = mockHttpPushFactory("application/json", HttpStatus.OK, ""); + final var target = ConnectivityModelFactory.newTargetBuilder() + .address("POST:/foo/{{ thing-json:attributes/location }}") + .authorizationContext(TestConstants.Authorization.AUTHORIZATION_CONTEXT) + .topics(ConnectivityModelFactory.newFilteredTopicBuilder(Topic.LIVE_MESSAGES) + .withExtraFields(ThingFieldSelector.fromString("attributes")) + .build()) + .build(); + + final var connection = TestConstants.createConnection() + .toBuilder() + .targets(List.of(target)) + .build(); + final var props = HttpPublisherActor.props(connection, + httpPushFactory, + mock(ConnectivityStatusResolver.class), + ConnectivityConfig.of(actorSystem.settings().config())); + final var publisherActor = childActorOf(props); + publisherCreated(this, publisherActor); + + // WHEN: HTTP publisher sends an HTTP request + final Message message = Message.newBuilder( + MessageHeaders.newBuilder(MessageDirection.TO, TestConstants.Things.THING_ID, "fetch-weather") + .build() + ).build(); + final Signal source = SendThingMessage.of(TestConstants.Things.THING_ID, message, DittoHeaders.empty()); + final var outboundSignal = + OutboundSignalFactory.newOutboundSignal(source, Collections.singletonList(target)); + final var externalMessage = + ExternalMessageFactory.newExternalMessageBuilder(Collections.emptyMap()) + .withText("payload") + .build(); + var adaptable = DittoProtocolAdapter.newInstance().toAdaptable(source); + adaptable = ProtocolFactory.setExtra( + adaptable, + Thing.newBuilder() + .setAttribute(JsonPointer.of("location"), JsonValue.of("Hamburg")) + .build() + .toJson() + ); + final var mapped = + OutboundSignalFactory.newMappedOutboundSignal(outboundSignal, adaptable, externalMessage); + + publisherActor.tell( + OutboundSignalFactory.newMultiMappedOutboundSignal(Collections.singletonList(mapped), getRef()), + getRef()); + + // THEN: The request is signed by the configured request signing process. + final var request = received.take(); + assertThat(received).isEmpty(); + assertThat(request.method()).isEqualTo(HttpMethods.POST); + assertThat(request.getUri().getPathString()).isEqualTo("/foo/Hamburg"); + }}; + } + @Test public void testReservedHeaders() throws Exception { // GIVEN: reserved headers are set