Skip to content

Commit

Permalink
Added test for ServiceUtils.logAndRethrow
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-baillie-ortoo committed Apr 19, 2022
1 parent 5ae972a commit c7a6997
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,39 @@
*
* @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
*/
public static void logAndRethrow( Exception exceptionToLog )
{
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;
}
}
Original file line number Diff line number Diff line change
@@ -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' );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>52.0</apiVersion>
<status>Active</status>
</ApexClass>

0 comments on commit c7a6997

Please sign in to comment.