From ecc6c0f56558fbb7ea61666a43d71775ba1a160c Mon Sep 17 00:00:00 2001 From: NZSmartie Date: Tue, 8 Aug 2017 09:02:24 +1200 Subject: [PATCH] Add equality checks to TestDiscoverDevice from e9d06f1 --- OICNet.Tests/OicDiscoverServiceTests.cs | 41 ++++++++++++++++++++- OICNet/CoreResources/OicDevice.cs | 47 +++++++++++++++++++++++++ OICNet/CoreResources/OicPlatform.cs | 39 ++++++++++++++++++-- OICNet/Utilities/Enumerables.cs | 26 ++++++++++++++ 4 files changed, 150 insertions(+), 3 deletions(-) create mode 100644 OICNet/Utilities/Enumerables.cs diff --git a/OICNet.Tests/OicDiscoverServiceTests.cs b/OICNet.Tests/OicDiscoverServiceTests.cs index 3a9adaa..673cc8c 100644 --- a/OICNet.Tests/OicDiscoverServiceTests.cs +++ b/OICNet.Tests/OicDiscoverServiceTests.cs @@ -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 @@ -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 + { + new CoreResources.OicDevice + { + RelativeUri = "/oid/d", + ResourceTypes = new List{"oic.d.light", "oic.wk.d"}, + Interfaces = new List{OicResourceInterface.ReadOnly, OicResourceInterface.Baseline}, + }, + new CoreResources.OicPlatform + { + RelativeUri = "/oid/p", + ResourceTypes = new List{"oic.wk.p"}, + Interfaces = new List{OicResourceInterface.ReadOnly, OicResourceInterface.Baseline}, + }, + new ResourceTypes.SwitchBinary + { + RelativeUri = "/switch", + ResourceTypes = new List{"oic.r.switch.binary"}, + Interfaces = new List{OicResourceInterface.Actuator, OicResourceInterface.Baseline}, + }, + new ResourceTypes.LightBrightness + { + RelativeUri = "/brightness", + ResourceTypes = new List{"oic.r.light.brightness"}, + Interfaces = new List{OicResourceInterface.Actuator, OicResourceInterface.Baseline}, + } + } + }; + Assert.AreEqual(expectedDevice.DeviceId, actualDevice.DeviceId); + Assert.AreEqual(expectedDevice.Resources, actualDevice.Resources); } } } diff --git a/OICNet/CoreResources/OicDevice.cs b/OICNet/CoreResources/OicDevice.cs index 6ff4078..d601b7b 100644 --- a/OICNet/CoreResources/OicDevice.cs +++ b/OICNet/CoreResources/OicDevice.cs @@ -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; } @@ -63,6 +66,30 @@ public class OicDevice : OicCoreResource public Guid PlatformId { get; set; } //} + /// + 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 //[ @@ -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 { @@ -364,5 +392,24 @@ public class LocalisedDescription /// [JsonProperty("value"), MaxLength(64)] public string Description { get; set; } + + /// + 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; + } + + /// + public override int GetHashCode() + { + return typeof(LocalisedDescription).GetHashCode(); + } } } diff --git a/OICNet/CoreResources/OicPlatform.cs b/OICNet/CoreResources/OicPlatform.cs index 1046bbe..b885b04 100644 --- a/OICNet/CoreResources/OicPlatform.cs +++ b/OICNet/CoreResources/OicPlatform.cs @@ -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; } @@ -58,13 +59,13 @@ public class OicPlatform : OicCoreResource /// Platform Resident OS Version /// [JsonProperty("mnos", NullValueHandling = NullValueHandling.Ignore, Required = Required.DisallowNull), MaxLength(64)] - public string PlatformOperatingSystemVersion { get; set; } + public string OperatingSystemVersion { get; set; } /// /// Platform Hardware Version /// [JsonProperty("mnhw", NullValueHandling = NullValueHandling.Ignore, Required = Required.DisallowNull), MaxLength(64)] - public string PlatformHardwareVersion { get; set; } + public string HardwareVersion { get; set; } /// /// Manufacturer's firmware version @@ -89,5 +90,39 @@ public class OicPlatform : OicCoreResource /// [JsonProperty("vid", NullValueHandling = NullValueHandling.Ignore, Required = Required.DisallowNull), MaxLength(64)] public string VendorId { get; set; } + + /// + 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() } diff --git a/OICNet/Utilities/Enumerables.cs b/OICNet/Utilities/Enumerables.cs new file mode 100644 index 0000000..82dffd6 --- /dev/null +++ b/OICNet/Utilities/Enumerables.cs @@ -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( + this IEnumerable first, IEnumerable second) + { + if (first == null && second == null) + { + return true; + } + if (first == null || second == null) + { + return false; + } + return first.SequenceEqual(second); + } + } +}