126 lines
3.7 KiB
C#
126 lines
3.7 KiB
C#
using Moq;
|
|
using Xunit;
|
|
using LCU.Api.Internal.FileOperations.Services;
|
|
using LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
|
using LCU.Library.Internal.FileOperationsModels.Models;
|
|
using LCU.Api.Internal.FileOperations.Configurations;
|
|
using LCU.Api.Internal.FileOperations.Services.QueueServices;
|
|
using LCU.Library.Internal.LCUServiceFactory.Services;
|
|
using System;
|
|
namespace LCU.Api.Internal.FileOperationsTests.Services;
|
|
|
|
public class FileOperationsServiceTest
|
|
{
|
|
private readonly FileOperationsService _fileOperationsService;
|
|
private readonly Mock<ILcuServiceFactory<IFileActionService>> _mockServiceFactory;
|
|
private Mock<IQueueFileOperationsService> _mockQueueFileOperationsService;
|
|
|
|
public FileOperationsServiceTest()
|
|
{
|
|
var apiConfiguration = new ApiConfiguration()
|
|
{
|
|
AttemptsLimit = 2
|
|
};
|
|
_mockQueueFileOperationsService = new Mock<IQueueFileOperationsService>();
|
|
_mockServiceFactory = new Mock<ILcuServiceFactory<IFileActionService>>();
|
|
_fileOperationsService = new FileOperationsService(
|
|
_mockQueueFileOperationsService.Object,
|
|
apiConfiguration,
|
|
_mockServiceFactory.Object);
|
|
;
|
|
}
|
|
|
|
[Fact]
|
|
public void Basic_Construct_Test()
|
|
{
|
|
Assert.NotNull(_fileOperationsService);
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldPerformFileOperationsAgain_AttempsLessThenLimit_True()
|
|
{
|
|
var request = new QueueRequest()
|
|
{
|
|
Attempts = 0
|
|
};
|
|
var requestResponse = new RequestResponse()
|
|
{
|
|
IsSuccessful = false
|
|
};
|
|
var result = _fileOperationsService.ShouldPerformFileOperationsAgain(request, requestResponse);
|
|
|
|
Assert.True(result);
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldPerformFileOperationsAgain_AttempsGreaterThenLimit_False()
|
|
{
|
|
var request = new QueueRequest()
|
|
{
|
|
Attempts = 3
|
|
};
|
|
|
|
var requestResponse = new RequestResponse()
|
|
{
|
|
IsSuccessful = false
|
|
};
|
|
var result = _fileOperationsService.ShouldPerformFileOperationsAgain(request, requestResponse);
|
|
|
|
Assert.False(result);
|
|
}
|
|
|
|
[Fact]
|
|
public void PerformFileOperation_IsSuccessful()
|
|
{
|
|
QueueRequest request = null;
|
|
|
|
var result = _fileOperationsService.PerformFileOperation(request);
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Single(result.MessageStack);
|
|
Assert.True(result.IsSuccessful);
|
|
}
|
|
|
|
[Fact]
|
|
public void PerformFileOperation_NullRequest_IsSuccessful()
|
|
{
|
|
var request = new QueueRequest()
|
|
{
|
|
Attempts = 3,
|
|
FileOperationType = FileOperationType.Move
|
|
};
|
|
|
|
var response = new RequestResponse();
|
|
|
|
var mockedFileActionService = new Mock<IFileActionService>();
|
|
_mockServiceFactory.Setup(x => x.GetService(request.FileOperationType)).Returns(mockedFileActionService.Object);
|
|
mockedFileActionService.Setup(x => x.ExecuteAction(request)).Returns(response);
|
|
|
|
var result = _fileOperationsService.PerformFileOperation(request);
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Empty(result.MessageStack);
|
|
Assert.True(result.IsSuccessful);
|
|
}
|
|
|
|
[Fact]
|
|
public void PerformFileOperation_ServiceFactoryException_IsNotSuccessful()
|
|
{
|
|
var request = new QueueRequest()
|
|
{
|
|
Attempts = 3,
|
|
FileOperationType = FileOperationType.Move
|
|
};
|
|
|
|
var response = new RequestResponse();
|
|
|
|
var mockedFileActionService = new Mock<IFileActionService>();
|
|
_mockServiceFactory.Setup(x => x.GetService(request.FileOperationType)).Throws(new Exception("Failure"));
|
|
|
|
var result = _fileOperationsService.PerformFileOperation(request);
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Single(result.MessageStack);
|
|
Assert.False(result.IsSuccessful);
|
|
}
|
|
} |