Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugFix/#1421_-_Unable to delete device in database when the device did not exist in IoTHub #1499

Merged
merged 5 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/AzureIoTHub.Portal.Server/Jobs/SyncConcentratorsJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace AzureIoTHub.Portal.Server.Jobs
using Microsoft.Azure.Devices.Shared;
using Microsoft.Extensions.Logging;
using Quartz;
using System.Linq;

[DisallowConcurrentExecution]
public class SyncConcentratorsJob : IJob
Expand Down Expand Up @@ -56,11 +57,18 @@ public async Task Execute(IJobExecutionContext context)

private async Task SyncConcentrators()
{
foreach (var twin in await GetTwinConcentrators())
var concentratorTwins = await GetTwinConcentrators();

foreach (var twin in concentratorTwins)
{
await CreateOrUpdateConcentrator(twin);
}

foreach (var item in (await this.concentratorRepository.GetAllAsync()).Where(device => !concentratorTwins.Exists(x => x.DeviceId == device.Id)))
{
this.concentratorRepository.Delete(item.Id);
}

await this.unitOfWork.SaveAsync();
}

Expand Down
15 changes: 14 additions & 1 deletion src/AzureIoTHub.Portal.Server/Jobs/SyncDevicesJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace AzureIoTHub.Portal.Server.Jobs
using Microsoft.Azure.Devices.Shared;
using Microsoft.Extensions.Logging;
using Quartz;
using System.Linq;

[DisallowConcurrentExecution]
public class SyncDevicesJob : IJob
Expand Down Expand Up @@ -66,7 +67,9 @@ public async Task Execute(IJobExecutionContext context)

private async Task SyncDevices()
{
foreach (var twin in await GetTwinDevices())
var deviceTwins = await GetTwinDevices();

foreach (var twin in deviceTwins)
{
var deviceModel = await this.deviceModelRepository.GetByIdAsync(twin.Tags[ModelId]?.ToString() ?? string.Empty);

Expand All @@ -86,6 +89,16 @@ private async Task SyncDevices()
}
}

foreach (var item in (await this.deviceRepository.GetAllAsync()).Where(device => !deviceTwins.Exists(x => x.DeviceId == device.Id)))
{
this.deviceRepository.Delete(item.Id);
}

foreach (var item in (await this.lorawanDeviceRepository.GetAllAsync()).Where(lorawanDevice => !deviceTwins.Exists(x => x.DeviceId == lorawanDevice.Id)))
{
this.lorawanDeviceRepository.Delete(item.Id);
}

await this.unitOfWork.SaveAsync();
}

Expand Down
10 changes: 9 additions & 1 deletion src/AzureIoTHub.Portal.Server/Jobs/SyncEdgeDeviceJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace AzureIoTHub.Portal.Server.Jobs
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using AzureIoTHub.Portal.Domain;
Expand Down Expand Up @@ -67,7 +68,9 @@ public async Task Execute(IJobExecutionContext context)

