From 71f70572ef6ae0f868cfe7b29163d40203213053 Mon Sep 17 00:00:00 2001 From: ZLoo Date: Thu, 25 Jul 2024 11:39:28 +0300 Subject: [PATCH] Validate arguments of public methods (#2246) Add tests for `CA1062`to validate arguments of public methods. --- test/Polly.Specs/Caching/CacheTResultSpecs.cs | 1042 ++++++++++++++++- 1 file changed, 1000 insertions(+), 42 deletions(-) diff --git a/test/Polly.Specs/Caching/CacheTResultSpecs.cs b/test/Polly.Specs/Caching/CacheTResultSpecs.cs index 360e330c1e6..ba6c8411b8e 100644 --- a/test/Polly.Specs/Caching/CacheTResultSpecs.cs +++ b/test/Polly.Specs/Caching/CacheTResultSpecs.cs @@ -14,11 +14,8 @@ public void Should_throw_when_action_is_null() ISyncCacheProvider syncCacheProvider = new StubCacheProvider().For(); ITtlStrategy ttlStrategy = new ContextualTtl().For(); Func cacheKeyStrategy = (_) => string.Empty; - Action onCacheGet = (_, _) => { }; - Action onCacheMiss = (_, _) => { }; - Action onCachePut = (_, _) => { }; - Action? onCacheGetError = null; - Action? onCachePutError = null; + Action onCache = (_, _) => { }; + Action? onCacheError = null; var instance = Activator.CreateInstance( typeof(CachePolicy), @@ -28,11 +25,11 @@ public void Should_throw_when_action_is_null() syncCacheProvider, ttlStrategy, cacheKeyStrategy, - onCacheGet, - onCacheMiss, - onCachePut, - onCacheGetError, - onCachePutError, + onCache, + onCache, + onCache, + onCacheError, + onCacheError, ], null)!; var instanceType = instance.GetType(); @@ -51,83 +48,1044 @@ public void Should_throw_when_action_is_null() public void Should_throw_when_cache_provider_is_null() { ISyncCacheProvider cacheProvider = null!; + ISyncCacheProvider cacheProviderGeneric = null!; + var ttl = TimeSpan.MaxValue; + ITtlStrategy ttlStrategy = new ContextualTtl(); + ITtlStrategy ttlStrategyGeneric = new ContextualTtl().For(); ICacheKeyStrategy cacheKeyStrategy = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]); + Func cacheKeyStrategyFunc = (_) => string.Empty; + Action onCache = (_, _) => { }; + Action? onCacheError = null; + const string CacheProviderExpected = "cacheProvider"; + + Action action = () => Policy.Cache(cacheProvider, ttl, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache(cacheProvider, ttlStrategy, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache(cacheProvider, ttl, cacheKeyStrategy, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache(cacheProvider, ttl, cacheKeyStrategyFunc, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategyFunc, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache( + cacheProvider, + ttl, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache( + cacheProvider, + ttlStrategy, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache( + cacheProvider, + ttl, + cacheKeyStrategy, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache( + cacheProvider, + ttlStrategy, + cacheKeyStrategy, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache( + cacheProvider, + ttl, + cacheKeyStrategyFunc, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache( + cacheProvider, + ttlStrategy, + cacheKeyStrategyFunc, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttl, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttlStrategy, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttlStrategyGeneric, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttl, cacheKeyStrategy, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttlStrategy, cacheKeyStrategy, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttlStrategyGeneric, cacheKeyStrategy, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttl, cacheKeyStrategyFunc, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttlStrategy, cacheKeyStrategyFunc, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttlStrategyGeneric, cacheKeyStrategyFunc, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttl, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategy, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategyGeneric, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttl, + cacheKeyStrategy, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategy, + cacheKeyStrategy, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategyGeneric, + cacheKeyStrategy, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttl, + cacheKeyStrategyFunc, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); - Action action = () => Policy.Cache(cacheProvider, TimeSpan.MaxValue); - action.Should().Throw().And.ParamName.Should().Be("cacheProvider"); + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategy, + cacheKeyStrategyFunc, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); - action = () => Policy.Cache(cacheProvider, new ContextualTtl(), cacheKeyStrategy); - action.Should().Throw().And.ParamName.Should().Be("cacheProvider"); + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategyGeneric, + cacheKeyStrategyFunc, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheProviderExpected); } [Fact] public void Should_throw_when_ttl_strategy_is_null() { ISyncCacheProvider cacheProvider = new StubCacheProvider(); + ISyncCacheProvider cacheProviderGeneric = new StubCacheProvider().For(); ITtlStrategy ttlStrategy = null!; - Action action = () => Policy.Cache(cacheProvider, ttlStrategy); - action.Should().Throw().And.ParamName.Should().Be("ttlStrategy"); + ITtlStrategy ttlStrategyGeneric = null!; + ICacheKeyStrategy cacheKeyStrategy = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]); + Func cacheKeyStrategyFunc = (_) => string.Empty; + Action onCache = (_, _) => { }; + Action? onCacheError = null; + const string TtlStrategyExpected = "ttlStrategy"; + + Action action = () => Policy.Cache(cacheProvider, ttlStrategy, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); + + action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); + + action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); + + action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategyFunc, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); + + action = () => Policy.Cache( + cacheProvider, + ttlStrategy, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); + + action = () => Policy.Cache( + cacheProvider, + ttlStrategy, + cacheKeyStrategy, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); + + action = () => Policy.Cache( + cacheProvider, + ttlStrategy, + cacheKeyStrategyFunc, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttlStrategy, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttlStrategyGeneric, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttlStrategy, cacheKeyStrategy, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttlStrategyGeneric, cacheKeyStrategy, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttlStrategy, cacheKeyStrategy, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttlStrategy, cacheKeyStrategyFunc, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttlStrategyGeneric, cacheKeyStrategyFunc, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategy, onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategyGeneric, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategy, + cacheKeyStrategy, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategyGeneric, + cacheKeyStrategy, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategy, + cacheKeyStrategyFunc, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategyGeneric, + cacheKeyStrategyFunc, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(TtlStrategyExpected); } [Fact] public void Should_throw_when_cache_key_strategy_is_null() { ISyncCacheProvider cacheProvider = new StubCacheProvider(); + ISyncCacheProvider cacheProviderGeneric = new StubCacheProvider().For(); var ttl = TimeSpan.MaxValue; ITtlStrategy ttlStrategy = new ContextualTtl(); + ITtlStrategy ttlStrategyGeneric = new ContextualTtl().For(); ICacheKeyStrategy cacheKeyStrategy = null!; Func cacheKeyStrategyFunc = null!; - Action onCacheGet = (_, _) => { }; - Action onCacheMiss = (_, _) => { }; - Action onCachePut = (_, _) => { }; - Action? onCacheGetError = null; - Action? onCachePutError = null; + Action onCache = (_, _) => { }; + Action? onCacheError = null; const string CacheKeyStrategyExpected = "cacheKeyStrategy"; - Action action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGetError); + Action action = () => Policy.Cache(cacheProvider, ttl, cacheKeyStrategy, onCacheError); action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); - action = () => Policy.Cache(cacheProvider, ttl, cacheKeyStrategyFunc); + action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheError); action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); - action = () => Policy.Cache(cacheProvider.For(), ttl, cacheKeyStrategy, onCacheGetError); + action = () => Policy.Cache(cacheProvider, ttl, cacheKeyStrategyFunc, onCacheError); action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); - action = () => Policy.Cache(cacheProvider.For(), ttlStrategy, cacheKeyStrategy, onCacheGetError); + action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategyFunc, onCacheError); action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); action = () => Policy.Cache( - cacheProvider.For(), + cacheProvider, ttl, cacheKeyStrategy, - onCacheGet, - onCacheMiss, - onCachePut, - onCacheGetError, - onCachePutError); + onCache, + onCache, + onCache, + onCacheError, + onCacheError); action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); action = () => Policy.Cache( - cacheProvider.For(), + cacheProvider, ttlStrategy, cacheKeyStrategy, - onCacheGet, - onCacheMiss, - onCachePut, - onCacheGetError, - onCachePutError); + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + + action = () => Policy.Cache(cacheProvider, + ttl, + cacheKeyStrategyFunc, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + + action = () => Policy.Cache( + cacheProvider, + ttlStrategy, + cacheKeyStrategyFunc, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttl, cacheKeyStrategy, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttlStrategy, cacheKeyStrategy, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttlStrategyGeneric, cacheKeyStrategy, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttl, cacheKeyStrategyFunc, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttlStrategy, cacheKeyStrategyFunc, onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + + action = () => Policy.Cache(cacheProviderGeneric, ttlStrategyGeneric, cacheKeyStrategyFunc, onCacheError); action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); action = () => Policy.Cache( - cacheProvider.For(), - ttlStrategy.For(), + cacheProviderGeneric, + ttl, + cacheKeyStrategy, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategy, + cacheKeyStrategy, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategyGeneric, + cacheKeyStrategy, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttl, + cacheKeyStrategyFunc, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategy, + cacheKeyStrategyFunc, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategyGeneric, + cacheKeyStrategyFunc, + onCache, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + } + + [Fact] + public void Should_throw_when_on_cache_get_is_null() + { + ISyncCacheProvider cacheProvider = new StubCacheProvider(); + ISyncCacheProvider cacheProviderGeneric = new StubCacheProvider().For(); + var ttl = TimeSpan.MaxValue; + ITtlStrategy ttlStrategy = new ContextualTtl(); + ITtlStrategy ttlStrategyGeneric = new ContextualTtl().For(); + ICacheKeyStrategy cacheKeyStrategy = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]); + Func cacheKeyStrategyFunc = (_) => string.Empty; + Action onCacheGet = null!; + Action onCache = (_, _) => { }; + Action? onCacheError = null; + const string OnCacheGetExpected = "onCacheGet"; + + Action action = () => Policy.Cache( + cacheProvider, + ttl, + onCacheGet, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheGetExpected); + + action = () => Policy.Cache( + cacheProvider, + ttlStrategy, + onCacheGet, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheGetExpected); + + action = () => Policy.Cache( + cacheProvider, + ttl, cacheKeyStrategy, onCacheGet, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheGetExpected); + + action = () => Policy.Cache( + cacheProvider, + ttlStrategy, + cacheKeyStrategy, + onCacheGet, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheGetExpected); + + action = () => Policy.Cache( + cacheProvider, + ttl, + cacheKeyStrategyFunc, + onCacheGet, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheGetExpected); + + action = () => Policy.Cache( + cacheProvider, + ttlStrategy, + cacheKeyStrategyFunc, + onCacheGet, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheGetExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttl, + onCacheGet, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheGetExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategy, + onCacheGet, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheGetExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategyGeneric, + onCacheGet, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheGetExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttl, + cacheKeyStrategy, + onCacheGet, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheGetExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategy, + cacheKeyStrategy, + onCacheGet, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheGetExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategyGeneric, + cacheKeyStrategy, + onCacheGet, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheGetExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttl, + cacheKeyStrategyFunc, + onCacheGet, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheGetExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategy, + cacheKeyStrategyFunc, + onCacheGet, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheGetExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategyGeneric, + cacheKeyStrategyFunc, + onCacheGet, + onCache, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheGetExpected); + } + + [Fact] + public void Should_throw_when_on_cache_miss_is_null() + { + ISyncCacheProvider cacheProvider = new StubCacheProvider(); + ISyncCacheProvider cacheProviderGeneric = new StubCacheProvider().For(); + var ttl = TimeSpan.MaxValue; + ITtlStrategy ttlStrategy = new ContextualTtl(); + ITtlStrategy ttlStrategyGeneric = new ContextualTtl().For(); + ICacheKeyStrategy cacheKeyStrategy = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]); + Func cacheKeyStrategyFunc = (_) => string.Empty; + Action onCacheMiss = null!; + Action onCache = (_, _) => { }; + Action? onCacheError = null; + const string OnCacheMissExpected = "onCacheMiss"; + + Action action = () => Policy.Cache( + cacheProvider, + ttl, + onCache, + onCacheMiss, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheMissExpected); + + action = () => Policy.Cache( + cacheProvider, + ttlStrategy, + onCache, + onCacheMiss, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheMissExpected); + + action = () => Policy.Cache( + cacheProvider, + ttl, + cacheKeyStrategy, + onCache, + onCacheMiss, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheMissExpected); + + action = () => Policy.Cache( + cacheProvider, + ttlStrategy, + cacheKeyStrategy, + onCache, + onCacheMiss, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheMissExpected); + + action = () => Policy.Cache( + cacheProvider, + ttl, + cacheKeyStrategyFunc, + onCache, + onCacheMiss, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheMissExpected); + + action = () => Policy.Cache( + cacheProvider, + ttlStrategy, + cacheKeyStrategyFunc, + onCache, + onCacheMiss, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheMissExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttl, + onCache, + onCacheMiss, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheMissExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategy, + onCache, + onCacheMiss, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheMissExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategyGeneric, + onCache, + onCacheMiss, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheMissExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttl, + cacheKeyStrategy, + onCache, + onCacheMiss, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheMissExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategy, + cacheKeyStrategy, + onCache, + onCacheMiss, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheMissExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategyGeneric, + cacheKeyStrategy, + onCache, + onCacheMiss, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheMissExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttl, + cacheKeyStrategyFunc, + onCache, + onCacheMiss, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheMissExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategy, + cacheKeyStrategyFunc, + onCache, onCacheMiss, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheMissExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategyGeneric, + cacheKeyStrategyFunc, + onCache, + onCacheMiss, + onCache, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCacheMissExpected); + } + + [Fact] + public void Should_throw_when_on_cache_put_is_null() + { + ISyncCacheProvider cacheProvider = new StubCacheProvider(); + ISyncCacheProvider cacheProviderGeneric = new StubCacheProvider().For(); + var ttl = TimeSpan.MaxValue; + ITtlStrategy ttlStrategy = new ContextualTtl(); + ITtlStrategy ttlStrategyGeneric = new ContextualTtl().For(); + ICacheKeyStrategy cacheKeyStrategy = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]); + Func cacheKeyStrategyFunc = (_) => string.Empty; + Action onCachePut = null!; + Action onCache = (_, _) => { }; + Action? onCacheError = null; + const string OnCachePutExpected = "onCachePut"; + + Action action = () => Policy.Cache( + cacheProvider, + ttl, + onCache, + onCache, onCachePut, - onCacheGetError, - onCachePutError); - action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCachePutExpected); + + action = () => Policy.Cache( + cacheProvider, + ttlStrategy, + onCache, + onCache, + onCachePut, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCachePutExpected); + + action = () => Policy.Cache( + cacheProvider, + ttl, + cacheKeyStrategy, + onCache, + onCache, + onCachePut, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCachePutExpected); + + action = () => Policy.Cache( + cacheProvider, + ttlStrategy, + cacheKeyStrategy, + onCache, + onCache, + onCachePut, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCachePutExpected); + + action = () => Policy.Cache( + cacheProvider, + ttl, + cacheKeyStrategyFunc, + onCache, + onCache, + onCachePut, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCachePutExpected); + + action = () => Policy.Cache( + cacheProvider, + ttlStrategy, + cacheKeyStrategyFunc, + onCache, + onCache, + onCachePut, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCachePutExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttl, + onCache, + onCache, + onCachePut, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCachePutExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategy, + onCache, + onCache, + onCachePut, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCachePutExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategyGeneric, + onCache, + onCache, + onCachePut, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCachePutExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttl, + cacheKeyStrategy, + onCache, + onCache, + onCachePut, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCachePutExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategy, + cacheKeyStrategy, + onCache, + onCache, + onCachePut, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCachePutExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategyGeneric, + cacheKeyStrategy, + onCache, + onCache, + onCachePut, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCachePutExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttl, + cacheKeyStrategyFunc, + onCache, + onCache, + onCachePut, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCachePutExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategy, + cacheKeyStrategyFunc, + onCache, + onCache, + onCachePut, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCachePutExpected); + + action = () => Policy.Cache( + cacheProviderGeneric, + ttlStrategyGeneric, + cacheKeyStrategyFunc, + onCache, + onCache, + onCachePut, + onCacheError, + onCacheError); + action.Should().Throw().And.ParamName.Should().Be(OnCachePutExpected); } #endregion