Skip to content

Commit

Permalink
Add suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mandur committed Oct 30, 2023
1 parent 60594dc commit 48b26d7
Showing 1 changed file with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,27 @@ public class DeviceProvisioningTest
private readonly Mock<RegistryManager> registryManager;

private const string NetworkName = "quickstartnetwork";
private readonly string devEUI = GetRandomHexNumber(16);
private static readonly string DevEUI = GetRandomHexNumber(16);
private const string Decoder = "DecoderValueSensor";
private const string LoRaVersion = "999.999.10"; // using an non-existing version to ensure it is not hardcoded with a valid value
private const string IotEdgeVersion = "1.4";

// OTAA Properties
private readonly string appKey = GetRandomHexNumber(32);
private readonly string appEui = GetRandomHexNumber(16);
private static readonly string AppKey = GetRandomHexNumber(32);
private static readonly string AppEui = GetRandomHexNumber(16);

// ABP properties
private readonly string appSKey = GetRandomHexNumber(32);
private readonly string nwkSKey = GetRandomHexNumber(32);
private static readonly string AppSKey = GetRandomHexNumber(32);
private static readonly string NwkSKey = GetRandomHexNumber(32);
private const string DevAddr = "027AEC7B";
private static readonly Random Random = new Random();
private static Random random = new Random();
public static string GetRandomHexNumber(int digits)
{
return string.Concat(Enumerable.Range(0, digits).Select(_ => Random.Next(16).ToString("X", CultureInfo.InvariantCulture)));
if(random == null)
{
random =new Random();
}
return string.Concat(Enumerable.Range(0, digits).Select(_ => random.Next(16).ToString("X", CultureInfo.InvariantCulture)));
}
public DeviceProvisioningTest()
{
Expand Down Expand Up @@ -68,14 +72,14 @@ public async Task AddABPDevice(bool deviceExistsInRegistry)
var savedTwin = new Twin();

this.registryManager.Setup(c => c.AddDeviceWithTwinAsync(
It.Is<Device>(d => d.Id == devEUI.ToString()),
It.Is<Device>(d => d.Id == DevEUI.ToString()),
It.IsNotNull<Twin>()))
.Callback((Device d, Twin t) =>
{
Assert.Equal(NetworkName, t.Tags[DeviceTags.NetworkTagName].ToString());
Assert.Equal(new string[] { DeviceTags.DeviceTypes.Leaf }, ((JArray)t.Tags[DeviceTags.DeviceTypeTagName]).Select(x => x.ToString()).ToArray());
Assert.Equal(appSKey, t.Properties.Desired[TwinProperty.AppSKey].ToString());
Assert.Equal(nwkSKey, t.Properties.Desired[TwinProperty.NwkSKey].ToString());
Assert.Equal(AppSKey, t.Properties.Desired[TwinProperty.AppSKey].ToString());
Assert.Equal(NwkSKey, t.Properties.Desired[TwinProperty.NwkSKey].ToString());
Assert.Equal(DevAddr, t.Properties.Desired[TwinProperty.DevAddr].ToString());
Assert.Equal(Decoder, t.Properties.Desired[TwinProperty.SensorDecoder].ToString());
Assert.Equal(string.Empty, t.Properties.Desired[TwinProperty.GatewayID].ToString());
Expand All @@ -89,17 +93,17 @@ public async Task AddABPDevice(bool deviceExistsInRegistry)
// If the device exists in registry we expect the getTwin to return the device.
if (deviceExistsInRegistry)
{
this.registryManager.Setup(x => x.GetTwinAsync(devEUI)).ReturnsAsync(savedTwin);
this.registryManager.Setup(x => x.GetTwinAsync(DevEUI)).ReturnsAsync(savedTwin);
}
else
{
this.registryManager.SetupSequence(x => x.GetTwinAsync(devEUI))
this.registryManager.SetupSequence(x => x.GetTwinAsync(DevEUI))
.ReturnsAsync((Twin?)null)
.ReturnsAsync(savedTwin);
}

// Act
var args = CreateArgs($"add --type abp --deveui {devEUI} --appskey {appSKey} --nwkskey {nwkSKey} --devaddr {DevAddr} --decoder {Decoder} --network {NetworkName}");
var args = CreateArgs($"add --type abp --deveui {DevEUI} --appskey {AppSKey} --nwkskey {NwkSKey} --devaddr {DevAddr} --decoder {Decoder} --network {NetworkName}");
var actual = await Program.Run(args, this.configurationHelper);

// Assert
Expand All @@ -108,7 +112,7 @@ public async Task AddABPDevice(bool deviceExistsInRegistry)
if (deviceExistsInRegistry)
{
this.registryManager.Verify(c => c.UpdateTwinAsync(
devEUI.ToString(),
DevEUI.ToString(),
It.IsNotNull<Twin>(),
It.IsAny<string>()), Times.Once());
this.registryManager.Verify(c => c.AddDeviceWithTwinAsync(
Expand All @@ -118,7 +122,7 @@ public async Task AddABPDevice(bool deviceExistsInRegistry)
else
{
this.registryManager.Verify(c => c.AddDeviceWithTwinAsync(
It.Is<Device>(d => d.Id == devEUI.ToString()),
It.Is<Device>(d => d.Id == DevEUI.ToString()),
It.IsNotNull<Twin>()), Times.Once());
}
}
Expand All @@ -132,14 +136,14 @@ public async Task AddOTAADevice(bool deviceExistsInRegistry)
var savedTwin = new Twin();

this.registryManager.Setup(c => c.AddDeviceWithTwinAsync(
It.Is<Device>(d => d.Id == devEUI.ToString()),
It.Is<Device>(d => d.Id == DevEUI.ToString()),
It.IsNotNull<Twin>()))
.Callback((Device d, Twin t) =>
{
Assert.Equal(NetworkName, t.Tags[DeviceTags.NetworkTagName].ToString());
Assert.Equal(new string[] { DeviceTags.DeviceTypes.Leaf }, ((JArray)t.Tags[DeviceTags.DeviceTypeTagName]).Select(x => x.ToString()).ToArray());
Assert.Equal(appKey, t.Properties.Desired[TwinProperty.AppKey].ToString());
Assert.Equal(appEui, t.Properties.Desired[TwinProperty.AppEUI].ToString());
Assert.Equal(AppKey, t.Properties.Desired[TwinProperty.AppKey].ToString());
Assert.Equal(AppEui, t.Properties.Desired[TwinProperty.AppEUI].ToString());
Assert.Equal(Decoder, t.Properties.Desired[TwinProperty.SensorDecoder].ToString());
Assert.Equal(string.Empty, t.Properties.Desired[TwinProperty.GatewayID].ToString());
savedTwin = t;
Expand All @@ -152,25 +156,25 @@ public async Task AddOTAADevice(bool deviceExistsInRegistry)
// If the device exists in registry we expect the getTwin to return the device.
if (deviceExistsInRegistry)
{
this.registryManager.Setup(x => x.GetTwinAsync(devEUI)).ReturnsAsync(savedTwin);
this.registryManager.Setup(x => x.GetTwinAsync(DevEUI)).ReturnsAsync(savedTwin);
}
else
{
this.registryManager.SetupSequence(x => x.GetTwinAsync(devEUI))
this.registryManager.SetupSequence(x => x.GetTwinAsync(DevEUI))
.ReturnsAsync((Twin?)null)
.ReturnsAsync(savedTwin);
}

// Act
var args = CreateArgs($"add --type otaa --deveui {devEUI} --appeui {appEui} --appkey {appKey} --decoder {Decoder} --network {NetworkName}");
var args = CreateArgs($"add --type otaa --deveui {DevEUI} --appeui {AppEui} --appkey {AppKey} --decoder {Decoder} --network {NetworkName}");
var actual = await Program.Run(args, this.configurationHelper);

// Assert
Assert.Equal(0, actual);
if (deviceExistsInRegistry)
{
this.registryManager.Verify(c => c.UpdateTwinAsync(
devEUI.ToString(),
DevEUI.ToString(),
It.IsNotNull<Twin>(),
It.IsAny<string>()), Times.Once());
this.registryManager.Verify(c => c.AddDeviceWithTwinAsync(
Expand All @@ -180,7 +184,7 @@ public async Task AddOTAADevice(bool deviceExistsInRegistry)
else
{
this.registryManager.Verify(c => c.AddDeviceWithTwinAsync(
It.Is<Device>(d => d.Id == devEUI.ToString()),
It.Is<Device>(d => d.Id == DevEUI.ToString()),
It.IsNotNull<Twin>()), Times.Once());
}
}
Expand All @@ -190,21 +194,21 @@ public async Task AddOTAADevice(bool deviceExistsInRegistry)
public async Task WhenBulkOperationFailed_AddDevice_Should_Return_False()
{
// Arrange
this.registryManager.Setup(c => c.AddDeviceWithTwinAsync(It.Is<Device>(d => d.Id == devEUI.ToString()), It.IsNotNull<Twin>()))
this.registryManager.Setup(c => c.AddDeviceWithTwinAsync(It.Is<Device>(d => d.Id == DevEUI.ToString()), It.IsNotNull<Twin>()))
.ReturnsAsync(new BulkRegistryOperationResult
{
IsSuccessful = false
});

// Act
var args = CreateArgs($"add --type otaa --deveui {devEUI} --appeui 8AFE71A145B253E49C3031AD068277A1 --appkey BE7A0000000014E2 --decoder MyDecoder --network myNetwork");
var args = CreateArgs($"add --type otaa --deveui {DevEUI} --appeui 8AFE71A145B253E49C3031AD068277A1 --appkey BE7A0000000014E2 --decoder MyDecoder --network myNetwork");
var actual = await Program.Run(args, this.configurationHelper);

// Assert
Assert.Equal(-1, actual);
}

[Theory]
[Theory]
[InlineData("2", "1", "3")]
[InlineData("2", "1", "3", "fakeNetworkId")]
[InlineData("2", "1", "3", "fakeNetworkId", "ws://fakelns:5000")]
Expand Down

0 comments on commit 48b26d7

Please sign in to comment.