HandlerMethod { @SuppressWarnings("unchecked") public <T> Envelope<T> execute(final Envelope<?> envelope) { trace(LOGGER, () -> format("Dispatching to handler %s.%s : %s", handlerInstance.getClass().toString(), handlerMethod.getName(), envelope)); try { final Object obj = handlerMethod.invoke(handlerInstance, envelope); trace(LOGGER, () -> { final Optional<Object> response = Optional.ofNullable(obj); if (response.isPresent() && response.get() instanceof JsonEnvelope) { return format("Response received from handler %s.%s : %s", handlerInstance.getClass().toString(), handlerMethod.getName(), response.get()); } return format("Response from handler %s.%s with id %s was void", handlerInstance.getClass().toString(), handlerMethod.getName(), envelope.metadata().id().toString()); }); return (Envelope<T>) obj; } catch (Exception ex) { if (ex.getCause() instanceof RuntimeException) { throw (RuntimeException) ex.getCause(); } else { throw handlerExecutionExceptionOf(envelope, ex.getCause()); } } } HandlerMethod(final Object object, final Method method, final Class<?> expectedReturnType); @SuppressWarnings("unchecked") Envelope<T> execute(final Envelope<?> envelope); @Override String toString(); boolean isDirect(); Class<?> getPayloadType();  }
@Test public void shouldExecuteAsynchronousHandlerMethod() throws Exception { Object result = asyncHandlerInstance().execute(envelope); verify(asynchronousCommandHandler).handles(envelope); assertThat(result, nullValue()); }
@Test public void shouldExecuteSynchronousHandlerMethod() throws Exception { when(synchronousCommandHandler.handles(envelope)).thenReturn(envelope); Object result = syncHandlerInstance().execute(envelope); assertThat(result, sameInstance(envelope)); }
@Test public void shouldHandlePojoAsynchronously() { final TestPojo testPojo = new TestPojo(); final Metadata metadata = Envelope.metadataBuilder() .withId(UUID.randomUUID()) .withName("test").build(); final Envelope<TestPojo> testPojoEnvelope = Envelope.envelopeFrom(metadata, testPojo); Object result = asyncPojoHandlerInstance().execute(testPojoEnvelope); assertThat(result, nullValue()); }
@SuppressWarnings("unchecked") @Test public void shouldHandlePojoSynchronously() { final TestPojo testPojo = new TestPojo(); final String payloadId = "3f47ab7e-aecc-4cec-9246-c32066ef5ba1"; final String payloadName = "payload name"; final long payloadVersion = 200L; testPojo.setPayloadId(payloadId); testPojo.setPayloadName(payloadName); testPojo.setPayloadVersion(payloadVersion); final Metadata metadata = Envelope.metadataBuilder() .withId(UUID.randomUUID()) .withName("test").build(); final Envelope<TestPojo> requestPojoEnvelope = Envelope.envelopeFrom(metadata, testPojo); final Envelope<TestPojo> result = syncPojoHandlerInstance().execute(requestPojoEnvelope); verify(synchronousPojoCommandHandler).handles(any(Envelope.class)); final TestPojo resultPojo = result.payload(); assertThat(resultPojo.getPayloadId(), is(payloadId)); assertThat(resultPojo.getPayloadName(), is(payloadName)); assertThat(resultPojo.getPayloadVersion(), is(payloadVersion)); }
@Test public void shouldRethrowRuntimeException() throws Exception { doThrow(new RuntimeException("messageABC")).when(asynchronousCommandHandler).handles(envelope); expectedException.expect(RuntimeException.class); expectedException.expectMessage("messageABC"); asyncHandlerInstance().execute(envelope); }
@Test public void shouldWrapCheckedException() throws Exception { final Exception thrownException = new Exception("messageABC"); doThrow(thrownException).when(checkedExcCommandHandler).handles(envelope); expectedException.expect(HandlerExecutionException.class); expectedException.expectCause(is(thrownException)); new HandlerMethod(checkedExcCommandHandler, method(new CheckedExceptionThrowingCommandHandler(), "handles"), JsonEnvelope.class).execute(envelope); }