public async Task AND_ItIsStoredInTheCache_THEN_ReturnContent()
        {
            StoredInCacheSetup();

            var result = await ContentServiceWithCaching.Get(_contentType, EmployerAccountsConfig.ApplicationId);

            Assert.AreEqual(ContentFromCache, result);
            MockCacheStorageService.Verify(c => c.TryGet(CacheKey, out ContentFromCache), Times.Once);
        }
예제 #2
0
        public async Task Verify_ClientApiIsCalled()
        {
            //act
            await _sut.Get(type, applicationId);

            //assert
            _mockHttpMessageHandler
            .Protected()
            .Verify("SendAsync", Times.Once(),
                    ItExpr.Is <HttpRequestMessage>(r => r.Method == HttpMethod.Get &&
                                                   r.RequestUri.AbsoluteUri == $"{_configuration.ApiBaseUrl}api/content?applicationId={applicationId}&type={type}"),
                    ItExpr.IsAny <CancellationToken>());
        }
        public async Task <string> Get(string type, string applicationId)
        {
            var cacheKey = $"{applicationId}_{type}".ToLowerInvariant();

            try
            {
                if (_cacheStorageService.TryGet(cacheKey, out string cachedContentBanner))
                {
                    return(cachedContentBanner);
                }

                var content = await _contentService.Get(type, applicationId);

                if (content != null)
                {
                    await _cacheStorageService.Save(cacheKey, content, _employerAccountsConfiguration.DefaultCacheExpirationInMinutes);
                }

                return(content);
            }
            catch
            {
                throw new ArgumentException($"Failed to get content for {cacheKey}");
            }
        }
예제 #4
0
        public async Task <GetContentResponse> Handle(GetContentRequest message)
        {
            var validationResult = _validator.Validate(message);

            if (!validationResult.IsValid())
            {
                throw new InvalidRequestException(validationResult.ValidationDictionary);
            }

            var applicationId = message.UseLegacyStyles ? _employerFinanceConfiguration.ApplicationId + "-legacy" : _employerFinanceConfiguration.ApplicationId;

            try
            {
                var content = await _service.Get(message.ContentType, applicationId);

                return(new GetContentResponse
                {
                    Content = content
                });
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"Failed to get Content {message.ContentType} for {applicationId}");

                return(new GetContentResponse
                {
                    HasFailed = true
                });
            }
        }