Skip to content

Commit

Permalink
Add equality checks to TestDiscoverDevice from e9d06f1
Browse files Browse the repository at this point in the history
  • Loading branch information
NZSmartie committed Aug 7, 2017
1 parent 9324bdd commit ecc6c0f
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 3 deletions.
41 changes: 40 additions & 1 deletion OICNet.Tests/OicDiscoverServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@ public void TestDiscoverDevice()

bool newDeviceCallbackInvoked = false;
var service = new OicResourceDiscoverClient();
OicDevice actualDevice = null;

service.AddInterface(mockInterface.Object);
service.NewDevice += (s, e) => newDeviceCallbackInvoked = true;
service.NewDevice += (s, e) =>
{
newDeviceCallbackInvoked = true;
actualDevice = e.Device;
};

// Act
mockInterface.Raise(i => i.ReceivedMessage += null, new OicReceivedMessageEventArgs
Expand All @@ -61,6 +66,40 @@ public void TestDiscoverDevice()

// Assert
Assert.IsTrue(newDeviceCallbackInvoked, $"{typeof(OicResourceDiscoverClient)}.{nameof(service.NewDevice)} was not invoked");

var expectedDevice = new OicDevice(null, null)
{
DeviceId = Guid.Parse("0685B960-736F-46F7-BEC0-9E6CBD61ADC1"),
Resources = new List<IOicResource>
{
new CoreResources.OicDevice
{
RelativeUri = "/oid/d",
ResourceTypes = new List<string>{"oic.d.light", "oic.wk.d"},
Interfaces = new List<OicResourceInterface>{OicResourceInterface.ReadOnly, OicResourceInterface.Baseline},
},
new CoreResources.OicPlatform
{
RelativeUri = "/oid/p",
ResourceTypes = new List<string>{"oic.wk.p"},
Interfaces = new List<OicResourceInterface>{OicResourceInterface.ReadOnly, OicResourceInterface.Baseline},
},
new ResourceTypes.SwitchBinary
{
RelativeUri = "/switch",
ResourceTypes = new List<string>{"oic.r.switch.binary"},
Interfaces = new List<OicResourceInterface>{OicResourceInterface.Actuator, OicResourceInterface.Baseline},
},
new ResourceTypes.LightBrightness
{
RelativeUri = "/brightness",
ResourceTypes = new List<string>{"oic.r.light.brightness"},
Interfaces = new List<OicResourceInterface>{OicResourceInterface.Actuator, OicResourceInterface.Baseline},
}
}
};
Assert.AreEqual(expectedDevice.DeviceId, actualDevice.DeviceId);
Assert.AreEqual(expectedDevice.Resources, actualDevice.Resources);
}
}
}
47 changes: 47 additions & 0 deletions OICNet/CoreResources/OicDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using Newtonsoft.Json;
using OICNet.Utilities;

