DecoratingRequester implements Requester { @Override public <T> T request(final Invocation<T> invocation) throws Exception { assertNotNull(invocation, "Invocation must not null"); logger.debug("Request with invocation: {}", invocation); Response<T> response; final Invocation<T> decorated = withDecorated(invocation); logger.trace("Decorated: {}", decorated); try { final T value = decorated.invoke(); logger.debug("Success: {}", value); response = Response.success(value); } catch (Exception e) { logger.debug("Failure: {}", e.toString()); response = Response.fail(e); response = handleFailover(decorated, response); } if (null != response.getError()) { final Exception error = response.getError(); error.setStackTrace(concatStackTrace(new Throwable().getStackTrace(), error.getStackTrace())); throw error; } return response.getValue(); } DecoratingRequester(); @Override T request(final Invocation<T> invocation);  }
@Test public void testSuccessRequestOnNoContext() throws Exception { final Requester requester = new DecoratingRequester(); final String expected = randomUUID().toString(); final String name = randomUUID().toString(); final String actual = requester.request(new TestInvocation<>(new RequestMethod<String>() { @Override public String getName() { return name; } @Override protected String runInternal(final List<Object> parameters) throws Exception { return expected; } })); assertEquals(expected, actual); }
@Test public void testFailRequestOnNoContext() throws Exception { final Requester requester = new DecoratingRequester(); final Exception expected = new IllegalStateException(); final String name = randomUUID().toString(); final TestInvocation<String> invocation = new TestInvocation<>(new RequestMethod<String>() { @Override public String getName() { return name; } @Override protected String runInternal(final List<Object> parameters) throws Exception { throw expected; } }); try { requester.request(invocation); fail("Should throw exception"); } catch (Exception actual) { assertEquals(expected, actual); } }
@Test public void testSuccessRequestOnContext() throws Throwable { runOnOtherThread(new Runnable() { @Override public void run() { try { ContextHolder.attach(context); final Requester requester = new DecoratingRequester(); final String expected = randomUUID().toString(); final String name = randomUUID().toString(); final String actual = requester.request(new TestInvocation<>(new RequestMethod<String>() { @Override public String getName() { return name; } @Override protected String runInternal(final List<Object> parameters) throws Exception { return expected; } })); assertEquals(expected, actual); } catch (Exception e) { throw new IllegalStateException(e); } finally { ContextHolder.remove(); } } }); }
@Test public void testFailureRequestOnContext() throws Exception { final Exception expected = new IllegalStateException(); runOnOtherThread(new Runnable() { @Override public void run() { try { ContextHolder.attach(context); final Requester requester = new DecoratingRequester(); final String name = randomUUID().toString(); final String ret = requester .request(new TestInvocation<>(new RequestMethod<String>() { @Override public String getName() { return name; } @Override protected String runInternal(final List<Object> parameters) throws Exception { throw expected; } })); fail("Should be throw error"); } catch (Exception actual) { assertEquals(expected, actual); } finally { ContextHolder.remove(); } } }); }