AcknowledgementForwarderActorStarter implements Supplier<Optional<ActorRef>> { @Override public Optional<ActorRef> get() { ActorRef actorRef = null; if (hasEffectiveAckRequests(signal)) { try { actorRef = startAckForwarderActor(dittoHeaders); } catch (final InvalidActorNameException e) { declineAllNonDittoAckRequests(getDuplicateCorrelationIdException(e)); } } return Optional.ofNullable(actorRef); } private  AcknowledgementForwarderActorStarter(final ActorContext context,
            final EntityIdWithType entityId,
            final Signal<?> signal,
            final AcknowledgementConfig acknowledgementConfig); @Override Optional<ActorRef> get(); Optional<String> getConflictFree();  }
@Test public void getEmptyOptionalIfNoAcknowledgementsRequested() { final DittoHeaders dittoHeaders = DittoHeaders.newBuilder().correlationId(testName.getMethodName()).build(); final AcknowledgementForwarderActorStarter underTest = getActorStarter(dittoHeaders); softly.assertThat(underTest.get()).isNotPresent(); Mockito.verifyNoInteractions(actorContext); }
@Test public void startForwarderActorSuccessfully() { final DittoHeaders dittoHeaders = DittoHeaders.newBuilder() .correlationId(testName.getMethodName()) .acknowledgementRequest(AcknowledgementRequest.of(DittoAcknowledgementLabel.TWIN_PERSISTED), AcknowledgementRequest.of(AcknowledgementLabel.of("my-ack"))) .build(); final AcknowledgementForwarderActorStarter underTest = getActorStarter(dittoHeaders); softly.assertThat(underTest.get()).isPresent(); }
@Test public void startForwarderActorWithDuplicateCorrelationId() { final AcknowledgementLabel customAckLabel = AcknowledgementLabel.of("my-ack"); final String correlationId = testName.getMethodName(); final DittoHeaders dittoHeaders = DittoHeaders.newBuilder() .correlationId(correlationId) .acknowledgementRequest(AcknowledgementRequest.of(DittoAcknowledgementLabel.TWIN_PERSISTED), AcknowledgementRequest.of(customAckLabel)) .build(); final AcknowledgementRequestDuplicateCorrelationIdException expectedException = AcknowledgementRequestDuplicateCorrelationIdException.newBuilder(correlationId) .dittoHeaders(dittoHeaders) .build(); final Acknowledgement expectedNack = Acknowledgement.of(customAckLabel, KNOWN_ENTITY_ID, HttpStatusCode.CONFLICT, dittoHeaders, expectedException.toJson()); final AcknowledgementForwarderActorStarter underTest = getActorStarter(dittoHeaders); new TestKit(actorSystem) {{ softly.assertThat(underTest.get()).as("first start").isPresent(); testProbe.expectNoMessage(FiniteDuration.apply(200, TimeUnit.MILLISECONDS)); softly.assertThat(underTest.get()).as("second start").isNotPresent(); final Acknowledgement firstNack = testProbe.expectMsgClass(Acknowledgement.class); expectNoMessage(); softly.assertThat(firstNack).isEqualTo(expectedNack); }}; }