Updating
This commit is contained in:
+79
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using LCU.Api.Internal.FileOperations.Services;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
namespace LCU.Api.Internal.FileOperationsTests.Services;
|
||||
|
||||
public class DirectFileOperationsServiceTest
|
||||
{
|
||||
private readonly DirectFileOperationsService _directFileOperationsService;
|
||||
private Mock<IFileOperationsService> _mockFileOperationsService;
|
||||
|
||||
public DirectFileOperationsServiceTest()
|
||||
{
|
||||
_mockFileOperationsService = new Mock<IFileOperationsService>();
|
||||
_directFileOperationsService = new DirectFileOperationsService(_mockFileOperationsService.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleDirectFileOperationsRequest_Return_IsSuccessful_True()
|
||||
{
|
||||
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>(), IsSuccessful = true };
|
||||
_mockFileOperationsService.Setup(service => service.PerformFileOperation(It.IsAny<QueueRequest>())).Returns(validRequestResponse);
|
||||
|
||||
var request = new MoveFileRequest()
|
||||
{
|
||||
SourcePath = "",
|
||||
SourceFile = "",
|
||||
DestinationPath = ""
|
||||
};
|
||||
|
||||
// act
|
||||
var result = await _directFileOperationsService.HandleDirectFileOperationsRequest(request, FileOperationType.Move);
|
||||
Assert.NotNull(result);
|
||||
Assert.True(validRequestResponse.IsSuccessful);
|
||||
_mockFileOperationsService.Verify(service => service.PerformFileOperation(It.IsAny<QueueRequest>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleDirectFileOperationsRequest_Return_IsSuccessful_False()
|
||||
{
|
||||
var validRequestResponse = new RequestResponse() { MessageStack = new List<string> { "test" }, IsSuccessful = false };
|
||||
_mockFileOperationsService.Setup(service => service.PerformFileOperation(It.IsAny<QueueRequest>())).Returns(validRequestResponse);
|
||||
|
||||
var request = new MoveFileRequest()
|
||||
{
|
||||
SourcePath = "",
|
||||
SourceFile = "",
|
||||
DestinationPath = ""
|
||||
};
|
||||
|
||||
// act
|
||||
var result = await _directFileOperationsService.HandleDirectFileOperationsRequest(request, FileOperationType.Move);
|
||||
Assert.NotNull(result);
|
||||
Assert.False(result.IsSuccessful);
|
||||
Assert.Single(result.MessageStack);
|
||||
_mockFileOperationsService.Verify(service => service.PerformFileOperation(It.IsAny<QueueRequest>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HandleDirectFileOperationsRequest_PerformFileOperation_ThrowsException()
|
||||
{
|
||||
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>(), IsSuccessful = true };
|
||||
_mockFileOperationsService.Setup(service => service.PerformFileOperation(It.IsAny<QueueRequest>())).Throws(new Exception("Test"));
|
||||
|
||||
var request = new MoveFileRequest()
|
||||
{
|
||||
SourcePath = "",
|
||||
SourceFile = "",
|
||||
DestinationPath = ""
|
||||
};
|
||||
|
||||
// act
|
||||
Assert.ThrowsAsync<ArgumentNullException>(async () => await _directFileOperationsService.HandleDirectFileOperationsRequest(request, FileOperationType.Move));
|
||||
_mockFileOperationsService.Verify(service => service.PerformFileOperation(It.IsAny<QueueRequest>()), Times.Once);
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
using LCU.Api.Internal.FileOperations.Services;
|
||||
using System;
|
||||
using Xunit;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperationsTests.Services
|
||||
{
|
||||
public class DirectFileServiceTest
|
||||
{
|
||||
private readonly DirectFileService _directFileService;
|
||||
|
||||
public DirectFileServiceTest()
|
||||
{
|
||||
_directFileService = new DirectFileService();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Sir_Throws_A_Lot()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(
|
||||
() => { _directFileService.GetFileBytes(" "); });
|
||||
Assert.Throws<ArgumentException>(
|
||||
() => { _directFileService.GetFileBytes("C:\\secret.txt"); });
|
||||
Assert.Throws<ArgumentException>(
|
||||
() => { _directFileService.GetFileBytes("asdf"); });
|
||||
}
|
||||
}
|
||||
}
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
namespace LCU.Api.Internal.FileOperationsTests.Services.Validations;
|
||||
|
||||
public class FileValidationTest
|
||||
{
|
||||
[Fact]
|
||||
public void IsSourceEmpty_SourceEmpty_ReturnIsValid_False()
|
||||
{
|
||||
// arrange
|
||||
var sourcePath = "";
|
||||
var responseRequest = new RequestResponse();
|
||||
|
||||
// act
|
||||
responseRequest = FileValidation.ValidateSourceNotEmpty(sourcePath, responseRequest);
|
||||
|
||||
// assert
|
||||
Assert.False(responseRequest.IsSuccessful);
|
||||
Assert.Equal("Source is empty and is required.", responseRequest.MessageStack.FirstOrDefault());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsSourceEmpty_SourceNotEmpty_ReturnIsValid_True()
|
||||
{
|
||||
// arrange
|
||||
var sourcePath = "TESTSource";
|
||||
var responseRequest = new RequestResponse();
|
||||
|
||||
// act
|
||||
responseRequest = FileValidation.ValidateSourceNotEmpty(sourcePath, responseRequest);
|
||||
|
||||
// assert
|
||||
Assert.True(responseRequest.IsSuccessful);
|
||||
Assert.Empty(responseRequest.MessageStack);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsDestinationEmpty_DestinationEmpty_ReturnIsValid_False()
|
||||
{
|
||||
// arrange
|
||||
var sourcePath = "";
|
||||
var responseRequest = new RequestResponse();
|
||||
|
||||
// act
|
||||
responseRequest = FileValidation.ValidateDestinationNotEmpty(sourcePath, responseRequest);
|
||||
|
||||
// assert
|
||||
Assert.False(responseRequest.IsSuccessful);
|
||||
Assert.Equal("Destination is empty and is required.", responseRequest.MessageStack.FirstOrDefault());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsDestinationEmpty_DestinationNotEmpty_ReturnIsValid_True()
|
||||
{
|
||||
// arrange
|
||||
var destinationPath = "TESTDestination";
|
||||
var responseRequest = new RequestResponse();
|
||||
|
||||
// act
|
||||
responseRequest = FileValidation.ValidateDestinationNotEmpty(destinationPath, responseRequest);
|
||||
|
||||
// assert
|
||||
Assert.True(responseRequest.IsSuccessful);
|
||||
Assert.Empty(responseRequest.MessageStack);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsFilenameEmpty_FilenameEmpty_ReturnIsValid_False()
|
||||
{
|
||||
// arrange
|
||||
var filename = "";
|
||||
var responseRequest = new RequestResponse();
|
||||
|
||||
// act
|
||||
responseRequest = FileValidation.ValidateFilenameNotEmpty(filename, responseRequest);
|
||||
|
||||
// assert
|
||||
Assert.False(responseRequest.IsSuccessful);
|
||||
Assert.Equal("Filename is empty and is required.", responseRequest.MessageStack.FirstOrDefault());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsFilenameEmpty_FilenameNotEmpty_ReturnIsValid_True()
|
||||
{
|
||||
// arrange
|
||||
var filename = "TESTFilename";
|
||||
var responseRequest = new RequestResponse();
|
||||
|
||||
// act
|
||||
responseRequest = FileValidation.ValidateDestinationNotEmpty(filename, responseRequest);
|
||||
|
||||
// assert
|
||||
Assert.True(responseRequest.IsSuccessful);
|
||||
Assert.Empty(responseRequest.MessageStack);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsFilenameValid_FilenameNotValid_ReturnIsValid_False()
|
||||
{
|
||||
// arrange
|
||||
var filename = "/**";
|
||||
var responseRequest = new RequestResponse();
|
||||
|
||||
// act
|
||||
responseRequest = FileValidation.ValidateFilename(filename, responseRequest);
|
||||
|
||||
// assert
|
||||
Assert.False(responseRequest.IsSuccessful);
|
||||
Assert.Equal("File name is invalid.", responseRequest.MessageStack.FirstOrDefault());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsFilenameValid_FilenameValid_ReturnIsValid_True()
|
||||
{
|
||||
// arrange
|
||||
var filename = "TESTFilename";
|
||||
var responseRequest = new RequestResponse();
|
||||
|
||||
// act
|
||||
responseRequest = FileValidation.ValidateFilename(filename, responseRequest);
|
||||
|
||||
// assert
|
||||
Assert.True(responseRequest.IsSuccessful);
|
||||
Assert.Empty(responseRequest.MessageStack);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user