Skip to content

Commit

Permalink
Added exception tests for put
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-baillie-ortoo committed Mar 14, 2022
1 parent 678acfd commit aedd0a1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public inherited sharing class ObjectCache
*/
public ObjectCache put( String key, IdGetter idGetter, Object objectToStore )
{
return put( key, idGetter, new List<Object>{ objectToStore } );
return put( key, idGetter.getIdFor( objectToStore ), objectToStore );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,68 @@ private without sharing class ObjectCacheTest
System.assertEquals( 'value1', got.cacheHits.get( 'id1' ), 'put, when given a single object and an id, will store it' );
}

@isTest
private static void put_whenGivenASingleObjectAndAnEmptyKey_throwsAnException() // NOPMD: Test method name format
{
ObjectCache cache = new ObjectCache().setScope( ObjectCache.CacheScope.SESSION );

Test.startTest();
String exceptionMessage;
try
{
cache.put( ' ', 'id', 'value' );
}
catch ( Contract.RequiresException e )
{
exceptionMessage = e.getMessage();
}
Test.stopTest();

ortoo_Asserts.assertContains( 'put called with a blank key', exceptionMessage, 'put, when given a single object and an empty key, will throw an exception' );
}

@isTest
private static void put_whenGivenASingleObjectAndAnEmptyId_throwsAnException() // NOPMD: Test method name format
{
ObjectCache cache = new ObjectCache().setScope( ObjectCache.CacheScope.SESSION );

Test.startTest();
String exceptionMessage;
try
{
cache.put( 'key', ' ', 'value' );
}
catch ( Contract.RequiresException e )
{
exceptionMessage = e.getMessage();
}
Test.stopTest();

ortoo_Asserts.assertContains( 'put called with a blank id', exceptionMessage, 'put, when given a single object and an empty id, will throw an exception' );
}

@isTest
private static void put_whenGivenASingleNullObject_throwsAnException() // NOPMD: Test method name format
{
ObjectCache cache = new ObjectCache().setScope( ObjectCache.CacheScope.SESSION );

Object nullObject = null;

Test.startTest();
String exceptionMessage;
try
{
cache.put( 'key', 'id', nullObject );
}
catch ( Contract.RequiresException e )
{
exceptionMessage = e.getMessage();
}
Test.stopTest();

ortoo_Asserts.assertContains( 'put called with a null objectToStore', exceptionMessage, 'put, when given a single object that is null, will throw an exception' );
}

@isTest
private static void remove_whenGivenAKeyThatExists_willRemoveAllObjectsFromThatKey() // NOPMD: Test method name format
{
Expand Down

0 comments on commit aedd0a1

Please sign in to comment.