diff --git a/UptimeSharp.Tests/AlertsTest.cs b/UptimeSharp.Tests/AlertsTest.cs index 17827c9..4c9f1a1 100644 --- a/UptimeSharp.Tests/AlertsTest.cs +++ b/UptimeSharp.Tests/AlertsTest.cs @@ -2,98 +2,74 @@ using System.Collections.Generic; using UptimeSharp.Models; using System; +using System.Threading.Tasks; namespace UptimeSharp.Tests { - public class AlertsTest : IDisposable + public class AlertsTest : TestsBase { - UptimeClient client; - - // this API key is associated with the test account uptimesharp@outlook.com - // please don't abuse it and create your own if you want to test the project! - string APIKey = "u97240-a24c634b3b84f1af602628e8"; + public AlertsTest() : base() { } - // setup - public AlertsTest() + [Fact] + public async Task AddInvalidAlertWithTypeSms() { - client = new UptimeClient(APIKey); - } - - - // teardown - public void Dispose() - { - client.GetAlerts().ForEach(alert => + await ThrowsAsync(async () => { - try - { - client.DeleteAlert(alert); - } - catch(UptimeSharpException e){} + await client.AddAlert(Models.AlertType.SMS, "+436601289172"); }); } [Fact] - public void AddInvalidAlertWithTypeSms() + public async Task AddInvalidAlertWithTypeTwitter() { - Assert.Throws(() => + await ThrowsAsync(async () => { - client.AddAlert(Models.AlertType.SMS, "+436601289172"); + await client.AddAlert(Models.AlertType.Twitter, "artistandsocial"); }); } [Fact] - public void AddInvalidAlertWithTypeTwitter() - { - Assert.Throws(() => - { - client.AddAlert(Models.AlertType.Twitter, "artistandsocial"); - }); - } - - - [Fact] - public void AddAndRemoveAlerts() + public async Task AddAndRemoveAlerts() { string email = "example@ceecore.com"; - Assert.True(client.AddAlert(AlertType.Email, email)); + Assert.True(await client.AddAlert(AlertType.Email, email)); - Alert origin = GetOriginAlert(email); + Alert origin = await GetOriginAlert(email); Assert.Equal(email, origin.Value); Assert.Equal(AlertType.Email, origin.Type); - client.DeleteAlert(origin); + await client.DeleteAlert(origin); - origin = GetOriginAlert(email); + origin = await GetOriginAlert(email); Assert.Null(origin); } [Fact] - public void AddAndRetrieveSpecificAlerts() + public async Task AddAndRetrieveSpecificAlerts() { - Assert.True(client.AddAlert(AlertType.Email, "example1@ceecore.com")); - Assert.True(client.AddAlert(AlertType.Boxcar, "example2@ceecore.com")); + Assert.True(await client.AddAlert(AlertType.Email, "example1@ceecore.com")); + Assert.True(await client.AddAlert(AlertType.Boxcar, "example2@ceecore.com")); - List alerts = client.GetAlerts(); + List alerts = await client.GetAlerts(); Assert.InRange(alerts.ToArray().Length, 2, 100); - List specificAlerts = client.GetAlerts(new string[] { alerts[0].ID, alerts[1].ID }); + List specificAlerts = await client.GetAlerts(new string[] { alerts[0].ID, alerts[1].ID }); Assert.Equal(2, specificAlerts.ToArray().Length); } - private Alert GetOriginAlert(string value) + private async Task GetOriginAlert(string value) { - List alerts = client.GetAlerts(); + List alerts = await client.GetAlerts(); Alert origin = null; alerts.ForEach(alert => diff --git a/UptimeSharp.Tests/ClientTest.cs b/UptimeSharp.Tests/ClientTest.cs index 2187155..9ce2142 100644 --- a/UptimeSharp.Tests/ClientTest.cs +++ b/UptimeSharp.Tests/ClientTest.cs @@ -4,20 +4,9 @@ using UptimeSharp.Models; namespace UptimeSharp.Tests { - public class ClientTest + public class ClientTest : TestsBase { - UptimeClient client; - - // this API key is associated with the test account uptimesharp@outlook.com - // please don't abuse it and create your own if you want to test the project! - string APIKey = "u97240-a24c634b3b84f1af602628e8"; - - - // setup - public ClientTest() - { - client = new UptimeClient(APIKey); - } + public ClientTest() : base() { } [Fact] diff --git a/UptimeSharp.Tests/MonitorsTest.cs b/UptimeSharp.Tests/MonitorsTest.cs index 9d6a5f5..b7435d4 100644 --- a/UptimeSharp.Tests/MonitorsTest.cs +++ b/UptimeSharp.Tests/MonitorsTest.cs @@ -2,37 +2,26 @@ using System; using System.Collections.Generic; using UptimeSharp.Models; +using System.Threading.Tasks; namespace UptimeSharp.Tests { - public class MonitorsTest : IDisposable + public class MonitorsTest : TestsBase { - UptimeClient client; - - // this API key is associated with the test account uptimesharp@outlook.com - // please don't abuse it and create your own if you want to test the project! - string APIKey = "u97240-a24c634b3b84f1af602628e8"; + public MonitorsTest() : base() { } - // setup - public MonitorsTest() + public async Task Dispose() { - client = new UptimeClient(APIKey); - } - - - // teardown - public void Dispose() - { - List monitors = client.GetMonitors(); - monitors.ForEach(monitor => client.DeleteMonitor(monitor)); + List monitors = await client.GetMonitors(); + monitors.ForEach(item => monitorsToDelete.Add(item.ID)); } [Fact] - public void AddHTTPMonitor() + public async Task AddHTTPMonitor() { - Assert.True(client.AddMonitor( + Assert.True(await client.AddMonitor( name: "test_1", uri: "http://test1.com" )); @@ -40,9 +29,9 @@ namespace UptimeSharp.Tests [Fact] - public void AddKeywordMonitor() + public async Task AddKeywordMonitor() { - Assert.True(client.AddMonitor( + Assert.True(await client.AddMonitor( name: "test_2", uri: "http://test2.com", type: Models.Type.Keyword, @@ -53,9 +42,9 @@ namespace UptimeSharp.Tests [Fact] - public void AddPingMonitor() + public async Task AddPingMonitor() { - Assert.True(client.AddMonitor( + Assert.True(await client.AddMonitor( name: "test_3", uri: "http://test3.com", type: Models.Type.Ping @@ -64,9 +53,9 @@ namespace UptimeSharp.Tests [Fact] - public void AddPortMonitor() + public async Task AddPortMonitor() { - Assert.True(client.AddMonitor( + Assert.True(await client.AddMonitor( name: "test_4", uri: "127.0.0.1", type: Models.Type.Port, @@ -77,16 +66,16 @@ namespace UptimeSharp.Tests [Fact] - public void GetMonitors() + public async Task GetMonitors() { - Assert.True(client.AddMonitor( + Assert.True(await client.AddMonitor( name: "test_5", uri: "255.0.0.1", type: Models.Type.Port, subtype: Subtype.HTTP )); - List items = client.GetMonitors(); + List items = await client.GetMonitors(); Monitor monitor = null; items.ForEach(item => @@ -106,14 +95,14 @@ namespace UptimeSharp.Tests [Fact] - public void ModifyAMonitor() + public async Task ModifyAMonitor() { - Assert.True(client.AddMonitor( + Assert.True(await client.AddMonitor( name: "test_6", uri: "http://test6.com" )); - List items = client.GetMonitors(); + List items = await client.GetMonitors(); Monitor monitor = null; items.ForEach(item => @@ -128,9 +117,9 @@ namespace UptimeSharp.Tests monitor.Name = "updated_test_6"; - Assert.True(client.ModifyMonitor(monitor)); + Assert.True(await client.ModifyMonitor(monitor)); - monitor = client.GetMonitor(monitor.ID); + monitor = await client.GetMonitor(monitor.ID); Assert.Equal(monitor.Name, "updated_test_6"); } diff --git a/UptimeSharp.Tests/TestsBase.cs b/UptimeSharp.Tests/TestsBase.cs new file mode 100644 index 0000000..3495564 --- /dev/null +++ b/UptimeSharp.Tests/TestsBase.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace UptimeSharp.Tests +{ + public class TestsBase : IDisposable + { + protected UptimeClient client; + + protected List alertsToDelete = new List(); + protected List monitorsToDelete = new List(); + + // this API key is associated with the test account uptimesharp@outlook.com + // please don't abuse it and create your own if you want to test the project! + protected string APIKey = "u97240-a24c634b3b84f1af602628e8"; + + + // setup + public TestsBase() + { + client = new UptimeClient(APIKey); + } + + + // teardown + public void Dispose() + { + alertsToDelete.ForEach(async id => + { + await client.DeleteAlert(id); + }); + monitorsToDelete.ForEach(async id => + { + await client.DeleteMonitor(id); + }); + } + + + // async throws + public static async Task ThrowsAsync(Func func) + { + var expected = typeof(TException); + Type actual = null; + try + { + await func(); + } + catch (Exception e) + { + actual = e.GetType(); + } + Assert.Equal(expected, actual); + } + } +} diff --git a/UptimeSharp.Tests/UptimeSharp.Tests.csproj b/UptimeSharp.Tests/UptimeSharp.Tests.csproj index 2ea9675..a539405 100644 --- a/UptimeSharp.Tests/UptimeSharp.Tests.csproj +++ b/UptimeSharp.Tests/UptimeSharp.Tests.csproj @@ -54,6 +54,7 @@ + @@ -65,6 +66,11 @@ + + + + +