public TableStorageIntegrationTests()
        {
            var readConfig = new ConfigurationBuilder().AddJsonFile("appSettings.json").Build();
            var config     = new ServicePrincipleConfig
            {
                InstanceName   = readConfig.GetValue <string>("InstanceName"),
                TenantId       = readConfig.GetValue <string>("TenantId"),
                SubscriptionId = readConfig.GetValue <string>("SubscriptionId"),
                AppId          = readConfig.GetValue <string>("AppId"),
                AppSecret      = readConfig.GetValue <string>("AppSecret"),
            };

            _tableStorageClient = new TableStorage(config);
            _tableStorageClient.Name.Should().Be(config.InstanceName);
            _tableStorageClient.CreateTable(TestTableName).GetAwaiter().GetResult();
        }
Esempio n. 2
0
        public void CreateAndGetTablesMultipleTasks()
        {
            //Multi thread.
            const int M = 32;

            Task.WaitAll(Enumerable.Range(0, M)
                         .Select(i => Task.Factory.StartNew(() =>
            {
                for (int k1 = 0; k1 < 10; k1++)
                {
                    TableStorage.CreateTable("table" + k1.ToString());
                }
            }))
                         .ToArray());

            Assert.AreEqual(10, TableStorage.GetTables().Distinct().Count());
        }
        public AuditLogIntegrationTests()
        {
            var readConfig = new ConfigurationBuilder().AddJsonFile("appSettings.json").Build();
            var config     = new ServicePrincipleConfig
            {
                InstanceName   = readConfig.GetValue <string>("InstanceName"),
                TenantId       = readConfig.GetValue <string>("TenantId"),
                SubscriptionId = readConfig.GetValue <string>("SubscriptionId"),
                AppId          = readConfig.GetValue <string>("AppId"),
                AppSecret      = readConfig.GetValue <string>("AppSecret"),
            };

            _tableStorage = new TableStorage(config);
            _tableStorage.CreateTable(_tableStorage.AuditTableName).GetAwaiter().GetResult();
            Thread.Sleep(5000);

            _auditLogger = _tableStorage;
        }
Esempio n. 4
0
        public void CreateAndGetTable()
        {
            var originalCount = TableStorage.GetTables().Count();

            //Single thread.
            for (int i = 0; i <= 5; i++)
            {
                TableStorage.CreateTable("table" + i.ToString());
            }

            Assert.AreEqual(6, TableStorage.GetTables().Count() - originalCount, "#A01");

            //Remove tables.
            Assert.False(TableStorage.DeleteTable("Table_that_does_not_exist"), "#A02");
            var isSuccess = TableStorage.DeleteTable("table" + 4.ToString());

            Assert.IsTrue(isSuccess, "#A03");
            Assert.AreEqual(5, TableStorage.GetTables().Count() - originalCount, "#A04");
        }
