-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
adfaece
commit 6c770cd
Showing
3 changed files
with
216 additions
and
6 deletions.
There are no files selected for viewing
214 changes: 214 additions & 0 deletions
214
src/AzureIoTHub.Portal.Tests.Unit/Server/Jobs/SyncConcentratorsJobTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
// 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.Jobs | ||
{ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using AutoFixture; | ||
using AzureIoTHub.Portal.Domain; | ||
using AzureIoTHub.Portal.Domain.Repositories; | ||
using AzureIoTHub.Portal.Server.Jobs; | ||
using AzureIoTHub.Portal.Server.Services; | ||
using Microsoft.Azure.Devices.Shared; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Moq; | ||
using NUnit.Framework; | ||
using Portal.Domain.Entities; | ||
using Quartz; | ||
using UnitTests.Bases; | ||
|
||
public class SyncConcentratorsTests : BackendUnitTest | ||
{ | ||
private IJob syncConcentratorsJob; | ||
|
||
private Mock<IExternalDeviceService> mockExternalDeviceService; | ||
private Mock<IConcentratorRepository> mockConcentratorRepository; | ||
private Mock<IUnitOfWork> mockUnitOfWork; | ||
|
||
public override void Setup() | ||
{ | ||
base.Setup(); | ||
|
||
this.mockExternalDeviceService = MockRepository.Create<IExternalDeviceService>(); | ||
this.mockConcentratorRepository = MockRepository.Create<IConcentratorRepository>(); | ||
this.mockUnitOfWork = MockRepository.Create<IUnitOfWork>(); | ||
|
||
_ = ServiceCollection.AddSingleton(this.mockExternalDeviceService.Object); | ||
_ = ServiceCollection.AddSingleton(this.mockConcentratorRepository.Object); | ||
_ = ServiceCollection.AddSingleton(this.mockUnitOfWork.Object); | ||
_ = ServiceCollection.AddSingleton<IJob, SyncConcentratorsJob>(); | ||
|
||
Services = ServiceCollection.BuildServiceProvider(); | ||
|
||
this.syncConcentratorsJob = Services.GetRequiredService<IJob>(); | ||
} | ||
|
||
[Test] | ||
public async Task ExecuteNewConcentratorShouldCreateEntity() | ||
{ | ||
// Arrange | ||
var mockJobExecutionContext = MockRepository.Create<IJobExecutionContext>(); | ||
|
||
var expectedTwinConcentrator = new Twin | ||
{ | ||
DeviceId = Fixture.Create<string>(), | ||
Tags = new TwinCollection | ||
{ | ||
["deviceName"] = Fixture.Create<string>(), | ||
["loraRegion"] = Fixture.Create<string>(), | ||
["deviceType"] = Fixture.Create<string>() | ||
} | ||
}; | ||
|
||
_ = this.mockExternalDeviceService | ||
.Setup(x => x.GetAllDevice( | ||
It.IsAny<string>(), | ||
It.Is<string>(x => x == "LoRa Concentrator"), | ||
It.IsAny<string>(), | ||
It.IsAny<string>(), | ||
It.IsAny<bool?>(), | ||
It.IsAny<bool?>(), | ||
It.IsAny<Dictionary<string, string>>(), | ||
It.Is<int>(x => x == 100))) | ||
.ReturnsAsync(new PaginationResult<Twin> | ||
{ | ||
Items = new List<Twin> | ||
{ | ||
expectedTwinConcentrator | ||
}, | ||
TotalItems = 1 | ||
}); | ||
|
||
_ = this.mockConcentratorRepository.Setup(repository => repository.GetByIdAsync(expectedTwinConcentrator.DeviceId)) | ||
.ReturnsAsync((Concentrator)null); | ||
|
||
_ = this.mockConcentratorRepository.Setup(repository => repository.InsertAsync(It.IsAny<Concentrator>())) | ||
.Returns(Task.CompletedTask); | ||
|
||
_ = this.mockUnitOfWork.Setup(work => work.SaveAsync()) | ||
.Returns(Task.CompletedTask); | ||
|
||
// Act | ||
await this.syncConcentratorsJob.Execute(mockJobExecutionContext.Object); | ||
|
||
// Assert | ||
MockRepository.VerifyAll(); | ||
} | ||
|
||
[Test] | ||
public async Task ExecuteExistingConcentratorWithGreaterVersionShouldUpdateEntity() | ||
{ | ||
// Arrange | ||
var mockJobExecutionContext = MockRepository.Create<IJobExecutionContext>(); | ||
|
||
var expectedTwinConcentrator = new Twin | ||
{ | ||
DeviceId = Fixture.Create<string>(), | ||
Tags = new TwinCollection | ||
{ | ||
["deviceName"] = Fixture.Create<string>(), | ||
["loraRegion"] = Fixture.Create<string>(), | ||
["deviceType"] = Fixture.Create<string>() | ||
}, | ||
Version = 2 | ||
}; | ||
|
||
var existingConcentrator = new Concentrator | ||
{ | ||
Id = expectedTwinConcentrator.DeviceId, | ||
Version = 1, | ||
}; | ||
|
||
_ = this.mockExternalDeviceService | ||
.Setup(x => x.GetAllDevice( | ||
It.IsAny<string>(), | ||
It.Is<string>(x => x == "LoRa Concentrator"), | ||
It.IsAny<string>(), | ||
It.IsAny<string>(), | ||
It.IsAny<bool?>(), | ||
It.IsAny<bool?>(), | ||
It.IsAny<Dictionary<string, string>>(), | ||
It.Is<int>(x => x == 100))) | ||
.ReturnsAsync(new PaginationResult<Twin> | ||
{ | ||
Items = new List<Twin> | ||
{ | ||
expectedTwinConcentrator | ||
}, | ||
TotalItems = 1 | ||
}); | ||
|
||
_ = this.mockConcentratorRepository.Setup(repository => repository.GetByIdAsync(expectedTwinConcentrator.DeviceId)) | ||
.ReturnsAsync(existingConcentrator); | ||
|
||
this.mockConcentratorRepository.Setup(repository => repository.Update(It.IsAny<Concentrator>())) | ||
.Verifiable(); | ||
|
||
_ = this.mockUnitOfWork.Setup(work => work.SaveAsync()) | ||
.Returns(Task.CompletedTask); | ||
|
||
// Act | ||
await this.syncConcentratorsJob.Execute(mockJobExecutionContext.Object); | ||
|
||
// Assert | ||
MockRepository.VerifyAll(); | ||
} | ||
|
||
[Test] | ||
public async Task ExecuteExistingConcentratorWithLowerOrEqualVersionShouldReturn() | ||
{ | ||
// Arrange | ||
var mockJobExecutionContext = MockRepository.Create<IJobExecutionContext>(); | ||
|
||
var expectedTwinConcentrator = new Twin | ||
{ | ||
DeviceId = Fixture.Create<string>(), | ||
Tags = new TwinCollection | ||
{ | ||
["deviceName"] = Fixture.Create<string>(), | ||
["loraRegion"] = Fixture.Create<string>(), | ||
["deviceType"] = Fixture.Create<string>() | ||
}, | ||
Version = 1 | ||
}; | ||
|
||
var existingConcentrator = new Concentrator | ||
{ | ||
Id = expectedTwinConcentrator.DeviceId, | ||
Version = 1, | ||
}; | ||
|
||
_ = this.mockExternalDeviceService | ||
.Setup(x => x.GetAllDevice( | ||
It.IsAny<string>(), | ||
It.Is<string>(x => x == "LoRa Concentrator"), | ||
It.IsAny<string>(), | ||
It.IsAny<string>(), | ||
It.IsAny<bool?>(), | ||
It.IsAny<bool?>(), | ||
It.IsAny<Dictionary<string, string>>(), | ||
It.Is<int>(x => x == 100))) | ||
.ReturnsAsync(new PaginationResult<Twin> | ||
{ | ||
Items = new List<Twin> | ||
{ | ||
expectedTwinConcentrator | ||
}, | ||
TotalItems = 1 | ||
}); | ||
|
||
_ = this.mockConcentratorRepository.Setup(repository => repository.GetByIdAsync(expectedTwinConcentrator.DeviceId)) | ||
.ReturnsAsync(existingConcentrator); | ||
|
||
_ = this.mockUnitOfWork.Setup(work => work.SaveAsync()) | ||
.Returns(Task.CompletedTask); | ||
|
||
// Act | ||
await this.syncConcentratorsJob.Execute(mockJobExecutionContext.Object); | ||
|
||
// Assert | ||
MockRepository.VerifyAll(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters