Skip to content

Commit

Permalink
Add SynConcentratorsJobTests
Browse files Browse the repository at this point in the history
  • Loading branch information
audserraCGI committed Oct 6, 2022
1 parent adfaece commit 6c770cd
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 6 deletions.
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Server.Jobs
using Quartz;
using UnitTests.Bases;

public class SyncDeviceJobTests : BackendUnitTest
public class SyncDevicesJobTests : BackendUnitTest
{
private IJob syncDevicesJob;

Expand Down
6 changes: 1 addition & 5 deletions src/AzureIoTHub.Portal/Server/Jobs/SyncConcentratorsJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,19 @@ namespace AzureIoTHub.Portal.Server.Jobs
[DisallowConcurrentExecution]
public class SyncConcentratorsJob : IJob
{
private readonly ILoRaWANConcentratorService concentratorService;
private readonly IConcentratorRepository concentratorRepository;
private readonly IExternalDeviceService externalDeviceService;
private readonly IMapper mapper;
private readonly IUnitOfWork unitOfWork;
private readonly ILogger<SyncConcentratorsJob> logger;

//private const string ModelId = "modelId";

public SyncConcentratorsJob(ILoRaWANConcentratorService concentratorService,
public SyncConcentratorsJob(
IConcentratorRepository concentratorRepository,
IExternalDeviceService externalDeviceService,
IMapper mapper,
IUnitOfWork unitOfWork,
ILogger<SyncConcentratorsJob> logger)
{
this.concentratorService = concentratorService;
this.concentratorRepository = concentratorRepository;
this.externalDeviceService = externalDeviceService;
this.mapper = mapper;
Expand Down

0 comments on commit 6c770cd

Please sign in to comment.