diff --git a/framework/default/ortoo-core/default/classes/utils/ServiceUtils.cls b/framework/default/ortoo-core/default/classes/utils/ServiceUtils.cls index b4d473d18ed..40d30cb80fb 100644 --- a/framework/default/ortoo-core/default/classes/utils/ServiceUtils.cls +++ b/framework/default/ortoo-core/default/classes/utils/ServiceUtils.cls @@ -3,12 +3,13 @@ * * @group Utils */ - // TODO: test public class ServiceUtils { /** * Logs the given exception using the LoggerService, strips the context (if it's an Ortoo Exception) - * and then rethrows the exception + * and then rethrows the exception. + * + * Note: will not log Exceptions.ValidationException * * @param Exception The exception to log */ @@ -16,12 +17,25 @@ public class ServiceUtils { Contract.requires( exceptionToLog != null, 'logAndRethrow called with a null exceptionToLog' ); - LoggerService.log( exceptionToLog ); + if ( shouldBeLogged( exceptionToLog ) ) + { + LoggerService.log( exceptionToLog ); + } + + stripContexts( exceptionToLog ); + throw exceptionToLog; + } + private static Boolean shouldBeLogged( Exception exceptionToLog ) + { + return ! (exceptionToLog instanceOf Exceptions.ValidationException); + } + + private static void stripContexts( Exception exceptionToLog ) + { if ( exceptionToLog instanceOf ortoo_Exception ) { ((ortoo_Exception)exceptionToLog).stripContexts(); } - throw exceptionToLog; } } \ No newline at end of file diff --git a/framework/default/ortoo-core/default/classes/utils/tests/ServiceUtilsTest.cls b/framework/default/ortoo-core/default/classes/utils/tests/ServiceUtilsTest.cls new file mode 100644 index 00000000000..2ef6e2dd1d5 --- /dev/null +++ b/framework/default/ortoo-core/default/classes/utils/tests/ServiceUtilsTest.cls @@ -0,0 +1,81 @@ +@isTest +private without sharing class ServiceUtilsTest +{ + @isTest + private static void logAndRethrow_whenGivenAStandardException_logsItAndRethrowsIt() // NOPMD: Test method name format + { + TestLoggerUtils.switchLoggingOn(); + TestLoggerService logger = TestLoggerUtils.registerTestLoggerService(); + + DmlException exceptionToLog = new DmlException( 'The exception to log' ); + Exception exceptionCaught; + + Test.startTest(); + try + { + ServiceUtils.logAndRethrow( exceptionToLog ); + } + catch( Exception e ) + { + exceptionCaught = e; + } + Test.stopTest(); + + logger.assertNumberOfLogCalls( 1, 'logAndRethrow, when given an exception, will log it with the logger service' ); + System.assertEquals( exceptionCaught, exceptionToLog, 'logAndRethrow, when given an exception, will rethrow it' ); + } + + @isTest + private static void logAndRethrow_whenGivenAnOrtooException_logsItStripsItAndRethrowsIt() // NOPMD: Test method name format + { + TestLoggerUtils.switchLoggingOn(); + TestLoggerService logger = TestLoggerUtils.registerTestLoggerService(); + + Amoss_Instance exceptionController = new Amoss_Instance( ortoo_Exception.class ); + exceptionController + .expects( 'stripContexts' ); + + Exception exceptionToLog = (Exception)exceptionController.getDouble(); + Exception exceptionCaught; + + Test.startTest(); + try + { + ServiceUtils.logAndRethrow( exceptionToLog ); + } + catch( Exception e ) + { + exceptionCaught = e; + } + Test.stopTest(); + + logger.assertNumberOfLogCalls( 1, 'logAndRethrow, when given an ortoo exception, will log it with the logger service' ); + System.assertEquals( String.valueOf( exceptionCaught ), String.valueOf( exceptionToLog ), 'logAndRethrow, when given an ortoo exception, will rethrow it' ); + + exceptionController.verify(); + } + + @isTest + private static void logAndRethrow_whenGivenAValidationException_willNotLogIt() // NOPMD: Test method name format + { + TestLoggerUtils.switchLoggingOn(); + TestLoggerService logger = TestLoggerUtils.registerTestLoggerService(); + + Exception exceptionToLog = new Exceptions.ValidationException( 'ValidationFailed' ); + Exception exceptionCaught; + + Test.startTest(); + try + { + ServiceUtils.logAndRethrow( exceptionToLog ); + } + catch( Exception e ) + { + exceptionCaught = e; + } + Test.stopTest(); + + logger.assertNumberOfLogCalls( 0, 'logAndRethrow, when given an ortoo validation exception, will not log it' ); + System.assertEquals( String.valueOf( exceptionCaught ), String.valueOf( exceptionToLog ), 'logAndRethrow, when given an ortoo validation exception, will rethrow it' ); + } +} \ No newline at end of file diff --git a/framework/default/ortoo-core/default/classes/utils/tests/ServiceUtilsTest.cls-meta.xml b/framework/default/ortoo-core/default/classes/utils/tests/ServiceUtilsTest.cls-meta.xml new file mode 100644 index 00000000000..dd61d1f917e --- /dev/null +++ b/framework/default/ortoo-core/default/classes/utils/tests/ServiceUtilsTest.cls-meta.xml @@ -0,0 +1,5 @@ + + + 52.0 + Active +