private async Task SyncEdgeDevices()
{
foreach (var twin in await GetTwinDevices())
var deviceTwins = await GetTwinDevices();

foreach (var twin in deviceTwins)
{
var deviceModel = await this.edgeDeviceModelRepository.GetByIdAsync(twin.Tags[ModelId]?.ToString() ?? string.Empty);

Expand All @@ -80,6 +83,11 @@ private async Task SyncEdgeDevices()
await CreateOrUpdateDevice(twin);
}

foreach (var item in (await this.edgeDeviceRepository.GetAllAsync()).Where(edgeDevice => !deviceTwins.Exists(twin => twin.DeviceId == edgeDevice.Id)))
{
this.edgeDeviceRepository.Delete(item.Id);
}

await this.unitOfWork.SaveAsync();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

namespace AzureIoTHub.Portal.Tests.Unit.Server.Jobs
{
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using AzureIoTHub.Portal.Domain;
Expand Down Expand Up @@ -87,6 +90,21 @@ public async Task ExecuteNewConcentratorShouldCreateEntity()
_ = this.mockConcentratorRepository.Setup(repository => repository.InsertAsync(It.IsAny<Concentrator>()))
.Returns(Task.CompletedTask);

_ = this.mockConcentratorRepository.Setup(x => x.GetAllAsync(It.IsAny<Expression<Func<Concentrator, bool>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<Concentrator>
{
new Concentrator
{
Id = expectedTwinConcentrator.DeviceId
},
new Concentrator
{
Id = Guid.NewGuid().ToString()
}
});

this.mockConcentratorRepository.Setup(x => x.Delete(It.IsAny<string>())).Verifiable();

_ = this.mockUnitOfWork.Setup(work => work.SaveAsync())
.Returns(Task.CompletedTask);

Expand Down Expand Up @@ -146,6 +164,15 @@ public async Task ExecuteExistingConcentratorWithGreaterVersionShouldUpdateEntit
this.mockConcentratorRepository.Setup(repository => repository.Update(It.IsAny<Concentrator>()))
.Verifiable();

_ = this.mockConcentratorRepository.Setup(x => x.GetAllAsync(It.IsAny<Expression<Func<Concentrator, bool>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<Concentrator>
{
new Concentrator
{
Id = expectedTwinConcentrator.DeviceId
}
});

_ = this.mockUnitOfWork.Setup(work => work.SaveAsync())
.Returns(Task.CompletedTask);

Expand Down Expand Up @@ -202,6 +229,15 @@ public async Task ExecuteExistingConcentratorWithLowerOrEqualVersionShouldReturn
_ = this.mockConcentratorRepository.Setup(repository => repository.GetByIdAsync(expectedTwinConcentrator.DeviceId))
.ReturnsAsync(existingConcentrator);

_ = this.mockConcentratorRepository.Setup(x => x.GetAllAsync(It.IsAny<Expression<Func<Concentrator, bool>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<Concentrator>
{
new Concentrator
{
Id = expectedTwinConcentrator.DeviceId
}
});

_ = this.mockUnitOfWork.Setup(work => work.SaveAsync())
.Returns(Task.CompletedTask);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

namespace AzureIoTHub.Portal.Tests.Unit.Server.Jobs
{
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using AzureIoTHub.Portal.Domain;
Expand Down Expand Up @@ -103,6 +106,36 @@ public async Task Execute_NewDevice_DeviceCreated()
_ = this.mockDeviceRepository.Setup(repository => repository.InsertAsync(It.IsAny<Device>()))
.Returns(Task.CompletedTask);

_ = this.mockDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny<Expression<Func<Device, bool>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<Device>
{
new Device
{
Id = expectedTwinDevice.DeviceId
},
new Device
{
Id = Guid.NewGuid().ToString()
}
});

this.mockDeviceRepository.Setup(x => x.Delete(It.IsAny<string>())).Verifiable();

_ = this.mockLorawanDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny<Expression<Func<LorawanDevice, bool>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<LorawanDevice>
{
new LorawanDevice
{
Id = expectedTwinDevice.DeviceId
},
new LorawanDevice
{
Id = Guid.NewGuid().ToString()
}
});

this.mockLorawanDeviceRepository.Setup(x => x.Delete(It.IsAny<string>())).Verifiable();

_ = this.mockUnitOfWork.Setup(work => work.SaveAsync())
.Returns(Task.CompletedTask);

Expand Down Expand Up @@ -181,6 +214,24 @@ public async Task Execute_ExistingDeviceWithGreeterVersion_DeviceUpdated()
this.mockDeviceRepository.Setup(repository => repository.Update(It.IsAny<Device>()))
.Verifiable();

_ = this.mockDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny<Expression<Func<Device, bool>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<Device>
{
new Device
{
Id = expectedTwinDevice.DeviceId
}
});

_ = this.mockLorawanDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny<Expression<Func<LorawanDevice, bool>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<LorawanDevice>
{
new LorawanDevice
{
Id = expectedTwinDevice.DeviceId
}
});

_ = this.mockUnitOfWork.Setup(work => work.SaveAsync())
.Returns(Task.CompletedTask);

Expand Down Expand Up @@ -240,6 +291,36 @@ public async Task Execute_NewLorawanDevice_LorawanDeviceCreated()
_ = this.mockLorawanDeviceRepository.Setup(repository => repository.InsertAsync(It.IsAny<LorawanDevice>()))
.Returns(Task.CompletedTask);

_ = this.mockDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny<Expression<Func<Device, bool>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<Device>
{
new Device
{
Id = expectedTwinDevice.DeviceId
},
new Device
{
Id = Guid.NewGuid().ToString()
}
});

this.mockDeviceRepository.Setup(x => x.Delete(It.IsAny<string>())).Verifiable();

_ = this.mockLorawanDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny<Expression<Func<LorawanDevice, bool>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<LorawanDevice>
{
new LorawanDevice
{
Id = expectedTwinDevice.DeviceId
},
new LorawanDevice
{
Id = Guid.NewGuid().ToString()
}
});

