From 490fc855cfd2ce2038576ed18018faee900f4c09 Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 2 Nov 2022 10:53:36 +0100 Subject: [PATCH 1/5] resolve #1421 --- .../Jobs/SyncEdgeDeviceJob.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/AzureIoTHub.Portal.Server/Jobs/SyncEdgeDeviceJob.cs b/src/AzureIoTHub.Portal.Server/Jobs/SyncEdgeDeviceJob.cs index 8765e8d9a..6bb8828d5 100644 --- a/src/AzureIoTHub.Portal.Server/Jobs/SyncEdgeDeviceJob.cs +++ b/src/AzureIoTHub.Portal.Server/Jobs/SyncEdgeDeviceJob.cs @@ -67,7 +67,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); @@ -80,6 +82,14 @@ private async Task SyncEdgeDevices() await CreateOrUpdateDevice(twin); } + foreach (var item in this.edgeDeviceRepository.GetAll()) + { + if (!deviceTwins.Exists(x => x.DeviceId == item.Id)) + { + this.edgeDeviceRepository.Delete(item.Id); + } + } + await this.unitOfWork.SaveAsync(); } From 4bc310926778baf4585dda24031bc5ac450ed690 Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 2 Nov 2022 11:12:18 +0100 Subject: [PATCH 2/5] fix test --- .../Jobs/SyncEdgeDeviceJob.cs | 2 +- .../Server/Jobs/SyncEdgeDeviceJobTest.cs | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/AzureIoTHub.Portal.Server/Jobs/SyncEdgeDeviceJob.cs b/src/AzureIoTHub.Portal.Server/Jobs/SyncEdgeDeviceJob.cs index 6bb8828d5..318e3797c 100644 --- a/src/AzureIoTHub.Portal.Server/Jobs/SyncEdgeDeviceJob.cs +++ b/src/AzureIoTHub.Portal.Server/Jobs/SyncEdgeDeviceJob.cs @@ -82,7 +82,7 @@ private async Task SyncEdgeDevices() await CreateOrUpdateDevice(twin); } - foreach (var item in this.edgeDeviceRepository.GetAll()) + foreach (var item in await this.edgeDeviceRepository.GetAllAsync()) { if (!deviceTwins.Exists(x => x.DeviceId == item.Id)) { diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Jobs/SyncEdgeDeviceJobTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Jobs/SyncEdgeDeviceJobTest.cs index 065131c91..28dc09ebf 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Jobs/SyncEdgeDeviceJobTest.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Jobs/SyncEdgeDeviceJobTest.cs @@ -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; @@ -110,6 +113,15 @@ public async Task ExecuteNewEdgeDeviceDeviceCreated() _ = this.mockEdgeDeviceRepository.Setup(x => x.InsertAsync(It.IsAny())) .Returns(Task.CompletedTask); + _ = this.mockEdgeDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny>>(), It.IsAny())) + .ReturnsAsync(new List + { + new EdgeDevice + { + Id = expectedTwinDevice.DeviceId + } + }); + _ = this.mockUnitOfWork.Setup(work => work.SaveAsync()) .Returns(Task.CompletedTask); @@ -192,6 +204,15 @@ public async Task ExecuteExistingEdgeDeviceWithGreeterVersionDeviceUpdated() this.mockEdgeDeviceRepository.Setup(repository => repository.Update(It.IsAny())) .Verifiable(); + _ = this.mockEdgeDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny>>(), It.IsAny())) + .ReturnsAsync(new List + { + new EdgeDevice + { + Id = expectedTwinDevice.DeviceId + } + }); + _ = this.mockUnitOfWork.Setup(work => work.SaveAsync()) .Returns(Task.CompletedTask); From d00a8917637c6c2a839b71f7a68a03e1363b6fcf Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 2 Nov 2022 11:37:10 +0100 Subject: [PATCH 3/5] update syncEdgeDevice test --- src/AzureIoTHub.Portal.Server/Jobs/SyncEdgeDeviceJob.cs | 9 ++++----- .../Server/Jobs/SyncEdgeDeviceJobTest.cs | 6 ++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/AzureIoTHub.Portal.Server/Jobs/SyncEdgeDeviceJob.cs b/src/AzureIoTHub.Portal.Server/Jobs/SyncEdgeDeviceJob.cs index 318e3797c..1bbb6dd68 100644 --- a/src/AzureIoTHub.Portal.Server/Jobs/SyncEdgeDeviceJob.cs +++ b/src/AzureIoTHub.Portal.Server/Jobs/SyncEdgeDeviceJob.cs @@ -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; @@ -14,6 +15,7 @@ namespace AzureIoTHub.Portal.Server.Jobs using Microsoft.Azure.Devices.Shared; using Microsoft.Extensions.Logging; using Quartz; + using static MudBlazor.CategoryTypes; [DisallowConcurrentExecution] public class SyncEdgeDeviceJob : IJob @@ -82,12 +84,9 @@ private async Task SyncEdgeDevices() await CreateOrUpdateDevice(twin); } - foreach (var item in await this.edgeDeviceRepository.GetAllAsync()) + foreach (var item in (await this.edgeDeviceRepository.GetAllAsync()).Where(edgeDevice => !deviceTwins.Exists(twin => twin.DeviceId == edgeDevice.Id))) { - if (!deviceTwins.Exists(x => x.DeviceId == item.Id)) - { - this.edgeDeviceRepository.Delete(item.Id); - } + this.edgeDeviceRepository.Delete(item.Id); } await this.unitOfWork.SaveAsync(); diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Jobs/SyncEdgeDeviceJobTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Jobs/SyncEdgeDeviceJobTest.cs index 28dc09ebf..800e25307 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Jobs/SyncEdgeDeviceJobTest.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Jobs/SyncEdgeDeviceJobTest.cs @@ -119,9 +119,15 @@ public async Task ExecuteNewEdgeDeviceDeviceCreated() new EdgeDevice { Id = expectedTwinDevice.DeviceId + }, + new EdgeDevice + { + Id = Guid.NewGuid().ToString() } }); + this.mockEdgeDeviceRepository.Setup(x => x.Delete(It.IsAny())).Verifiable(); + _ = this.mockUnitOfWork.Setup(work => work.SaveAsync()) .Returns(Task.CompletedTask); From 83f4900ac2746c2efe2af398cd813aa344fa65f8 Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 2 Nov 2022 11:41:16 +0100 Subject: [PATCH 4/5] delete useless using --- src/AzureIoTHub.Portal.Server/Jobs/SyncEdgeDeviceJob.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/AzureIoTHub.Portal.Server/Jobs/SyncEdgeDeviceJob.cs b/src/AzureIoTHub.Portal.Server/Jobs/SyncEdgeDeviceJob.cs index 1bbb6dd68..c746c08a0 100644 --- a/src/AzureIoTHub.Portal.Server/Jobs/SyncEdgeDeviceJob.cs +++ b/src/AzureIoTHub.Portal.Server/Jobs/SyncEdgeDeviceJob.cs @@ -15,7 +15,6 @@ namespace AzureIoTHub.Portal.Server.Jobs using Microsoft.Azure.Devices.Shared; using Microsoft.Extensions.Logging; using Quartz; - using static MudBlazor.CategoryTypes; [DisallowConcurrentExecution] public class SyncEdgeDeviceJob : IJob From 6d6c0c20b2a0cbf428e01f2ed1261e1cd6671fb1 Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 2 Nov 2022 14:28:51 +0100 Subject: [PATCH 5/5] feature delete useless device in db for device, lorawan device and concentrator --- .../Jobs/SyncConcentratorsJob.cs | 10 +- .../Jobs/SyncDevicesJob.cs | 15 ++- .../Server/Jobs/SyncConcentratorsJobTests.cs | 36 +++++++ .../Server/Jobs/SyncDevicesJobTests.cs | 99 +++++++++++++++++++ 4 files changed, 158 insertions(+), 2 deletions(-) diff --git a/src/AzureIoTHub.Portal.Server/Jobs/SyncConcentratorsJob.cs b/src/AzureIoTHub.Portal.Server/Jobs/SyncConcentratorsJob.cs index a8d116c93..0aeafa403 100644 --- a/src/AzureIoTHub.Portal.Server/Jobs/SyncConcentratorsJob.cs +++ b/src/AzureIoTHub.Portal.Server/Jobs/SyncConcentratorsJob.cs @@ -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 @@ -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(); } diff --git a/src/AzureIoTHub.Portal.Server/Jobs/SyncDevicesJob.cs b/src/AzureIoTHub.Portal.Server/Jobs/SyncDevicesJob.cs index 2bcac14d4..e30342038 100644 --- a/src/AzureIoTHub.Portal.Server/Jobs/SyncDevicesJob.cs +++ b/src/AzureIoTHub.Portal.Server/Jobs/SyncDevicesJob.cs @@ -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 @@ -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); @@ -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(); } diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Jobs/SyncConcentratorsJobTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Jobs/SyncConcentratorsJobTests.cs index a19f81b66..8f984cbe9 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Jobs/SyncConcentratorsJobTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Jobs/SyncConcentratorsJobTests.cs @@ -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; @@ -87,6 +90,21 @@ public async Task ExecuteNewConcentratorShouldCreateEntity() _ = this.mockConcentratorRepository.Setup(repository => repository.InsertAsync(It.IsAny())) .Returns(Task.CompletedTask); + _ = this.mockConcentratorRepository.Setup(x => x.GetAllAsync(It.IsAny>>(), It.IsAny())) + .ReturnsAsync(new List + { + new Concentrator + { + Id = expectedTwinConcentrator.DeviceId + }, + new Concentrator + { + Id = Guid.NewGuid().ToString() + } + }); + + this.mockConcentratorRepository.Setup(x => x.Delete(It.IsAny())).Verifiable(); + _ = this.mockUnitOfWork.Setup(work => work.SaveAsync()) .Returns(Task.CompletedTask); @@ -146,6 +164,15 @@ public async Task ExecuteExistingConcentratorWithGreaterVersionShouldUpdateEntit this.mockConcentratorRepository.Setup(repository => repository.Update(It.IsAny())) .Verifiable(); + _ = this.mockConcentratorRepository.Setup(x => x.GetAllAsync(It.IsAny>>(), It.IsAny())) + .ReturnsAsync(new List + { + new Concentrator + { + Id = expectedTwinConcentrator.DeviceId + } + }); + _ = this.mockUnitOfWork.Setup(work => work.SaveAsync()) .Returns(Task.CompletedTask); @@ -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>>(), It.IsAny())) + .ReturnsAsync(new List + { + new Concentrator + { + Id = expectedTwinConcentrator.DeviceId + } + }); + _ = this.mockUnitOfWork.Setup(work => work.SaveAsync()) .Returns(Task.CompletedTask); diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Jobs/SyncDevicesJobTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Jobs/SyncDevicesJobTests.cs index 8d47d9203..a0db997a9 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Jobs/SyncDevicesJobTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Jobs/SyncDevicesJobTests.cs @@ -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; @@ -103,6 +106,36 @@ public async Task Execute_NewDevice_DeviceCreated() _ = this.mockDeviceRepository.Setup(repository => repository.InsertAsync(It.IsAny())) .Returns(Task.CompletedTask); + _ = this.mockDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny>>(), It.IsAny())) + .ReturnsAsync(new List + { + new Device + { + Id = expectedTwinDevice.DeviceId + }, + new Device + { + Id = Guid.NewGuid().ToString() + } + }); + + this.mockDeviceRepository.Setup(x => x.Delete(It.IsAny())).Verifiable(); + + _ = this.mockLorawanDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny>>(), It.IsAny())) + .ReturnsAsync(new List + { + new LorawanDevice + { + Id = expectedTwinDevice.DeviceId + }, + new LorawanDevice + { + Id = Guid.NewGuid().ToString() + } + }); + + this.mockLorawanDeviceRepository.Setup(x => x.Delete(It.IsAny())).Verifiable(); + _ = this.mockUnitOfWork.Setup(work => work.SaveAsync()) .Returns(Task.CompletedTask); @@ -181,6 +214,24 @@ public async Task Execute_ExistingDeviceWithGreeterVersion_DeviceUpdated() this.mockDeviceRepository.Setup(repository => repository.Update(It.IsAny())) .Verifiable(); + _ = this.mockDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny>>(), It.IsAny())) + .ReturnsAsync(new List + { + new Device + { + Id = expectedTwinDevice.DeviceId + } + }); + + _ = this.mockLorawanDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny>>(), It.IsAny())) + .ReturnsAsync(new List + { + new LorawanDevice + { + Id = expectedTwinDevice.DeviceId + } + }); + _ = this.mockUnitOfWork.Setup(work => work.SaveAsync()) .Returns(Task.CompletedTask); @@ -240,6 +291,36 @@ public async Task Execute_NewLorawanDevice_LorawanDeviceCreated() _ = this.mockLorawanDeviceRepository.Setup(repository => repository.InsertAsync(It.IsAny())) .Returns(Task.CompletedTask); + _ = this.mockDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny>>(), It.IsAny())) + .ReturnsAsync(new List + { + new Device + { + Id = expectedTwinDevice.DeviceId + }, + new Device + { + Id = Guid.NewGuid().ToString() + } + }); + + this.mockDeviceRepository.Setup(x => x.Delete(It.IsAny())).Verifiable(); + + _ = this.mockLorawanDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny>>(), It.IsAny())) + .ReturnsAsync(new List + { + new LorawanDevice + { + Id = expectedTwinDevice.DeviceId + }, + new LorawanDevice + { + Id = Guid.NewGuid().ToString() + } + }); + + this.mockLorawanDeviceRepository.Setup(x => x.Delete(It.IsAny())).Verifiable(); + _ = this.mockUnitOfWork.Setup(work => work.SaveAsync()) .Returns(Task.CompletedTask); @@ -318,6 +399,24 @@ public async Task Execute_ExistingLorawanDeviceWithGreeterVersion_LorawanDeviceU this.mockLorawanDeviceRepository.Setup(repository => repository.Update(It.IsAny())) .Verifiable(); + _ = this.mockDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny>>(), It.IsAny())) + .ReturnsAsync(new List + { + new Device + { + Id = expectedTwinDevice.DeviceId + } + }); + + _ = this.mockLorawanDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny>>(), It.IsAny())) + .ReturnsAsync(new List + { + new LorawanDevice + { + Id = expectedTwinDevice.DeviceId + } + }); + _ = this.mockUnitOfWork.Setup(work => work.SaveAsync()) .Returns(Task.CompletedTask);