public async Task Handle(T message, IMessageHandlerContext context)
        {
            try
            {
                var actionType = message.ActionType;
                Log.Info($"Received {actionType} message: {message.ToJSON()}");

                if ((ConfigHelper.SupportedActionTypes & actionType) != actionType)
                {
                    throw new UnsupportedActionTypeException($"Unsupported Action Type: {actionType}");
                }

                var tenantId = GetMessageHeaderValue(ActionMessageHeaders.TenantId, context);
                var tenants  = await TenantInfoRetriever.GetTenants();

                TenantInformation tenant;
                if (!tenants.TryGetValue(tenantId, out tenant))
                {
                    throw new EntityNotFoundException($"Failed to find Tenant Info for Tenant ID {tenantId}.");
                }

                var tenantConnectionString = tenant.BlueprintConnectionString;
                if (string.IsNullOrWhiteSpace(tenantConnectionString))
                {
                    throw new EntityNotFoundException($"Invalid Connection String provided for tenant {tenantId}.");
                }

                Logger.Log($"Creating repository with tenant connection string: {tenantConnectionString}", message, tenant);
                var repository = CreateRepository(actionType, tenantConnectionString);

                var transactionStatus = await TransactionValidator.GetStatus(message, tenant, repository);

                if (transactionStatus == TransactionStatus.RolledBack)
                {
                    Logger.Log("Discarding message for rolled back transaction", message, tenant);
                    return;
                }

                message.NSBMessageId = GetMessageHeaderValue(Headers.MessageId, context);
                var timeSent = GetMessageHeaderValue(Headers.TimeSent, context);
                try
                {
                    message.NSBRetryCount = GetMessageHeaderValue(Headers.DelayedRetries, context);
                }
                catch (MessageHeaderValueNotFoundException)
                {
                    message.NSBRetryCount = "0";
                }

                Logger.Log($"Started handling {actionType} action. Message ID: {message.NSBMessageId}. Time Sent: {timeSent}", message, tenant);
                var result = await ActionHelper.HandleAction(tenant, message, repository);

                Logger.Log($"Finished handling {actionType} action. Result: {result}. Message ID: {message.NSBMessageId}. Time Sent: {timeSent}", message, tenant);
            }
            catch (Exception ex)
            {
                Log.Error($"Failed to handle {message.ActionType} Message {message.ToJSON()} due to an exception: {ex.Message}", ex);
                throw;
            }
        }
Exemple #2
0
        public async Task TenantInfoRetriever_ReturnsTenants()
        {
            // arrange
            var mockConfigHelper = new Mock <IConfigHelper>();

            mockConfigHelper.Setup(m => m.CacheExpirationMinutes).Returns(1);
            var mockActionHandlerServiceRepository = new Mock <IBaseRepository>();
            var sqlTenants = new List <TenantInformation>();

            for (int i = 0; i < 5; i++)
            {
                sqlTenants.Add(
                    new TenantInformation
                {
                    TenantId = $"TenantId{i}",
                    BlueprintConnectionString = $"BlueprintConnectionString{i}",
                    AdminStoreLog             = $"AdminStoreLog{i}",
                    ExpirationDate            = DateTime.MaxValue,
                    PackageLevel = i,
                    PackageName  = $"PackageName{i}",
                    StartDate    = DateTime.MinValue,
                    TenantName   = $"TenantName{i}"
                });
            }
            mockActionHandlerServiceRepository.Setup(m => m.GetTenantsFromTenantsDb()).ReturnsAsync(sqlTenants);
            var tenantInfoRetriever = new TenantInfoRetriever(mockActionHandlerServiceRepository.Object, mockConfigHelper.Object);

            // act
            var tenants = await tenantInfoRetriever.GetTenants();

            // assert
            Assert.AreEqual(sqlTenants.Count, tenants.Count);
            foreach (var sqlTenant in sqlTenants)
            {
                var tenant = tenants[sqlTenant.TenantId];
                Assert.AreEqual(sqlTenant.TenantId, tenant.TenantId);
                Assert.AreEqual(sqlTenant.BlueprintConnectionString, tenant.BlueprintConnectionString);
                Assert.AreEqual(sqlTenant.AdminStoreLog, tenant.AdminStoreLog);
                Assert.AreEqual(sqlTenant.ExpirationDate, tenant.ExpirationDate);
                Assert.AreEqual(sqlTenant.PackageLevel, tenant.PackageLevel);
                Assert.AreEqual(sqlTenant.PackageName, tenant.PackageName);
                Assert.AreEqual(sqlTenant.StartDate, tenant.StartDate);
                Assert.AreEqual(sqlTenant.TenantName, tenant.TenantName);
            }
        }