Esempio n. 5
0
        public void InsertAndGetMethodSingleThread()
        {
            const string tableName = "myTable";

            TableStorage.CreateTable(tableName);

            const int partitionCount = 10;

            //Creating entities: a hundred. Pkey created with the last digit of a number between 0 and 99.
            var entities =
                Enumerable.Range(0, 100).Select(
                    i =>
                    new CloudEntity <object>
            {
                PartitionKey = "Pkey-" + (i % partitionCount).ToString("0"),
                RowKey       = "RowKey-" + i.ToString("00"),
                Value        = new object()
            });

            //Insert entities.
            TableStorage.Insert(tableName, entities);

            //retrieve all of them.
            var retrievedEntities1 = TableStorage.Get <object>(tableName);

            Assert.AreEqual(100, retrievedEntities1.Count(), "#B01");

            //Test overloads...
            var retrievedEntites2 = TableStorage.Get <object>(tableName, "Pkey-9");

            Assert.AreEqual(10, retrievedEntites2.Count(), "#B02");

            var retrievedEntities3 = TableStorage.Get <object>(
                tableName, "Pkey-7", new[] { "RowKey-27", "RowKey-37", "IAmNotAKey" });

            Assert.AreEqual(2, retrievedEntities3.Count(), "#B03");

            //The following tests handle the exclusive and inclusive bounds of key search.
            var retrieved4 = TableStorage.Get <object>(tableName, "Pkey-1", "RowKey-01", "RowKey-91");

            Assert.AreEqual(9, retrieved4.Count(), "#B04");

            var retrieved5 = TableStorage.Get <object>(tableName, "Pkey-1", "RowKey-01", null);

            Assert.AreEqual(10, retrieved5.Count(), "#B05");

            var retrieved6 = TableStorage.Get <object>(tableName, "Pkey-1", null, null);

            Assert.AreEqual(10, retrieved6.Count(), "#B06");

            var retrieved7 = TableStorage.Get <object>(tableName, "Pkey-1", null, "RowKey-21");

            Assert.AreEqual(2, retrieved7.Count(), "#B07");

            //The next test should handle non existing table names.
            //var isSuccess = false;

            // TODO: Looks like something is not finished here

            var emptyEnumeration = TableStorage.Get <object>("IAmNotATable", "IaMNotAPartiTion");

            Assert.AreEqual(0, emptyEnumeration.Count(), "#B08");
        }
Esempio n. 6
0
        public void InsertUpdateAndDeleteSingleThread()
        {
            const string tableName    = "myTable";
            const string newTableName = "myNewTable";

            TableStorage.CreateTable(tableName);

            const int partitionCount = 10;

            var entities =
                Enumerable.Range(0, 100).Select(
                    i =>
                    new CloudEntity <object>
            {
                PartitionKey = "Pkey-" + (i % partitionCount).ToString("0"),
                RowKey       = "RowKey-" + i.ToString("00"),
                Value        = new object()
            });

            TableStorage.Insert(tableName, entities);

            var isSucces = false;

            try
            {
                TableStorage.Insert(
                    tableName, new[] { new CloudEntity <object> {
                                           PartitionKey = "Pkey-6", RowKey = "RowKey-56"
                                       } });
            }
            catch (Exception exception)
            {
                isSucces = (exception as InvalidOperationException) == null ? false : true;
            }
            Assert.IsTrue(isSucces);

            TableStorage.CreateTable(newTableName);
            TableStorage.Insert(
                newTableName,
                new[]
                { new CloudEntity <object> {
                      PartitionKey = "Pkey-6", RowKey = "RowKey-56", Value = new object()
                  } });

            Assert.AreEqual(2, TableStorage.GetTables().Count());

            TableStorage.Update(
                newTableName,
                new[] { new CloudEntity <object> {
                            PartitionKey = "Pkey-6", RowKey = "RowKey-56", Value = 2000
                        } },
                true);
            Assert.AreEqual(
                2000, (int)TableStorage.Get <object>(newTableName, "Pkey-6", new[] { "RowKey-56" }).First().Value);

            TableStorage.Delete <object>(newTableName, "Pkey-6", new[] { "RowKey-56" });

            var retrieved = TableStorage.Get <object>(newTableName);

            Assert.AreEqual(0, retrieved.Count());
        }
