public void GetGlobalClient_ShouldReturnClient()
        {
            var config = new Mock <IConfigurationProvider>();

            config.Setup(x => x.GetGlobalConfigurationAsNameValueCollection(It.IsAny <string>(),
                                                                            It.IsAny <string>())).Returns(GetNamevalueCollection("us-east-1"));

            var clientFactory = new KmsClientFactory(config.Object);

            var client = clientFactory.GetGlobalClient();

            Assert.NotNull(client);

            //Should return same client .i.e returns client from cache
            var client2 = clientFactory.GetGlobalClient();

            Assert.NotNull(client);
            Assert.Equal(client, client2);
        }
        public void GetTenantSpecificClient_ShouldReturnClient()
        {
            var tenant = "demo";
            var config = new Mock <IConfigurationProvider>();

            config.Setup(x => x.GetTenantConfigurationAsNameValueCollection(tenant,
                                                                            It.IsAny <string>(), It.IsAny <string>())).Returns(GetNamevalueCollection("us-east-1"));

            var clientFactory = new KmsClientFactory(config.Object);

            var clientA = clientFactory.GetTenantSpecificClient(tenant);

            Assert.NotNull(clientA);

            //Should return same client .i.e returns client from cache
            var clientB = clientFactory.GetTenantSpecificClient(tenant);

            Assert.NotNull(clientB);
            Assert.Equal(clientA, clientB);
        }
        public async Task Process_ApplicationEvent_ShouldRemoveUnusedClients()
        {
            var tenant = "demo_tenant";
            var config = new Mock <IConfigurationProvider>();

            config.Setup(x => x.GetTenantConfigurationAsNameValueCollectionAsync(tenant,
                                                                                 It.IsAny <string>(), It.IsAny <string>())).ReturnsAsync(GetNamevalueCollection("us-west-1"));

            //Get client first time, should create new client and return
            var clientFactory = new KmsClientFactory(config.Object);
            var clientA       = await clientFactory.GetTenantSpecificClientAsync(tenant);

            Assert.NotNull(clientA);

            //Get the client second time, should return client from cache
            var clientB = await clientFactory.GetTenantSpecificClientAsync(tenant);

            Assert.NotNull(clientB);
            Assert.Equal(clientA, clientB);

            //Update consul settings
            config.Setup(x => x.GetTenantConfigurationAsNameValueCollectionAsync(tenant,
                                                                                 It.IsAny <string>(), It.IsAny <string>())).ReturnsAsync(GetNamevalueCollection("us-west-2"));

            var eventData = new ApplicationEvent
            {
                Id        = Guid.NewGuid().ToString(),
                Name      = "in-memory-consul-cache-refresh",
                TimeStamp = DateTime.Now
            };

            //Process is invoked on consule event and clears unused clients
            clientFactory.Process(eventData);
            Thread.Sleep(5000);

            // Get the client third time, this should return different client instance for same tenant as settings in consul is updated
            var updatedClient = await clientFactory.GetTenantSpecificClientAsync(tenant);

            Assert.NotNull(updatedClient);
            Assert.NotEqual(updatedClient, clientA);
        }
        public void GetCient_ShouldReturnClient()
        {
            var clientFactory = new KmsClientFactory(null);

            var settings = GetKmsSettings("us-east-1");
            var clientA  = clientFactory.GetClient(settings);

            Assert.NotNull(clientA);

            //Should return same client .i.e returns client from cache
            var clientB = clientFactory.GetClient(settings);

            Assert.NotNull(clientB);
            Assert.Equal(clientA, clientB);

            //Should return different client
            settings = GetKmsSettings("us-east-2");
            var clientC = clientFactory.GetClient(settings);

            Assert.NotNull(clientA);
            Assert.NotEqual(clientA, clientC);
        }