public async Task Then_The_Item_Is_Saved_To_The_Cache_Using_Key_And_First_ConfigName_And_Expiry_Passed(
            string keyName,
            int expiryInHours,
            string appName,
            string appName2,
            TestObject test,
            [Frozen] Mock <IDistributedCache> distributedCache,
            [Frozen] Mock <IConfiguration> configuration,
            CacheStorageService service)
        {
            //Arrange
            configuration.SetupGet(x => x[It.Is <string>(s => s.Equals("ConfigNames"))]).Returns($"{appName},{appName2}");

            //Act
            await service.SaveToCache(keyName, test, expiryInHours);

            //Assert
            distributedCache.Verify(x =>
                                    x.SetAsync(
                                        $"{appName}_{keyName}",
                                        It.Is <byte[]>(c => Encoding.UTF8.GetString(c).Equals(JsonConvert.SerializeObject(test))),
                                        It.Is <DistributedCacheEntryOptions>(c
                                                                             => c.AbsoluteExpirationRelativeToNow.Value.Hours == TimeSpan.FromHours(expiryInHours).Hours),
                                        It.IsAny <CancellationToken>()),
                                    Times.Once);
        }
Beispiel #2
0
        public async Task Then_The_Item_Is_Removed_From_The_Cache(
            string keyName,
            [Frozen] Mock <IDistributedCache> distributedCache,
            CacheStorageService service)
        {
            //Act
            await service.DeleteFromCache(keyName);

            //Assert
            distributedCache.Verify(x => x.RemoveAsync(keyName, It.IsAny <CancellationToken>()), Times.Once);
        }
Beispiel #3
0
        public void Setup()
        {
            cacheAbsoluteExipration = new TimeSpan(30, 0, 0, 0);
            cacheSlidingExpiration  = new TimeSpan(1, 0, 0, 0);

            _mockDistributedCache = new Mock <IDistributedCache>();

            _sut = new CacheStorageService(_mockDistributedCache.Object, memoryCache, new FatSharedComponentsConfiguration {
                CacheMemoryAbsoluteExpirySeconds = 5
            });
        }
Beispiel #4
0
        public void SavesToDistributedCache()
        {
            memoryCache = new MemoryCache(new MemoryCacheOptions());

            _sut = new CacheStorageService(_mockDistributedCache.Object, memoryCache, new FatSharedComponentsConfiguration {
                CacheMemoryAbsoluteExpirySeconds = 5
            });

            var response = _sut.SaveToCache <TrainingProviderDetailsViewModel>("testkey", GetTestTrainingProviderDetails(), cacheAbsoluteExipration, cacheSlidingExpiration);

            _mockDistributedCache.Verify(s => s.SetAsync(It.IsAny <string>(), It.IsAny <byte[]>(), It.IsAny <DistributedCacheEntryOptions>(), It.IsAny <CancellationToken>()), Times.Once());
        }
Beispiel #5
0
        public void GetsFromDistributedCacheIfKeyDoesntExistInMemoryCache()
        {
            memoryCache = new MemoryCache(new MemoryCacheOptions());

            _sut = new CacheStorageService(_mockDistributedCache.Object, memoryCache, new FatSharedComponentsConfiguration {
                CacheMemoryAbsoluteExpirySeconds = 5
            });

            var response = _sut.RetrieveFromCache <TrainingProviderDetailsViewModel>("123456");

            _mockDistributedCache.Verify(s => s.GetAsync(It.IsAny <string>(), It.IsAny <CancellationToken>()), Times.Once());
        }
