From 00823a147ba0ca4877b7e0d47af78bcc55dd01d4 Mon Sep 17 00:00:00 2001 From: Robert Baillie Date: Mon, 14 Mar 2022 09:02:26 +0000 Subject: [PATCH] Started to test 'remove' methods --- .../caching/tests/ObjectCacheTest.cls | 55 ++++++++++++++++++- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/ObjectCacheTest.cls b/framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/ObjectCacheTest.cls index bfdccc76abc..46a38df7cfe 100644 --- a/framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/ObjectCacheTest.cls +++ b/framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/ObjectCacheTest.cls @@ -423,8 +423,59 @@ private without sharing class ObjectCacheTest System.assertEquals( objectsToStore[1], retrieval.cacheHits.get( OBJECT_ID ), 'put, when given objects with the same ids and keys, the latter will overwrite the earlier' ); } - // remove, when called with a key that exists, will remove all objects from the cache for that key - // remove, when called with a key that does not exist, will not throw an exception + @isTest + private static void remove_whenGivenAKeyThatExists_willRemoveAllObjectsFromThatKey() // NOPMD: Test method name format + { + ObjectCache cache = new ObjectCache().setScope( ObjectCache.CacheScope.SESSION ) + .put( 'key', new StringIdGetter(), new List{ 'value1', 'value2', 'value3' } ); + + Test.startTest(); + new ObjectCache().setScope( ObjectCache.CacheScope.SESSION ).remove( 'key' ); + Test.stopTest(); + + ObjectCache.CacheRetrieval retrieval; + + retrieval = new ObjectCache().setScope( ObjectCache.CacheScope.SESSION ).get( 'key', 'value1Id' ); + System.assertEquals( 0, retrieval.cacheHits.size(), 'remove, when given a key that exists, will remove all objects from that key - 1' ); + + retrieval = new ObjectCache().setScope( ObjectCache.CacheScope.SESSION ).get( 'key', 'value2Id' ); + System.assertEquals( 0, retrieval.cacheHits.size(), 'remove, when given a key that exists, will remove all objects from that key - 2' ); + + retrieval = new ObjectCache().setScope( ObjectCache.CacheScope.SESSION ).get( 'key', 'value3Id' ); + System.assertEquals( 0, retrieval.cacheHits.size(), 'remove, when given a key that exists, will remove all objects from that key - 3' ); + } + + @isTest + private static void remove_whenGivenAKeyThatExists_willNotRemoveObjectsFromOtherKeys() // NOPMD: Test method name format + { + ObjectCache cache = new ObjectCache().setScope( ObjectCache.CacheScope.SESSION ) + .put( 'key', new StringIdGetter(), new List{ 'value1', 'value2', 'value3' } ); + + Test.startTest(); + new ObjectCache().setScope( ObjectCache.CacheScope.SESSION ).remove( 'otherKey' ); + Test.stopTest(); + + ObjectCache.CacheRetrieval retrieval; + + retrieval = new ObjectCache().setScope( ObjectCache.CacheScope.SESSION ).get( 'key', 'value1Id' ); + System.assertEquals( 1, retrieval.cacheHits.size(), 'remove, when given a key that exists, will not remove obejcts from other keys - 1' ); + + retrieval = new ObjectCache().setScope( ObjectCache.CacheScope.SESSION ).get( 'key', 'value2Id' ); + System.assertEquals( 1, retrieval.cacheHits.size(), 'remove, when given a key that exists, will not remove obejcts from other keys - 2' ); + + retrieval = new ObjectCache().setScope( ObjectCache.CacheScope.SESSION ).get( 'key', 'value3Id' ); + System.assertEquals( 1, retrieval.cacheHits.size(), 'remove, when given a key that exists, will not remove obejcts from other keys - 3' ); + } + + @isTest + private static void remove_whenGivenAKeyThatDoesNotExist_willNotThrowAnExecption() // NOPMD: Test method name format + { + Test.startTest(); + new ObjectCache().setScope( ObjectCache.CacheScope.SESSION ).remove( 'key' ); + Test.stopTest(); + + System.assert( true, 'remove, when given a key that does not exist, will not throw an exception' ); + } // remove, when called with a key and and a set of Ids that exist, will remove the ones that do, leave the others // remove, when called with a key and and a set of Ids that do not exist, will not throw an exception