MessageCommandAckRequestSetter extends AbstractCommandAckRequestSetter<MessageCommand<?, ?>> { public static MessageCommandAckRequestSetter getInstance() { return INSTANCE; } private  MessageCommandAckRequestSetter(); static MessageCommandAckRequestSetter getInstance(); @Override boolean isApplicable(final MessageCommand<?, ?> command); @Override @SuppressWarnings({"unchecked", "rawtypes"}) Class<MessageCommand<?, ?>> getMatchedClass();  }
@Test public void tryToApplyNullCommand() { final MessageCommandAckRequestSetter underTest = MessageCommandAckRequestSetter.getInstance(); assertThatNullPointerException() .isThrownBy(() -> underTest.apply(null)) .withMessage("The command must not be null!") .withNoCause(); }
@Test public void addLiveResponseAckLabelByDefault() { final DittoHeaders dittoHeaders = DittoHeaders.newBuilder() .channel("live") .randomCorrelationId() .build(); final MessageCommand<?, ?> command = SendThingMessage.of(THING_ID, MESSAGE, dittoHeaders); final MessageCommand<?, ?> expected = command.setDittoHeaders(DittoHeaders.newBuilder(dittoHeaders) .acknowledgementRequest(AcknowledgementRequest.of(DittoAcknowledgementLabel.LIVE_RESPONSE)) .responseRequired(true) .build()); final MessageCommandAckRequestSetter underTest = MessageCommandAckRequestSetter.getInstance(); assertThat(underTest.apply(command)).isEqualTo(expected); }
@Test public void removeLiveResponseAckLabelWhenResponseRequiredFalse() { final DittoHeaders dittoHeaders = DittoHeaders.newBuilder() .channel("live") .responseRequired(false) .acknowledgementRequest(AcknowledgementRequest.of(DittoAcknowledgementLabel.LIVE_RESPONSE)) .randomCorrelationId() .build(); final MessageCommand<?, ?> command = SendThingMessage.of(THING_ID, MESSAGE, dittoHeaders); final DittoHeaders expectedHeaders = DittoHeaders.newBuilder(dittoHeaders) .acknowledgementRequests(Collections.emptyList()) .responseRequired(false) .build(); final MessageCommand<?, ?> expected = command.setDittoHeaders(expectedHeaders); final MessageCommandAckRequestSetter underTest = MessageCommandAckRequestSetter.getInstance(); final MessageCommand<?, ?> appliedCommand = underTest.apply(command); assertThat(appliedCommand.getDittoHeaders()).isEqualTo(expectedHeaders); assertThat(appliedCommand).isEqualTo(expected); }
@Test public void removeLiveResponseAckLabelFromOthersWhenResponseRequiredFalse() { final AcknowledgementRequest ackRequest1 = AcknowledgementRequest.of(AcknowledgementLabel.of("FOO")); final AcknowledgementRequest ackRequest2 = AcknowledgementRequest.of(AcknowledgementLabel.of("BAR")); final DittoHeaders dittoHeaders = DittoHeaders.newBuilder() .channel("live") .responseRequired(false) .acknowledgementRequest(ackRequest1, AcknowledgementRequest.of(DittoAcknowledgementLabel.LIVE_RESPONSE), ackRequest2) .randomCorrelationId() .build(); final MessageCommand<?, ?> command = SendThingMessage.of(THING_ID, MESSAGE, dittoHeaders); final DittoHeaders expectedHeaders = DittoHeaders.newBuilder(dittoHeaders) .acknowledgementRequest(ackRequest1, ackRequest2) .responseRequired(false) .build(); final MessageCommand<?, ?> expected = command.setDittoHeaders(expectedHeaders); final MessageCommandAckRequestSetter underTest = MessageCommandAckRequestSetter.getInstance(); final MessageCommand<?, ?> appliedCommand = underTest.apply(command); assertThat(appliedCommand.getDittoHeaders()).isEqualTo(expectedHeaders); assertThat(appliedCommand).isEqualTo(expected); }
@Test public void filterOutOtherBuiltInDittoAcknowledgementLabels() { final DittoHeaders dittoHeaders = DittoHeaders.newBuilder() .channel("live") .acknowledgementRequest(AcknowledgementRequest.of(DittoAcknowledgementLabel.TWIN_PERSISTED), AcknowledgementRequest.of(DittoAcknowledgementLabel.LIVE_RESPONSE)) .randomCorrelationId() .build(); final MessageCommand<?, ?> command = SendThingMessage.of(THING_ID, MESSAGE, dittoHeaders); final DittoHeaders expectedHeaders = dittoHeaders.toBuilder() .acknowledgementRequest(AcknowledgementRequest.of(DittoAcknowledgementLabel.LIVE_RESPONSE)) .responseRequired(true) .build(); final MessageCommand<?, ?> expected = SendThingMessage.of(THING_ID, MESSAGE, expectedHeaders); final MessageCommandAckRequestSetter underTest = MessageCommandAckRequestSetter.getInstance(); assertThat(underTest.apply(command)).isEqualTo(expected); }
@Test public void addsLiveResponseAckLabelToAlreadyRequiredAckLabels() { final AcknowledgementRequest ackRequest1 = AcknowledgementRequest.of(AcknowledgementLabel.of("FOO")); final AcknowledgementRequest ackRequest2 = AcknowledgementRequest.of(AcknowledgementLabel.of("BAR")); final DittoHeaders dittoHeaders = DittoHeaders.newBuilder() .channel("live") .acknowledgementRequest(ackRequest1, ackRequest2) .randomCorrelationId() .responseRequired(true) .build(); final MessageCommand<?, ?> command = SendThingMessage.of(THING_ID, MESSAGE, dittoHeaders); final MessageCommandAckRequestSetter underTest = MessageCommandAckRequestSetter.getInstance(); final DittoHeaders expectedHeaders = dittoHeaders.toBuilder() .acknowledgementRequest(ackRequest1, ackRequest2, AcknowledgementRequest.of(DittoAcknowledgementLabel.LIVE_RESPONSE)) .build(); final MessageCommand<?, ?> expectedCommand = command.setDittoHeaders(expectedHeaders); final MessageCommand<?, ?> appliedCommand = underTest.apply(command); assertThat(appliedCommand.getDittoHeaders()).isEqualTo(expectedHeaders); assertThat(appliedCommand).isEqualTo(expectedCommand); }
@Test public void notAddingLiveResponseAckLabelToExplicitlyEmptyRequiredAckLabels() { final DittoHeaders dittoHeaders = DittoHeaders.newBuilder() .channel("live") .acknowledgementRequests(Collections.emptyList()) .randomCorrelationId() .responseRequired(true) .build(); final MessageCommand<?, ?> command = SendThingMessage.of(THING_ID, MESSAGE, dittoHeaders); final MessageCommandAckRequestSetter underTest = MessageCommandAckRequestSetter.getInstance(); final DittoHeaders expectedHeaders = dittoHeaders.toBuilder() .acknowledgementRequests(Collections.emptyList()) .build(); final MessageCommand<?, ?> expectedCommand = command.setDittoHeaders(expectedHeaders); final MessageCommand<?, ?> appliedCommand = underTest.apply(command); assertThat(appliedCommand.getDittoHeaders()).isEqualTo(expectedHeaders); assertThat(appliedCommand).isEqualTo(expectedCommand); }