From 44d49b83b8751bd835c7080bf6bb681e5b4bee71 Mon Sep 17 00:00:00 2001 From: qJake Date: Sun, 31 Jan 2021 14:09:56 -0600 Subject: [PATCH 1/2] Refactor incorrect test namespace --- HADotNet.Core.Tests/AutomationTests.cs | 11 +- HADotNet.Core.Tests/CalendarTests.cs | 3 +- HADotNet.Core.Tests/ConfigTests.cs | 69 +++++---- HADotNet.Core.Tests/DiscoveryTests.cs | 65 ++++---- HADotNet.Core.Tests/EntityTests.cs | 90 ++++++------ HADotNet.Core.Tests/ErrorLogTests.cs | 72 ++++----- HADotNet.Core.Tests/EventTests.cs | 66 ++++----- HADotNet.Core.Tests/HistoryTests.cs | 196 ++++++++++++------------- HADotNet.Core.Tests/LogbookTests.cs | 2 +- HADotNet.Core.Tests/RootApiTests.cs | 3 +- HADotNet.Core.Tests/ServiceTests.cs | 3 +- HADotNet.Core.Tests/StatesTests.cs | 3 +- HADotNet.Core.Tests/TemplateTests.cs | 68 ++++----- HADotNet.Core/HADotNet.Core.csproj | 6 +- 14 files changed, 325 insertions(+), 332 deletions(-) diff --git a/HADotNet.Core.Tests/AutomationTests.cs b/HADotNet.Core.Tests/AutomationTests.cs index 40253fc..46d85d8 100644 --- a/HADotNet.Core.Tests/AutomationTests.cs +++ b/HADotNet.Core.Tests/AutomationTests.cs @@ -1,12 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using HADotNet.Core; -using HADotNet.Core.Clients; +using HADotNet.Core.Clients; using HADotNet.Core.Models; using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; -namespace Tests +namespace HADotNet.Core.Tests { /// /// The tests for . diff --git a/HADotNet.Core.Tests/CalendarTests.cs b/HADotNet.Core.Tests/CalendarTests.cs index 52ca71a..4a4c126 100644 --- a/HADotNet.Core.Tests/CalendarTests.cs +++ b/HADotNet.Core.Tests/CalendarTests.cs @@ -1,10 +1,9 @@ -using HADotNet.Core; using HADotNet.Core.Clients; using NUnit.Framework; using System; using System.Threading.Tasks; -namespace Tests +namespace HADotNet.Core.Tests { public class CalendarTests { diff --git a/HADotNet.Core.Tests/ConfigTests.cs b/HADotNet.Core.Tests/ConfigTests.cs index 1b62328..65a4e26 100644 --- a/HADotNet.Core.Tests/ConfigTests.cs +++ b/HADotNet.Core.Tests/ConfigTests.cs @@ -1,36 +1,35 @@ -using HADotNet.Core; -using HADotNet.Core.Clients; -using NUnit.Framework; -using System; -using System.Threading.Tasks; - -namespace Tests -{ - public class ConfigTests - { - private Uri Instance { get; set; } - private string ApiKey { get; set; } - - [SetUp] - public void Setup() - { - Instance = new Uri(Environment.GetEnvironmentVariable("HADotNet:Tests:Instance")); - ApiKey = Environment.GetEnvironmentVariable("HADotNet:Tests:ApiKey"); - - ClientFactory.Initialize(Instance, ApiKey); - } - - [Test] - public async Task ShouldRetrieveConfiguration() - { - var client = ClientFactory.GetClient(); - - var config = await client.GetConfiguration(); - - Assert.IsNotNull(config); - Assert.IsNotNull(config.UnitSystem); - Assert.AreNotEqual(0, config.Components.Count); - - } - } +using HADotNet.Core.Clients; +using NUnit.Framework; +using System; +using System.Threading.Tasks; + +namespace HADotNet.Core.Tests +{ + public class ConfigTests + { + private Uri Instance { get; set; } + private string ApiKey { get; set; } + + [SetUp] + public void Setup() + { + Instance = new Uri(Environment.GetEnvironmentVariable("HADotNet:Tests:Instance")); + ApiKey = Environment.GetEnvironmentVariable("HADotNet:Tests:ApiKey"); + + ClientFactory.Initialize(Instance, ApiKey); + } + + [Test] + public async Task ShouldRetrieveConfiguration() + { + var client = ClientFactory.GetClient(); + + var config = await client.GetConfiguration(); + + Assert.IsNotNull(config); + Assert.IsNotNull(config.UnitSystem); + Assert.AreNotEqual(0, config.Components.Count); + + } + } } \ No newline at end of file diff --git a/HADotNet.Core.Tests/DiscoveryTests.cs b/HADotNet.Core.Tests/DiscoveryTests.cs index 61b14f7..d98f74f 100644 --- a/HADotNet.Core.Tests/DiscoveryTests.cs +++ b/HADotNet.Core.Tests/DiscoveryTests.cs @@ -1,34 +1,33 @@ -using HADotNet.Core; -using HADotNet.Core.Clients; -using NUnit.Framework; -using System; -using System.Threading.Tasks; - -namespace Tests -{ - public class DiscoveryTests - { - private Uri Instance { get; set; } - private string ApiKey { get; set; } - - [SetUp] - public void Setup() - { - Instance = new Uri(Environment.GetEnvironmentVariable("HADotNet:Tests:Instance")); - ApiKey = Environment.GetEnvironmentVariable("HADotNet:Tests:ApiKey"); - - ClientFactory.Initialize(Instance, ApiKey); - } - - [Test] - public async Task ShouldRetrieveDiscoveryInfo() - { - var client = ClientFactory.GetClient(); - - var discovery = await client.GetDiscoveryInfo(); - - Assert.IsNotNull(discovery); - Assert.IsNotEmpty(discovery.LocationName); - } - } +using HADotNet.Core.Clients; +using NUnit.Framework; +using System; +using System.Threading.Tasks; + +namespace HADotNet.Core.Tests +{ + public class DiscoveryTests + { + private Uri Instance { get; set; } + private string ApiKey { get; set; } + + [SetUp] + public void Setup() + { + Instance = new Uri(Environment.GetEnvironmentVariable("HADotNet:Tests:Instance")); + ApiKey = Environment.GetEnvironmentVariable("HADotNet:Tests:ApiKey"); + + ClientFactory.Initialize(Instance, ApiKey); + } + + [Test] + public async Task ShouldRetrieveDiscoveryInfo() + { + var client = ClientFactory.GetClient(); + + var discovery = await client.GetDiscoveryInfo(); + + Assert.IsNotNull(discovery); + Assert.IsNotEmpty(discovery.LocationName); + } + } } \ No newline at end of file diff --git a/HADotNet.Core.Tests/EntityTests.cs b/HADotNet.Core.Tests/EntityTests.cs index ccc3487..46e4c33 100644 --- a/HADotNet.Core.Tests/EntityTests.cs +++ b/HADotNet.Core.Tests/EntityTests.cs @@ -1,46 +1,46 @@ -using HADotNet.Core; -using HADotNet.Core.Clients; -using NUnit.Framework; -using System; -using System.Linq; -using System.Threading.Tasks; - -namespace Tests -{ - public class EntityTests - { - private Uri Instance { get; set; } - private string ApiKey { get; set; } - - [SetUp] - public void Setup() - { - Instance = new Uri(Environment.GetEnvironmentVariable("HADotNet:Tests:Instance")); - ApiKey = Environment.GetEnvironmentVariable("HADotNet:Tests:ApiKey"); - - ClientFactory.Initialize(Instance, ApiKey); - } - - [Test] - public async Task ShouldRetrieveEntityList() - { - var client = ClientFactory.GetClient(); - - var entities = await client.GetEntities(); - - Assert.IsNotNull(entities); - Assert.AreNotEqual(0, entities.Count()); - } - - [Test] - public async Task ShouldRetrieveEntityListWithDomainFilter() - { - var client = ClientFactory.GetClient(); - - var entities = await client.GetEntities("light"); - - Assert.IsNotNull(entities); - Assert.AreNotEqual(0, entities.Count()); - } - } +using HADotNet.Core; +using HADotNet.Core.Clients; +using NUnit.Framework; +using System; +using System.Linq; +using System.Threading.Tasks; + +namespace HADotNet.Core.Tests +{ + public class EntityTests + { + private Uri Instance { get; set; } + private string ApiKey { get; set; } + + [SetUp] + public void Setup() + { + Instance = new Uri(Environment.GetEnvironmentVariable("HADotNet:Tests:Instance")); + ApiKey = Environment.GetEnvironmentVariable("HADotNet:Tests:ApiKey"); + + ClientFactory.Initialize(Instance, ApiKey); + } + + [Test] + public async Task ShouldRetrieveEntityList() + { + var client = ClientFactory.GetClient(); + + var entities = await client.GetEntities(); + + Assert.IsNotNull(entities); + Assert.AreNotEqual(0, entities.Count()); + } + + [Test] + public async Task ShouldRetrieveEntityListWithDomainFilter() + { + var client = ClientFactory.GetClient(); + + var entities = await client.GetEntities("light"); + + Assert.IsNotNull(entities); + Assert.AreNotEqual(0, entities.Count()); + } + } } \ No newline at end of file diff --git a/HADotNet.Core.Tests/ErrorLogTests.cs b/HADotNet.Core.Tests/ErrorLogTests.cs index 38678fa..4950370 100644 --- a/HADotNet.Core.Tests/ErrorLogTests.cs +++ b/HADotNet.Core.Tests/ErrorLogTests.cs @@ -1,37 +1,37 @@ -using HADotNet.Core; -using HADotNet.Core.Clients; -using NUnit.Framework; -using System; -using System.Threading.Tasks; - -namespace Tests -{ - public class ErrorLogTests - { - private Uri Instance { get; set; } - private string ApiKey { get; set; } - - [SetUp] - public void Setup() - { - Instance = new Uri(Environment.GetEnvironmentVariable("HADotNet:Tests:Instance")); - ApiKey = Environment.GetEnvironmentVariable("HADotNet:Tests:ApiKey"); - - ClientFactory.Initialize(Instance, ApiKey); - } - - [Test] - public async Task ShouldRetrieveErrorLogList() - { - var client = ClientFactory.GetClient(); - - var log = await client.GetErrorLogEntries(); - - Assert.IsNotNull(log); - Assert.AreNotEqual(0, log.LogEntries.Count); - Assert.AreNotEqual(0, log.Errors.Count); - Assert.AreNotEqual(0, log.Warnings.Count); - Assert.AreEqual(7, log[7].Count); - } - } +using HADotNet.Core; +using HADotNet.Core.Clients; +using NUnit.Framework; +using System; +using System.Threading.Tasks; + +namespace HADotNet.Core.Tests +{ + public class ErrorLogTests + { + private Uri Instance { get; set; } + private string ApiKey { get; set; } + + [SetUp] + public void Setup() + { + Instance = new Uri(Environment.GetEnvironmentVariable("HADotNet:Tests:Instance")); + ApiKey = Environment.GetEnvironmentVariable("HADotNet:Tests:ApiKey"); + + ClientFactory.Initialize(Instance, ApiKey); + } + + [Test] + public async Task ShouldRetrieveErrorLogList() + { + var client = ClientFactory.GetClient(); + + var log = await client.GetErrorLogEntries(); + + Assert.IsNotNull(log); + Assert.AreNotEqual(0, log.LogEntries.Count); + Assert.AreNotEqual(0, log.Errors.Count); + Assert.AreNotEqual(0, log.Warnings.Count); + Assert.AreEqual(7, log[7].Count); + } + } } \ No newline at end of file diff --git a/HADotNet.Core.Tests/EventTests.cs b/HADotNet.Core.Tests/EventTests.cs index 03de3df..530ffc7 100644 --- a/HADotNet.Core.Tests/EventTests.cs +++ b/HADotNet.Core.Tests/EventTests.cs @@ -1,34 +1,34 @@ -using HADotNet.Core; -using HADotNet.Core.Clients; -using NUnit.Framework; -using System; -using System.Threading.Tasks; - -namespace Tests -{ - public class EventTests - { - private Uri Instance { get; set; } - private string ApiKey { get; set; } - - [SetUp] - public void Setup() - { - Instance = new Uri(Environment.GetEnvironmentVariable("HADotNet:Tests:Instance")); - ApiKey = Environment.GetEnvironmentVariable("HADotNet:Tests:ApiKey"); - - ClientFactory.Initialize(Instance, ApiKey); - } - - [Test] - public async Task ShouldRetrieveEventList() - { - var client = ClientFactory.GetClient(); - - var events = await client.GetEvents(); - - Assert.IsNotNull(events); - Assert.AreNotEqual(0, events.Count); - } - } +using HADotNet.Core; +using HADotNet.Core.Clients; +using NUnit.Framework; +using System; +using System.Threading.Tasks; + +namespace HADotNet.Core.Tests +{ + public class EventTests + { + private Uri Instance { get; set; } + private string ApiKey { get; set; } + + [SetUp] + public void Setup() + { + Instance = new Uri(Environment.GetEnvironmentVariable("HADotNet:Tests:Instance")); + ApiKey = Environment.GetEnvironmentVariable("HADotNet:Tests:ApiKey"); + + ClientFactory.Initialize(Instance, ApiKey); + } + + [Test] + public async Task ShouldRetrieveEventList() + { + var client = ClientFactory.GetClient(); + + var events = await client.GetEvents(); + + Assert.IsNotNull(events); + Assert.AreNotEqual(0, events.Count); + } + } } \ No newline at end of file diff --git a/HADotNet.Core.Tests/HistoryTests.cs b/HADotNet.Core.Tests/HistoryTests.cs index c6bd7f9..b14f572 100644 --- a/HADotNet.Core.Tests/HistoryTests.cs +++ b/HADotNet.Core.Tests/HistoryTests.cs @@ -1,99 +1,99 @@ -using HADotNet.Core; -using HADotNet.Core.Clients; -using NUnit.Framework; -using System; -using System.Threading.Tasks; - -namespace Tests -{ - public class HistoryTests - { - private Uri Instance { get; set; } - private string ApiKey { get; set; } - - [SetUp] - public void Setup() - { - Instance = new Uri(Environment.GetEnvironmentVariable("HADotNet:Tests:Instance")); - ApiKey = Environment.GetEnvironmentVariable("HADotNet:Tests:ApiKey"); - - ClientFactory.Initialize(Instance, ApiKey); - } - - [Test] - public async Task ShouldRetrieveAllHistory() - { - var client = ClientFactory.GetClient(); - - var allHistory = await client.GetHistory(); - - Assert.IsNotNull(allHistory); - Assert.IsNotEmpty(allHistory[0].EntityId); - Assert.AreNotEqual(0, allHistory.Count); - Assert.AreNotEqual(0, allHistory[0].Count); - } - - [Test] - public async Task ShouldRetrieveHistoryByEntityId() - { - var client = ClientFactory.GetClient(); - - var history = await client.GetHistory("light.jakes_office"); - - Assert.IsNotNull(history); - Assert.IsNotEmpty(history.EntityId); - Assert.AreNotEqual(0, history.Count); - } - - [Test] - public async Task ShouldRetrieveHistoryByStartDate() - { - var client = ClientFactory.GetClient(); - - var allHistory = await client.GetHistory(DateTimeOffset.Now.Subtract(TimeSpan.FromDays(2))); - - Assert.IsNotNull(allHistory); - Assert.IsNotEmpty(allHistory[0].EntityId); - Assert.AreNotEqual(0, allHistory.Count); - Assert.AreNotEqual(0, allHistory[0].Count); - } - - [Test] - public async Task ShouldRetrieveHistoryByStartAndEndDate() - { - var client = ClientFactory.GetClient(); - - var allHistory = await client.GetHistory(DateTimeOffset.Now.Subtract(TimeSpan.FromDays(2)), DateTimeOffset.Now.Subtract(new TimeSpan(1, 12, 0, 0))); - - Assert.IsNotNull(allHistory); - Assert.IsNotEmpty(allHistory[0].EntityId); - Assert.AreNotEqual(0, allHistory.Count); - Assert.AreNotEqual(0, allHistory[0].Count); - } - - [Test] - public async Task ShouldRetrieveHistoryByStartDateAndDuration() - { - var client = ClientFactory.GetClient(); - - var allHistory = await client.GetHistory(DateTimeOffset.Now.Subtract(TimeSpan.FromDays(2)), TimeSpan.FromHours(18)); - - Assert.IsNotNull(allHistory); - Assert.IsNotEmpty(allHistory[0].EntityId); - Assert.AreNotEqual(0, allHistory.Count); - Assert.AreNotEqual(0, allHistory[0].Count); - } - - [Test] - public async Task ShouldRetrieveHistoryByStartAndEndDateAndEntityId() - { - var client = ClientFactory.GetClient(); - - var history = await client.GetHistory("group.family_room_lights", DateTimeOffset.Now.Subtract(TimeSpan.FromDays(2)), DateTimeOffset.Now.Subtract(new TimeSpan(1, 12, 0, 0))); - - Assert.IsNotNull(history); - Assert.IsNotEmpty(history.EntityId); - Assert.AreNotEqual(0, history.Count); - } - } +using HADotNet.Core; +using HADotNet.Core.Clients; +using NUnit.Framework; +using System; +using System.Threading.Tasks; + +namespace HADotNet.Core.Tests +{ + public class HistoryTests + { + private Uri Instance { get; set; } + private string ApiKey { get; set; } + + [SetUp] + public void Setup() + { + Instance = new Uri(Environment.GetEnvironmentVariable("HADotNet:Tests:Instance")); + ApiKey = Environment.GetEnvironmentVariable("HADotNet:Tests:ApiKey"); + + ClientFactory.Initialize(Instance, ApiKey); + } + + [Test] + public async Task ShouldRetrieveAllHistory() + { + var client = ClientFactory.GetClient(); + + var allHistory = await client.GetHistory(); + + Assert.IsNotNull(allHistory); + Assert.IsNotEmpty(allHistory[0].EntityId); + Assert.AreNotEqual(0, allHistory.Count); + Assert.AreNotEqual(0, allHistory[0].Count); + } + + [Test] + public async Task ShouldRetrieveHistoryByEntityId() + { + var client = ClientFactory.GetClient(); + + var history = await client.GetHistory("light.jakes_office"); + + Assert.IsNotNull(history); + Assert.IsNotEmpty(history.EntityId); + Assert.AreNotEqual(0, history.Count); + } + + [Test] + public async Task ShouldRetrieveHistoryByStartDate() + { + var client = ClientFactory.GetClient(); + + var allHistory = await client.GetHistory(DateTimeOffset.Now.Subtract(TimeSpan.FromDays(2))); + + Assert.IsNotNull(allHistory); + Assert.IsNotEmpty(allHistory[0].EntityId); + Assert.AreNotEqual(0, allHistory.Count); + Assert.AreNotEqual(0, allHistory[0].Count); + } + + [Test] + public async Task ShouldRetrieveHistoryByStartAndEndDate() + { + var client = ClientFactory.GetClient(); + + var allHistory = await client.GetHistory(DateTimeOffset.Now.Subtract(TimeSpan.FromDays(2)), DateTimeOffset.Now.Subtract(new TimeSpan(1, 12, 0, 0))); + + Assert.IsNotNull(allHistory); + Assert.IsNotEmpty(allHistory[0].EntityId); + Assert.AreNotEqual(0, allHistory.Count); + Assert.AreNotEqual(0, allHistory[0].Count); + } + + [Test] + public async Task ShouldRetrieveHistoryByStartDateAndDuration() + { + var client = ClientFactory.GetClient(); + + var allHistory = await client.GetHistory(DateTimeOffset.Now.Subtract(TimeSpan.FromDays(2)), TimeSpan.FromHours(18)); + + Assert.IsNotNull(allHistory); + Assert.IsNotEmpty(allHistory[0].EntityId); + Assert.AreNotEqual(0, allHistory.Count); + Assert.AreNotEqual(0, allHistory[0].Count); + } + + [Test] + public async Task ShouldRetrieveHistoryByStartAndEndDateAndEntityId() + { + var client = ClientFactory.GetClient(); + + var history = await client.GetHistory("group.family_room_lights", DateTimeOffset.Now.Subtract(TimeSpan.FromDays(2)), DateTimeOffset.Now.Subtract(new TimeSpan(1, 12, 0, 0))); + + Assert.IsNotNull(history); + Assert.IsNotEmpty(history.EntityId); + Assert.AreNotEqual(0, history.Count); + } + } } \ No newline at end of file diff --git a/HADotNet.Core.Tests/LogbookTests.cs b/HADotNet.Core.Tests/LogbookTests.cs index 7f26f5b..f2dd48b 100644 --- a/HADotNet.Core.Tests/LogbookTests.cs +++ b/HADotNet.Core.Tests/LogbookTests.cs @@ -4,7 +4,7 @@ using System; using System.Threading.Tasks; -namespace Tests +namespace HADotNet.Core.Tests { public class LogbookTests { diff --git a/HADotNet.Core.Tests/RootApiTests.cs b/HADotNet.Core.Tests/RootApiTests.cs index b8d56ea..4626b91 100644 --- a/HADotNet.Core.Tests/RootApiTests.cs +++ b/HADotNet.Core.Tests/RootApiTests.cs @@ -1,10 +1,9 @@ -using HADotNet.Core; using HADotNet.Core.Clients; using NUnit.Framework; using System; using System.Threading.Tasks; -namespace Tests +namespace HADotNet.Core.Tests { public class RootApiTests { diff --git a/HADotNet.Core.Tests/ServiceTests.cs b/HADotNet.Core.Tests/ServiceTests.cs index 33b83bb..4fa3a80 100644 --- a/HADotNet.Core.Tests/ServiceTests.cs +++ b/HADotNet.Core.Tests/ServiceTests.cs @@ -1,10 +1,9 @@ -using HADotNet.Core; using HADotNet.Core.Clients; using NUnit.Framework; using System; using System.Threading.Tasks; -namespace Tests +namespace HADotNet.Core.Tests { public class ServiceTests { diff --git a/HADotNet.Core.Tests/StatesTests.cs b/HADotNet.Core.Tests/StatesTests.cs index 2534855..ce377c1 100644 --- a/HADotNet.Core.Tests/StatesTests.cs +++ b/HADotNet.Core.Tests/StatesTests.cs @@ -1,11 +1,10 @@ -using HADotNet.Core; using HADotNet.Core.Clients; using NUnit.Framework; using System; using System.Collections.Generic; using System.Threading.Tasks; -namespace Tests +namespace HADotNet.Core.Tests { public class StatesTests { diff --git a/HADotNet.Core.Tests/TemplateTests.cs b/HADotNet.Core.Tests/TemplateTests.cs index cb9b866..f3e1755 100644 --- a/HADotNet.Core.Tests/TemplateTests.cs +++ b/HADotNet.Core.Tests/TemplateTests.cs @@ -1,35 +1,35 @@ -using HADotNet.Core; -using HADotNet.Core.Clients; -using NUnit.Framework; -using System; -using System.Text.RegularExpressions; -using System.Threading.Tasks; - -namespace Tests -{ - public class TemplateTests - { - private Uri Instance { get; set; } - private string ApiKey { get; set; } - - [SetUp] - public void Setup() - { - Instance = new Uri(Environment.GetEnvironmentVariable("HADotNet:Tests:Instance")); - ApiKey = Environment.GetEnvironmentVariable("HADotNet:Tests:ApiKey"); - - ClientFactory.Initialize(Instance, ApiKey); - } - - [Test] - public async Task ShouldRenderSunTemplate() - { - var client = ClientFactory.GetClient(); - - var result = await client.RenderTemplate("The sun is {{ states('sun.sun') }}"); - - Assert.IsNotNull(result); - Assert.IsTrue(Regex.IsMatch(result, "^The sun is (?:above_horizon|below_horizon)$")); - } - } +using HADotNet.Core; +using HADotNet.Core.Clients; +using NUnit.Framework; +using System; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace HADotNet.Core.Tests +{ + public class TemplateTests + { + private Uri Instance { get; set; } + private string ApiKey { get; set; } + + [SetUp] + public void Setup() + { + Instance = new Uri(Environment.GetEnvironmentVariable("HADotNet:Tests:Instance")); + ApiKey = Environment.GetEnvironmentVariable("HADotNet:Tests:ApiKey"); + + ClientFactory.Initialize(Instance, ApiKey); + } + + [Test] + public async Task ShouldRenderSunTemplate() + { + var client = ClientFactory.GetClient(); + + var result = await client.RenderTemplate("The sun is {{ states('sun.sun') }}"); + + Assert.IsNotNull(result); + Assert.IsTrue(Regex.IsMatch(result, "^The sun is (?:above_horizon|below_horizon)$")); + } + } } \ No newline at end of file diff --git a/HADotNet.Core/HADotNet.Core.csproj b/HADotNet.Core/HADotNet.Core.csproj index 7d88547..39d97bd 100644 --- a/HADotNet.Core/HADotNet.Core.csproj +++ b/HADotNet.Core/HADotNet.Core.csproj @@ -12,15 +12,15 @@ home-assistant external api library HADotNet Core Library true - 1.4.0 + 1.4.1 Apache-2.0 https://github.com/qJake/HADotNet https://github.com/qJake/HADotNet.git git 7.3 - 1.4.0.0 - 1.4.0.0 + 1.4.1.0 + 1.4.1.0 From d7271cb3edab956a972089014df08f068d6a1ace Mon Sep 17 00:00:00 2001 From: qJake Date: Sun, 31 Jan 2021 15:06:07 -0600 Subject: [PATCH 2/2] Exception refactor and bump to v1.5 --- .../HADotNet.Core.Tests.csproj | 4 ++ HADotNet.Core.Tests/InfoTests.cs | 28 +++++++-- HADotNet.Core/BaseClient.cs | 9 +-- HADotNet.Core/Clients/InfoClient.cs | 37 ++++++++++- HADotNet.Core/Domain/HttpResponseException.cs | 63 +++++++++++++++++++ .../Domain/SupervisorNotFoundException.cs | 31 +++++++++ HADotNet.Core/HADotNet.Core.csproj | 6 +- HADotNet.Core/Models/AddonObject.cs | 6 +- HADotNet.Core/Models/HostInfoObject.cs | 16 ++--- README.md | 1 + 10 files changed, 176 insertions(+), 25 deletions(-) create mode 100644 HADotNet.Core/Domain/HttpResponseException.cs create mode 100644 HADotNet.Core/Domain/SupervisorNotFoundException.cs diff --git a/HADotNet.Core.Tests/HADotNet.Core.Tests.csproj b/HADotNet.Core.Tests/HADotNet.Core.Tests.csproj index 458e4a2..96099c0 100644 --- a/HADotNet.Core.Tests/HADotNet.Core.Tests.csproj +++ b/HADotNet.Core.Tests/HADotNet.Core.Tests.csproj @@ -5,6 +5,10 @@ false + + TRACE + + diff --git a/HADotNet.Core.Tests/InfoTests.cs b/HADotNet.Core.Tests/InfoTests.cs index 632208b..ba1a613 100644 --- a/HADotNet.Core.Tests/InfoTests.cs +++ b/HADotNet.Core.Tests/InfoTests.cs @@ -1,10 +1,18 @@ using System; using System.Threading.Tasks; using HADotNet.Core.Clients; +using HADotNet.Core.Domain; using NUnit.Framework; namespace HADotNet.Core.Tests { + /// + /// Tests for the Supervisor Info endpoints. + /// + /// + /// NOTE: If you want to test against a non-Supervisor instance, you'll need to set + /// the compilation symbol TEST_ENV_HA_CORE which will test for the correct exception. + /// public class InfoTests { private Uri Instance { get; set; } @@ -22,25 +30,33 @@ public void Setup() [Test] public async Task ShouldRetrieveSupervisorInfo() { - var client = ClientFactory.GetClient(); - + var client = ClientFactory.GetClient(); + +#if TEST_ENV_HA_CORE + Assert.ThrowsAsync(async () => await client.GetSupervisorInfo()); +#else var info = await client.GetSupervisorInfo(); Assert.AreEqual("ok", info.Result); Assert.IsNotNull(info.Data); Assert.IsNotNull(info.Data.Version); +#endif } [Test] public async Task ShouldRetrieveHostInfo() { - var client = ClientFactory.GetClient(); - + var client = ClientFactory.GetClient(); + +#if TEST_ENV_HA_CORE + Assert.ThrowsAsync(async () => await client.GetHostInfo()); +#else var info = await client.GetHostInfo(); Assert.AreEqual("ok", info.Result); Assert.IsNotNull(info.Data); Assert.IsNotNull(info.Data.OperatingSystem); +#endif } [Test] @@ -48,11 +64,15 @@ public async Task ShouldRetrieveCoreInfo() { var client = ClientFactory.GetClient(); +#if TEST_ENV_HA_CORE + Assert.ThrowsAsync(async () => await client.GetCoreInfo()); +#else var info = await client.GetCoreInfo(); Assert.AreEqual("ok", info.Result); Assert.IsNotNull(info.Data); Assert.IsNotNull(info.Data.Version); +#endif } } } diff --git a/HADotNet.Core/BaseClient.cs b/HADotNet.Core/BaseClient.cs index a9182ef..c84be87 100644 --- a/HADotNet.Core/BaseClient.cs +++ b/HADotNet.Core/BaseClient.cs @@ -1,6 +1,7 @@ using System; using System.Net; using System.Threading.Tasks; +using HADotNet.Core.Domain; using Newtonsoft.Json; using RestSharp; @@ -55,7 +56,7 @@ protected async Task Get(string path) where T : class return JsonConvert.DeserializeObject(resp.Content); } - throw new Exception($"Unexpected response code {(int)resp.StatusCode} from Home Assistant API endpoint {path}."); + throw new HttpResponseException((int)resp.StatusCode, resp.ResponseStatus.ToString(), resp.ResponseUri.PathAndQuery, $"Unexpected GET response code {(int)resp.StatusCode} from Home Assistant API endpoint {path}."); } /// @@ -98,7 +99,7 @@ protected async Task Post(string path, object body, bool isRawBody = false return JsonConvert.DeserializeObject(resp.Content); } - throw new Exception($"Unexpected response code {(int)resp.StatusCode} from Home Assistant API endpoint {path}."); + throw new HttpResponseException((int)resp.StatusCode, resp.ResponseStatus.ToString(), resp.ResponseUri.PathAndQuery, $"Unexpected POST response code {(int)resp.StatusCode} from Home Assistant API endpoint {path}."); } /// @@ -128,7 +129,7 @@ protected async Task Delete(string path) where T : class return JsonConvert.DeserializeObject(resp.Content); } - throw new Exception($"Unexpected response code {(int)resp.StatusCode} from Home Assistant API endpoint {path}."); + throw new HttpResponseException((int)resp.StatusCode, resp.ResponseStatus.ToString(), resp.ResponseUri.PathAndQuery, $"Unexpected DELETE response code {(int)resp.StatusCode} from Home Assistant API endpoint {path}."); } /// @@ -147,7 +148,7 @@ protected async Task Delete(string path) if (!(resp.StatusCode == HttpStatusCode.OK || resp.StatusCode == HttpStatusCode.NoContent)) { - throw new Exception($"Unexpected response code {(int)resp.StatusCode} from Home Assistant API endpoint {path}."); + throw new HttpResponseException((int)resp.StatusCode, resp.ResponseStatus.ToString(), resp.ResponseUri.PathAndQuery, $"Unexpected DELETE response code {(int)resp.StatusCode} from Home Assistant API endpoint {path}."); } } } diff --git a/HADotNet.Core/Clients/InfoClient.cs b/HADotNet.Core/Clients/InfoClient.cs index 1812f18..d274676 100644 --- a/HADotNet.Core/Clients/InfoClient.cs +++ b/HADotNet.Core/Clients/InfoClient.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using HADotNet.Core.Domain; using HADotNet.Core.Models; namespace HADotNet.Core.Clients @@ -20,18 +21,48 @@ public InfoClient(Uri instance, string apiKey) : base(instance, apiKey) { } /// Retrieves Supervisor information. /// /// A representing Supervisor informatio. - public async Task> GetSupervisorInfo() => await Get>("/api/hassio/supervisor/info"); + public async Task> GetSupervisorInfo() + { + try + { + return await Get>("/api/hassio/supervisor/info"); + } + catch (HttpResponseException hrex) when (hrex.StatusCode == 404) + { + throw new SupervisorNotFoundException("This does not appear to be a Home Assistant Supervisor instance. See inner exception for more details.", hrex); + } + } /// /// Retrieves Host information. /// /// A representing Host informatio. - public async Task> GetHostInfo() => await Get>("/api/hassio/host/info"); + public async Task> GetHostInfo() + { + try + { + return await Get>("/api/hassio/host/info"); + } + catch (HttpResponseException hrex) when (hrex.StatusCode == 404) + { + throw new SupervisorNotFoundException("This does not appear to be a Home Assistant Supervisor instance. See inner exception for more details.", hrex); + } + } /// /// Retrieves Core information. /// /// A representing Host informatio. - public async Task> GetCoreInfo() => await Get>("/api/hassio/core/info"); + public async Task> GetCoreInfo() + { + try + { + return await Get>("/api/hassio/core/info"); + } + catch (HttpResponseException hrex) when (hrex.StatusCode == 404) + { + throw new SupervisorNotFoundException("This does not appear to be a Home Assistant Supervisor instance. See inner exception for more details.", hrex); + } + } } } diff --git a/HADotNet.Core/Domain/HttpResponseException.cs b/HADotNet.Core/Domain/HttpResponseException.cs new file mode 100644 index 0000000..2811033 --- /dev/null +++ b/HADotNet.Core/Domain/HttpResponseException.cs @@ -0,0 +1,63 @@ +using System; + +namespace HADotNet.Core.Domain +{ + /// + /// Represents a failed HTTP call to a Home Assistant endpoint. + /// + public class HttpResponseException : Exception + { + /// + /// Gets the status code for the HTTP response. + /// + public int StatusCode { get; } + + /// + /// Gets the network description, if the error was at the network level. + /// + public string NetworkDescription { get; } + + /// + /// Gets the original request path. + /// + public string RequestPath { get; } + + /// + /// Gets the error response body. + /// + public string ResponseBody { get; } + + /// + /// Initializes a new HttpResponseException. + /// + public HttpResponseException(int statusCode, string networkDescription, string requestPath, string responseBody) + { + StatusCode = statusCode; + NetworkDescription = networkDescription; + RequestPath = requestPath; + ResponseBody = responseBody; + } + + /// + /// Initializes a new HttpResponseException. + /// + public HttpResponseException(int statusCode, string networkDescription, string requestPath, string responseBody, string message) : base(message) + { + StatusCode = statusCode; + NetworkDescription = networkDescription; + RequestPath = requestPath; + ResponseBody = responseBody; + } + + /// + /// Initializes a new HttpResponseException. + /// + public HttpResponseException(int statusCode, string networkDescription, string requestPath, string responseBody, string message, Exception innerException) : base(message, innerException) + { + StatusCode = statusCode; + NetworkDescription = networkDescription; + RequestPath = requestPath; + ResponseBody = responseBody; + } + } +} diff --git a/HADotNet.Core/Domain/SupervisorNotFoundException.cs b/HADotNet.Core/Domain/SupervisorNotFoundException.cs new file mode 100644 index 0000000..21c6af6 --- /dev/null +++ b/HADotNet.Core/Domain/SupervisorNotFoundException.cs @@ -0,0 +1,31 @@ +using System; + +namespace HADotNet.Core.Domain +{ + /// + /// The exception that occurs when a Supervisor-only API call is made to a non-Supervisor environment. + /// + public class SupervisorNotFoundException : Exception + { + /// + /// Initializes a new instance of the SupervisorNotFoundException. + /// + public SupervisorNotFoundException() + { + } + + /// + /// Initializes a new instance of the SupervisorNotFoundException. + /// + public SupervisorNotFoundException(string message) : base(message) + { + } + + /// + /// Initializes a new instance of the SupervisorNotFoundException. + /// + public SupervisorNotFoundException(string message, Exception innerException) : base(message, innerException) + { + } + } +} diff --git a/HADotNet.Core/HADotNet.Core.csproj b/HADotNet.Core/HADotNet.Core.csproj index 39d97bd..2609c95 100644 --- a/HADotNet.Core/HADotNet.Core.csproj +++ b/HADotNet.Core/HADotNet.Core.csproj @@ -12,15 +12,15 @@ home-assistant external api library HADotNet Core Library true - 1.4.1 + 1.5.0 Apache-2.0 https://github.com/qJake/HADotNet https://github.com/qJake/HADotNet.git git 7.3 - 1.4.1.0 - 1.4.1.0 + 1.5.0.0 + 1.5.0.0 diff --git a/HADotNet.Core/Models/AddonObject.cs b/HADotNet.Core/Models/AddonObject.cs index ff89ef4..beb56f6 100644 --- a/HADotNet.Core/Models/AddonObject.cs +++ b/HADotNet.Core/Models/AddonObject.cs @@ -14,13 +14,13 @@ public class AddonObject public string Description { get; set; } /// - /// True if icon is available, otherwise False. + /// True if icon is available, otherwise False. /// [JsonProperty("icon")] public bool Icon { get; set; } /// - /// True if logo is available, otherwise False. + /// True if logo is available, otherwise False. /// [JsonProperty("logo")] public bool Logo { get; set; } @@ -50,7 +50,7 @@ public class AddonObject public string State { get; set; } /// - /// True if update is available, otherwise False. + /// True if update is available, otherwise False. /// [JsonProperty("update_available")] public bool UpdateAvailable { get; set; } diff --git a/HADotNet.Core/Models/HostInfoObject.cs b/HADotNet.Core/Models/HostInfoObject.cs index 1f4d125..648ef00 100644 --- a/HADotNet.Core/Models/HostInfoObject.cs +++ b/HADotNet.Core/Models/HostInfoObject.cs @@ -9,43 +9,43 @@ namespace HADotNet.Core.Models public class HostInfoObject { /// - /// Gets or sets the chassis. + /// Gets or sets the chassis type. /// [JsonProperty("chassis")] public string Chassis { get; set; } /// - /// Gets or sets the CPE. + /// Gets or sets the CPE string. /// [JsonProperty("cpe")] public string Cpe { get; set; } /// - /// Gets or sets the deployment. + /// Gets or sets the deployment type (e.g. production). /// [JsonProperty("deployment")] public string Deployment { get; set; } /// - /// Gets or sets the disk free. + /// Gets or sets the disk free, expressed in GB. /// [JsonProperty("disk_free")] public double DiskFree { get; set; } /// - /// Gets or sets the disk total. + /// Gets or sets the disk total, expressed in GB. /// [JsonProperty("disk_total")] public double DiskTotal { get; set; } /// - /// Gets or sets the disk used. + /// Gets or sets the disk used, expressed in GB. /// [JsonProperty("disk_used")] public double DiskUsed { get; set; } /// - /// Gets or sets the features. + /// Gets or sets the feature list. /// [JsonProperty("features")] public IList Features { get; set; } @@ -57,7 +57,7 @@ public class HostInfoObject public string Hostname { get; set; } /// - /// Gets or sets the kernel. + /// Gets or sets the kernel version. /// [JsonProperty("kernel")] public string Kernel { get; set; } diff --git a/README.md b/README.md index 321ab7e..9af1626 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ A simple, straighforward .NET Standard library for the [Home Assistant](https:// * Logbook API * Services API * States API +* Supervisor API (Supervisor-based installations only) * Template API ## Getting Started