RestServicesUtils { public static ResponseBuilder handleError( int status, RestError restError, String lang ) { List<RoboconfError> errors = new ArrayList<> (); errors.add( restError ); if( restError.getException() instanceof InvalidApplicationException ) { errors.addAll(((InvalidApplicationException) restError.getException()).getErrors()); } StringBuilder sb = new StringBuilder(); String sep = "\n\n"; for( String s : RoboconfErrorHelpers.formatErrors( errors, null, false ).values()) { sb.append( s ); sb.append( sep ); sep = "\n"; } String msg = sb.toString().trim() .replaceAll( "\"", "\\\"" ) .replaceAll( "\n", "\\\\n" ) .replaceAll( "\r", "\\\\r" ) .replaceAll( "\t", "\\\\t" ); return Response.status( status ).entity( "{\"reason\":\"" + msg + "\"}" ); } private  RestServicesUtils(); static ResponseBuilder handleError( int status, RestError restError, String lang ); static ResponseBuilder handleError( Status status, RestError restError, String lang ); static String lang( Manager manager );  }
@Test public void testHandleError_noException() { Response response = RestServicesUtils.handleError( Status.FORBIDDEN, new RestError( REST_APP_EXEC_ERROR ), null ).build(); Assert.assertEquals( Status.FORBIDDEN.getStatusCode(), response.getStatus()); Assert.assertEquals( "{\"reason\":\"" + tb.getString( REST_APP_EXEC_ERROR.toString()) + "\"}", response.getEntity()); }
@Test public void testHandleError_noException_withDetails() { Response response = RestServicesUtils.handleError( Status.FORBIDDEN, new RestError( REST_APP_EXEC_ERROR, name( "toto" )), null ).build(); Assert.assertEquals( Status.FORBIDDEN.getStatusCode(), response.getStatus()); Assert.assertEquals( "{\"reason\":\"" + tb.getString( REST_APP_EXEC_ERROR.toString()) + " Name: toto\"}", response.getEntity()); }
@Test public void testHandleError_withException() { Exception e = new RuntimeException( "hi" ); Response response = RestServicesUtils.handleError( Status.FORBIDDEN, new RestError( REST_APP_EXEC_ERROR, e ), null ).build(); StringBuilder expected = new StringBuilder(); expected.append( "{\"reason\":\"" ); expected.append( tb.getString( REST_APP_EXEC_ERROR.toString()) ); expected.append( " Exception Type: java.lang.RuntimeException. Log Reference: " ); Assert.assertEquals( Status.FORBIDDEN.getStatusCode(), response.getStatus()); Assert.assertTrue( response.getEntity().toString().startsWith( expected.toString())); Assert.assertTrue( response.getEntity().toString().endsWith( "\"}" )); }
@Test public void testHandleError_withException_withDetails() { Exception e = new RuntimeException( "hi" ); Response response = RestServicesUtils.handleError( Status.FORBIDDEN, new RestError( REST_APP_EXEC_ERROR, e, name( "titi" )), null ).build(); StringBuilder expected = new StringBuilder(); expected.append( "{\"reason\":\"" ); expected.append( tb.getString( REST_APP_EXEC_ERROR.toString())); expected.append( " Name: titi. Exception Type: java.lang.RuntimeException. Log Reference: " ); Assert.assertEquals( Status.FORBIDDEN.getStatusCode(), response.getStatus()); Assert.assertTrue( response.getEntity().toString().startsWith( expected.toString())); Assert.assertTrue( response.getEntity().toString().endsWith( "\"}" )); }
@Test public void testHandleError_withInvalidApplicationException_withDetails() { RoboconfError error = new RoboconfError( CMD_CANNOT_HAVE_ANY_PARENT, value( "t" )); Exception e = new InvalidApplicationException( error ); Response response = RestServicesUtils.handleError( Status.FORBIDDEN, new RestError( REST_APP_EXEC_ERROR, e, name( "titi" )), null ).build(); Assert.assertEquals( Status.FORBIDDEN.getStatusCode(), response.getStatus()); StringBuilder expected = new StringBuilder(); expected.append( "{\"reason\":\"" ); expected.append( tb.getString( REST_APP_EXEC_ERROR.toString())); expected.append( " Name: titi. " ); expected.append( "Exception Type: net.roboconf.dm.management.exceptions.InvalidApplicationException." ); expected.append( " Log Reference: " ); Assert.assertTrue( response.getEntity().toString().startsWith( expected.toString())); expected = new StringBuilder(); expected.append( "\\n\\n" ); expected.append( tb.getString( CMD_CANNOT_HAVE_ANY_PARENT.toString())); expected.append( " Value: t\"}" ); Assert.assertTrue( response.getEntity().toString().endsWith( expected.toString())); }