Skip to content

Commit

Permalink
-Renamed ProcessPrtgRecord to ProcessRecordEx
Browse files Browse the repository at this point in the history
-Implemented unit tests to validate types that derive from PrtgCmdlet don't implement ProcessRecord
  • Loading branch information
lordmilko committed Feb 6, 2017
1 parent 9980b93 commit 4b25d58
Show file tree
Hide file tree
Showing 29 changed files with 97 additions and 61 deletions.
29 changes: 29 additions & 0 deletions PrtgAPI.Tests.UnitTests/InfrastructureTests/AssemblyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PrtgAPI.PowerShell.Base;

namespace PrtgAPI.Tests.UnitTests.InfrastructureTests
{
[TestClass]
public class AssemblyTests
{
[TestMethod]
public void PrtgCmdletTypes_DontImplement_ProcessRecord()
{
var assembly = Assembly.GetAssembly(typeof(PrtgCmdlet));

var types = assembly.GetTypes().Where(t => typeof(PrtgCmdlet).IsAssignableFrom(t)).ToList();
var result = types.Where(t => t.GetMethod("ProcessRecord", BindingFlags.Instance | BindingFlags.NonPublic)?.DeclaringType == t && t != typeof(PrtgCmdlet)).ToList();

if (result.Count > 0)
{
Assert.Fail($"Types that derive from {nameof(PrtgCmdlet)} are not allowed to override method ProcessRecord. The following types contain ProcessRecord: {string.Join(", ", result.Select(t => t.Name))}");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PrtgAPI.PowerShell.Base;
using PrtgAPI.PowerShell.Cmdlets;
using PrtgAPI.Tests.UnitTests.InfrastructureTests.Support;

namespace PrtgAPI.Tests.UnitTests.InfrastructureTests
Expand Down
2 changes: 2 additions & 0 deletions PrtgAPI.Tests.UnitTests/PrtgAPI.Tests.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Management.Automation" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
Expand All @@ -59,6 +60,7 @@
<Compile Include="Helpers\Assert2.cs" />
<Compile Include="Helpers\ReflectionHelpers.cs" />
<Compile Include="Helpers\ResponseHelpers.cs" />
<Compile Include="InfrastructureTests\AssemblyTests.cs" />
<Compile Include="ObjectTests\ChannelTests.cs" />
<Compile Include="ObjectTests\DeviceTests.cs" />
<Compile Include="ObjectTests\Items\BaseItem.cs" />
Expand Down
15 changes: 8 additions & 7 deletions PrtgAPI/PowerShell/Base/PrtgCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ private void AddEvent(Action<object, RetryRequestEventArgs> item)
retryEventAdded = true;
}
}

}

private void RemoveEvent()
Expand All @@ -61,17 +60,20 @@ private void RemoveEvent()
}

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet. Do not override this method; override <see cref="ProcessPrtgRecord"/> instead.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected abstract void ProcessRecordEx();