this.mockLorawanDeviceRepository.Setup(x => x.Delete(It.IsAny<string>())).Verifiable();

_ = this.mockUnitOfWork.Setup(work => work.SaveAsync())
.Returns(Task.CompletedTask);

Expand Down Expand Up @@ -318,6 +399,24 @@ public async Task Execute_ExistingLorawanDeviceWithGreeterVersion_LorawanDeviceU
this.mockLorawanDeviceRepository.Setup(repository => repository.Update(It.IsAny<LorawanDevice>()))
.Verifiable();

_ = this.mockDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny<Expression<Func<Device, bool>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<Device>
{
new Device
{
Id = expectedTwinDevice.DeviceId
}
});

_ = this.mockLorawanDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny<Expression<Func<LorawanDevice, bool>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<LorawanDevice>
{
new LorawanDevice
{
Id = expectedTwinDevice.DeviceId
}
});

_ = this.mockUnitOfWork.Setup(work => work.SaveAsync())
.Returns(Task.CompletedTask);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

namespace AzureIoTHub.Portal.Tests.Unit.Server.Jobs
{
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using AzureIoTHub.Portal.Domain;
Expand Down Expand Up @@ -110,6 +113,21 @@ public async Task ExecuteNewEdgeDeviceDeviceCreated()
_ = this.mockEdgeDeviceRepository.Setup(x => x.InsertAsync(It.IsAny<EdgeDevice>()))
.Returns(Task.CompletedTask);

_ = this.mockEdgeDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny<Expression<Func<EdgeDevice, bool>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<EdgeDevice>
{
new EdgeDevice
{
Id = expectedTwinDevice.DeviceId
},
new EdgeDevice
{
Id = Guid.NewGuid().ToString()
}
});

this.mockEdgeDeviceRepository.Setup(x => x.Delete(It.IsAny<string>())).Verifiable();

_ = this.mockUnitOfWork.Setup(work => work.SaveAsync())
.Returns(Task.CompletedTask);

Expand Down Expand Up @@ -192,6 +210,15 @@ public async Task ExecuteExistingEdgeDeviceWithGreeterVersionDeviceUpdated()
this.mockEdgeDeviceRepository.Setup(repository => repository.Update(It.IsAny<EdgeDevice>()))
.Verifiable();

_ = this.mockEdgeDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny<Expression<Func<EdgeDevice, bool>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<EdgeDevice>
{
new EdgeDevice
{
Id = expectedTwinDevice.DeviceId
}
});

_ = this.mockUnitOfWork.Setup(work => work.SaveAsync())
.Returns(Task.CompletedTask);

Expand Down