Skip to content

Commit

Permalink
Added test and robustness to SessionCache
Browse files Browse the repository at this point in the history
Fixed missing assertion text from NullDomainTest
  • Loading branch information
rob-baillie-ortoo committed Mar 10, 2022
1 parent 3906e1a commit 525aa2c
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* An implementation of the ICacheAdaptor that is benign - conforms to the Null Object Pattern
* An implementation of the ICacheAdaptor that is benign - conforms to the Null Object Pattern.
*
* Allows for the use of code that will automatically interact with a cache and be able to switch that off dynamically and simply.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public inherited sharing class SessionCache implements ICacheAdaptor
*/
public Object get( String key )
{
Contract.requires( key != null, 'get called with a null key' );
return Cache.Session.get( key );
}

Expand All @@ -48,6 +49,12 @@ public inherited sharing class SessionCache implements ICacheAdaptor
*/
public void put( String key, Object value, Integer lifespan )
{
Contract.requires( key != null, 'put called with a null key' );
Contract.requires( value != null, 'put called with a null value (call remove instead)' );
Contract.requires( lifespan != null, 'put called with a null lifespan' );
Contract.requires( lifespan >= 0, 'put called with a negative lifespan' );
// Note that the maximum is handled by Salesforce, just in case it increases

Cache.Session.put( key, value, lifespan, Cache.Visibility.NAMESPACE, true ); // immutable outside of namespace
}

Expand All @@ -59,6 +66,8 @@ public inherited sharing class SessionCache implements ICacheAdaptor
*/
public Boolean contains( String key )
{
Contract.requires( key != null, 'contains called with a null key' );

return Cache.Session.contains( key );
}

Expand All @@ -69,6 +78,8 @@ public inherited sharing class SessionCache implements ICacheAdaptor
*/
public void remove( String key )
{
Contract.requires( key != null, 'remove called with a null key' );

Cache.Session.remove( key );
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
@isTest
private without sharing class SessionCacheTest
{
private final static Integer DEFAULT_LIFESPAN = 1000;

@isTest
private static void hasAccessToCache_whenCalled_returnsTrue() // NOPMD: Test method name format
{
Boolean got = new SessionCache().hasAccessToCache();
System.assertEquals( true, got, 'hasAccessToCache, when called, will return true' );
}

@isTest
private static void isACache_whenCalled_returnsTrue() // NOPMD: Test method name format
{
Boolean got = new SessionCache().isACache();
System.assertEquals( true, got, 'hasAccessToCache, when called, will return true' );
}

@isTest
private static void get_whenCalledWithAKeyNotInTheCache_returnsNull() // NOPMD: Test method name format
{
Object got = new SessionCache().get( 'doesnotexist' );
System.assertEquals( null, got, 'get, when called with a key that is not in the cache, will return null' );
}

@isTest
private static void get_whenCalledWithAKeyThatWasPut_returnsIt() // NOPMD: Test method name format
{
String expected = 'thecachedthing';
new SessionCache().put( 'key', expected, DEFAULT_LIFESPAN );

Object got = new SessionCache().get( 'key' );
System.assertEquals( expected, got, 'get, when called with a key that is in the cache, will return it' );
}

@isTest
private static void put_whenCalledMultipleTimesWithTheSameKey_overwritesIt() // NOPMD: Test method name format
{
new SessionCache().put( 'key', '1', DEFAULT_LIFESPAN );
new SessionCache().put( 'key', '2', DEFAULT_LIFESPAN );
new SessionCache().put( 'key', '3', DEFAULT_LIFESPAN );
new SessionCache().put( 'key', '4', DEFAULT_LIFESPAN );

Object got = new SessionCache().get( 'key' );
System.assertEquals( '4', got, 'put, when called multiple times with the same key, overwrites it' );
}

@isTest
private static void put_whenCalledMultipleTimesWithDifferentKeys_storesThem() // NOPMD: Test method name format
{
new SessionCache().put( 'key1', '1', DEFAULT_LIFESPAN );
new SessionCache().put( 'key2', '2', DEFAULT_LIFESPAN );
new SessionCache().put( 'key3', '3', DEFAULT_LIFESPAN );
new SessionCache().put( 'key4', '4', DEFAULT_LIFESPAN );

Object got = new SessionCache().get( 'key2' );
System.assertEquals( '2', got, 'put, when called multiple times with different keys, stores them' );
}

@isTest
private static void contains_whenCalledForAKeyThatWasPut_returnsTrue() // NOPMD: Test method name format
{
new SessionCache().put( 'keythatexists', '1', DEFAULT_LIFESPAN );

Boolean got = new SessionCache().contains( 'keythatexists' );
System.assertEquals( true, got, 'contains, when called for a key that was put, returns true' );
}

@isTest
private static void contains_whenCalledForAKeyThatWasNotPut_returnsFalse() // NOPMD: Test method name format
{
new SessionCache().put( 'keythatexists', '1', DEFAULT_LIFESPAN );

Boolean got = new SessionCache().contains( 'keythatdoesnotexist' );
System.assertEquals( false, got, 'contains, when called for a key that was not put, returns false' );
}

@isTest
private static void remove_whenCalledForAKeyThatWasPut_removesIt() // NOPMD: Test method name format
{
new SessionCache().put( 'originalkey', '1', DEFAULT_LIFESPAN );

new SessionCache().remove( 'originalkey' );

System.assertEquals( false, new SessionCache().contains( 'originalkey' ), 'remove, when called for a key that was put, will remove it - checking contains' );
System.assertEquals( null, new SessionCache().get( 'originalkey' ), 'remove, when called for a key that was put, will remove it - checking get' );
}

@isTest
private static void remove_whenCalledForAKeyThatWasNotPut_doesNotError() // NOPMD: Test method name format
{
new SessionCache().remove( 'keythatneverexisted' );

System.assertEquals( false, new SessionCache().contains( 'keythatneverexisted' ), 'remove, when called for a key that was put, will remove it - checking contains' );
System.assertEquals( null, new SessionCache().get( 'keythatneverexisted' ), 'remove, when called for a key that was put, will remove it - checking get' );
}
}
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>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ private without sharing class NullDomainTest
private static void constructorClass_buildsANullDomain() // NOPMD: Test method name format
{
NullDomain nullDomain = (NullDomain)new NullDomain.Constructor().construct( LIST_OF_SOBJECTS );
System.assertNotEquals( null, nullDomain );
System.assertNotEquals( null, nullDomain, 'constructor, when called, does not throw an exception' );
}

@isTest
Expand Down

0 comments on commit 525aa2c

Please sign in to comment.