Esempio n. 7
0
        public static async Task Run([TimerTrigger("0 0 0 * * *")] TimerInfo myTimer, ILogger log)
        {
            string message         = string.Empty;
            string messageContent  = string.Empty;
            string responseMessage = string.Empty;

            var connectingString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
            var serviceParam     = Environment.GetEnvironmentVariable("ServiceParameter").Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).
                                   ToDictionary(x => x.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries)[0],
                                                x => x.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries)[1]);

            var adminId    = Environment.GetEnvironmentVariable("AdminId");
            var csLink     = Environment.GetEnvironmentVariable("CSContact");
            var adminArray = adminId.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            var csArray    = csLink.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            try
            {
                var serviceList = await TableStorage <UserServiceInfoModel> .CreateTable(connectingString, "ClientServiceInfo");

                var macList = await TableStorage <TradeCallMACRecordModel> .CreateTable(connectingString, "MACTable");

                // Only check alive service
                var mac = macList.FindAsync("ServiceStatus",
                                            TableStorage <TradeCallMACRecordModel> .SelectCondition.NotEqual,
                                            "Terminated").Result;
                var now = DateTime.Now;

                // Comfirm the MAC is availble to use notify funtion
                foreach (var alive in mac)
                {
                    bool bChange = false;
                    var  days    = Math.Ceiling(new TimeSpan(alive.ServiceExpiredDate.Ticks - now.Ticks).TotalDays);
                    if (days >= 0)
                    {
                        alive.RemainedDays = Convert.ToInt16(days);
                        bChange            = true;
                    }

                    if (alive.RemainedDays <= 0)
                    {
                        alive.ServiceStatus = "Expired";
                        bChange             = true;
                    }

                    if (bChange)
                    {
                        await macList.UpdateAsync(alive);
                    }
                }

                var clientList = await TableStorage <UserLineInfo> .CreateTable(connectingString, "ClientInfo");

                var serviceTable = serviceList.FindAsync("ServiceStatus",
                                                         TableStorage <UserServiceInfoModel> .SelectCondition.NotEqual,
                                                         "Terminated").Result;

                var admin = new List <UserLineInfo>();
                foreach (var item in adminArray)
                {
                    var user = clientList.FindAsync("User", item).Result;
                    if (user != null)
                    {
                        admin.Add(user);
                    }
                }

                var agents = string.Empty;
                foreach (var item in csArray)
                {
                    agents += (item + "\n");
                }

                foreach (var service in serviceTable)
                {
                    var days = Math.Ceiling(new TimeSpan(service.ServiceExpiredDate.Ticks - now.Ticks).TotalDays);
                    if (days >= 0)
                    {
                        service.RemainedDays = Convert.ToInt16(days);
                    }

                    if (days <= Convert.ToInt16(serviceParam["ServiceRemindDays"]) && service.ExpiredNotifyTimes > 0)
                    {
                        var client = clientList.FindAsync("User", service.RowKey).Result;

                        if (days > 0)
                        {
                            messageContent = $"@{client.UserName}, 您的交易通知服務將於 {service.ServiceExpiredDate.ToString("yyyy/MM/dd")} 到期,請儘快連絡營業員\n{agents}";
                        }
                        else
                        {
                            messageContent = $"@{client.UserName}, 您的交易通知服務已到期,請連絡營業員\n{agents}";
                        }

                        if (client != null)
                        {
                            using (var notifyClient = new NotifyClient())
                            {
                                if (client.NotifyToken.Length > 1)
                                {
                                    await notifyClient.SentNotifyToUser(client.NotifyToken,
                                                                        $"\n\n@{client.UserName} - 到期通知\n\n{messageContent}");
                                }
                            }
                        }

                        if (admin.Count > 0)
                        {
                            foreach (var item in admin)
                            {
                                using (var notifyClient = new NotifyClient())
                                {
                                    if (item.NotifyToken.Length > 1)
                                    {
                                        await notifyClient.SentNotifyToUser(item.NotifyToken,
                                                                            $"\n\n@{client.UserName} - 到期通知\n\n" +
                                                                            $"會員 @{client.UserName} 交易通知服務將於 {days}日 後到期,請通知會員");
                                    }
                                }
                            }
                        }
                    }

                    if (days <= 0 && service.ServiceStatus == "Alive")
                    {
                        service.ServiceStatus = "Expired";
                    }

                    if (days <= 0 && service.ExpiredNotifyTimes > 0)
                    {
                        service.ExpiredNotifyTimes--;
                    }

                    await serviceList.UpdateAsync(service);
                }

                message = $"推播完成";
            }
            catch (Exception ex)
            {
                message = $"推播未完成,錯誤原因: {ex.ToString()}";
            }
            finally
            {
                log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}, {message}");
            }
        }