Skip to content

Commit

Permalink
#1727 added unit test, using "thing-json:" placeholder in HTTP path
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Jäckle <thomas.jaeckle@beyonnex.io>
  • Loading branch information
thjaeckle committed Sep 14, 2023
1 parent 0dd5f33 commit c6fbf8b
Showing 1 changed file with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c6fbf8b

Please sign in to comment.