DefaultSendRequestContext implements ConversationContext.SendRequestContext<T> { @Override public <R> ConversationContext.SendRequestContext<R> unwrap(Function<T, R> unwrapper) { if (expectClazz == null) { throw new ConversationContext.PlcWiringException("expectResponse must be called before first unwrap"); } if (onTimeoutConsumer == null) { onTimeoutConsumer = e -> { }; } commands.addLast(Either.left(unwrapper)); return new DefaultSendRequestContext<>(commands, timeout, finisher, request, context, expectClazz, packetConsumer, onTimeoutConsumer, errorConsumer); } DefaultSendRequestContext(Consumer<HandlerRegistration> finisher, T request, ConversationContext<T> context); protected  DefaultSendRequestContext(Deque<Either<Function<?, ?>, Predicate<?>>> commands, Duration timeout, Consumer<HandlerRegistration> finisher, Object request, ConversationContext<?> context, Class<?> expectClazz, Consumer<?> packetConsumer, Consumer<TimeoutException> onTimeoutConsumer, BiConsumer<?, ? extends Throwable> errorConsumer); @Override ConversationContext.SendRequestContext<T> expectResponse(Class<T> clazz, Duration timeout); @Override ConversationContext.SendRequestContext<T> check(Predicate<T> checker); @Override DefaultContextHandler handle(Consumer<T> packetConsumer); @Override ConversationContext.SendRequestContext<T> onTimeout(Consumer<TimeoutException> onTimeoutConsumer); @Override ConversationContext.SendRequestContext<T> onError(BiConsumer<T, E> errorConsumer); @Override ConversationContext.SendRequestContext<R> unwrap(Function<T, R> unwrapper); @Override ConversationContext.SendRequestContext<R> only(Class<R> clazz);  }
@Test void unwrap() { assertThat(SUT.expectClazz, nullValue()); assertThat(SUT.onTimeoutConsumer, nullValue()); assertThrows(ConversationContext.PlcWiringException.class, () -> SUT.unwrap(o -> o.toString())); SUT.expectResponse(Object.class, null); SUT.onTimeout(e -> e.printStackTrace()); ConversationContext.SendRequestContext<String> unwrap = SUT.unwrap(o -> o.toString()); assertThat(unwrap, notNullValue()); assertThat(SUT.commands, hasSize(2)); assertThat(SUT.expectClazz, notNullValue()); assertThat(SUT.onTimeoutConsumer, notNullValue()); }