/
IoTHubServiceProxyE2ETests.cs
128 lines (104 loc) · 5.74 KB
/
IoTHubServiceProxyE2ETests.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
119
120
121
122
123
124
125
126
127
128
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Shared;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Microsoft.Azure.Devices.E2ETests
{
using TransportType = Microsoft.Azure.Devices.TransportType;
[TestClass]
[TestCategory("IoTHub-E2E")]
[TestCategory("ProxyE2ETests")]
public class IoTHubServiceProxyE2ETests
{
private const string DevicePrefix = "E2E_ServiceProxy_";
private const string JobDeviceId = "JobsSample_Device";
private const string JobTestTagName = "JobsSample_Tag";
private const int JobTestTagValue = 100;
private static TestLogging _log = TestLogging.GetInstance();
private static string ConnectionString = Configuration.IoTHub.ConnectionString;
private static string ProxyServerAddress = Configuration.IoTHub.ProxyServerAddress;
[TestMethod]
public async Task ServiceClient_Message_SendSingleMessage_WithProxy()
{
ServiceClientTransportSettings transportSettings = new ServiceClientTransportSettings();
transportSettings.AmqpProxy = new WebProxy(ProxyServerAddress);
transportSettings.HttpProxy = new WebProxy(ProxyServerAddress);
await SendSingleMessageService(transportSettings).ConfigureAwait(false);
}
[TestMethod]
public async Task RegistryManager_AddAndRemoveDevice_WithProxy()
{
HttpTransportSettings httpTransportSettings = new HttpTransportSettings();
httpTransportSettings.Proxy = new WebProxy(ProxyServerAddress);
await RegistryManager_AddDevice(httpTransportSettings).ConfigureAwait(false);
}
[TestMethod]
public async Task RegistryManager_AddAndRemoveDeviceWithScope()
{
RegistryManager registryManager = RegistryManager.CreateFromConnectionString(ConnectionString);
var edgeDevice = new Device(Guid.NewGuid().ToString())
{
Capabilities = new DeviceCapabilities { IotEdge = true }
};
edgeDevice = await registryManager.AddDeviceAsync(edgeDevice).ConfigureAwait(false);
var leafDevice = new Device(Guid.NewGuid().ToString()) { Scope = edgeDevice.Scope };
Device receivedDevice = await registryManager.AddDeviceAsync(leafDevice).ConfigureAwait(false);
Assert.IsNotNull(receivedDevice);
Assert.AreEqual(leafDevice.Id, receivedDevice.Id);
Assert.AreEqual(leafDevice.Scope, receivedDevice.Scope);
await registryManager.RemoveDeviceAsync(leafDevice.Id).ConfigureAwait(false);
await registryManager.RemoveDeviceAsync(edgeDevice.Id).ConfigureAwait(false);
}
[TestMethod]
public async Task JobClient_ScheduleAndRunTwinJob_WithProxy()
{
HttpTransportSettings httpTransportSettings = new HttpTransportSettings();
httpTransportSettings.Proxy = new WebProxy(ProxyServerAddress);
await JobClient_ScheduleAndRunTwinJob(httpTransportSettings).ConfigureAwait(false);
}
private async Task SendSingleMessageService(ServiceClientTransportSettings transportSettings)
{
TestDevice testDevice = await TestDevice.GetTestDeviceAsync(DevicePrefix).ConfigureAwait(false);
DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(testDevice.ConnectionString);
ServiceClient serviceClient = ServiceClient.CreateFromConnectionString(ConnectionString, TransportType.Amqp, transportSettings);
string payload;
string p1Value;
Message testMessage = ComposeD2CTestMessage(out payload, out p1Value);
await serviceClient.SendAsync(testDevice.Id, testMessage).ConfigureAwait(false);
}
private async Task RegistryManager_AddDevice(HttpTransportSettings httpTransportSettings)
{
string deviceName = DevicePrefix + Guid.NewGuid();
RegistryManager registryManager = RegistryManager.CreateFromConnectionString(ConnectionString, httpTransportSettings);
await registryManager.AddDeviceAsync(new Device(deviceName)).ConfigureAwait(false);
await registryManager.RemoveDeviceAsync(deviceName).ConfigureAwait(false);
}
private async Task JobClient_ScheduleAndRunTwinJob(HttpTransportSettings httpTransportSettings)
{
string jobId = "JOBSAMPLE" + Guid.NewGuid().ToString();
string query = $"DeviceId IN ['{JobDeviceId}']";
Twin twin = new Twin(JobDeviceId);
twin.Tags = new TwinCollection();
twin.Tags[JobTestTagName] = JobDeviceId;
JobClient jobClient = JobClient.CreateFromConnectionString(ConnectionString, httpTransportSettings);
JobResponse createJobResponse = await jobClient.ScheduleTwinUpdateAsync(jobId, query, twin, DateTime.UtcNow, (long)TimeSpan.FromMinutes(2).TotalSeconds).ConfigureAwait(false);
JobResponse jobResponse = await jobClient.GetJobAsync(jobId).ConfigureAwait(false);
}
private Message ComposeD2CTestMessage(out string payload, out string p1Value)
{
payload = Guid.NewGuid().ToString();
p1Value = Guid.NewGuid().ToString();
_log.WriteLine($"{nameof(ComposeD2CTestMessage)}: payload='{payload}' p1Value='{p1Value}'");
return new Message(Encoding.UTF8.GetBytes(payload))
{
Properties = { ["property1"] = p1Value }
};
}
}
}