/// <summary>
/// Performs record-by-record processing for the cmdlet. Do not override this method; override <see cref="ProcessRecordEx"/> instead.
/// </summary>
protected override void ProcessRecord()
{
AddEvent(OnRetryRequest);

//todo: need to update all cmdlets to not use processrecord

try
{
ProcessPrtgRecord();
ProcessRecordEx();
}
catch
{
Expand All @@ -88,14 +90,13 @@ protected override void ProcessRecord()
}

/// <summary>
/// Provides a one-time, post-processing functionality for the cmdlet.
/// Performs one-time, post-processing functionality for the cmdlet. This function is only run when the cmdlet successfully runs to completion.
/// </summary>
protected override void EndProcessing()
{
RemoveEvent();
}

protected abstract void ProcessPrtgRecord();

/// <summary>
/// Writes a list to the output pipeline.
Expand Down
4 changes: 2 additions & 2 deletions PrtgAPI/PowerShell/Base/PrtgObjectCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public abstract class PrtgObjectCmdlet<T> : PrtgCmdlet
protected abstract IEnumerable<T> GetRecords();

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessPrtgRecord()
protected override void ProcessRecordEx()
{
var records = GetRecords();

Expand Down
4 changes: 2 additions & 2 deletions PrtgAPI/PowerShell/Base/PrtgTableCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ protected PrtgTableCmdlet(Content content, int? progressThreshold)
}

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessPrtgRecord()
protected override void ProcessRecordEx()
{
var parameters = CreateParameters();

Expand Down
2 changes: 1 addition & 1 deletion PrtgAPI/PowerShell/Cmdlets/ConnectPrtgServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class ConnectPrtgServer : PSCmdlet
public int? RetryDelay { get; set; }

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessRecord()
{
Expand Down
2 changes: 1 addition & 1 deletion PrtgAPI/PowerShell/Cmdlets/DisconnectPrtgServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace PrtgAPI.PowerShell.Cmdlets
public class DisconnectPrtgServer : PSCmdlet
{
/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessRecord()
{
Expand Down
2 changes: 1 addition & 1 deletion PrtgAPI/PowerShell/Cmdlets/GetPrtgClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace PrtgAPI.PowerShell.Cmdlets
public class GetPrtgClient : PSCmdlet
{
/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessRecord()
{
Expand Down
14 changes: 7 additions & 7 deletions PrtgAPI/PowerShell/Cmdlets/Incomplete/CloneObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public class CloneDevice : PrtgCmdlet
public string Hostname { get; set; }

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessPrtgRecord()
protected override void ProcessRecordEx()
{
client.Clone(Device.Id, Name, Hostname ?? Device.Host, DestinationId);
}
Expand All @@ -62,9 +62,9 @@ public class CloneSensor : CloneSensorOrGroup
public Sensor Sensor { get; set; }

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessPrtgRecord() => ProcessPrtgRecord(Sensor.Id);
protected override void ProcessRecordEx() => ProcessPrtgRecord(Sensor.Id);
}

/// <summary>
Expand All @@ -79,9 +79,9 @@ public class CloneGroup : CloneSensorOrGroup
public Group Group { get; set; }

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessPrtgRecord() => ProcessPrtgRecord(Group.Id);
protected override void ProcessRecordEx() => ProcessPrtgRecord(Group.Id);
}

/// <summary>
Expand All @@ -100,7 +100,7 @@ public abstract class CloneSensorOrGroup : PrtgCmdlet
public int DestinationId { get; set; }

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
/// <param name="objectId">The ID of the object to clone.</param>
protected void ProcessPrtgRecord(int objectId)
Expand Down
4 changes: 2 additions & 2 deletions PrtgAPI/PowerShell/Cmdlets/Incomplete/GetObjectProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class GetObjectProperty : PrtgCmdlet
public PrtgObject Object { get; set; }

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessPrtgRecord()
protected override void ProcessRecordEx()
{
var settings = client.GetObjectProperties(Object.Id);
WriteObject(settings);
Expand Down
4 changes: 2 additions & 2 deletions PrtgAPI/PowerShell/Cmdlets/Incomplete/SetChannelProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public class SetChannelProperty : PrtgCmdlet
public object Value { get; set; }

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessPrtgRecord()
protected override void ProcessRecordEx()
{
//i think we should modify setobjectproperty to detect if we're clearing limits or one of the other ones with fields and clear the associated fields as well. we could POTENTIALLY
//even have an enum on those properties so we can enumerate all the fields we need to clear
Expand Down
2 changes: 1 addition & 1 deletion PrtgAPI/PowerShell/Cmdlets/NewSearchFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class NewSearchFilter : PSCmdlet
public string Value { get; set; }

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessRecord()
{
Expand Down
6 changes: 3 additions & 3 deletions PrtgAPI/PowerShell/Cmdlets/ObjectData/GetDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ public GetDevice() : base(Content.Devices, null)
}

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessPrtgRecord()
protected override void ProcessRecordEx()
{
if (Probe != null)
AddPipelineFilter(Property.Probe, Probe.Name);
else if (Group != null)
AddPipelineFilter(Property.ParentId, Group.Id);

base.ProcessPrtgRecord();
base.ProcessRecordEx();
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions PrtgAPI/PowerShell/Cmdlets/ObjectData/GetGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ public GetGroup() : base(Content.Groups, null)
}

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessPrtgRecord()
protected override void ProcessRecordEx()
{
if (Probe != null)
AddPipelineFilter(Property.Probe, Probe.Name);
else if (Group != null)
AddPipelineFilter(Property.ParentId, Group.Id);

base.ProcessPrtgRecord();
base.ProcessRecordEx();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public class GetNotificationTriggerTypes : PrtgCmdlet
public int Id { get; set; }

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessPrtgRecord()
protected override void ProcessRecordEx()
{
var types = client.GetNotificationTriggerTypes(Id);

Expand Down
6 changes: 3 additions & 3 deletions PrtgAPI/PowerShell/Cmdlets/ObjectData/GetSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public GetSensor() : base(Content.Sensors, 500)
}

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessPrtgRecord()
protected override void ProcessRecordEx()
{
if (Device != null)
AddPipelineFilter(Property.ParentId, Device.Id);
Expand All @@ -65,7 +65,7 @@ protected override void ProcessPrtgRecord()
}
}

base.ProcessPrtgRecord();
base.ProcessRecordEx();
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions PrtgAPI/PowerShell/Cmdlets/ObjectData/GetSensorTotals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ namespace PrtgAPI.PowerShell.Cmdlets
public class GetSensorTotals : PrtgCmdlet
{
/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessPrtgRecord()
protected override void ProcessRecordEx()
{
var result = client.GetSensorTotals();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public class AcknowledgeSensor : PrtgCmdlet
public SwitchParameter Forever { get; set; }

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessPrtgRecord()
protected override void ProcessRecordEx()
{
int? duration = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public abstract class BaseSetNotificationTrigger : PrtgCmdlet
public TriggerParameters Parameters { get; set; }

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessPrtgRecord()
protected override void ProcessRecordEx()
{
if(ShouldProcess($"Object ID: {Parameters.ObjectId} (Type: {Parameters.Type}, Action: {Parameters.OnNotificationAction})"))
client.AddNotificationTrigger(Parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public class EditNotificationTriggerProperty : PrtgCmdlet
public object Value { get; set; }

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessPrtgRecord()
protected override void ProcessRecordEx()
{
if (Value is PSObject)
Value = ((PSObject) Value).BaseObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class NewNotificationTriggerParameter : PSCmdlet
public TriggerType? Type { get; set; }

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessRecord()
{
Expand Down
4 changes: 2 additions & 2 deletions PrtgAPI/PowerShell/Cmdlets/ObjectManipulation/PauseObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public class PauseObject : PrtgCmdlet
public SwitchParameter Forever { get; set; }

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessPrtgRecord()
protected override void ProcessRecordEx()
{
int? duration = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public class RefreshObject : PrtgCmdlet
public SensorOrDeviceOrGroupOrProbe Object { get; set; }

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessPrtgRecord()
protected override void ProcessRecordEx()
{
if(ShouldProcess($"'{Object.Name}' (ID: {Object.Id})"))
client.CheckNow(Object.Id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public class RemoveNotificationTrigger : PrtgCmdlet
public SwitchParameter Force { get; set; }

/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// Performs record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessPrtgRecord()
protected override void ProcessRecordEx()
{
if (ShouldProcess($"'{Trigger.OnNotificationAction}' (Object ID: {Trigger.ObjectId}, Sub ID: {Trigger.SubId})"))
{
Expand Down
Loading

0 comments on commit 4b25d58

Please sign in to comment.