From 432ecfc036f927924242a2c331c2f499ed93fa97 Mon Sep 17 00:00:00 2001 From: Audrey Serra Date: Thu, 10 Mar 2022 15:15:05 +0100 Subject: [PATCH] Added DeviceTagServiceTest + Fix code scanning issues --- .../Services/DeviceTagServiceTest.cs | 55 +++++++++++++++++++ .../Server/Services/ConfigService.cs | 4 +- .../Server/Services/DeviceService.cs | 8 +-- .../Server/Services/DeviceTagService.cs | 29 +++++----- 4 files changed, 77 insertions(+), 19 deletions(-) diff --git a/src/AzureIoTHub.Portal.Server.Tests.Unit/Services/DeviceTagServiceTest.cs b/src/AzureIoTHub.Portal.Server.Tests.Unit/Services/DeviceTagServiceTest.cs index f319abc26..a0c0f4696 100644 --- a/src/AzureIoTHub.Portal.Server.Tests.Unit/Services/DeviceTagServiceTest.cs +++ b/src/AzureIoTHub.Portal.Server.Tests.Unit/Services/DeviceTagServiceTest.cs @@ -194,5 +194,60 @@ public void GetAllTagsNamesShouldReturnAList() this.mockRepository.VerifyAll(); } + + [Test] + public void GetAllSearchableTagsNamesShouldReturnAList() + { + // Arrange + var deviceTagService = this.CreateDeviceTagService(); + var boolList = new List() { true, false, true }; + var returnedIndex = boolList.Count; + + var mockTableResponse = this.mockRepository.Create>(); + var mockEnumerator = this.mockRepository.Create>(); + _ = mockEnumerator.Setup(x => x.Dispose()).Callback(() => { }); + _ = mockEnumerator.Setup(x => x.MoveNext()).Returns(() => + { + return returnedIndex-- > 0; + }); + + _ = mockEnumerator.Setup(x => x.Current) + .Returns(() => + { + var result = new TableEntity(DeviceTagService.DefaultPartitionKey, "test") + { + [nameof(DeviceTag.Searchable)] = boolList[returnedIndex], + }; + + return result; + }); + + _ = mockTableResponse.Setup(x => x.GetEnumerator()).Returns(mockEnumerator.Object); + + _ = mockDeviceTagTableClient.Setup(c => c.Query(It.IsAny(), It.IsAny(), It.IsAny>(), It.IsAny())) + .Returns(mockTableResponse.Object); + + _ = mockTableClientFactory.Setup(c => c.GetDeviceTagSettings()) + .Returns(mockDeviceTagTableClient.Object); + + _ = mockDeviceTagMapper.Setup(c => c.GetDeviceTag(It.IsAny())) + .Returns((TableEntity entity) => new DeviceTag + { + Name = Guid.NewGuid().ToString(), + Label = "test", + Required = true, + Searchable = bool.Parse(entity[nameof(DeviceTag.Searchable)].ToString()) + }); + + // Act + var result = deviceTagService.GetAllSearchableTagsNames(); + + // Assert + Assert.IsNotNull(result); + Assert.AreEqual(2, result.Count()); + Assert.IsAssignableFrom(result.First()); + + this.mockRepository.VerifyAll(); + } } } diff --git a/src/AzureIoTHub.Portal/Server/Services/ConfigService.cs b/src/AzureIoTHub.Portal/Server/Services/ConfigService.cs index 90fb5eef4..6324c41be 100644 --- a/src/AzureIoTHub.Portal/Server/Services/ConfigService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/ConfigService.cs @@ -1,4 +1,4 @@ -// Copyright (c) CGI France. All rights reserved. +// 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.Services @@ -62,7 +62,7 @@ public async Task RolloutDeviceConfiguration(string modelId, string modelName, D newConfiguration.TargetCondition = $"tags.modelId = '{modelId}'"; newConfiguration.Content.DeviceContent = desiredProperties; - await this.registryManager.AddConfigurationAsync(newConfiguration); + _ = await this.registryManager.AddConfigurationAsync(newConfiguration); } } } diff --git a/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs b/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs index 3405af550..418f0c23a 100644 --- a/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs @@ -1,4 +1,4 @@ -// Copyright (c) CGI France. All rights reserved. +// 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.Services @@ -28,7 +28,7 @@ public DeviceService( /// IEnumerable twin. public async Task> GetAllEdgeDevice() { - IQuery queryEdgeDevice = this.registryManager.CreateQuery("SELECT * FROM devices.modules WHERE devices.modules.moduleId = '$edgeHub' GROUP BY deviceId", 10); + var queryEdgeDevice = this.registryManager.CreateQuery("SELECT * FROM devices.modules WHERE devices.modules.moduleId = '$edgeHub' GROUP BY deviceId", 10); while (queryEdgeDevice.HasMoreResults) { @@ -97,11 +97,11 @@ public async Task GetDeviceTwin(string deviceId) /// Twin of the device. public async Task GetDeviceTwinWithModule(string deviceId) { - IQuery devicesWithModules = this.registryManager.CreateQuery($"SELECT * FROM devices.modules WHERE devices.modules.moduleId = '$edgeAgent' AND deviceId in ['{deviceId}']"); + var devicesWithModules = this.registryManager.CreateQuery($"SELECT * FROM devices.modules WHERE devices.modules.moduleId = '$edgeAgent' AND deviceId in ['{deviceId}']"); while (devicesWithModules.HasMoreResults) { - IEnumerable devicesTwins = await devicesWithModules.GetNextAsTwinAsync(); + var devicesTwins = await devicesWithModules.GetNextAsTwinAsync(); return devicesTwins.ElementAt(0); } diff --git a/src/AzureIoTHub.Portal/Server/Services/DeviceTagService.cs b/src/AzureIoTHub.Portal/Server/Services/DeviceTagService.cs index 595361bbd..3928c4f16 100644 --- a/src/AzureIoTHub.Portal/Server/Services/DeviceTagService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/DeviceTagService.cs @@ -1,13 +1,16 @@ -using Azure.Data.Tables; -using AzureIoTHub.Portal.Server.Factories; -using AzureIoTHub.Portal.Server.Mappers; -using AzureIoTHub.Portal.Shared.Models.V10.Device; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +// 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.Services { + using Azure.Data.Tables; + using AzureIoTHub.Portal.Server.Factories; + using AzureIoTHub.Portal.Server.Mappers; + using AzureIoTHub.Portal.Shared.Models.V10.Device; + using System.Collections.Generic; + using System.Linq; + using System.Threading.Tasks; + public class DeviceTagService : IDeviceTagService { /// @@ -70,14 +73,14 @@ public async Task UpdateTags(List tags) foreach (var item in query) { - await this.tableClientFactory - .GetDeviceTagSettings() - .DeleteEntityAsync(item.PartitionKey, item.RowKey); + _ = await this.tableClientFactory + .GetDeviceTagSettings() + .DeleteEntityAsync(item.PartitionKey, item.RowKey); } - foreach (DeviceTag tag in tags) + foreach (var tag in tags) { - TableEntity entity = new TableEntity() + var entity = new TableEntity() { PartitionKey = DefaultPartitionKey, RowKey = tag.Name @@ -95,7 +98,7 @@ await this.tableClientFactory private async Task SaveEntity(TableEntity entity, DeviceTag tag) { this.deviceTagMapper.UpdateTableEntity(entity, tag); - await this.tableClientFactory + _ = await this.tableClientFactory .GetDeviceTagSettings() .AddEntityAsync(entity); }