RuntimeExceptionMapper extends CandlepinExceptionMapper implements ExceptionMapper<RuntimeException> { @Override public Response toResponse(RuntimeException exception) { MediaType responseMediaType = determineBestMediaType(); ResponseBuilder bldr = null; Throwable cause = exception.getCause() == null ? exception : exception.getCause(); if (cause instanceof CandlepinException) { bldr = getBuilder((CandlepinException) cause, responseMediaType); } else if (exception instanceof CandlepinException) { bldr = getBuilder((CandlepinException) exception, responseMediaType); } else { bldr = getDefaultBuilder(cause, null, responseMediaType); } return bldr.build(); }  @Override Response toResponse(RuntimeException exception);  }
@Test public void toResponseBasicRuntimeException() { when(req.getHeader(HttpHeaderNames.ACCEPT)).thenReturn("application/json"); Response r = rem.toResponse(new RuntimeException("test exception")); assertNotNull(r); assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), r.getStatus()); MultivaluedMap<String, Object> mm = r.getMetadata(); assertNotNull(mm); assertEquals(MediaType.APPLICATION_JSON_TYPE, mm.get("Content-Type").get(0)); assertEquals("${version}-${release}", r.getMetadata().get(VersionUtil.VERSION_HEADER).get(0)); verifyMessage(r, rtmsg("test exception")); }
@Disabled @Test public void defaultOptionsException() { Response forex = mock(Response.class); when(req.getHeader(HttpHeaderNames.ACCEPT)).thenReturn("application/json"); Response r = rem.toResponse(new DefaultOptionsMethodException("", forex)); assertEquals(forex, r); }
@Test public void nullAcceptHeader() { when(req.getHeader(HttpHeaderNames.ACCEPT)).thenReturn(null); Response r = rem.toResponse(new RuntimeException("null accept header")); assertNotNull(r); MultivaluedMap<String, Object> mm = r.getMetadata(); assertNotNull(mm); assertEquals(MediaType.APPLICATION_JSON_TYPE, mm.get("Content-Type").get(0)); assertEquals("${version}-${release}", r.getMetadata().get(VersionUtil.VERSION_HEADER).get(0)); verifyMessage(r, rtmsg("null accept header")); }
@Test public void candlepinException() { CandlepinException ce = mock(CandlepinException.class); when(ce.httpReturnCode()).thenReturn(Status.CONFLICT); ExceptionMessage em = new ExceptionMessage().setDisplayMessage("you screwed up"); when(ce.message()).thenReturn(em); when(req.getHeader(HttpHeaderNames.ACCEPT)).thenReturn("application/json"); assertEquals("you screwed up", em.toString()); Response r = rem.toResponse(ce); assertNotNull(r); assertEquals(Status.CONFLICT.getStatusCode(), r.getStatus()); verifyMessage(r, "you screwed up"); }
@Test public void candlepinExceptionWithChildNotCandleping() { CandlepinException ce = mock(CandlepinException.class); when(ce.httpReturnCode()).thenReturn(Status.CONFLICT); when(ce.message()).thenReturn( new ExceptionMessage().setDisplayMessage("you screwed up")); when(req.getHeader(HttpHeaderNames.ACCEPT)).thenReturn("application/json"); when(ce.getCause()).thenReturn(new ConflictException("you screwed up")); Response r = rem.toResponse(ce); assertNotNull(r); assertEquals(Status.CONFLICT.getStatusCode(), r.getStatus()); verifyMessage(r, "you screwed up"); }
@Test public void childException() { when(req.getHeader(HttpHeaderNames.ACCEPT)).thenReturn("application/json"); RuntimeException child = new RuntimeException("child ex"); RuntimeException re = new RuntimeException("foobar", child); Response r = rem.toResponse(re); assertNotNull(r); assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), r.getStatus()); verifyMessage(r, rtmsg("child ex")); }
@Test public void jbossBadRequestException() { String foo = "foo"; BadRequestException bre = new BadRequestException(foo); Response r = rem.toResponse(bre); assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), r.getStatus()); verifyMessage(r, "Runtime Error"); }