public void GetAuthorizationToken_WhenCacheIsNotExpired_CallsOneTimeCallApiMethod()
        {
            var configurationStub   = new Configuration();
            var authRequestResponse = CreateAuthRequestResponse();
            var apiClientMock       = CreateApiClient(authRequestResponse);
            var currentTime         = new DateTime(2000, 1, 1);
            var dateTimeProvider    = new SettableDateTimeProvider(currentTime);
            var cacheServiceStub1   = new CacheService(dateTimeProvider);
            var cacheServiceStub2   = new CacheService(dateTimeProvider);

            var authServiceInstance1 = new AuthService(configurationStub, apiClientMock, cacheServiceStub1);

            authServiceInstance1.GetAuthorizationToken();
            var authServiceInstance2 = new AuthService(configurationStub, apiClientMock, cacheServiceStub2);

            authServiceInstance2.GetAuthorizationToken();

            apiClientMock.Received(1).CallApi(Arg.Any <string>(),
                                              Arg.Any <Method>(),
                                              Arg.Any <List <KeyValuePair <string, string> > >(),
                                              Arg.Any <object>(),
                                              Arg.Any <Dictionary <string, string> >(),
                                              Arg.Any <Dictionary <string, string> >(),
                                              Arg.Any <Dictionary <string, FileParameter> >(),
                                              Arg.Any <Dictionary <string, string> >(),
                                              Arg.Any <string>());
        }
        public void GetAuthorizationToken_WhenCacheIsExpired_CallsTwoTimesCallApiMethod()
        {
            var configurationStub   = new Configuration();
            var authRequestResponse = CreateAuthRequestResponse();
            var apiClientMock       = CreateApiClient(authRequestResponse);
            var currentTime         = new DateTime(2000, 1, 1);
            var dateTimeProvider    = new SettableDateTimeProvider(currentTime);
            var cacheServiceStub    = new CacheService(dateTimeProvider);

            var authService = new AuthService(configurationStub, apiClientMock, cacheServiceStub);

            authService.GetAuthorizationToken();

            DateTime newCurrentTime = currentTime.AddMinutes(20);

            dateTimeProvider.Now = newCurrentTime;

            authService.GetAuthorizationToken();

            apiClientMock.Received(2).CallApi(Arg.Any <string>(),
                                              Arg.Any <Method>(),
                                              Arg.Any <List <KeyValuePair <string, string> > >(),
                                              Arg.Any <object>(),
                                              Arg.Any <Dictionary <string, string> >(),
                                              Arg.Any <Dictionary <string, string> >(),
                                              Arg.Any <Dictionary <string, FileParameter> >(),
                                              Arg.Any <Dictionary <string, string> >(),
                                              Arg.Any <string>());
        }
        public void AddOrUpdate_WhenCalledAndKeyIsNotInCache_AddsValueInCache()
        {
            var currentTime      = new DateTime(2000, 1, 1);
            var dateTimeProvider = new SettableDateTimeProvider(currentTime);
            var cacheService     = new CacheService(dateTimeProvider);
            var tokenResponse    = CreateTokenResponse();

            var cacheKey = "cacheKey";

            cacheService.AddOrUpdate(cacheKey, tokenResponse);

            var cachedValue = cacheService.Get(cacheKey);

            Assert.AreSame(cachedValue, tokenResponse);
        }
        public void Get_WhenCalledForACachedValue_ReturnsBasedOnInvalidCacheWindow(int windowInSeconds, bool isValid)
        {
            var currentTime      = new DateTime(2000, 1, 1);
            var dateTimeProvider = new SettableDateTimeProvider(currentTime);
            var cacheService     = new CacheService(dateTimeProvider);
            var tokenResponse    = CreateTokenResponse();

            var cacheKey = "cacheKey";

            cacheService.AddOrUpdate(cacheKey, tokenResponse);

            dateTimeProvider.Now = currentTime.AddSeconds(tokenResponse.ExpiresIn).Subtract(TimeSpan.FromSeconds(windowInSeconds));
            var cachedValue     = cacheService.Get(cacheKey);
            var expectedIsValid = cachedValue != null;

            Assert.AreEqual(expectedIsValid, isValid);
        }
        public void AddOrUpdate_WhenCalledTwoTimesForTheSameKey_OverwritesValueInCache()
        {
            var currentTime      = new DateTime(2000, 1, 1);
            var dateTimeProvider = new SettableDateTimeProvider(currentTime);
            var cacheService     = new CacheService(dateTimeProvider);
            var tokenResponse    = CreateTokenResponse();

            var cacheKey = "cacheKey";

            cacheService.AddOrUpdate(cacheKey, tokenResponse);

            var newTokenResponse = CreateTokenResponse();

            cacheService.AddOrUpdate(cacheKey, newTokenResponse);

            var cachedValue = cacheService.Get(cacheKey);

            Assert.AreSame(cachedValue, newTokenResponse);
        }
        public void Get_WhenCacheIsExpired_ReturnsNull()
        {
            var currentTime      = new DateTime(2000, 1, 1);
            var dateTimeProvider = new SettableDateTimeProvider(currentTime);
            var cacheService     = new CacheService(dateTimeProvider);
            var tokenResponse    = CreateTokenResponse();

            var cacheKey = "cacheKey";

            cacheService.AddOrUpdate(cacheKey, tokenResponse);

            var newCurrentTime = currentTime.AddMinutes(20);

            dateTimeProvider.Now = newCurrentTime;

            var response = cacheService.Get(cacheKey);

            Assert.AreEqual(null, response);
        }
        public void Get_WhenCacheIsNotExpired_ReturnsCachedValue()
        {
            var currentTime          = new DateTime(2000, 1, 1);
            var datetimeProviderStub = new SettableDateTimeProvider(currentTime);
            var tokenResponse        = CreateTokenResponse();

            var cacheService = new CacheService(datetimeProviderStub);
            var cacheKey     = "cacheKey";

            cacheService.AddOrUpdate(cacheKey, tokenResponse);

            datetimeProviderStub.Now = currentTime.AddMinutes(10);
            var response = cacheService.Get(cacheKey);

            Assert.AreEqual("access_token", response.AccessToken);
            Assert.AreEqual("token_type", response.TokenType);
            Assert.AreEqual(1000, response.ExpiresIn);
            Assert.AreEqual("https://rest.com", response.RestInstanceUrl);
            Assert.AreEqual("https://soap.com", response.SoapInstanceUrl);
        }