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_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"); } } } }
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 SetProperty(TriggerParameters parameters) { var property = parameters.GetType().GetProperties().First(p => p.GetCustomAttribute <PropertyParameterAttribute>()?.Name == Property.ToString()); property.SetValue(parameters, Value); if (ShouldProcess($"{Trigger.OnNotificationAction} (Object ID: {Trigger.ObjectId})", $"Edit-NotificationTriggerProperty {Property} = '{Value}'")) { client.SetNotificationTrigger(parameters); } }
private void ValidateNewTrigger(TriggerParameters parameters, NotificationTrigger trigger, bool empty) { foreach (var paramProp in parameters.GetType().GetProperties2()) { 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; var paramValue = paramProp.GetValue(parameters)?.ToString(); var triggerValue = triggerProp.GetValue(trigger)?.ToString(); if (empty && paramValue == null) { switch (triggerProp.Name) { case nameof(NotificationTrigger.Latency): paramValue = "60"; break; case nameof(NotificationTrigger.EscalationLatency): paramValue = "300"; break; case nameof(NotificationTrigger.Threshold): paramValue = "0"; break; case nameof(NotificationTrigger.RepeatInterval): paramValue = "0"; break; } } Assert.AreEqual(paramValue, triggerValue, triggerProp.Name); //when we create a trigger without customization, some fields get default values //we should have verification of those values, but ONLY when we're doing //verification without customization. maybe we should have a bool on validatenewtrigger //that indicates whether this is without customization, and ONLY THEN do we say ok //paramValue can be null but triggerValue can be <something> } } if (!found) { Assert.Fail($"Couldn't find notification trigger property that corresponded to parameter property '{paramProp.Name}'"); } } }
private void SetProperty(TriggerParameters parameters) { //Get the TriggerParameters PropertyInfo that corresponds to the specified TriggerProperty var property = parameters.GetType().GetProperties().FirstOrDefault(p => p.GetCustomAttribute <PropertyParameterAttribute>()?.Name == Property.ToString()); if (property == null) { throw new InvalidOperationException($"Property '{Property}' does not exist on triggers of type '{parameters.Type}'"); } Value = ParseValueIfRequired(property, Value); property.SetValue(parameters, Value); if (ShouldProcess($"{Trigger.OnNotificationAction} (Object ID: {Trigger.ObjectId})", $"Edit-NotificationTriggerProperty {Property} = '{Value}'")) { ExecuteOperation(() => client.SetNotificationTrigger(parameters), $"Setting trigger property {Property} to value '{Value}'"); } }
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 = parameters.GetType().GetProperties2(); 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."); } prop.SetValue(parameters, ReflectionHelpers.GetDefaultUnderlying(prop.PropertyType)); 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."); } }
private void TriggerParameters_Create_FromExistingTrigger(NotificationTrigger trigger, TriggerParameters parameters) { foreach (var paramProp in parameters.GetType().GetProperties2()) { 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}'"); } } }
//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."); } } } }