From 525aa2ced230aa9158f2359f1384f2390a7a66c2 Mon Sep 17 00:00:00 2001 From: Robert Baillie Date: Thu, 10 Mar 2022 15:14:09 +0000 Subject: [PATCH] Added test and robustness to SessionCache Fixed missing assertion text from NullDomainTest --- .../fflib-extension/caching/NullCache.cls | 2 +- .../fflib-extension/caching/SessionCache.cls | 11 +++ .../caching/tests/SessionCacheTest.cls | 98 +++++++++++++++++++ .../tests/SessionCacheTest.cls-meta.xml | 5 + .../null-objects/tests/NullDomainTest.cls | 2 +- 5 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/SessionCacheTest.cls create mode 100644 framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/SessionCacheTest.cls-meta.xml diff --git a/framework/default/ortoo-core/default/classes/fflib-extension/caching/NullCache.cls b/framework/default/ortoo-core/default/classes/fflib-extension/caching/NullCache.cls index 8bac52b4ab9..c2bf10eb631 100644 --- a/framework/default/ortoo-core/default/classes/fflib-extension/caching/NullCache.cls +++ b/framework/default/ortoo-core/default/classes/fflib-extension/caching/NullCache.cls @@ -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. * diff --git a/framework/default/ortoo-core/default/classes/fflib-extension/caching/SessionCache.cls b/framework/default/ortoo-core/default/classes/fflib-extension/caching/SessionCache.cls index f65ceb5cd5c..90c8441d770 100644 --- a/framework/default/ortoo-core/default/classes/fflib-extension/caching/SessionCache.cls +++ b/framework/default/ortoo-core/default/classes/fflib-extension/caching/SessionCache.cls @@ -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 ); } @@ -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 } @@ -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 ); } @@ -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 ); } diff --git a/framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/SessionCacheTest.cls b/framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/SessionCacheTest.cls new file mode 100644 index 00000000000..5bcdfb4067e --- /dev/null +++ b/framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/SessionCacheTest.cls @@ -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' ); + } +} \ No newline at end of file diff --git a/framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/SessionCacheTest.cls-meta.xml b/framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/SessionCacheTest.cls-meta.xml new file mode 100644 index 00000000000..dd61d1f917e --- /dev/null +++ b/framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/SessionCacheTest.cls-meta.xml @@ -0,0 +1,5 @@ + + + 52.0 + Active + diff --git a/framework/default/ortoo-core/default/classes/null-objects/tests/NullDomainTest.cls b/framework/default/ortoo-core/default/classes/null-objects/tests/NullDomainTest.cls index 1740494c825..d57a00b9f34 100644 --- a/framework/default/ortoo-core/default/classes/null-objects/tests/NullDomainTest.cls +++ b/framework/default/ortoo-core/default/classes/null-objects/tests/NullDomainTest.cls @@ -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