/// <summary>
        /// Provides cached translations between UniqueIds and USI values.
        /// </summary>
        /// <param name="cacheProvider">The cache where the database-specific maps (dictionaries) are stored, expiring after 4 hours of inactivity.</param>
        /// <param name="edFiOdsInstanceIdentificationProvider">Identifies the ODS instance for the current call.</param>
        /// <param name="uniqueIdToUsiValueMapper">A component that maps between USI and UniqueId values.</param>
        /// <param name="personIdentifiersProvider">A component that retrieves all Person identifiers.</param>
        /// <param name="slidingExpiration">Indicates how long the cache values will remain in memory after being used before all the cached values are removed.</param>
        /// <param name="absoluteExpirationPeriod">Indicates the maximum time that the cache values will remain in memory before being refreshed.</param>
        /// <param name="synchronousInitialization">Indicates whether the cache should wait until all the Person identifiers are loaded before responding, or if using the value mappers initially to avoid an initial delay is preferable.</param>
        public PersonUniqueIdToUsiCache(
            ICacheProvider cacheProvider,
            IEdFiOdsInstanceIdentificationProvider edFiOdsInstanceIdentificationProvider,
            IUniqueIdToUsiValueMapper uniqueIdToUsiValueMapper,
            IPersonIdentifiersProvider personIdentifiersProvider,
            TimeSpan slidingExpiration,
            TimeSpan absoluteExpirationPeriod,
            bool synchronousInitialization)
        {
            _cacheProvider = cacheProvider;
            _edFiOdsInstanceIdentificationProvider = edFiOdsInstanceIdentificationProvider;
            _uniqueIdToUsiValueMapper  = uniqueIdToUsiValueMapper;
            _personIdentifiersProvider = personIdentifiersProvider;
            _synchronousInitialization = synchronousInitialization;

            if (slidingExpiration < TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(slidingExpiration), "TimeSpan cannot be a negative value.");
            }

            if (absoluteExpirationPeriod < TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(absoluteExpirationPeriod), "TimeSpan cannot be a negative value.");
            }

            // Use sliding expiration value, if both are set.
            if (slidingExpiration > TimeSpan.Zero && absoluteExpirationPeriod > TimeSpan.Zero)
            {
                absoluteExpirationPeriod = TimeSpan.Zero;
            }

            _slidingExpiration        = slidingExpiration;
            _absoluteExpirationPeriod = absoluteExpirationPeriod;
        }
            protected override void Arrange()
            {
                _edFiOdsInstanceIdentificationProvider = Stub <IEdFiOdsInstanceIdentificationProvider>();
                _personIdentifiersProvider             = Stub <IPersonIdentifiersProvider>();

                A.CallTo(() => _personIdentifiersProvider.GetAllPersonIdentifiers(A <string> ._))
                .Returns(Task.Run(() => (IEnumerable <PersonIdentifiersValueMap>) new PersonIdentifiersValueMap[0]));

                _usiValueMapper = Stub <IUniqueIdToUsiValueMapper>();

                A.CallTo(() => _usiValueMapper.GetUsi(A <string> ._, A <string> ._))
                .Returns(new PersonIdentifiersValueMap());

                _idValueMapper = Stub <IUniqueIdToIdValueMapper>();

                A.CallTo(() => _idValueMapper.GetId(A <string> ._, A <string> ._))
                .Returns(new PersonIdentifiersValueMap());

                SetupCaching();

                void SetupCaching()
                {
                    _memoryCacheProvider = new MemoryCacheProvider {
                        MemoryCache = new MemoryCache(IsolatedForUnitTest)
                    };

                    _usiCache = new PersonUniqueIdToUsiCache(
                        _memoryCacheProvider, _edFiOdsInstanceIdentificationProvider, _usiValueMapper, _personIdentifiersProvider,
                        TimeSpan.Zero, TimeSpan.Zero,
                        synchronousInitialization: true);
                }
            }
            protected override void Arrange()
            {
                _edFiOdsInstanceIdentificationProvider = Stub <IEdFiOdsInstanceIdentificationProvider>();
                _personIdentifiersProvider             = Stub <IPersonIdentifiersProvider>();

                A.CallTo(() => _personIdentifiersProvider.GetAllPersonIdentifiers(A <string> ._))
                .Returns(Task.Run(() => (IEnumerable <PersonIdentifiersValueMap>) new PersonIdentifiersValueMap[0]));

                _usiValueMapper = Stub <IUniqueIdToUsiValueMapper>();

                A.CallTo(() => _usiValueMapper.GetUsi(A <string> ._, A <string> ._)).Returns(new PersonIdentifiersValueMap());

                _idValueMapper = Stub <IUniqueIdToIdValueMapper>();

                A.CallTo(() => _idValueMapper.GetId(A <string> ._, A <string> ._))
                .Returns(new PersonIdentifiersValueMap());

                var memorycacheoption = A.Fake <IOptions <MemoryCacheOptions> >();

                MemoryCache memoryCache = new MemoryCache(memorycacheoption);

                _memoryCacheProvider = new MemoryCacheProvider(memoryCache);

                _usiCache = new PersonUniqueIdToUsiCache(
                    _memoryCacheProvider, _edFiOdsInstanceIdentificationProvider, _usiValueMapper, _personIdentifiersProvider,
                    TimeSpan.Zero, TimeSpan.Zero,
                    synchronousInitialization: true);
            }
            protected override void Arrange()
            {
                _edFiOdsInstanceIdentificationProvider = new FakeEdFiOdsInstanceIdentificationProvider();

                _personIdentifiersProvider = Stub <IPersonIdentifiersProvider>();

                A.CallTo(() => _personIdentifiersProvider.GetAllPersonIdentifiers(A <string> .That.IsEqualTo(Staff)))
                .Returns(
                    Task.Run(
                        () =>
                        (IEnumerable <PersonIdentifiersValueMap>)
                        new[]
                {
                    new PersonIdentifiersValueMap
                    {
                        UniqueId = "ABC123",
                        Usi      = 11
                    }
                })).Once();

                A.CallTo(() => _personIdentifiersProvider.GetAllPersonIdentifiers(A <string> .That.IsEqualTo(Staff)))
                .Returns(
                    Task.Run(
                        () =>
                        (IEnumerable <PersonIdentifiersValueMap>)
                        new[]
                {
                    new PersonIdentifiersValueMap
                    {
                        UniqueId = "CDE234",
                        Usi      = 11
                    }
                })).Once();

                // USI value mapper gets call twice during Act step, with first value on ODS instance 1, and second on ODS instance 2
                _usiValueMapper = Stub <IUniqueIdToUsiValueMapper>();

                SetupCaching();

                void SetupCaching()
                {
                    var memorycacheoption = A.Fake <IOptions <MemoryCacheOptions> >();

                    MemoryCache memoryCache = new MemoryCache(memorycacheoption);

                    _memoryCacheProvider = new MemoryCacheProvider(memoryCache);

                    _usiCache = new PersonUniqueIdToUsiCache(
                        _memoryCacheProvider,
                        _edFiOdsInstanceIdentificationProvider,
                        _usiValueMapper,
                        _personIdentifiersProvider,
                        TimeSpan.Zero,
                        TimeSpan.Zero,
                        synchronousInitialization: true);
                }
            }
            protected override void Arrange()
            {
                _edfiOdsInstanceIdentificationProvider = Stub <IEdFiOdsInstanceIdentificationProvider>();
                _usiValueMapper = Stub <IUniqueIdToUsiValueMapper>();

                _suppliedUsiValueMap = new PersonIdentifiersValueMap
                {
                    UniqueId = Guid.NewGuid()
                               .ToString(),
                    Usi = 10,
                    Id  = Guid.NewGuid() // Id is also provided by the USI value mapper
                };

                A.CallTo(() => _usiValueMapper.GetUniqueId(A <string> ._, A <int> ._))
                .Returns(_suppliedUsiValueMap);

                _personIdentifiersProvider = Stub <IPersonIdentifiersProvider>();

                A.CallTo(() => _personIdentifiersProvider.GetAllPersonIdentifiers(A <string> ._))
                .Returns(Task.Run(() => (IEnumerable <PersonIdentifiersValueMap>) new PersonIdentifiersValueMap[0]));

                SetupCaching();

                void SetupCaching()
                {
                    var memorycacheoption = A.Fake <IOptions <MemoryCacheOptions> >();

                    MemoryCache memoryCache = new MemoryCache(memorycacheoption);

                    _memoryCacheProvider = new MemoryCacheProvider(memoryCache);

                    _usiCache = new PersonUniqueIdToUsiCache(
                        _memoryCacheProvider,
                        _edfiOdsInstanceIdentificationProvider,
                        _usiValueMapper,
                        _personIdentifiersProvider,
                        TimeSpan.Zero,
                        TimeSpan.Zero,
                        synchronousInitialization: true);

                    PersonUniqueIdToUsiCache.GetCache = () => _usiCache;
                }
            }
            protected override void Arrange()
            {
                _edFiOdsInstanceIdentificationProvider = new FakeEdFiOdsInstanceIdentificationProvider();
                _personIdentifiersProvider             = Stub <IPersonIdentifiersProvider>();

                A.CallTo(() => _personIdentifiersProvider.GetAllPersonIdentifiers(A <string> ._))
                .Returns(Task.Run(() => (IEnumerable <PersonIdentifiersValueMap>) new PersonIdentifiersValueMap[0]));

                _suppliedIdForUniqueIdABC123 = Guid.NewGuid();

                // USI value mapper gets call twice during Act step, with first value on ODS instance 1, and second on ODS instance 2
                _usiValueMapper = Stub <IUniqueIdToUsiValueMapper>();

                A.CallTo(() => _usiValueMapper.GetUsi(A <string> .That.IsEqualTo(Staff), A <string> .That.IsEqualTo("ABC123")))
                .Returns(
                    new PersonIdentifiersValueMap
                {
                    UniqueId = "ABC123",
                    Usi      = 11
                }).Once();

                A.CallTo(() => _usiValueMapper.GetUsi(A <string> .That.IsEqualTo(Staff), A <string> .That.IsEqualTo("ABC123")))
                .Returns(
                    new PersonIdentifiersValueMap
                {
                    UniqueId = "ABC123",
                    Usi      = 22
                }).Once();

                _idValueMapper = Stub <IUniqueIdToIdValueMapper>();

                A.CallTo(() => _idValueMapper.GetId(A <string> .That.IsEqualTo(Staff), A <string> .That.IsEqualTo("ABC123")))
                .Returns(
                    new PersonIdentifiersValueMap
                {
                    UniqueId = "ABC123",
                    Id       = _suppliedIdForUniqueIdABC123
                }).Once();

                SetupCaching();

                void SetupCaching()
                {
                    _memoryCacheProvider = new MemoryCacheProvider {
                        MemoryCache = new MemoryCache(IsolatedForUnitTest)
                    };

                    _idCache = new PersonUniqueIdToIdCache(
                        _memoryCacheProvider,
                        _edFiOdsInstanceIdentificationProvider,
                        _idValueMapper);

                    _usiCache = new PersonUniqueIdToUsiCache(
                        _memoryCacheProvider,
                        _edFiOdsInstanceIdentificationProvider,
                        _usiValueMapper,
                        _personIdentifiersProvider,
                        TimeSpan.Zero,
                        TimeSpan.Zero,
                        synchronousInitialization: true);
                }
            }
            protected override void Arrange()
            {
                _edfiOdsInstanceIdentificationProvider = Stub <IEdFiOdsInstanceIdentificationProvider>();
                _idValueMapper             = Stub <IUniqueIdToIdValueMapper>();
                _usiValueMapper            = Stub <IUniqueIdToUsiValueMapper>();
                _personIdentifiersProvider = Stub <IPersonIdentifiersProvider>();

                _suppliedUsiValueMap = new PersonIdentifiersValueMap
                {
                    UniqueId = Guid.NewGuid()
                               .ToString(),
                    Usi = 10,
                    Id  = Guid.NewGuid() // Id is also provided by the USI value mapper
                };

                _suppliedPersonIdentifiers = new List <PersonIdentifiersValueMap>
                {
                    new PersonIdentifiersValueMap
                    {
                        UniqueId = Guid.NewGuid()
                                   .ToString(),
                        Usi = 100,
                        Id  = Guid.NewGuid()
                    },
                    new PersonIdentifiersValueMap
                    {
                        UniqueId = Guid.NewGuid()
                                   .ToString(),
                        Usi = 101,
                        Id  = Guid.NewGuid()
                    }
                };

                A.CallTo(() => _idValueMapper.GetId(A <string> ._, A <string> ._))
                .Returns(new PersonIdentifiersValueMap());

                A.CallTo(() => _usiValueMapper.GetUsi(A <string> ._, A <string> ._))
                .Returns(_suppliedUsiValueMap);

                A.CallTo(() => _personIdentifiersProvider.GetAllPersonIdentifiers(A <string> ._))
                .Returns(Task.Run(() => (IEnumerable <PersonIdentifiersValueMap>)_suppliedPersonIdentifiers));

                SetupCaching();

                void SetupCaching()
                {
                    _memoryCacheProvider = new MemoryCacheProvider {
                        MemoryCache = new MemoryCache(IsolatedForUnitTest)
                    };

                    _idCache = new PersonUniqueIdToIdCache(
                        _memoryCacheProvider,
                        _edfiOdsInstanceIdentificationProvider,
                        _idValueMapper);

                    _usiCache = new PersonUniqueIdToUsiCache(
                        _memoryCacheProvider,
                        _edfiOdsInstanceIdentificationProvider,
                        _usiValueMapper,
                        _personIdentifiersProvider,
                        TimeSpan.Zero,
                        TimeSpan.Zero,
                        synchronousInitialization: true);

                    PersonUniqueIdToUsiCache.GetCache = () => _usiCache;
                }
            }
            protected override void Arrange()
            {
                _edfiOdsInstanceIdentificationProvider = Stub <IEdFiOdsInstanceIdentificationProvider>();
                _idValueMapper             = Stub <IUniqueIdToIdValueMapper>();
                _usiValueMapper            = Stub <IUniqueIdToUsiValueMapper>();
                _personIdentifiersProvider = Stub <IPersonIdentifiersProvider>();

                // usi value mapper
                _suppliedUsiValueMap = new PersonIdentifiersValueMap
                {
                    UniqueId = Guid.NewGuid()
                               .ToString(),
                    Usi = 10
                };

                A.CallTo(() => _usiValueMapper.GetUsi(A <string> ._, A <string> ._))
                .Returns(_suppliedUsiValueMap);

                // id value mapper
                _suppliedIdValueMap = new PersonIdentifiersValueMap
                {
                    UniqueId = _suppliedUsiValueMap.UniqueId, // Same uniqueId
                    Id       = Guid.NewGuid()
                };

                A.CallTo(() => _idValueMapper.GetId(A <string> ._, A <string> ._))
                .Returns(_suppliedIdValueMap);

                _suppliedPersonIdentifiersValueMap = new PersonIdentifiersValueMap
                {
                    UniqueId = Guid.NewGuid()
                               .ToString(),
                    Usi = 100
                };

                A.CallTo(() => _personIdentifiersProvider.GetAllPersonIdentifiers(A <string> ._))
                .Returns(
                    Task.Run(
                        () =>
                {
                    //Force slight delay during initialization to mimic real behavior.
                    Thread.Sleep(10);

                    return
                    ((IEnumerable <PersonIdentifiersValueMap>) new[] { _suppliedPersonIdentifiersValueMap });
                }
                        ));

                SetupCaching();

                void SetupCaching()
                {
                    _memoryCacheProvider = new MemoryCacheProvider {
                        MemoryCache = new MemoryCache(IsolatedForUnitTest)
                    };

                    _idCache = new PersonUniqueIdToIdCache(
                        _memoryCacheProvider, _edfiOdsInstanceIdentificationProvider, _idValueMapper);

                    _usiCache = new PersonUniqueIdToUsiCache(
                        _memoryCacheProvider, _edfiOdsInstanceIdentificationProvider, _usiValueMapper, _personIdentifiersProvider,
                        TimeSpan.Zero, TimeSpan.Zero,
                        synchronousInitialization: false);
                }
            }