diff --git a/src/IoTHub.Portal.Infrastructure/Jobs/AWS/SyncThingTypesJob.cs b/src/IoTHub.Portal.Infrastructure/Jobs/AWS/SyncThingTypesJob.cs index df0327c3b..984a167f6 100644 --- a/src/IoTHub.Portal.Infrastructure/Jobs/AWS/SyncThingTypesJob.cs +++ b/src/IoTHub.Portal.Infrastructure/Jobs/AWS/SyncThingTypesJob.cs @@ -87,6 +87,29 @@ private async Task SyncThingTypesAsDeviceModels() await DeleteThingTypes(thingTypes); } + + private async Task> Remove5mnDeprecatedThingTypes(List thingTypes) + { + var awsThingTypes = new List< DescribeThingTypeResponse>(); + + foreach (var thingType in thingTypes) + { + var diffInMinutes = (DateTime.Now.Subtract(thingType.ThingTypeMetadata.DeprecationDate)).TotalMinutes; + if (thingType.ThingTypeMetadata.Deprecated && diffInMinutes > 5) + { + _ = await this.amazonIoTClient.DeleteThingTypeAsync(new DeleteThingTypeRequest + { + ThingTypeName = thingType.ThingTypeName + }); + } + else + { + awsThingTypes.Add(thingType); + } + } + + return awsThingTypes; + } private async Task> GetAllThingTypes() { var thingTypes = new List(); @@ -116,6 +139,8 @@ private async Task> GetAllThingTypes() } while (!string.IsNullOrEmpty(nextToken)); + thingTypes = await Remove5mnDeprecatedThingTypes(thingTypes); + return thingTypes; } diff --git a/src/IoTHub.Portal.Tests.Unit/Infrastructure/Jobs/AWS/SyncThingTypesJobTests.cs b/src/IoTHub.Portal.Tests.Unit/Infrastructure/Jobs/AWS/SyncThingTypesJobTests.cs index fc190008b..7a2255ab6 100644 --- a/src/IoTHub.Portal.Tests.Unit/Infrastructure/Jobs/AWS/SyncThingTypesJobTests.cs +++ b/src/IoTHub.Portal.Tests.Unit/Infrastructure/Jobs/AWS/SyncThingTypesJobTests.cs @@ -115,15 +115,20 @@ public async Task Execute_SyncNewAndExistingAndDepprecatedThingTypes_DeviceModel ThingTypeMetadata = new ThingTypeMetadata { Deprecated = true, + DeprecationDate = new DateTime(2023, 6, 11, 10, 30, 0) } }; + + _ = this.iaAmazon.Setup(client => client.DescribeThingTypeAsync(It.Is(c => c.ThingTypeName == existingThingType.ThingTypeName), It.IsAny())) - .ReturnsAsync(existingThingType); + .ReturnsAsync(existingThingType); _ = this.iaAmazon.Setup(client => client.DescribeThingTypeAsync(It.Is(c => c.ThingTypeName == newThingType.ThingTypeName), It.IsAny())) - .ReturnsAsync(newThingType); + .ReturnsAsync(newThingType); _ = this.iaAmazon.Setup(client => client.DescribeThingTypeAsync(It.Is(c => c.ThingTypeName == depcrecatedThingType.ThingTypeName), It.IsAny())) .ReturnsAsync(depcrecatedThingType); + _ = this.iaAmazon.Setup(client => client.DeleteThingTypeAsync(It.Is(c => c.ThingTypeName == depcrecatedThingType.ThingTypeName), It.IsAny())) + .ReturnsAsync(Fixture.Create); _ = this.mockExternalDeviceService.Setup(client => client.IsEdgeDeviceModel(It.IsAny())) .ReturnsAsync(false); diff --git a/src/IoTHub.Portal.Tests.Unit/Infrastructure/Services/AWS_Tests/AwsConfigTests.cs b/src/IoTHub.Portal.Tests.Unit/Infrastructure/Services/AWS_Tests/AwsConfigTests.cs index c28364709..cd1a8c297 100644 --- a/src/IoTHub.Portal.Tests.Unit/Infrastructure/Services/AWS_Tests/AwsConfigTests.cs +++ b/src/IoTHub.Portal.Tests.Unit/Infrastructure/Services/AWS_Tests/AwsConfigTests.cs @@ -3,33 +3,32 @@ namespace IoTHub.Portal.Tests.Unit.Infrastructure.Services.AWS_Tests { - using Amazon.GreengrassV2; - using IoTHub.Portal.Application.Managers; - using IoTHub.Portal.Domain.Repositories; - using IoTHub.Portal.Domain; - using IoTHub.Portal.Tests.Unit.UnitTests.Bases; - using NUnit.Framework; - using IoTHub.Portal.Application.Services; - using AutoMapper; - using IoTHub.Portal.Infrastructure.Services.AWS; - using Microsoft.Extensions.DependencyInjection; - using Moq; + using System.Collections.Generic; + using System.IO; + using System.Linq; using System.Net; - using Amazon.GreengrassV2.Model; + using System.Text; using System.Threading; - using IoTHub.Portal.Models.v10; - using AutoFixture; using System.Threading.Tasks; + using Amazon.GreengrassV2; + using Amazon.GreengrassV2.Model; using Amazon.IoT; using Amazon.IoT.Model; + using AutoFixture; + using AutoMapper; + using FluentAssertions; + using IoTHub.Portal.Application.Managers; + using IoTHub.Portal.Application.Services; + using IoTHub.Portal.Domain; using IoTHub.Portal.Domain.Entities; - using System.Collections.Generic; - using System.IO; - using System.Text; + using IoTHub.Portal.Domain.Repositories; + using IoTHub.Portal.Infrastructure.Services.AWS; + using IoTHub.Portal.Models.v10; + using IoTHub.Portal.Tests.Unit.UnitTests.Bases; + using Microsoft.Extensions.DependencyInjection; + using Moq; using Newtonsoft.Json.Linq; - using System.Linq; - using FluentAssertions; - using System; + using NUnit.Framework; [TestFixture] public class AwsConfigTests : BackendUnitTest