MDCDecoratingHandler implements OutputHandler { @Override public void transitionInto(RunState runState, EventRouter eventRouter) { try (var closer = Closer.create()) { closer.register(MDC.putCloseable(WFI_ID, runState.workflowInstance().toString())); closer.register(MDC.putCloseable(WFI_STATE_COUNTER, Long.toString(runState.counter()))); closer.register(MDC.putCloseable(WFI_STATE_NAME, runState.state().toString())); delegate.transitionInto(runState, eventRouter); } catch (IOException e) { throw new RuntimeException(e); } } MDCDecoratingHandler(OutputHandler delegate); @Override void transitionInto(RunState runState, EventRouter eventRouter);  }
@Parameters(source = State.class) @Test public void shouldDecorateMDC(State state) { when(runState.state()).thenReturn(state); when(runState.counter()).thenReturn(COUNTER); var id = new AtomicReference<String>(); var stateCounter = new AtomicReference<String>(); var stateName = new AtomicReference<String>(); doAnswer(a -> { id.set(MDC.get("wfi-id")); stateCounter.set(MDC.get("wfi-state-counter")); stateName.set(MDC.get("wfi-state-name")); return null; }).when(delegate).transitionInto(any(), any()); sut.transitionInto(runState, eventRouter); assertThat(id.get(), is(WORKFLOW_INSTANCE.toString())); assertThat(stateCounter.get(), is(String.valueOf(COUNTER))); assertThat(stateName.get(), is(state.toString())); }
@Test public void propagatesIOException() { when(runState.state()).thenReturn(State.RUNNING); when(runState.counter()).thenReturn(COUNTER); var cause = new IOException("foo!"); doAnswer(a -> sneakyThrow(cause)).when(delegate).transitionInto(any(), any()); exception.expect(RuntimeException.class); exception.expectCause(is(cause)); sut.transitionInto(runState, eventRouter); }