Flatwhite.WebApi.FlatwhiteStatusController.Store C# (CSharp) Méthode

Store() private méthode

private Store ( int id ) : Task
id int
Résultat Task
        public async Task<IHttpActionResult> Store(int id = 0)
        {
            var all = new List<KeyValuePair<string, object>>();

            var syncStore = _cacheStoreProvider.GetCacheStore(id);
            if (syncStore != null)
            {
                all.AddRange(syncStore.GetAll());
            }
            var asyncStore = _cacheStoreProvider.GetAsyncCacheStore(id);
            if (asyncStore != null && !(asyncStore is CacheStoreAdaptor))
            {
                all.AddRange(await asyncStore.GetAllAsync());
            }

            var items = new List<CacheItemStatus>();
            foreach (var k in all)
            {
                var cacheItem = k.Value as CacheItem;
                if (cacheItem != null)
                {
                    items.Add(new CacheItemStatus(cacheItem));
                }
                else
                {
                    items.Add(new CacheItemStatus(k.Value) { Key = k.Key });
                }
            }

            foreach (var i in items)
            {
                if (Global.Cache.PhoenixFireCage.ContainsKey(i.Key))
                {
                    i.PhoenixStatus = Global.Cache.PhoenixFireCage[i.Key].GetCurrentState().GetState();
                }
            }

            return Json(items,
                new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore,
                    ContractResolver = new CamelCasePropertyNamesContractResolver(),
                    Formatting = Formatting.Indented
                });
        }

Usage Example

        public async Task Store_action_should_get_all_cache_item_from_stores_that_matched_the_id()
        {
            Global.Cache.PhoenixFireCage.Add("item1", Substitute.For<Phoenix>(Substitute.For<_IInvocation>(), new CacheItem()));
            Global.Cache.PhoenixFireCage.Add("item2", Substitute.For<Phoenix>(Substitute.For<_IInvocation>(), new CacheItem()));
            Global.Cache.PhoenixFireCage.Add("item5", Substitute.For<Phoenix>(Substitute.For<_IInvocation>(), new CacheItem()));
            Global.Cache.PhoenixFireCage.Add("item6", Substitute.For<Phoenix>(Substitute.For<_IInvocation>(), new CacheItem()));

            var syncStore = Substitute.For<ICacheStore>();
            syncStore.GetAll().Returns(new List<KeyValuePair<string, object>>
            {
                new KeyValuePair<string, object>("item0", null),
                new KeyValuePair<string, object>("item1", new CacheItem {Data = "data", Key = "item1"}),
                new KeyValuePair<string, object>("item2", new CacheItem {Data = new MediaTypeHeaderValue("text/json"), Key = "item2"}),
                new KeyValuePair<string, object>("item3", new MediaTypeHeaderValue("application/xml")),
            });

            var asyncStore = Substitute.For<IAsyncCacheStore>();
            asyncStore.GetAllAsync().Returns(Task.FromResult(new List<KeyValuePair<string, object>>
            {
                new KeyValuePair<string, object>("item4", new BadObject()),
                new KeyValuePair<string, object>("item5", new WebApiCacheItem {Content = null, Key = "item5"}),
                new KeyValuePair<string, object>("item6", new WebApiCacheItem {Content = new byte[0], Key = "item4"}),
                new KeyValuePair<string, object>("item7", new MediaTypeHeaderValue("application/xml")),
            }));

            var provider = Substitute.For<ICacheStoreProvider>();
            provider.GetCacheStore(1).Returns(syncStore);
            provider.GetAsyncCacheStore(1).Returns(asyncStore);
            var controller = new FlatwhiteStatusController(provider);

            // Action
            var result = await controller.Store(1);
            var jsonResult = (JsonResult<List<FlatwhiteStatusController.CacheItemStatus>>) result;

            // Assert
            Assert.AreEqual(8, jsonResult.Content.Count);
        }