ClientSecurityExceptionMessageResolver { public void consumeExceptionMessage(final Throwable exception, final Consumer<String> messageConsumer) { if (shouldDisplayError().test(exception)) { final String message = isSecurityManagementException().test(exception) ? getSecurityExceptionMessage().apply((SecurityManagementException) exception) : getExceptionMessage().apply(exception); messageConsumer.accept(message); } }  @PostConstruct void registerMessageResolvers(); void consumeExceptionMessage(final Throwable exception,
                                        final Consumer<String> messageConsumer);  }
@Test @SuppressWarnings("unchecked") public void testConsumeSecurityExceptions() { final Consumer<String> c0 = mock(Consumer.class); tested.consumeExceptionMessage(new SecurityManagementException("custom"), c0); verify(c0, times(1)).accept(eq("custom")); final Consumer<String> c1 = mock(Consumer.class); tested.consumeExceptionMessage(new EntityNotFoundException("id1"), c1); verify(c1, times(1)).accept(contains("id1")); final Consumer<String> c2 = mock(Consumer.class); tested.consumeExceptionMessage(new UserNotFoundException("user1"), c2); verify(c2, times(1)).accept(contains("user1")); final Consumer<String> c3 = mock(Consumer.class); tested.consumeExceptionMessage(new GroupNotFoundException("group1"), c3); verify(c3, times(1)).accept(contains("group1")); final Consumer<String> c4 = mock(Consumer.class); tested.consumeExceptionMessage(new UnsupportedServiceCapabilityException(Capability.CAN_ADD_USER), c4); verify(c4, times(1)).accept(contains(Capability.CAN_ADD_USER.name())); final Consumer<String> c5 = mock(Consumer.class); tested.consumeExceptionMessage(new UserAlreadyExistsException("aUser"), c5); verify(c5, times(1)).accept(contains("aUser")); final Consumer<String> c6 = mock(Consumer.class); tested.consumeExceptionMessage(new RealmManagementNotAuthorizedException("aRealm"), c6); verify(c6, times(1)).accept(contains("aRealm")); }
@Test @SuppressWarnings("unchecked") public void testSkipSecurityExceptions() { final Consumer<String> c1 = mock(Consumer.class); tested.consumeExceptionMessage(new NoImplementationAvailableException(), c1); verify(c1, never()).accept(anyString()); }
@Test @SuppressWarnings("unchecked") public void testConsumeExceptions() { final Consumer<String> c1 = mock(Consumer.class); tested.consumeExceptionMessage(new RuntimeException("anErrorMessage"), c1); verify(c1, times(1)).accept(eq("anErrorMessage")); final Consumer<String> c2 = mock(Consumer.class); tested.consumeExceptionMessage(new RuntimeException(new RuntimeException("rootMessage")), c2); verify(c2, times(1)).accept(eq("rootMessage")); }