private void TriggerParameters_Create_FromExistingTrigger(NotificationTrigger trigger, TriggerParameters parameters)
        {
            //shouldnt we _actually_ be checking that the values are the same?
            //and, shouldnt we be populating _all_ properties of the trigger first?

            //then, we need to make sure we can clone a trigger into some new parameters and add them successfully
            //and THEN, we need to write some tests for that

            foreach (var paramProp in PrtgAPIHelpers.GetNormalProperties(parameters.GetType()))
            {
                bool found = false;

                foreach (var triggerProp in trigger.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                {
                    if ((paramProp.Name == "TriggerInternal" && triggerProp.Name == "Trigger") ||
                        (paramProp.Name == "State" && triggerProp.Name == "StateTrigger") ||
                        (paramProp.Name == triggerProp.Name))
                    {
                        found = true;
                        Assert.IsTrue(paramProp.GetValue(parameters) != null, $"Parameter '{paramProp}' was null");
                    }
                }

                if (!found)
                {
                    Assert.Fail($"Couldn't find notification trigger property that corresponded to parameter property '{paramProp.Name}'");
                }
            }
        }
        private void TriggerParameters_AllProperties_HaveValues(TriggerParameters parameters, Func <PropertyInfo, bool> additionalChecks = null)
        {
            if (additionalChecks == null)
            {
                additionalChecks = p => false;
            }

            foreach (var prop in PrtgAPIHelpers.GetNormalProperties(parameters.GetType()))
            {
                var val = prop.GetValue(parameters);

                if (prop.Name.EndsWith("NotificationAction"))
                {
                    Assert.IsTrue(val.ToString() != TriggerParameters.EmptyNotificationAction().ToString(), $"Property '{prop.Name}' had the empty notification action");
                }

                else
                {
                    if (!additionalChecks(prop))
                    {
                        Assert.IsTrue(val != null, $"Property '{prop.Name}' had value did not have a value.");
                    }
                }
            }
        }
        private void TriggerParameters_AllProperties_HaveDefault(TriggerParameters parameters, Func <PropertyInfo, bool> additionalChecks = null)
        {
            TestReflectionHelpers.NullifyProperties(parameters);

            if (additionalChecks == null)
            {
                additionalChecks = p => false;
            }

            foreach (var prop in PrtgAPIHelpers.GetNormalProperties(parameters.GetType()))
            {
                var val = prop.GetValue(parameters);

                if (prop.Name.EndsWith("NotificationAction"))
                {
                    Assert.IsTrue(val.ToString() == TriggerParameters.EmptyNotificationAction().ToString(), $"Property '{prop.Name}' had value {val}");
                }
                else
                {
                    if (!additionalChecks(prop))
                    {
                        Assert.IsTrue(val == null, $"Property '{prop.Name}' was not null");
                    }
                }
            }
        }
Beispiel #4
0
        private void TriggerParameters_Edit_CanSetUnsetValue(TriggerParameters parameters)
        {
            var properties = PrtgAPIHelpers.GetNormalProperties(parameters.GetType());

            foreach (var prop in properties)
            {
                prop.SetValue(parameters, null);
                if (prop.Name == nameof(TriggerProperty.OnNotificationAction) || prop.Name == nameof(TriggerProperty.OffNotificationAction) || prop.Name == nameof(TriggerProperty.EscalationNotificationAction))
                {
                    Assert.IsTrue(prop.GetValue(parameters).ToString() == TriggerParameters.EmptyNotificationAction().ToString(), $"Property '{prop.Name}' was not empty.");
                }
                else
                {
                    Assert.IsTrue(prop.GetValue(parameters) == null, $"Property '{prop.Name}' was not null.");
                }

                object defaultValue = null;

                if (prop.PropertyType.Name == nameof(TriggerChannel))
                {
                    defaultValue = new TriggerChannel(1234);
                }
                else if (prop.Name == nameof(VolumeTriggerParameters.UnitSize))
                {
                    if (parameters is VolumeTriggerParameters)
                    {
                        defaultValue = TestReflectionUtilities.GetDefaultUnderlying(typeof(DataVolumeUnit)).ToString().ToEnum <DataUnit>();
                    }
                    else
                    {
                        defaultValue = TestReflectionUtilities.GetDefaultUnderlying(prop.PropertyType);
                    }
                }
                else
                {
                    defaultValue = TestReflectionUtilities.GetDefaultUnderlying(prop.PropertyType);
                }

                prop.SetValue(parameters, defaultValue);
                Assert.IsTrue(prop.GetValue(parameters) != null, $"Property '{prop.Name}' was null.");
            }
        }
        private void TriggerParameters_Edit_CanSetUnsetValue(TriggerParameters parameters)
        {
            var properties = PrtgAPIHelpers.GetNormalProperties(parameters.GetType());

            foreach (var prop in properties)
            {
                prop.SetValue(parameters, null);
                if (prop.Name == nameof(TriggerProperty.OnNotificationAction) || prop.Name == nameof(TriggerProperty.OffNotificationAction) || prop.Name == nameof(TriggerProperty.EscalationNotificationAction))
                {
                    Assert.IsTrue(prop.GetValue(parameters).ToString() == TriggerParameters.EmptyNotificationAction().ToString(), $"Property '{prop.Name}' was not empty.");
                }
                else
                {
                    Assert.IsTrue(prop.GetValue(parameters) == null, $"Property '{prop.Name}' was not null.");
                }

                var defaultValue = prop.PropertyType.Name == "TriggerChannel" ? new TriggerChannel(1234) : TestReflectionHelpers.GetDefaultUnderlying(prop.PropertyType);

                prop.SetValue(parameters, defaultValue);
                Assert.IsTrue(prop.GetValue(parameters) != null, $"Property '{prop.Name}' was null.");
            }
        }
        //for edit mode, it IS valid to set fields to null, since that indicates we're not going to edit that field
        //we then also need to analyze the url we get and confirm it doesnt have any fields in it
        //also we need to check when we add something and then nullify it it goes away

        #endregion

        private void TriggerParameters_MandatoryFields_CannotBeNull(TriggerParameters parameters)
        {
            foreach (var prop in PrtgAPIHelpers.GetNormalProperties(parameters.GetType()))
            {
                var attr        = prop.GetCustomAttribute <RequireValueAttribute>();
                var valRequired = attr?.ValueRequired ?? false;

                try
                {
                    prop.SetValue(parameters, null);

                    if (valRequired)
                    {
                        Assert.Fail($"Property '{prop.Name}' requires a value however did not generate an exception.");
                    }
                }
                catch (TargetInvocationException ex)
                {
                    var inner = ex.InnerException as InvalidOperationException;

                    if (inner == null)
                    {
                        throw;
                    }

                    if (!inner.Message.StartsWith("Trigger property"))
                    {
                        throw;
                    }

                    if (!valRequired)
                    {
                        Assert.Fail($"Property '{prop.Name}' does not require a value, however the property responded as if it does.");
                    }
                }
            }
        }