OAuth2TokenInterceptor extends AbstractPhaseInterceptor<Message> { @Override public void handleMessage(final Message message) throws Fault { @SuppressWarnings("unchecked") Map<String, List<String>> headers = (Map<String, List<String>>) message.get(Message.PROTOCOL_HEADERS); if (headers == null) { headers = new TreeMap<String, List<String>>(); message.put(Message.PROTOCOL_HEADERS, headers); } headers.put(AUTHORIZATION_HEADER_NAME, Collections.singletonList(getHeaderValue())); } OAuth2TokenInterceptor(final AccessTokens accessTokens, final String serviceId); @Override void handleMessage(final Message message); static void check(final boolean expression, final String message);  }
@Test public void working() { String serviceId = "targetService"; AccessTokens accessTokens = Mockito.mock(AccessTokens.class); Mockito.when(accessTokens.get(Mockito.eq(serviceId))).thenReturn("0123456789"); Message message = new MessageImpl(); OAuth2TokenInterceptor interceptor = new OAuth2TokenInterceptor(accessTokens, serviceId); interceptor.handleMessage(message); Assertions.assertThat(message.get(Message.PROTOCOL_HEADERS)).isNotNull(); Map<String, List<String>> headers = (Map<String, List<String>>) message.get(Message.PROTOCOL_HEADERS); Assertions.assertThat(headers).containsValue(Collections.singletonList("Bearer 0123456789")); Mockito.verify(accessTokens, Mockito.atLeast(1)).get(Mockito.eq(serviceId)); }