Java Code Examples for akka.testkit.TestProbe#reply()

The following examples show how to use akka.testkit.TestProbe#reply() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThingsSseRouteBuilderTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private static void replySourceRef(final TestProbe testProbe, final Source<?, ?> source) {
    testProbe.reply(
            source.toMat(StreamRefs.sourceRef(), Keep.right())
                    .run(ActorMaterializer.create(actorSystem))
                    .toCompletableFuture()
                    .join()
    );
}
 
Example 2
Source File: ConnectionPersistenceActorTest.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void forwardSearchCommands() {
    new TestKit(actorSystem) {{
        final ConnectionId myConnectionId = ConnectionId.of(UUID.randomUUID().toString());
        final TestProbe clientActorsProbe = TestProbe.apply("clientActors", actorSystem);
        final TestProbe conciergeForwarderProbe = TestProbe.apply("conciergeForwarder", actorSystem);
        // Mock the client actors so that they forward all signals to clientActorsProbe with their own reference
        final ClientActorPropsFactory propsFactory = (a, b, c) -> Props.create(AbstractActor.class, () ->
                new AbstractActor() {
                    @Override
                    public Receive createReceive() {
                        return ReceiveBuilder.create()
                                .match(WithDittoHeaders.class, message ->
                                        clientActorsProbe.ref()
                                                .tell(WithSender.of(message, getSelf()), getSender())
                                )
                                .build();
                    }
                });
        final DittoProtocolSub dittoProtocolSub = Mockito.mock(DittoProtocolSub.class);
        when(dittoProtocolSub.subscribe(any(), any(), any())).thenReturn(CompletableFuture.completedStage(null));
        final Props connectionActorProps = Props.create(ConnectionPersistenceActor.class, () ->
                new ConnectionPersistenceActor(myConnectionId, dittoProtocolSub, conciergeForwarderProbe.ref(),
                        propsFactory, null, 999
                ));

        // GIVEN: connection persistence actor created with 2 client actors that are allowed to start on same node
        final ActorRef underTest = actorSystem.actorOf(connectionActorProps, myConnectionId.toString());
        underTest.tell(createClosedConnectionWith2Clients, getRef());
        expectMsgClass(CreateConnectionResponse.class);
        underTest.tell(OpenConnection.of(myConnectionId, DittoHeaders.empty()), getRef());
        assertThat(clientActorsProbe.expectMsgClass(WithSender.class).getMessage())
                .isInstanceOf(OpenConnection.class);
        clientActorsProbe.reply(new Status.Success("connected"));
        expectMsgClass(OpenConnectionResponse.class);

        // WHEN: 2 CreateSubscription commands are received
        // THEN: The 2 commands land in different client actors
        underTest.tell(CreateSubscription.of(DittoHeaders.empty()), getRef());
        underTest.tell(CreateSubscription.of(DittoHeaders.empty()), getRef());
        final WithSender<?> createSubscription1 = clientActorsProbe.expectMsgClass(WithSender.class);
        final WithSender<?> createSubscription2 = clientActorsProbe.expectMsgClass(WithSender.class);
        assertThat(createSubscription1.getSender()).isNotEqualTo(createSubscription2.getSender());

        // WHEN: RequestSubscription command is sent with the prefix of a received CreateSubscription command
        // THEN: The command lands in the same client actor
        final String subscriptionId1 =
                ((CreateSubscription) createSubscription1.getMessage()).getPrefix().orElseThrow() + "-suffix";
        underTest.tell(RequestFromSubscription.of(subscriptionId1, 99L, DittoHeaders.empty()), getRef());
        final WithSender<?> requestSubscription1 = clientActorsProbe.expectMsgClass(WithSender.class);
        assertThat(requestSubscription1.getMessage()).isInstanceOf(RequestFromSubscription.class);
        assertThat(requestSubscription1.getSender()).isEqualTo(createSubscription1.getSender());

        // Same for CancelSubscription
        final String subscriptionId2 =
                ((CreateSubscription) createSubscription2.getMessage()).getPrefix().orElseThrow() + "-suffix";
        underTest.tell(CancelSubscription.of(subscriptionId2, DittoHeaders.empty()), getRef());
        final WithSender<?> cancelSubscription2 = clientActorsProbe.expectMsgClass(WithSender.class);
        assertThat(cancelSubscription2.getSender()).isEqualTo(createSubscription2.getSender());
    }};
}
 
Example 3
Source File: MessageMappingProcessorActorTest.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testSignalEnrichment() {
    // GIVEN: test probe actor started with configured values
    final TestProbe conciergeForwarderProbe = TestProbe.apply("mockConciergeForwarderProbe", actorSystem);
    setUpConciergeForwarder(conciergeForwarderProbe.ref());

    new TestKit(actorSystem) {{
        // GIVEN: MessageMappingProcessor started with a test probe as the configured enrichment provider
        final ActorRef underTest = createMessageMappingProcessorActor(this);

        // WHEN: a signal is received with 2 targets, one with enrichment and one without
        final JsonFieldSelector extraFields = JsonFieldSelector.newInstance("attributes/x", "attributes/y");
        final AuthorizationSubject targetAuthSubject = AuthorizationSubject.newInstance("target:auth-subject");
        final AuthorizationSubject targetAuthSubjectWithoutIssuer = AuthorizationSubject.newInstance("auth-subject");
        final Target targetWithEnrichment = ConnectivityModelFactory.newTargetBuilder()
                .address("target/address")
                .authorizationContext(AuthorizationContext.newInstance(DittoAuthorizationContextType.UNSPECIFIED,
                        targetAuthSubject))
                .topics(ConnectivityModelFactory.newFilteredTopicBuilder(Topic.TWIN_EVENTS)
                        .withExtraFields(extraFields)
                        .build())
                .build();
        final Target targetWithoutEnrichment = ConnectivityModelFactory.newTargetBuilder(targetWithEnrichment)
                .topics(Topic.TWIN_EVENTS)
                .build();
        final Signal<?> signal = TestConstants.thingModified(Collections.emptyList());
        final OutboundSignal outboundSignal = OutboundSignalFactory.newOutboundSignal(signal,
                Arrays.asList(targetWithEnrichment, targetWithoutEnrichment));
        underTest.tell(outboundSignal, getRef());

        // THEN: a mapped signal without enrichment arrives first
        expectPublishedMappedMessage(expectMsgClass(PublishMappedMessage.class), signal, targetWithoutEnrichment);

        // THEN: Receive a RetrieveThing command from the facade.
        final RetrieveThing retrieveThing = conciergeForwarderProbe.expectMsgClass(RetrieveThing.class);
        assertThat(retrieveThing.getSelectedFields()).contains(extraFields);
        assertThat(retrieveThing.getDittoHeaders().getAuthorizationContext()).containsExactly(targetAuthSubject, targetAuthSubjectWithoutIssuer);

        final JsonObject extra = JsonObject.newBuilder().set("/attributes/x", 5).build();
        conciergeForwarderProbe.reply(
                RetrieveThingResponse.of(retrieveThing.getEntityId(), extra, retrieveThing.getDittoHeaders()));

        // THEN: Receive an outbound signal with extra fields.
        expectPublishedMappedMessage(expectMsgClass(PublishMappedMessage.class), signal, targetWithEnrichment,
                mapped -> assertThat(mapped.getAdaptable().getPayload().getExtra()).contains(extra));
    }};
}