Skip to content

Commit

Permalink
-Removed redundant "ModifyAction" parameter on trigger parameter cons…
Browse files Browse the repository at this point in the history
…tructors

-Created additional trigger parameter constructor for modifying an existing NotificationTrigger object without redundant parameter requiring its object ID be specified
-Fixed a bug wherein Edit-NotificationTriggerProperty would fail when attempting to edit enum properties
-TriggerParameters will now throw an exception if you attempt to edit an inherited notification trigger
  • Loading branch information
lordmilko committed Oct 15, 2017
1 parent 3a6fe58 commit 88a91c0
Show file tree
Hide file tree
Showing 15 changed files with 185 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ public void Action_NotificationTrigger_AddWithCustomization_Threshold()
[TestMethod]
public void Action_NotificationTrigger_CreateFromExistingTrigger_State()
{
AddRemoveTriggerFromExisting(TriggerType.State, trigger => new StateTriggerParameters(Settings.Device, trigger, ModifyAction.Add));
AddRemoveTriggerFromExisting(TriggerType.State, trigger => new StateTriggerParameters(Settings.Device, trigger));
}

[TestMethod]
public void Action_NotificationTrigger_CreateFromExistingTrigger_Threshold_Device()
{
AddRemoveTriggerFromExisting(TriggerType.Threshold, trigger => new ThresholdTriggerParameters(Settings.Device, trigger, ModifyAction.Add));
AddRemoveTriggerFromExisting(TriggerType.Threshold, trigger => new ThresholdTriggerParameters(Settings.Device, trigger));
}

[TestMethod]
Expand All @@ -180,7 +180,7 @@ public void Action_NotificationTrigger_CreateFromExistingTrigger_Threshold_Senso

//Clone the trigger

var newParameters = new ThresholdTriggerParameters(Settings.ChannelSensor, triggers.First(), ModifyAction.Add);
var newParameters = new ThresholdTriggerParameters(Settings.ChannelSensor, triggers.First());

client.AddNotificationTrigger(newParameters);

Expand All @@ -199,19 +199,19 @@ public void Action_NotificationTrigger_CreateFromExistingTrigger_Threshold_Senso
[TestMethod]
public void Action_NotificationTrigger_CreateFromExistingTrigger_Speed()
{
AddRemoveTriggerFromExisting(TriggerType.Speed, trigger => new SpeedTriggerParameters(Settings.Device, trigger, ModifyAction.Add));
AddRemoveTriggerFromExisting(TriggerType.Speed, trigger => new SpeedTriggerParameters(Settings.Device, trigger));
}

[TestMethod]
public void Action_NotificationTrigger_CreateFromExistingTrigger_Volume()
{
AddRemoveTriggerFromExisting(TriggerType.Volume, trigger => new VolumeTriggerParameters(Settings.Device, trigger, ModifyAction.Add));
AddRemoveTriggerFromExisting(TriggerType.Volume, trigger => new VolumeTriggerParameters(Settings.Device, trigger));
}

[TestMethod]
public void Action_NotificationTrigger_CreateFromExistingTrigger_Change()
{
AddRemoveTriggerFromExisting(TriggerType.Change, trigger => new ChangeTriggerParameters(Settings.Device, trigger, ModifyAction.Add));
AddRemoveTriggerFromExisting(TriggerType.Change, trigger => new ChangeTriggerParameters(Settings.Device, trigger));
}

