Exemplo n.º 1
0
        /// <summary>
        /// Add the given alert rule to the alert rules store.
        /// </summary>
        /// <returns>A task represents this operation.</returns>
        /// <param name="addAlertRule">The model that contains all the require parameters for adding alert rule.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <exception cref="SmartSignalsManagementApiException">This exception is thrown when we failed to add the alert rule.</exception>
        public async Task AddAlertRuleAsync(AlertRuleApiEntity addAlertRule, CancellationToken cancellationToken)
        {
            Diagnostics.EnsureArgumentNotNull(() => addAlertRule);

            // Verify given input model is valid
            if (!IsAddAlertRuleModelValid(addAlertRule, out var validationError))
            {
                throw new SmartSignalsManagementApiException(validationError, HttpStatusCode.BadRequest);
            }

            try
            {
                await this.alertRuleStore.AddOrReplaceAlertRuleAsync(
                    new AlertRule
                {
                    Id              = Guid.NewGuid().ToString(),
                    Name            = addAlertRule.Name,
                    Description     = addAlertRule.Description,
                    SignalId        = addAlertRule.SignalId,
                    ResourceId      = addAlertRule.ResourceId,
                    Cadence         = TimeSpan.FromMinutes(addAlertRule.CadenceInMinutes),
                    EmailRecipients = addAlertRule.EmailRecipients
                },
                    cancellationToken);
            }
            catch (AlertRuleStoreException e)
            {
                throw new SmartSignalsManagementApiException("Failed to add the given alert rule", e, HttpStatusCode.InternalServerError);
            }
        }
Exemplo n.º 2
0
        public async Task WhenAddingSignalHappyFlow()
        {
            var addSignalModel = new AlertRuleApiEntity()
            {
                SignalId         = Guid.NewGuid().ToString(),
                ResourceId       = "resourceId",
                CadenceInMinutes = 1440
            };

            this.alertRuleStoreMock.Setup(s => s.AddOrReplaceAlertRuleAsync(It.IsAny <AlertRule>(), It.IsAny <CancellationToken>())).Returns(Task.CompletedTask);

            // This shouldn't throw any exception
            await this.alertRuleApi.AddAlertRuleAsync(addSignalModel, CancellationToken.None);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Validates if the given model for adding alert rule is valid.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="errorInformation">The error information which will be filled in case validation will fail.</param>
        /// <returns>True in case model is valid, else false.</returns>
        private static bool IsAddAlertRuleModelValid(AlertRuleApiEntity model, out string errorInformation)
        {
            if (string.IsNullOrWhiteSpace(model.SignalId))
            {
                errorInformation = "Signal ID can't be empty";
                return(false);
            }

            if (model.CadenceInMinutes <= 0)
            {
                errorInformation = "CadenceInMinutes parameter must be a positive integer";
                return(false);
            }

            errorInformation = string.Empty;
            return(true);
        }
Exemplo n.º 4
0
        public async Task WhenAddingSignalButCadenceValueIsInvalidCronValueThenThrowException()
        {
            var addSignalModel = new AlertRuleApiEntity()
            {
                SignalId         = Guid.NewGuid().ToString(),
                ResourceId       = "resourceId",
                CadenceInMinutes = 0
            };

            try
            {
                await this.alertRuleApi.AddAlertRuleAsync(addSignalModel, CancellationToken.None);
            }
            catch (SmartSignalsManagementApiException e)
            {
                Assert.AreEqual(HttpStatusCode.BadRequest, e.StatusCode);
                return;
            }

            Assert.Fail("Invalid CRON value should throw an exception");
        }
Exemplo n.º 5
0
        public async Task WhenAddingSignalButModelIsInvalidBecauseSignalIdIsEmptyThenThrowException()
        {
            var addSignalModel = new AlertRuleApiEntity()
            {
                SignalId         = string.Empty,
                ResourceId       = "resourceId",
                CadenceInMinutes = 1440
            };

            try
            {
                await this.alertRuleApi.AddAlertRuleAsync(addSignalModel, CancellationToken.None);
            }
            catch (SmartSignalsManagementApiException e)
            {
                Assert.AreEqual(HttpStatusCode.BadRequest, e.StatusCode);
                return;
            }

            Assert.Fail("Invalid model should throw an exception");
        }
Exemplo n.º 6
0
        public async Task WhenAddingSignalButStoreThrowsExceptionThenThrowTheWrappedException()
        {
            var addSignalModel = new AlertRuleApiEntity()
            {
                SignalId         = Guid.NewGuid().ToString(),
                ResourceId       = "resourceId",
                CadenceInMinutes = 1440
            };

            this.alertRuleStoreMock.Setup(s => s.AddOrReplaceAlertRuleAsync(It.IsAny <AlertRule>(), It.IsAny <CancellationToken>()))
            .ThrowsAsync(new AlertRuleStoreException(string.Empty, new Exception()));

            try
            {
                await this.alertRuleApi.AddAlertRuleAsync(addSignalModel, CancellationToken.None);
            }
            catch (SmartSignalsManagementApiException e)
            {
                Assert.AreEqual(HttpStatusCode.InternalServerError, e.StatusCode);
                return;
            }

            Assert.Fail("Exception coming from the Signals store should cause to an exception from the controller");
        }