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 4 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/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 @@ -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