private void AddRemoveTriggerFromExisting(TriggerType triggerType, Func<NotificationTrigger, TriggerParameters> getParameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ Describe "Edit-NotificationTriggerProperty_IT" {
{ $trigger | Edit-NotificationTriggerProperty Channel "blah" } | Should Throw "Object of type 'System.String' cannot be converted to type 'PrtgAPI.TriggerChannel'"
}

It "can assign an enum value" {
$trigger = Get-Device -Id (Settings Device) | Get-NotificationTrigger -Type State -Inherited $false
$trigger.Count | Should Be 1

$trigger | Edit-NotificationTriggerProperty State Warning

$newTrigger = Get-Device -Id (Settings Device) | Get-NotificationTrigger -Type State -Inherited $false

$newTrigger.Threshold | Should Be Warning

#todo: should we maybe make state public?
}

It "throws editing an inherited notification trigger" {
$device = Get-Device -Id (Settings Device)

$trigger = @($device | Get-Trigger | where Inherited -EQ $true)

$trigger.Count | Should Be 1
$trigger.Threshold | Should Be "Down"

{ $trigger | Edit-TriggerProperty State Warning } | Should Throw "this trigger is inherited"
}

It "throws setting an Channel TriggerChannel on a device" {
$device = Get-Device -Id (Settings Device)

Expand Down Expand Up @@ -71,7 +95,7 @@ Describe "Edit-NotificationTriggerProperty_IT" {
}
finally
{
$trigger | Remove-Trigger
$trigger | Remove-Trigger -Force
}
}

Expand All @@ -96,7 +120,7 @@ Describe "Edit-NotificationTriggerProperty_IT" {
}
finally
{
$trigger | Remove-Trigger
$trigger | Remove-Trigger -Force
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ private void TriggerParameters_AllProperties_HaveValues(TriggerParameters parame
public void StateTriggerParameters_Create_FromExistingTrigger()
{
var trigger = GetMultipleItems().First(t => t.Type == TriggerType.State);
var parameters = new StateTriggerParameters(1234, trigger, ModifyAction.Add);
var parameters = new StateTriggerParameters(1234, trigger);

TriggerParameters_Create_FromExistingTrigger(trigger, parameters);
}
Expand All @@ -332,7 +332,7 @@ public void StateTriggerParameters_Create_FromExistingTrigger()
public void ChangeTriggerParameters_Create_FromExistingTrigger()
{
var trigger = GetMultipleItems().First(t => t.Type == TriggerType.Change);
var parameters = new ChangeTriggerParameters(1234, trigger, ModifyAction.Add);
var parameters = new ChangeTriggerParameters(1234, trigger);

TriggerParameters_Create_FromExistingTrigger(trigger, parameters);
}
Expand All @@ -341,7 +341,7 @@ public void ChangeTriggerParameters_Create_FromExistingTrigger()
public void VolumeTriggerParameters_Create_FromExistingTrigger()
{
var trigger = GetMultipleItems().First(t => t.Type == TriggerType.Volume);
var parameters = new VolumeTriggerParameters(1234, trigger, ModifyAction.Add);
var parameters = new VolumeTriggerParameters(1234, trigger);

TriggerParameters_Create_FromExistingTrigger(trigger, parameters);
}
Expand All @@ -350,7 +350,7 @@ public void VolumeTriggerParameters_Create_FromExistingTrigger()
public void SpeedTriggerParameters_Create_FromExistingTrigger()
{
var trigger = GetMultipleItems().First(t => t.Type == TriggerType.Speed);
var parameters = new SpeedTriggerParameters(1234, trigger, ModifyAction.Add);
var parameters = new SpeedTriggerParameters(1234, trigger);

TriggerParameters_Create_FromExistingTrigger(trigger, parameters);
}
Expand All @@ -359,7 +359,7 @@ public void SpeedTriggerParameters_Create_FromExistingTrigger()
public void ThresholdTriggerParameters_Create_FromExistingTrigger()
{
var trigger = GetMultipleItems().First(t => t.Type == TriggerType.Threshold);
var parameters = new ThresholdTriggerParameters(1234, trigger, ModifyAction.Add);
var parameters = new ThresholdTriggerParameters(1234, trigger);

TriggerParameters_Create_FromExistingTrigger(trigger, parameters);
}
Expand All @@ -368,15 +368,15 @@ public void ThresholdTriggerParameters_Create_FromExistingTrigger()
[ExpectedException(typeof(ArgumentNullException))]
public void TriggerParameters_Create_FromNullTrigger()
{
var parameters = new StateTriggerParameters(1234, null, ModifyAction.Add);
var parameters = new StateTriggerParameters(1234, null);
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void TriggerParameters_Create_FromInvalidTriggerType()
{
var trigger = GetMultipleItems().First(t => t.Type == TriggerType.State);
var parameters = new ChangeTriggerParameters(1234, trigger, ModifyAction.Add);
var parameters = new ChangeTriggerParameters(1234, trigger);
}

private void TriggerParameters_Create_FromExistingTrigger(NotificationTrigger trigger, TriggerParameters parameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ Describe "New-NotificationTriggerParameter" {
}

It "can create EditFrom parameter set" {
$triggers.Count | Should Be 1
$device2 = Run Device { Get-Device }
$device2.Id = 0

$trigger = Run NotificationTrigger { $device2 | Get-NotificationTrigger -Type State }

$trigger.Count | Should Be 1

$parameters = $triggers | New-TriggerParameter
$parameters = $trigger | New-TriggerParameter

$parameters.Action | Should Be Edit
}
Expand Down Expand Up @@ -66,4 +71,10 @@ Describe "New-NotificationTriggerParameter" {

{ $parameters.Channel = "Banana" } | Should Throw "type must be convertable"
}

It "throws creating parameters from an inherited trigger" {
$triggers.Count | Should Be 1

{ $triggers | New-TriggerParameter } | Should Throw "trigger is inherited"
}
}
2 changes: 1 addition & 1 deletion PrtgAPI/Objects/NotificationTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public string Unit
private int? threshold;

/// <summary>
/// Value threshold required before this notification is activated.
/// Value threshold or object state required before this notification is activated.
/// Applies to: Threshold, Speed, Volume Triggers
/// </summary>
[DataMember(Name = "threshold")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,26 @@ public ChangeTriggerParameters(int objectId) : base(TriggerType.Change, objectId
/// <summary>
/// Initializes a new instance of the <see cref="ChangeTriggerParameters"/> class for editing an existing notification trigger.
/// </summary>
/// <param name="objectId">The object ID the trigger is applied to.</param>
/// <param name="objectId">The object ID the trigger is applied to. Note: if the trigger is inherited, the ParentId should be specified.</param>
/// <param name="triggerId">The sub ID of the trigger on its parent object.</param>
public ChangeTriggerParameters(int objectId, int triggerId) : base(TriggerType.Change, objectId, triggerId, ModifyAction.Edit)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ChangeTriggerParameters"/> class for editing or creating a new trigger from an existing <see cref="TriggerType.Change"/> <see cref="NotificationTrigger"/>.
/// Initializes a new instance of the <see cref="ChangeTriggerParameters"/> class for creating a new trigger from an existing <see cref="TriggerType.Change"/> <see cref="NotificationTrigger"/>.
/// </summary>
/// <param name="objectId">The object ID the trigger will apply to.</param>
/// <param name="sourceTrigger">The notification trigger whose properties should be used.</param>
/// <param name="action">Whether these parameters will create a new trigger or edit an existing one.</param>
public ChangeTriggerParameters(int objectId, NotificationTrigger sourceTrigger, ModifyAction action) : base(TriggerType.Change, objectId, sourceTrigger, action)
public ChangeTriggerParameters(int objectId, NotificationTrigger sourceTrigger) : base(TriggerType.Change, objectId, sourceTrigger, ModifyAction.Add)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ChangeTriggerParameters"/> class for editing an existing <see cref="TriggerType.Change"/> <see cref="NotificationTrigger"/>.
/// </summary>
/// <param name="sourceTrigger">The notification trigger whose properties should be used.</param>
public ChangeTriggerParameters(NotificationTrigger sourceTrigger) : base(TriggerType.Change, sourceTrigger.ObjectId, sourceTrigger, ModifyAction.Edit)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,30 +102,34 @@ public SpeedTriggerParameters(int objectId) : base(TriggerType.Speed, objectId,
/// <summary>
/// Initializes a new instance of the <see cref="SpeedTriggerParameters"/> class for editing an existing notification trigger.
/// </summary>
/// <param name="objectId">The object ID the trigger is applied to.</param>
/// <param name="objectId">The object ID the trigger is applied to. Note: if the trigger is inherited, the ParentId should be specified.</param>
/// <param name="triggerId">The sub ID of the trigger on its parent object.</param>
public SpeedTriggerParameters(int objectId, int triggerId) : base(TriggerType.Speed, objectId, triggerId, ModifyAction.Edit)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="SpeedTriggerParameters"/> class for editing or creating a new trigger from an existing <see cref="TriggerType.Speed"/> <see cref="NotificationTrigger"/>.
/// Initializes a new instance of the <see cref="SpeedTriggerParameters"/> class for creating a new trigger from an existing <see cref="TriggerType.Speed"/> <see cref="NotificationTrigger"/>.
/// </summary>
/// <param name="objectId">The object ID the trigger will apply to.</param>
/// <param name="sourceTrigger">The notification trigger whose properties should be used.</param>
/// <param name="action">Whether these parameters will create a new trigger or edit an existing one.</param>
public SpeedTriggerParameters(int objectId, NotificationTrigger sourceTrigger, ModifyAction action) : base(TriggerType.Speed, objectId, sourceTrigger, action)
public SpeedTriggerParameters(int objectId, NotificationTrigger sourceTrigger) : base(TriggerType.Speed, objectId, sourceTrigger, ModifyAction.Add)
{
OffNotificationAction = sourceTrigger.OffNotificationAction;
Channel = sourceTrigger.Channel;
Latency = sourceTrigger.Latency;
Condition = sourceTrigger.Condition;
Threshold = sourceTrigger.ThresholdInternal;
UnitTime = sourceTrigger.UnitTime;
UnitSize = sourceTrigger.UnitSize;
}

/// <summary>
/// Initializes a new instance of the <see cref="SpeedTriggerParameters"/> class for editing an existing <see cref="TriggerType.Speed"/> <see cref="NotificationTrigger"/>.
/// </summary>
/// <param name="sourceTrigger">The notification trigger whose properties should be used.</param>
public SpeedTriggerParameters(NotificationTrigger sourceTrigger) : base(TriggerType.Speed, sourceTrigger.ObjectId, sourceTrigger, ModifyAction.Edit)
{
if (action == ModifyAction.Add)
{
OffNotificationAction = sourceTrigger.OffNotificationAction;
Channel = sourceTrigger.Channel;
Latency = sourceTrigger.Latency;
Condition = sourceTrigger.Condition;
Threshold = sourceTrigger.ThresholdInternal;
UnitTime = sourceTrigger.UnitTime;
UnitSize = sourceTrigger.UnitSize;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,29 +89,33 @@ public StateTriggerParameters(int objectId) : base(TriggerType.State, objectId,
/// <summary>
/// Initializes a new instance of the <see cref="StateTriggerParameters"/> class for editing an existing notification trigger.
/// </summary>
/// <param name="objectId">The object ID the trigger is applied to.</param>
/// <param name="objectId">The object ID the trigger is applied to. Note: if the trigger is inherited, the ParentId should be specified.</param>
/// <param name="triggerId">The sub ID of the trigger on its parent object.</param>
public StateTriggerParameters(int objectId, int triggerId) : base(TriggerType.State, objectId, triggerId, ModifyAction.Edit)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="StateTriggerParameters"/> class for editing or creating a new trigger from an existing <see cref="TriggerType.State"/> <see cref="NotificationTrigger"/>.
/// Initializes a new instance of the <see cref="StateTriggerParameters"/> class for creating a new trigger from an existing <see cref="TriggerType.State"/> <see cref="NotificationTrigger"/>.
/// </summary>
/// <param name="objectId">The object ID the trigger will apply to.</param>
/// <param name="sourceTrigger">The notification trigger whose properties should be used.</param>
/// <param name="action">Whether these parameters will create a new trigger or edit an existing one.</param>
public StateTriggerParameters(int objectId, NotificationTrigger sourceTrigger, ModifyAction action) : base(TriggerType.State, objectId, sourceTrigger, action)
public StateTriggerParameters(int objectId, NotificationTrigger sourceTrigger) : base(TriggerType.State, objectId, sourceTrigger, ModifyAction.Add)
{
OffNotificationAction = sourceTrigger.OffNotificationAction;
EscalationNotificationAction = sourceTrigger.EscalationNotificationAction;
Latency = sourceTrigger.Latency;
EscalationLatency = sourceTrigger.EscalationLatency;
RepeatInterval = sourceTrigger.RepeatInterval;
State = sourceTrigger.StateTrigger;
}

/// <summary>
/// Initializes a new instance of the <see cref="StateTriggerParameters"/> class for editing an existing <see cref="TriggerType.State"/> <see cref="NotificationTrigger"/>.
/// </summary>
/// <param name="sourceTrigger">The notification trigger whose properties should be used.</param>
public StateTriggerParameters(NotificationTrigger sourceTrigger) : base(TriggerType.State, sourceTrigger.ObjectId, sourceTrigger, ModifyAction.Edit)
{
if (action == ModifyAction.Add)
{
OffNotificationAction = sourceTrigger.OffNotificationAction;
EscalationNotificationAction = sourceTrigger.EscalationNotificationAction;
Latency = sourceTrigger.Latency;
EscalationLatency = sourceTrigger.EscalationLatency;
RepeatInterval = sourceTrigger.RepeatInterval;
State = sourceTrigger.StateTrigger;
}
}
}
}
Loading

0 comments on commit 88a91c0

Please sign in to comment.