public async void Can_Get_From_DB_Then_Set_To_Cache_Before_Cached()
        {
            var guid       = Guid.NewGuid();
            var guidString = guid.ToString("N").ToUpper();
            var expected   = new GuidData()
            {
                Guid = guid, User = "******", Expire = DateTimeOffset.UtcNow.AddDays(30).ToUnixTimeSeconds()
            };

            _cacheMock.GetAsync(Arg.Is <string>(x => x == "GuidData_" + guidString)).Returns(default(byte[]));
            _dbMock.GetGuidData(Arg.Is <Guid>(x => x == guid)).Returns(expected);

            var actual = await _repo.GetGuidData(guid);

            Assert.NotNull(actual);
            Assert.Equal(expected.Guid, actual.Guid);
            Assert.Equal(expected.User, actual.User);
            Assert.Equal(expected.Expire, actual.Expire);

            await _cacheMock.Received(1).GetAsync(Arg.Any <string>());

            await _cacheMock.Received(1).SetAsync(Arg.Any <string>(), Arg.Any <byte[]>(), Arg.Any <DistributedCacheEntryOptions>());

            await _dbMock.Received(1).GetGuidData(Arg.Any <Guid>());
        }
        public async Task Can_Get_Returns_200_OK_When_A_Valid_Guid_Given()
        {
            var guid       = Guid.NewGuid();
            var guidString = guid.ToString("N").ToUpper();
            var raw        = new { user = "******", expire = DateTimeOffset.UtcNow.AddDays(30).ToUnixTimeSeconds().ToString() };

            _repoMock.GetGuidData(Arg.Is <Guid>(x => x == guid)).Returns((new GuidData()
            {
                Guid = guid, User = raw.user, Expire = long.Parse(raw.expire)
            }));

            var req = GetHttpRequestMessage(raw, $"guid/{guidString}", HttpMethod.Get);

            var resp = await _client.SendAsync(req);

            Assert.Equal(HttpStatusCode.OK, resp.StatusCode);

            var actual = JsonConvert.DeserializeObject <GuidDataResponse>(await resp.Content.ReadAsStringAsync());

            Assert.NotNull(actual);
            Assert.Equal(guidString, actual.Guid);
            Assert.Equal(raw.user, actual.User);
            Assert.Equal(raw.expire, actual.Expire);

            await _repoMock.Received(1).GetGuidData(Arg.Any <Guid>());
        }
        public async void Can_Get_From_Cache_After_Cached()
        {
            var guid       = Guid.NewGuid();
            var guidString = guid.ToString("N").ToUpper();
            var raw        = new GuidData {
                Guid = guid, User = "******", Expire = DateTimeOffset.UtcNow.AddDays(30).ToUnixTimeSeconds()
            };
            var cached = JsonConvert.SerializeObject(raw);

            _cacheMock.GetAsync(Arg.Is <string>(x => x == "GuidData_" + guidString)).Returns(Encoding.ASCII.GetBytes(cached));

            var actual = await _repo.GetGuidData(guid);

            Assert.NotNull(actual);
            Assert.Equal(guid, actual.Guid);

            await _dbMock.DidNotReceive().GetGuidData(Arg.Any <Guid>());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Get a specific GuidData
        /// </summary>
        /// <param name="guid">The guid</param>
        /// <returns></returns>
        async Task <GuidDataResponse> IGuidDataService.GetGuidData(string guid)
        {
            if (string.IsNullOrWhiteSpace(guid))
            {
                throw new ArgumentNullException();
            }

            var result = await _repo.GetGuidData(Guid.Parse(guid));

            return(result?.ToContract());
        }
Exemplo n.º 5
0
        public async Task Can_Get_Returns_GuidData_When_Guid_Exist()
        {
            var guid       = Guid.NewGuid();
            var guidString = guid.ToString("N").ToUpper();
            var raw        = new { user = "******", expire = DateTimeOffset.UtcNow.AddDays(30).ToUnixTimeSeconds().ToString() };

            _repoMock.GetGuidData(Arg.Is <Guid>(x => x == guid)).Returns((new GuidData()
            {
                Guid = guid, User = raw.user, Expire = long.Parse(raw.expire)
            }));

            var actual = await _provider.GetGuidData(guidString);

            Assert.NotNull(actual);
            Assert.Equal(guidString, actual.Guid);
            Assert.Equal(raw.user, actual.User);
            Assert.Equal(raw.expire, actual.Expire);

            await _repoMock.Received(1).GetGuidData(Arg.Any <Guid>());
        }
        /// <summary>
        /// Get from cache, if not cached, get from repository, then set cache
        /// </summary>
        /// <param name="guid">The guid</param>
        /// <returns>The GuidData</returns>
        public async Task <GuidData> GetGuidData(Guid guid)
        {
            var cahced = await _cache.GetStringAsync(_cacheKey(guid));

            if (cahced != null)
            {
                return(JsonConvert.DeserializeObject <GuidData>(cahced));
            }
            else
            {
                var stored = await _decorated.GetGuidData(guid);

                if (stored != null)
                {
                    await _cache.SetStringAsync(_cacheKey(guid), JsonConvert.SerializeObject(stored), _expire(stored.Expire));
                }
                return(stored);
            }
        }