Skip to content

Commit

Permalink
Fixed SetChannelProperty not serializing ValueLookup properly (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
lordmilko committed May 6, 2019
1 parent c5843f8 commit 89f03aa
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ Describe "Set-ChannelProperty_IT" -Tag @("PowerShell", "IntegrationTest") {
(& $channel) | Set-ChannelProperty ValueLookup "banana"
$newChannel = (& $channel)
$newChannel.ValueLookup | Should Be "None"

$channel = { Get-Sensor -Id (Settings ExeXml) | Get-Channel Value }

(& $channel) | Set-ChannelProperty ValueLookup "prtg.standardlookups.yesno.stateyesok"
SetValue "ValueLookup" "None"
}

It "sets ValueLookup to None when an invalid value is specified" {
Expand Down
21 changes: 21 additions & 0 deletions PrtgAPI.Tests.UnitTests/CSharp/ObjectData/ChannelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PrtgAPI.Tests.UnitTests.Support;
using PrtgAPI.Tests.UnitTests.Support.TestItems;
using PrtgAPI.Tests.UnitTests.Support.TestResponses;

Expand Down Expand Up @@ -79,6 +80,26 @@ private void Channel_SetProperty(ChannelProperty property, object value)
client.SetChannelProperty(1234, channelId, property, value);
}

[TestMethod]
[TestCategory("UnitTest")]
public void Channel_SetProperty_ValueLookup_NormalLookup() => Channel_SetValueLookup("potato", "potato%7Cpotato");

[TestMethod]
[TestCategory("UnitTest")]
public void Channel_SetProperty_ValueLookup_None() => Channel_SetValueLookup("none", "%7CNone");

[TestMethod]
[TestCategory("UnitTest")]
public void Channel_SetProperty_ValueLookup_Null() => Channel_SetValueLookup(null, "%7CNone");

private void Channel_SetValueLookup(object value, string url)
{
Execute(
c => c.SetChannelProperty(1001, 1, ChannelProperty.ValueLookup, value),
UnitRequest.EditSettings($"id=1001&valuelookup_1={url}")
);
}

[TestMethod]
[TestCategory("UnitTest")]
public void Channel_Unit_CalculatesProperly()
Expand Down
2 changes: 2 additions & 0 deletions PrtgAPI/Enums/Serialization/ChannelProperty.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel;
using PrtgAPI.Attributes;
using PrtgAPI.Request.Serialization.ValueConverters;

namespace PrtgAPI
{
Expand Down Expand Up @@ -47,6 +48,7 @@ public enum ChannelProperty
/// A standard or custom value lookup that allows this sensor's value to be displayed as text in a gauge or a switch.
/// </summary>
[Description("valuelookup")]
[ValueConverter(typeof(ValueLookupConverter))]
ValueLookup,

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions PrtgAPI/PrtgAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
<Compile Include="Parameters\Interfaces\ICustomParameterContainer.cs" />
<Compile Include="Parameters\ObjectManipulation\ApproveProbeParameters.cs" />
<Compile Include="Parameters\ObjectManipulation\NewObjectParameters\Sensors\FactorySensorParameters.cs" />
<Compile Include="Request\Serialization\ValueConverters\ValueLookupConverter.cs" />
<Compile Include="Request\Version\ChannelLimitAnalysis.cs" />
<Compile Include="Request\Version\SetChannelPropertyGrouping.cs" />
<Compile Include="Request\Version\SetChannelPropertyTask.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;

namespace PrtgAPI.Request.Serialization.ValueConverters
{
class ValueLookupConverter : IValueConverter
{
internal static ValueLookupConverter Instance = new ValueLookupConverter();

public object Serialize(object value)
{
var none = "|None";

if (value != null)
{
if (value.ToString().Equals("None", StringComparison.OrdinalIgnoreCase))
return none;
else
return $"{value}|{value}";
}
else
return none;
}

public object Deserialize(object value) => value;
}
}

0 comments on commit 89f03aa

Please sign in to comment.