From aedd0a14e7a91ea53b792e7d21eabb6184d959f9 Mon Sep 17 00:00:00 2001 From: Robert Baillie Date: Mon, 14 Mar 2022 11:56:45 +0000 Subject: [PATCH] Added exception tests for put --- .../fflib-extension/caching/ObjectCache.cls | 2 +- .../caching/tests/ObjectCacheTest.cls | 62 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/framework/default/ortoo-core/default/classes/fflib-extension/caching/ObjectCache.cls b/framework/default/ortoo-core/default/classes/fflib-extension/caching/ObjectCache.cls index cb9d41fab8e..9be2b90d047 100644 --- a/framework/default/ortoo-core/default/classes/fflib-extension/caching/ObjectCache.cls +++ b/framework/default/ortoo-core/default/classes/fflib-extension/caching/ObjectCache.cls @@ -274,7 +274,7 @@ public inherited sharing class ObjectCache */ public ObjectCache put( String key, IdGetter idGetter, Object objectToStore ) { - return put( key, idGetter, new List{ objectToStore } ); + return put( key, idGetter.getIdFor( objectToStore ), objectToStore ); } /** 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 65c9f81b0f0..166f935c85c 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 @@ -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 {