Beispiel #6
0
        public async Task Then_If_The_Item_Does_Not_Exist_Default_Is_Returned(
            string keyName,
            [Frozen] Mock <IDistributedCache> distributedCache,
            CacheStorageService service)
        {
            //Arrange
            distributedCache.Setup(x => x.GetAsync(keyName, It.IsAny <CancellationToken>()))
            .ReturnsAsync(new byte[0]);

            //Act
            var item = await service.RetrieveFromCache <TestObject>(keyName);

            //Assert
            Assert.IsNull(item);
        }
        public async Task Then_The_Item_Is_Removed_From_The_Cache(
            string keyName,
            string appName,
            [Frozen] Mock <IDistributedCache> distributedCache,
            [Frozen] Mock <IConfiguration> configuration,
            CacheStorageService service)
        {
            //Arrange
            configuration.SetupGet(x => x[It.Is <string>(s => s.Equals("ConfigNames"))]).Returns(appName);

            //Act
            await service.DeleteFromCache(keyName);

            //Assert
            distributedCache.Verify(x => x.RemoveAsync($"{appName}_{keyName}", It.IsAny <CancellationToken>()), Times.Once);
        }
Beispiel #8
0
        public async Task Then_The_Item_Is_Retrieved_From_The_Cache_By_Key(
            string keyName,
            int expiryInHours,
            TestObject test,
            [Frozen] Mock <IDistributedCache> distributedCache,
            CacheStorageService service)
        {
            //Arrange
            distributedCache.Setup(x => x.GetAsync(keyName, It.IsAny <CancellationToken>()))
            .ReturnsAsync(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(test)));

            //Act
            var item = await service.RetrieveFromCache <TestObject>(keyName);

            //Assert
            Assert.IsNotNull(item);
            item.Should().BeEquivalentTo(test);
        }
        public async Task And_List_Does_Not_Exist_Then_Default_Is_Returned(
            string keyName,
            string appName,
            [Frozen] Mock <IDistributedCache> distributedCache,
            [Frozen] Mock <IConfiguration> configuration,
            CacheStorageService service)
        {
            //Arrange
            configuration.SetupGet(x => x[It.Is <string>(s => s.Equals("ConfigNames"))]).Returns(appName);
            distributedCache.Setup(x => x.GetAsync($"{appName}_{keyName}", It.IsAny <CancellationToken>()))
            .ReturnsAsync(new byte[0]);

            //Act
            var item = await service.RetrieveFromCache <List <TestObject> >(keyName);

            //Assert
            Assert.IsNull(item);
        }
Beispiel #10
0
        public async Task Then_The_Item_Is_Saved_To_The_Cache_Using_Key_And_Expiry_Passed(
            string keyName,
            int expiryInHours,
            TestObject test,
            [Frozen] Mock <IDistributedCache> distributedCache,
            CacheStorageService service)
        {
            //Act
            await service.SaveToCache(keyName, test, expiryInHours);

            //Assert
            distributedCache.Verify(x =>
                                    x.SetAsync(
                                        keyName,
                                        It.Is <byte[]>(c => Encoding.UTF8.GetString(c).Equals(JsonConvert.SerializeObject(test))),
                                        It.Is <DistributedCacheEntryOptions>(c
                                                                             => c.AbsoluteExpirationRelativeToNow.Value.Hours == TimeSpan.FromHours(expiryInHours).Hours),
                                        It.IsAny <CancellationToken>()),
                                    Times.Once);
        }
        public async Task Then_The_Item_Is_Retrieved_From_The_Cache_By_Key(
            string keyName,
            int expiryInHours,
            TestObject test,
            string appName,
            [Frozen] Mock <IDistributedCache> distributedCache,
            [Frozen] Mock <IConfiguration> configuration,
            CacheStorageService service)
        {
            //Arrange
            configuration.SetupGet(x => x[It.Is <string>(s => s.Equals("ConfigNames"))]).Returns(appName);
            distributedCache.Setup(x => x.GetAsync($"{appName}_{keyName}", It.IsAny <CancellationToken>()))
            .ReturnsAsync(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(test)));

            //Act
            var item = await service.RetrieveFromCache <TestObject>(keyName);

            //Assert
            Assert.IsNotNull(item);
            item.Should().BeEquivalentTo(test);
        }