From 8005ccf9e9ef5b2d12b9778437d12a1bd9200858 Mon Sep 17 00:00:00 2001 From: Kevin BEAUGRAND Date: Thu, 24 Nov 2022 22:37:52 +0100 Subject: [PATCH] Fix #1474 - remove unused mappers --- .../Factories/TableClientFactory.cs | 1 - .../Entities/EdgeModuleCommand.cs | 16 -- .../Entities/EntityBase.cs | 17 -- .../Mappers/DeviceTagMapper.cs | 45 ------ .../Mappers/EdgeModelMapper.cs | 78 --------- .../Mappers/IEdgeDeviceModelMapper.cs | 19 --- src/AzureIoTHub.Portal.Server/Startup.cs | 2 - .../Server/Mappers/DeviceTagMapperTests.cs | 78 --------- .../Server/Mappers/EdgeModelMapperTest.cs | 149 ------------------ 9 files changed, 405 deletions(-) delete mode 100644 src/AzureIoTHub.Portal.Server/Entities/EdgeModuleCommand.cs delete mode 100644 src/AzureIoTHub.Portal.Server/Entities/EntityBase.cs delete mode 100644 src/AzureIoTHub.Portal.Server/Mappers/DeviceTagMapper.cs delete mode 100644 src/AzureIoTHub.Portal.Server/Mappers/EdgeModelMapper.cs delete mode 100644 src/AzureIoTHub.Portal.Server/Mappers/IEdgeDeviceModelMapper.cs delete mode 100644 src/AzureIoTHub.Portal.Tests.Unit/Server/Mappers/DeviceTagMapperTests.cs delete mode 100644 src/AzureIoTHub.Portal.Tests.Unit/Server/Mappers/EdgeModelMapperTest.cs diff --git a/src/AzureIoTHub.Portal.Infrastructure/Factories/TableClientFactory.cs b/src/AzureIoTHub.Portal.Infrastructure/Factories/TableClientFactory.cs index 0a0d7ac7e..39d9e87dc 100644 --- a/src/AzureIoTHub.Portal.Infrastructure/Factories/TableClientFactory.cs +++ b/src/AzureIoTHub.Portal.Infrastructure/Factories/TableClientFactory.cs @@ -4,7 +4,6 @@ namespace AzureIoTHub.Portal.Infrastructure.Factories { using Azure.Data.Tables; - using AzureIoTHub.Portal.Domain; public class TableClientFactory : ITableClientFactory { diff --git a/src/AzureIoTHub.Portal.Server/Entities/EdgeModuleCommand.cs b/src/AzureIoTHub.Portal.Server/Entities/EdgeModuleCommand.cs deleted file mode 100644 index 8129f855d..000000000 --- a/src/AzureIoTHub.Portal.Server/Entities/EdgeModuleCommand.cs +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright (c) CGI France. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace AzureIoTHub.Portal.Server.Entities -{ - /// - /// Represents an edge module command. - /// - public class EdgeModuleCommand : EntityBase - { - /// - /// The property name - /// - public string Name { get; set; } - } -} diff --git a/src/AzureIoTHub.Portal.Server/Entities/EntityBase.cs b/src/AzureIoTHub.Portal.Server/Entities/EntityBase.cs deleted file mode 100644 index 4c05aee3c..000000000 --- a/src/AzureIoTHub.Portal.Server/Entities/EntityBase.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) CGI France. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace AzureIoTHub.Portal.Server.Entities -{ - using Azure.Data.Tables; - using Azure; - using System; - - public abstract class EntityBase : ITableEntity - { - public string PartitionKey { get; set; } - public string RowKey { get; set; } - public DateTimeOffset? Timestamp { get; set; } - public ETag ETag { get; set; } - } -} diff --git a/src/AzureIoTHub.Portal.Server/Mappers/DeviceTagMapper.cs b/src/AzureIoTHub.Portal.Server/Mappers/DeviceTagMapper.cs deleted file mode 100644 index 0ac0f4974..000000000 --- a/src/AzureIoTHub.Portal.Server/Mappers/DeviceTagMapper.cs +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) CGI France. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace AzureIoTHub.Portal.Server.Mappers -{ - using System; - using Azure.Data.Tables; - using AzureIoTHub.Portal.Models.v10; - - public class DeviceTagMapper : IDeviceTagMapper - { - /// - /// Gets a device tag setting. - /// - /// The entity. - /// A device tag setting - public DeviceTagDto GetDeviceTag(TableEntity entity) - { - ArgumentNullException.ThrowIfNull(entity, nameof(entity)); - - return new DeviceTagDto - { - Name = entity.RowKey, - Label = entity[nameof(DeviceTagDto.Label)].ToString(), - Required = bool.Parse(entity[nameof(DeviceTagDto.Required)].ToString() ?? "false"), - Searchable = bool.Parse(entity[nameof(DeviceTagDto.Searchable)].ToString() ?? "false") - }; - } - - /// - /// Updates the table entity. - /// - /// The entity. - /// The device tag object. - public void UpdateTableEntity(TableEntity tagEntity, DeviceTagDto element) - { - ArgumentNullException.ThrowIfNull(tagEntity, nameof(tagEntity)); - ArgumentNullException.ThrowIfNull(element, nameof(element)); - - tagEntity[nameof(DeviceTagDto.Label)] = element.Label; - tagEntity[nameof(DeviceTagDto.Required)] = element.Required; - tagEntity[nameof(DeviceTagDto.Searchable)] = element.Searchable; - } - } -} diff --git a/src/AzureIoTHub.Portal.Server/Mappers/EdgeModelMapper.cs b/src/AzureIoTHub.Portal.Server/Mappers/EdgeModelMapper.cs deleted file mode 100644 index 4dcfe405a..000000000 --- a/src/AzureIoTHub.Portal.Server/Mappers/EdgeModelMapper.cs +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright (c) CGI France. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace AzureIoTHub.Portal.Server.Mappers -{ - using System; - using System.Collections.Generic; - using System.Linq; - using Azure.Data.Tables; - using AzureIoTHub.Portal.Models.v10; - using AzureIoTHub.Portal.Server.Entities; - using AzureIoTHub.Portal.Server.Managers; - using AzureIoTHub.Portal.Shared.Models.v10; - - public class EdgeModelMapper : IEdgeDeviceModelMapper - { - private readonly IDeviceModelImageManager deviceModelImageManager; - - public EdgeModelMapper(IDeviceModelImageManager deviceModelImageManager) - { - this.deviceModelImageManager = deviceModelImageManager; - } - - /// - /// Create IoT edge model list item. - /// - /// Table entity. - /// IoTEdgeModelListItem. - public IoTEdgeModelListItem CreateEdgeDeviceModelListItem(TableEntity entity) - { - ArgumentNullException.ThrowIfNull(entity, nameof(entity)); - - return new IoTEdgeModelListItem - { - ModelId = entity.RowKey, - ImageUrl = this.deviceModelImageManager.ComputeImageUri(entity.RowKey), - Name = entity[nameof(IoTEdgeModelListItem.Name)]?.ToString(), - Description = entity[nameof(IoTEdgeModelListItem.Description)]?.ToString(), - }; - } - - public IoTEdgeModel CreateEdgeDeviceModel(TableEntity entity, List ioTEdgeModules, List ioTEdgeRoutes, IEnumerable commands) - { - ArgumentNullException.ThrowIfNull(entity, nameof(entity)); - var result = new IoTEdgeModel - { - ModelId = entity.RowKey, - ImageUrl = this.deviceModelImageManager.ComputeImageUri(entity.RowKey), - Name = entity[nameof(IoTEdgeModelListItem.Name)]?.ToString(), - Description = entity[nameof(IoTEdgeModelListItem.Description)]?.ToString(), - EdgeModules = ioTEdgeModules, - EdgeRoutes = ioTEdgeRoutes - }; - foreach (var command in commands) - { - var module = result.EdgeModules.SingleOrDefault(x => (x.ModuleName + "-" + command.Name).Equals(command.RowKey, StringComparison.Ordinal)); - if (module == null) - { - continue; - } - module.Commands.Add(new IoTEdgeModuleCommand - { - Name = command.Name, - }); - } - return result; - } - - public void UpdateTableEntity(TableEntity entity, IoTEdgeModel model) - { - ArgumentNullException.ThrowIfNull(entity, nameof(entity)); - ArgumentNullException.ThrowIfNull(model, nameof(model)); - - entity[nameof(IoTEdgeModel.Name)] = model.Name; - entity[nameof(IoTEdgeModel.Description)] = model.Description; - } - } -} diff --git a/src/AzureIoTHub.Portal.Server/Mappers/IEdgeDeviceModelMapper.cs b/src/AzureIoTHub.Portal.Server/Mappers/IEdgeDeviceModelMapper.cs deleted file mode 100644 index ed1dd8bd7..000000000 --- a/src/AzureIoTHub.Portal.Server/Mappers/IEdgeDeviceModelMapper.cs +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) CGI France. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace AzureIoTHub.Portal.Server.Mappers -{ - using System.Collections.Generic; - using Azure.Data.Tables; - using AzureIoTHub.Portal.Models.v10; - using AzureIoTHub.Portal.Server.Entities; - - public interface IEdgeDeviceModelMapper - { - IoTEdgeModelListItem CreateEdgeDeviceModelListItem(TableEntity entity); - - IoTEdgeModel CreateEdgeDeviceModel(TableEntity entity, List ioTEdgeModules, List ioTEdgeRoutes, IEnumerable commands); - - void UpdateTableEntity(TableEntity entity, IoTEdgeModel model); - } -} diff --git a/src/AzureIoTHub.Portal.Server/Startup.cs b/src/AzureIoTHub.Portal.Server/Startup.cs index ed61d8dfd..de53ca417 100644 --- a/src/AzureIoTHub.Portal.Server/Startup.cs +++ b/src/AzureIoTHub.Portal.Server/Startup.cs @@ -140,8 +140,6 @@ public void ConfigureServices(IServiceCollection services) _ = services.AddTransient, LoRaDeviceTwinMapper>(); _ = services.AddTransient, DeviceModelMapper>(); _ = services.AddTransient, LoRaDeviceModelMapper>(); - _ = services.AddTransient(); - _ = services.AddTransient(); _ = services.AddTransient(); _ = services.AddTransient(); diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Mappers/DeviceTagMapperTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Mappers/DeviceTagMapperTests.cs deleted file mode 100644 index fe47abed8..000000000 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Mappers/DeviceTagMapperTests.cs +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright (c) CGI France. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace AzureIoTHub.Portal.Tests.Unit.Server.Mappers -{ - using Azure.Data.Tables; - using Models.v10; - using AzureIoTHub.Portal.Server.Mappers; - using Moq; - using NUnit.Framework; - - [TestFixture] - public class DeviceTagMapperTests - { - private MockRepository mockRepository; - - [SetUp] - public void SetUp() - { - this.mockRepository = new MockRepository(MockBehavior.Strict); - } - - private static DeviceTagMapper CreateDeviceTagMapper() - { - return new DeviceTagMapper(); - } - - [Test] - public void GetDeviceTagStateUnderTestExpectedBehavior() - { - // Arrange - var deviceTagMapper = CreateDeviceTagMapper(); - var entity = new TableEntity - { - RowKey = "ExpectedRowKey" - }; - - entity["Label"] = "ExpectedLabel"; - entity["Required"] = false; - entity["Searchable"] = false; - - // Act - var result = deviceTagMapper.GetDeviceTag(entity); - - // Assert - Assert.AreEqual("ExpectedRowKey", result.Name); - Assert.AreEqual("ExpectedLabel", result.Label); - Assert.IsFalse(result.Required); - Assert.IsFalse(result.Searchable); - this.mockRepository.VerifyAll(); - } - - [Test] - public void UpdateTableEntityStateUnderTestExpectedBehavior() - { - // Arrange - var deviceTagMapper = CreateDeviceTagMapper(); - var entity = new TableEntity(); - - var element = new DeviceTagDto - { - Name = "ExpectedName", - Label = "ExpectedLabel", - Required = true, - Searchable = true - }; - - // Act - deviceTagMapper.UpdateTableEntity(entity, element); - - // Assert - Assert.AreEqual("ExpectedLabel", entity["Label"]); - Assert.IsTrue(bool.Parse(entity["Required"].ToString())); - Assert.IsTrue(bool.Parse(entity["Searchable"].ToString())); - this.mockRepository.VerifyAll(); - } - } -} diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Mappers/EdgeModelMapperTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Mappers/EdgeModelMapperTest.cs deleted file mode 100644 index fb911451b..000000000 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Mappers/EdgeModelMapperTest.cs +++ /dev/null @@ -1,149 +0,0 @@ -// Copyright (c) CGI France. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace AzureIoTHub.Portal.Tests.Unit.Server.Mappers -{ - using System; - using System.Collections.Generic; - using System.Linq; - using Azure.Data.Tables; - using AzureIoTHub.Portal.Models.v10; - using AzureIoTHub.Portal.Server.Entities; - using AzureIoTHub.Portal.Server.Managers; - using AzureIoTHub.Portal.Server.Mappers; - using Moq; - using NUnit.Framework; - - [TestFixture] - public class EdgeModelMapperTest - { - private MockRepository mockRepository; - - private Mock mockDeviceModelImageManager; - - [SetUp] - public void SetUp() - { - this.mockRepository = new MockRepository(MockBehavior.Strict); - - this.mockDeviceModelImageManager = this.mockRepository.Create(); - } - - private EdgeModelMapper CreateEdgeModelMapper() - { - return new EdgeModelMapper(this.mockDeviceModelImageManager.Object); - } - - [Test] - public void CreateEdgeDeviceModelListItemShouldReturnIoTEdgeModelListItemObject() - { - // Arrange - var edgeModelMapper = CreateEdgeModelMapper(); - - var partitionKey = Guid.NewGuid().ToString(); - var rowKey = "000-000-001"; - - _ = this.mockDeviceModelImageManager - .Setup(x => x.ComputeImageUri(It.IsAny())) - .Returns(new Uri("http://fake.local/000-000-001")); - - var entity = new TableEntity(partitionKey, rowKey) - { - ["Name"] = "test-name", - ["Description"] = "description_test", - }; - - // Act - var result = edgeModelMapper.CreateEdgeDeviceModelListItem(entity); - - // Assert - Assert.IsNotNull(result); - Assert.AreEqual(rowKey, result.ModelId); - Assert.AreEqual("test-name", result.Name); - - this.mockRepository.VerifyAll(); - } - - [Test] - public void CreateEdgeDeviceModelShouldReturnIoTEdgeModelObject() - { - // Arrange - var edgeModelMapper = CreateEdgeModelMapper(); - - var partitionKey = Guid.NewGuid().ToString(); - var rowKey = "000-000-001"; - - _ = this.mockDeviceModelImageManager - .Setup(x => x.ComputeImageUri(It.IsAny())) - .Returns(new Uri("http://fake.local/000-000-001")); - - var entity = new TableEntity(partitionKey, rowKey) - { - ["Name"] = "test-name", - ["Description"] = "description_test", - }; - - var modules = new List() - { - new IoTEdgeModule - { - ModuleName = "module" - } - }; - - var routes = new List() - { - new IoTEdgeRoute - { - Name = "route", - Value = "FROM source WHERE condition INTO sink", - } - }; - - var commands = new List() - { - new EdgeModuleCommand - { - PartitionKey = partitionKey, - Name = "Test", - RowKey = modules.First().ModuleName + "-" + "Test", - } - }; - - // Act - var result = edgeModelMapper.CreateEdgeDeviceModel(entity, modules, routes, commands); - - // Assert - Assert.IsNotNull(result); - Assert.AreEqual(rowKey, result.ModelId); - Assert.AreEqual("test-name", result.Name); - Assert.AreEqual(1, result.EdgeModules.Count); - Assert.AreEqual(1, result.EdgeRoutes.Count); - Assert.AreEqual(1, result.EdgeModules.First().Commands.Count); - - this.mockRepository.VerifyAll(); - } - - [Test] - public void UpdateTableEntityShouldUpdateTemplate() - { - // Arrange - var edgeModelMapper = CreateEdgeModelMapper(); - - var entity = new TableEntity(); - var edgeModel = new IoTEdgeModel() - { - ModelId = Guid.NewGuid().ToString(), - Name = "test", - Description = "description test" - }; - - // Act - edgeModelMapper.UpdateTableEntity(entity, edgeModel); - - // Assert - Assert.AreEqual("test", entity["Name"]); - Assert.AreEqual("description test", entity["Description"]); - } - } -}