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

DeviceTagService Test #428

Merged
merged 1 commit into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,60 @@ public void GetAllTagsNamesShouldReturnAList()

this.mockRepository.VerifyAll();
}

[Test]
public void GetAllSearchableTagsNamesShouldReturnAList()
{
// Arrange
var deviceTagService = this.CreateDeviceTagService();
var boolList = new List<bool>() { true, false, true };
var returnedIndex = boolList.Count;

var mockTableResponse = this.mockRepository.Create<Pageable<TableEntity>>();
var mockEnumerator = this.mockRepository.Create<IEnumerator<TableEntity>>();
_ = 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<TableEntity>(It.IsAny<string>(), It.IsAny<int?>(), It.IsAny<IEnumerable<string>>(), It.IsAny<CancellationToken>()))
.Returns(mockTableResponse.Object);

_ = mockTableClientFactory.Setup(c => c.GetDeviceTagSettings())
.Returns(mockDeviceTagTableClient.Object);

_ = mockDeviceTagMapper.Setup(c => c.GetDeviceTag(It.IsAny<TableEntity>()))
.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<string>(result.First());

this.mockRepository.VerifyAll();
}
}
}
4 changes: 2 additions & 2 deletions src/AzureIoTHub.Portal/Server/Services/ConfigService.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
}
}
}
8 changes: 4 additions & 4 deletions src/AzureIoTHub.Portal/Server/Services/DeviceService.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -28,7 +28,7 @@ public DeviceService(
/// <returns>IEnumerable twin.</returns>
public async Task<IEnumerable<Twin>> 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)
{
Expand Down Expand Up @@ -97,11 +97,11 @@ public async Task<Twin> GetDeviceTwin(string deviceId)
/// <returns>Twin of the device.</returns>
public async Task<Twin> 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<Twin> devicesTwins = await devicesWithModules.GetNextAsTwinAsync();
var devicesTwins = await devicesWithModules.GetNextAsTwinAsync();

return devicesTwins.ElementAt(0);
}
Expand Down
29 changes: 16 additions & 13 deletions src/AzureIoTHub.Portal/Server/Services/DeviceTagService.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
Expand Down Expand Up @@ -70,14 +73,14 @@ public async Task UpdateTags(List<DeviceTag> 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
Expand All @@ -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);
}
Expand Down