Java Code Examples for org.springframework.cloud.netflix.zuul.util.ZuulRuntimeException#getCause()

The following examples show how to use org.springframework.cloud.netflix.zuul.util.ZuulRuntimeException#getCause() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServiceAuthenticationFilterTest.java    From api-layer with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void givenValidJwt_whenCommandFailed_thenInternalError() {
    String jwtToken = "validJwtToken";
    AuthenticationCommand cmd = createJwtValidationCommand(jwtToken);
    doThrow(new RuntimeException()).when(cmd).apply(null);
    doReturn(TokenAuthentication.createAuthenticated("user", jwtToken)).when(authenticationService).validateJwtToken(jwtToken);
    CounterFactory.initialize(new CounterFactory() {
        @Override
        public void increment(String name) {
        }
    });

    try {
        serviceAuthenticationFilter.run();
        fail();
    } catch (ZuulRuntimeException zre) {
        assertTrue(zre.getCause() instanceof ZuulException);
        ZuulException ze = (ZuulException) zre.getCause();
        assertEquals(500, ze.nStatusCode);
    }
}
 
Example 2
Source File: ServiceAuthenticationFilterTest.java    From api-layer with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testRun() {
    Mockito.when(serviceAuthenticationService.getAuthenticationCommand(anyString(), any())).thenReturn(command);

    HttpServletRequest request = mock(HttpServletRequest.class);

    RequestContext requestContext = mock(RequestContext.class);
    when(requestContext.getRequest()).thenReturn(request);
    when(requestContext.get(SERVICE_ID_KEY)).thenReturn("service");
    RequestContext.testSetCurrentContext(requestContext);

    when(authenticationService.getJwtTokenFromRequest(any())).thenReturn(Optional.of("token"));

    serviceAuthenticationFilter.run();
    verify(serviceAuthenticationService, times(1)).getAuthenticationCommand("service", "token");
    verify(command, times(1)).apply(null);

    when(authenticationService.getJwtTokenFromRequest(any())).thenReturn(Optional.empty());
    serviceAuthenticationFilter.run();
    verify(serviceAuthenticationService, times(1)).getAuthenticationCommand("service", null);
    verify(serviceAuthenticationService, times(2)).getAuthenticationCommand(anyString(), any());

    reset(requestContext);
    reset(authenticationService);
    CounterFactory.initialize(new CounterFactory() {
        @Override
        public void increment(String name) {
        }
    });
    when(requestContext.get(SERVICE_ID_KEY)).thenReturn("error");
    when(authenticationService.getJwtTokenFromRequest(any())).thenReturn(Optional.of("token"));
    when(serviceAuthenticationService.getAuthenticationCommand(eq("error"), any()))
        .thenThrow(new RuntimeException("Potential exception"));
    try {
        serviceAuthenticationFilter.run();
        fail();
    } catch (ZuulRuntimeException zre) {
        assertTrue(zre.getCause() instanceof ZuulException);
        ZuulException ze = (ZuulException) zre.getCause();
        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), ze.nStatusCode);
        assertEquals(String.valueOf(new RuntimeException("Potential exception")), ze.errorCause);
    }
}