namespace OICNet.CoreResources
{
[OicResourceType("oic.wk.d")]
#pragma warning disable CS0659 // Type overrides Object.Equals(object o) but does not override Object.GetHashCode()
public class OicDevice : OicCoreResource
{
public override bool ShouldSerializeInterfaces() { return false; }
Expand Down Expand Up @@ -63,6 +66,30 @@ public class OicDevice : OicCoreResource
public Guid PlatformId { get; set; }
//}

/// <inheritdoc />
public override bool Equals(object obj)
{
var other = obj as OicDevice;
if (other == null)
return false;
if (DeviceId != other.DeviceId)
return false;
if (ServerVersion != other.ServerVersion)
return false;
if (SpecVersions != other.SpecVersions)
return false;
if (!LocalisedDescriptions.NullRespectingSequenceEqual(other.LocalisedDescriptions))
return false;
if (SoftwareVersion != other.SoftwareVersion)
return false;
if (!ManufacturerName.NullRespectingSequenceEqual(other.ManufacturerName))
return false;
if (Model != other.Model)
return false;
if (PlatformId != other.PlatformId)
return false;
return true;
}

#region Todo: Create sub-classes of the enclosed JSON schema
//[
Expand Down Expand Up @@ -349,6 +376,7 @@ public class OicDevice : OicCoreResource

#endregion
}
#pragma warning restore CS0659 // Type overrides Object.Equals(object o) but does not override Object.GetHashCode()

public class LocalisedDescription
{
Expand All @@ -364,5 +392,24 @@ public class LocalisedDescription
/// </summary>
[JsonProperty("value"), MaxLength(64)]
public string Description { get; set; }

/// <inheritdoc />
public override bool Equals(object obj)
{
var other = obj as LocalisedDescription;
if (other == null)
return false;
if (Culture != other.Culture)
return false;
if (Description!= other.Description)
return false;
return true;
}

/// <inheritdoc />
public override int GetHashCode()
{
return typeof(LocalisedDescription).GetHashCode();
}
}
}
39 changes: 37 additions & 2 deletions OICNet/CoreResources/OicPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace OICNet.CoreResources
{
[OicResourceType("oic.wk.p")]
#pragma warning disable CS0659 // Type overrides Object.Equals(object o) but does not override Object.GetHashCode()
public class OicPlatform : OicCoreResource
{
public override bool ShouldSerializeInterfaces() { return false; }
Expand Down Expand Up @@ -58,13 +59,13 @@ public class OicPlatform : OicCoreResource
/// Platform Resident OS Version
/// </summary>
[JsonProperty("mnos", NullValueHandling = NullValueHandling.Ignore, Required = Required.DisallowNull), MaxLength(64)]
public string PlatformOperatingSystemVersion { get; set; }
public string OperatingSystemVersion { get; set; }

/// <summary>
/// Platform Hardware Version
/// </summary>
[JsonProperty("mnhw", NullValueHandling = NullValueHandling.Ignore, Required = Required.DisallowNull), MaxLength(64)]
public string PlatformHardwareVersion { get; set; }
public string HardwareVersion { get; set; }

/// <summary>
/// Manufacturer's firmware version
Expand All @@ -89,5 +90,39 @@ public class OicPlatform : OicCoreResource
/// </summary>
[JsonProperty("vid", NullValueHandling = NullValueHandling.Ignore, Required = Required.DisallowNull), MaxLength(64)]
public string VendorId { get; set; }

/// <inheritdoc />
public override bool Equals(object obj)
{
var other = obj as OicPlatform;
if (other == null)
return false;
if(PlatformId != other.PlatformId)
return false;
if (ManufacturerName != other.ManufacturerName)
return false;
if (ManufacturerUrl != other.ManufacturerUrl)
return false;
if (ModelNumber != other.ModelNumber)
return false;
if (ManufacturingDate != other.ManufacturingDate)
return false;
if (PlatformVersion != other.PlatformVersion)
return false;
if (OperatingSystemVersion != other.OperatingSystemVersion)
return false;
if (HardwareVersion != other.HardwareVersion)
return false;
if (FirmwareVersion != other.FirmwareVersion)
return false;
if (SupportURL != other.SupportURL)
return false;
if (CurrentTime != other.CurrentTime)
return false;
if (VendorId != other.VendorId)
return false;
return true;
}
}
#pragma warning restore CS0659 // Type overrides Object.Equals(object o) but does not override Object.GetHashCode()
}
26 changes: 26 additions & 0 deletions OICNet/Utilities/Enumerables.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OICNet.Utilities
{
internal static class Enumerables
{
// Thanks to https://stackoverflow.com/a/22165416 for this quick fix
internal static bool NullRespectingSequenceEqual<T>(
this IEnumerable<T> first, IEnumerable<T> second)
{
if (first == null && second == null)
{
return true;
}
if (first == null || second == null)
{
return false;
}
return first.SequenceEqual(second);
}
}
}

0 comments on commit ecc6c0f

Please sign in to comment.