-
Notifications
You must be signed in to change notification settings - Fork 3
/
TestMerakiDashboardClientMocking.cs
118 lines (109 loc) · 4.78 KB
/
TestMerakiDashboardClientMocking.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Moq;
using Moq.Protected;
using Xunit;
namespace Meraki.Dashboard.Test
{
/// <summary>
/// Ensure <see cref="MerakiDashboardClient"/> can be mocked.
/// </summary>
public class TestMerakiDashboardClientMocking
{
[Fact]
public async void TestMocking()
{
Organization organization = new Organization
{
Name = "White house",
Id = "Org Id"
};
Network network = new Network
{
Id = "Network Id",
Name = "White House Network",
NetworkType = "Wireless",
OrganizationId = organization.Id,
Tags = "tags",
TimeZone = "UTC-06:00"
};
Device device = new Device
{
Address = "1600 Pennsylania Ave Washington DC",
LanIpAddress = new IPAddress(new byte[] { 127, 0, 0, 1 }),
Lattitude = 38.8977,
Longitude = 77.0365,
Mac = "00:00:00:00:00:00",
Model = "MR33",
Name = "Presidential AP",
NetworkId = network.Id,
Serial = "AAAA-BBBB-CCCC",
Tags = "Tags"
};
DeviceClient dclient = new DeviceClient
{
Id = Guid.NewGuid().ToString(),
Description = "description",
IPAddress = "127.0.0.1",
MacAddress = "80:c5:f2:79:be:e1",
MDnsName = "Test",
DhcpHostName = "Test",
SwitchPort = null,
User = "test@meraki.com",
VLan = "10"
};
MerakiDashboardClientOptions merakiDashboardClientSettings = new MerakiDashboardClientOptions
{
ApiKey = "api key",
BaseAddress = new Uri("http://myapi.com")
};
Mock<MerakiDashboardClient> merakiDashboardlientMock = new Mock<MerakiDashboardClient>(MockBehavior.Strict, merakiDashboardClientSettings);
merakiDashboardlientMock.Setup(merakiDashboardClient => merakiDashboardClient.GetOrganizationsAsync())
.Returns(Task.FromResult(new[] { organization }));
merakiDashboardlientMock.Setup(merakiDashboardClient => merakiDashboardClient.GetOrganizationNetworksAsync(organization))
.Returns(Task.FromResult(new[] { network }));
merakiDashboardlientMock.Setup(merakiDashboardClient => merakiDashboardClient.GetNetworkDevicesAsync(network))
.Returns(Task.FromResult(new[] { device }));
merakiDashboardlientMock.Setup(merakiDashboardClient => merakiDashboardClient.GetDeviceClientsAsync(device.Serial, new TimeSpan(24, 0, 0)))
.Returns(Task.FromResult(new[] { dclient }));
// Required by mocking framework
merakiDashboardlientMock.Protected().Setup("Dispose", true);
using (MerakiDashboardClient merakiDashboardClient = merakiDashboardlientMock.Object)
{
Assert.Equal(new[] { device }, await GetDevicesInAnOrganization(merakiDashboardClient));
var deviceClients = await merakiDashboardClient.GetDeviceClientsAsync(device.Serial, new TimeSpan(24, 0, 0));
Assert.NotNull(deviceClients);
Assert.Single(deviceClients);
Assert.Equal(dclient.MacAddress, deviceClients.Single().MacAddress);
}
merakiDashboardlientMock.VerifyAll();
}
/// <summary>
/// Get the devices in all networks in all organizations the current API key can access.
/// </summary>
/// <param name="merakiDashboardClient">
/// The <see cref="MerakiDashboardClient"/> to access the device swith.
/// </param>
/// <returns>
/// The <see cref="Device"/>s discovered.
/// </returns>
private async Task<IEnumerable<Device>> GetDevicesInAnOrganization(MerakiDashboardClient merakiDashboardClient)
{
List<Device> result = new List<Device>();
foreach (Organization organization in await merakiDashboardClient.GetOrganizationsAsync())
{
foreach (Network network in await merakiDashboardClient.GetOrganizationNetworksAsync(organization))
{
foreach (Device device in await merakiDashboardClient.GetNetworkDevicesAsync(network))
{
result.Add(device);
}
}
}
return result;
}
}
}