Updating
This commit is contained in:
+243
@@ -0,0 +1,243 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using LCU.Api.Internal.FileOperations.Controllers;
|
||||
using LCU.Api.Internal.FileOperations.Services;
|
||||
using LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.Exceptions;
|
||||
namespace LCU.Api.Internal.FileOperationsTests.Controllers;
|
||||
|
||||
public class DirectFileOperationsControllerTest
|
||||
{
|
||||
private readonly DirectFileOperationsController _controller;
|
||||
private readonly Mock<IDirectFileOperationsService> _mockDirectFileOperationsService;
|
||||
private readonly Mock<IActionService<MoveFileRequest>> _mockMoveFileService;
|
||||
private readonly Mock<IActionService<WriteFileRequest>> _mockWriteFileService;
|
||||
private readonly Mock<IActionService<CreateDirectoryRequest>> _mockCreateDirectoryService;
|
||||
private readonly Mock<IActionService<UnzipFileRequest>> _mockUnzipFileService;
|
||||
private readonly Mock<IActionService<ZipDirectoryRequest>> _mockZipDirectoryService;
|
||||
private readonly Mock<IActionService<DeleteDirectoryRequest>> _mockDeleteDirectoryService;
|
||||
private readonly Mock<IActionService<DeleteFileRequest>> _mockDeleteFileService;
|
||||
private readonly Mock<IActionService<AppendFileRequest>> _mockAppendFileService;
|
||||
|
||||
public DirectFileOperationsControllerTest()
|
||||
{
|
||||
_mockDirectFileOperationsService = new Mock<IDirectFileOperationsService>();
|
||||
_mockMoveFileService = new Mock<IActionService<MoveFileRequest>>();
|
||||
_mockWriteFileService = new Mock<IActionService<WriteFileRequest>>();
|
||||
_mockCreateDirectoryService = new Mock<IActionService<CreateDirectoryRequest>>();
|
||||
_mockUnzipFileService = new Mock<IActionService<UnzipFileRequest>>();
|
||||
_mockZipDirectoryService = new Mock<IActionService<ZipDirectoryRequest>>();
|
||||
_mockDeleteDirectoryService = new Mock<IActionService<DeleteDirectoryRequest>>();
|
||||
_mockDeleteFileService = new Mock<IActionService<DeleteFileRequest>>();
|
||||
_mockAppendFileService = new Mock<IActionService<AppendFileRequest>>();
|
||||
_controller = new DirectFileOperationsController(_mockDirectFileOperationsService.Object,
|
||||
_mockMoveFileService.Object,
|
||||
_mockWriteFileService.Object,
|
||||
_mockCreateDirectoryService.Object,
|
||||
_mockUnzipFileService.Object,
|
||||
_mockZipDirectoryService.Object,
|
||||
_mockDeleteDirectoryService.Object,
|
||||
_mockDeleteFileService.Object,
|
||||
_mockAppendFileService.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MoveFilePost_OK()
|
||||
{
|
||||
// arrange
|
||||
var request = new MoveFileRequest()
|
||||
{
|
||||
SourcePath = "",
|
||||
SourceFile = "",
|
||||
DestinationPath = ""
|
||||
};
|
||||
|
||||
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>(), IsSuccessful = true };
|
||||
_mockMoveFileService.Setup(service => service.PreQueueValidation(It.IsAny<MoveFileRequest>())).Returns(validRequestResponse);
|
||||
_mockDirectFileOperationsService.Setup(service =>
|
||||
service.HandleDirectFileOperationsRequest(It.IsAny<MoveFileRequest>(), FileOperationType.Move))
|
||||
.ReturnsAsync(validRequestResponse)
|
||||
.Verifiable();
|
||||
|
||||
// act
|
||||
var result = await _controller.MoveFile(request);
|
||||
Assert.NotNull(result);
|
||||
var okResult = result as OkObjectResult;
|
||||
|
||||
// assert
|
||||
Assert.NotNull(okResult);
|
||||
Assert.Equal(200, okResult.StatusCode);
|
||||
_mockMoveFileService.Verify(service =>
|
||||
service.PreQueueValidation(It.IsAny<MoveFileRequest>()), Times.Once);
|
||||
_mockDirectFileOperationsService.Verify(service =>
|
||||
service.HandleDirectFileOperationsRequest(It.IsAny<MoveFileRequest>(), FileOperationType.Move), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MoveFilePost_InvalidRequest()
|
||||
{
|
||||
// arrange
|
||||
var request = new MoveFileRequest() { };
|
||||
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>() { "Invalid" }, IsSuccessful = false };
|
||||
_mockMoveFileService.Setup(service => service.PreQueueValidation(It.IsAny<MoveFileRequest>())).Returns(validRequestResponse);
|
||||
// act
|
||||
var result = await _controller.MoveFile(request);
|
||||
|
||||
Assert.NotNull(result);
|
||||
var badRequest = result as BadRequestObjectResult;
|
||||
Assert.Equal(400, badRequest.StatusCode);
|
||||
Assert.Equal($"Request is invalid. Response: {String.Join("\n ", validRequestResponse.MessageStack.ToArray())}", badRequest.Value);
|
||||
_mockMoveFileService.Verify(service =>
|
||||
service.PreQueueValidation(It.IsAny<MoveFileRequest>()), Times.Once);
|
||||
_mockDirectFileOperationsService.Verify(service =>
|
||||
service.HandleDirectFileOperationsRequest(It.IsAny<MoveFileRequest>(), FileOperationType.Move), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MoveFile_Failure_UnexpectedException()
|
||||
{
|
||||
var request = new MoveFileRequest()
|
||||
{
|
||||
SourcePath = "",
|
||||
SourceFile = "",
|
||||
DestinationPath = ""
|
||||
};
|
||||
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>(), IsSuccessful = true };
|
||||
_mockMoveFileService.Setup(service => service.PreQueueValidation(It.IsAny<MoveFileRequest>())).Returns(validRequestResponse).Verifiable();
|
||||
_mockDirectFileOperationsService.Setup(service =>
|
||||
service.HandleDirectFileOperationsRequest(It.IsAny<MoveFileRequest>(), FileOperationType.Move))
|
||||
.ThrowsAsync(new Exception("Thrown From Unit Test"))
|
||||
.Verifiable();
|
||||
|
||||
|
||||
|
||||
var result = await _controller.MoveFile(request);
|
||||
Assert.NotNull(result);
|
||||
|
||||
var statusResult = result as StatusCodeResult;
|
||||
Assert.NotNull(statusResult);
|
||||
Assert.Equal(500, statusResult!.StatusCode);
|
||||
_mockMoveFileService.Verify(service =>
|
||||
service.PreQueueValidation(It.IsAny<MoveFileRequest>()), Times.Once);
|
||||
_mockDirectFileOperationsService.Verify(service =>
|
||||
service.HandleDirectFileOperationsRequest(It.IsAny<MoveFileRequest>(), FileOperationType.Move), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MoveFile_Failure_ServiceException()
|
||||
{
|
||||
var request = new MoveFileRequest()
|
||||
{
|
||||
SourcePath = "",
|
||||
SourceFile = "",
|
||||
DestinationPath = ""
|
||||
};
|
||||
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>(), IsSuccessful = true };
|
||||
_mockMoveFileService.Setup(service => service.PreQueueValidation(It.IsAny<MoveFileRequest>())).Returns(validRequestResponse).Verifiable();
|
||||
_mockDirectFileOperationsService.Setup(service =>
|
||||
service.HandleDirectFileOperationsRequest(It.IsAny<MoveFileRequest>(), FileOperationType.Move))
|
||||
.ThrowsAsync(new ServiceException("Thrown From Unit Test"))
|
||||
.Verifiable();
|
||||
|
||||
|
||||
|
||||
var result = await _controller.MoveFile(request);
|
||||
Assert.NotNull(result);
|
||||
|
||||
var statusResult = result as ObjectResult;
|
||||
Assert.NotNull(statusResult);
|
||||
Assert.Equal(500, statusResult!.StatusCode);
|
||||
_mockMoveFileService.Verify(service =>
|
||||
service.PreQueueValidation(It.IsAny<MoveFileRequest>()), Times.Once);
|
||||
_mockDirectFileOperationsService.Verify(service =>
|
||||
service.HandleDirectFileOperationsRequest(It.IsAny<MoveFileRequest>(), FileOperationType.Move), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AppendFilePost_Ok()
|
||||
{
|
||||
var request = new AppendFileRequest()
|
||||
{
|
||||
FileData = "Text to write",
|
||||
DestinationPath = "SomePath",
|
||||
Extension = ".txt",
|
||||
FileName = "FileName"
|
||||
};
|
||||
|
||||
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>(), IsSuccessful = true };
|
||||
_mockAppendFileService.Setup(service => service.PreQueueValidation(It.IsAny<AppendFileRequest>())).Returns(validRequestResponse);
|
||||
_mockDirectFileOperationsService.Setup(service =>
|
||||
service.HandleDirectFileOperationsRequest(It.IsAny<AppendFileRequest>(), FileOperationType.AppendFile))
|
||||
.ReturnsAsync(validRequestResponse)
|
||||
.Verifiable();
|
||||
|
||||
// act
|
||||
var result = await _controller.AppendFile(request);
|
||||
Assert.NotNull(result);
|
||||
var okResult = result as OkObjectResult;
|
||||
|
||||
// assert
|
||||
Assert.NotNull(okResult);
|
||||
Assert.Equal(200, okResult.StatusCode);
|
||||
_mockAppendFileService.Verify(service =>
|
||||
service.PreQueueValidation(It.IsAny<AppendFileRequest>()), Times.Once);
|
||||
_mockDirectFileOperationsService.Verify(service =>
|
||||
service.HandleDirectFileOperationsRequest(It.IsAny<AppendFileRequest>(), FileOperationType.AppendFile), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AppendFilePost_InvalidRequest()
|
||||
{
|
||||
// arrange
|
||||
var request = new AppendFileRequest() { };
|
||||
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>() { "Invalid" }, IsSuccessful = false };
|
||||
_mockAppendFileService.Setup(service => service.PreQueueValidation(It.IsAny<AppendFileRequest>())).Returns(validRequestResponse);
|
||||
// act
|
||||
var result = await _controller.AppendFile(request);
|
||||
|
||||
Assert.NotNull(result);
|
||||
var badRequest = result as BadRequestObjectResult;
|
||||
Assert.Equal(400, badRequest.StatusCode);
|
||||
Assert.Equal($"Request is invalid. Response: {String.Join("\n ", validRequestResponse.MessageStack.ToArray())}", badRequest.Value);
|
||||
_mockAppendFileService.Verify(service =>
|
||||
service.PreQueueValidation(It.IsAny<AppendFileRequest>()), Times.Once);
|
||||
_mockDirectFileOperationsService.Verify(service =>
|
||||
service.HandleDirectFileOperationsRequest(It.IsAny<AppendFileRequest>(), FileOperationType.AppendFile), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AppendFile_Failure_ServiceException()
|
||||
{
|
||||
var request = new AppendFileRequest()
|
||||
{
|
||||
FileData = "Text to write",
|
||||
DestinationPath = "SomePath",
|
||||
Extension = ".txt",
|
||||
FileName = "FileName"
|
||||
};
|
||||
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>(), IsSuccessful = true };
|
||||
_mockAppendFileService.Setup(service => service.PreQueueValidation(It.IsAny<AppendFileRequest>())).Returns(validRequestResponse).Verifiable();
|
||||
_mockDirectFileOperationsService.Setup(service =>
|
||||
service.HandleDirectFileOperationsRequest(It.IsAny<AppendFileRequest>(), FileOperationType.AppendFile))
|
||||
.ThrowsAsync(new ServiceException("Thrown From Unit Test"))
|
||||
.Verifiable();
|
||||
|
||||
|
||||
|
||||
var result = await _controller.AppendFile(request);
|
||||
Assert.NotNull(result);
|
||||
|
||||
var statusResult = result as ObjectResult;
|
||||
Assert.NotNull(statusResult);
|
||||
Assert.Equal(500, statusResult!.StatusCode);
|
||||
_mockAppendFileService.Verify(service =>
|
||||
service.PreQueueValidation(It.IsAny<AppendFileRequest>()), Times.Once);
|
||||
_mockDirectFileOperationsService.Verify(service =>
|
||||
service.HandleDirectFileOperationsRequest(It.IsAny<AppendFileRequest>(), FileOperationType.AppendFile), Times.Once);
|
||||
}
|
||||
}
|
||||
+302
@@ -0,0 +1,302 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using LCU.Api.Internal.FileOperations.Controllers;
|
||||
using LCU.Api.Internal.FileOperations.Services;
|
||||
using LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Api.Internal.FileOperations.Services.QueueServices;
|
||||
namespace LCU.Api.Internal.FileOperationsTests.Controllers;
|
||||
|
||||
public class FileOperationsControllerTest
|
||||
{
|
||||
private readonly FileOperationsController _controller;
|
||||
private readonly Mock<IQueueFileOperationsService> _mockFileOperationsService;
|
||||
private readonly Mock<IActionService<MoveFileRequest>> _mockMoveFileService;
|
||||
private readonly Mock<IActionService<CopyFileRequest>> _mockCopyFileService;
|
||||
private readonly Mock<IActionService<ModifyFileRequest>> _mockModifyFileService;
|
||||
private readonly Mock<IActionService<DeleteFileRequest>> _mockDeleteFileService;
|
||||
private readonly Mock<IActionService<RenameFileRequest>> _mockRenameFileService;
|
||||
private readonly Mock<IActionService<ZipDirectoryRequest>> _mockZipDirectoryService;
|
||||
private readonly Mock<IActionService<UnzipFileRequest>> _mockUnzipFileService;
|
||||
private readonly Mock<IActionService<UnzipPasswordProtectedFileRequest>> _mockUnzipPasswordProtectedFileService;
|
||||
private readonly Mock<IActionService<CreateDirectoryRequest>> _mockCreateDirectoryService;
|
||||
private readonly Mock<IActionService<RenameDirectoryRequest>> _mockRenameDirectoryService;
|
||||
private readonly Mock<IActionService<BulkOperationRequest<MoveFileRequest>>> _mockBulkMoveFileService;
|
||||
private readonly Mock<IActionService<EncryptFileDataRequest>> _mockEncryptFileDataService;
|
||||
private readonly Mock<IActionService<DecryptFileDataRequest>> _mockDecryptFileDataService;
|
||||
private readonly Mock<IActionService<WriteFileRequest>> _mockWriteFileDataService;
|
||||
private readonly Mock<IActionService<DeleteDirectoryRequest>> _mockDeleteDirectoryService;
|
||||
private readonly Mock<IActionService<DeleteEmptyDirectoryRequest>> _mockDeleteEmptyDirectoriesService;
|
||||
private readonly Mock<IDirectFileService> _mockDirectFileService;
|
||||
|
||||
public FileOperationsControllerTest()
|
||||
{
|
||||
_mockFileOperationsService = new Mock<IQueueFileOperationsService>();
|
||||
_mockMoveFileService = new Mock<IActionService<MoveFileRequest>>();
|
||||
_mockCopyFileService = new Mock<IActionService<CopyFileRequest>>();
|
||||
_mockModifyFileService = new Mock<IActionService<ModifyFileRequest>>();
|
||||
_mockDeleteFileService = new Mock<IActionService<DeleteFileRequest>>();
|
||||
_mockRenameFileService = new Mock<IActionService<RenameFileRequest>>();
|
||||
_mockZipDirectoryService = new Mock<IActionService<ZipDirectoryRequest>>();
|
||||
_mockUnzipFileService = new Mock<IActionService<UnzipFileRequest>>();
|
||||
_mockUnzipPasswordProtectedFileService = new Mock<IActionService<UnzipPasswordProtectedFileRequest>>();
|
||||
_mockCreateDirectoryService = new Mock<IActionService<CreateDirectoryRequest>>();
|
||||
_mockRenameDirectoryService = new Mock<IActionService<RenameDirectoryRequest>>();
|
||||
_mockBulkMoveFileService = new Mock<IActionService<BulkOperationRequest<MoveFileRequest>>>();
|
||||
_mockEncryptFileDataService = new Mock<IActionService<EncryptFileDataRequest>>();
|
||||
_mockDecryptFileDataService = new Mock<IActionService<DecryptFileDataRequest>>();
|
||||
_mockWriteFileDataService = new Mock<IActionService<WriteFileRequest>>();
|
||||
_mockDeleteDirectoryService = new Mock<IActionService<DeleteDirectoryRequest>>();
|
||||
_mockDeleteEmptyDirectoriesService = new Mock<IActionService<DeleteEmptyDirectoryRequest>>();
|
||||
_mockDirectFileService = new Mock<IDirectFileService>();
|
||||
|
||||
_controller = new FileOperationsController(
|
||||
_mockFileOperationsService.Object,
|
||||
_mockMoveFileService.Object,
|
||||
_mockCopyFileService.Object,
|
||||
_mockModifyFileService.Object,
|
||||
_mockDeleteFileService.Object,
|
||||
_mockRenameFileService.Object,
|
||||
_mockZipDirectoryService.Object,
|
||||
_mockUnzipFileService.Object,
|
||||
_mockUnzipPasswordProtectedFileService.Object,
|
||||
_mockCreateDirectoryService.Object,
|
||||
_mockRenameDirectoryService.Object,
|
||||
_mockBulkMoveFileService.Object,
|
||||
_mockEncryptFileDataService.Object,
|
||||
_mockDecryptFileDataService.Object,
|
||||
_mockWriteFileDataService.Object,
|
||||
_mockDeleteDirectoryService.Object,
|
||||
_mockDeleteEmptyDirectoriesService.Object,
|
||||
_mockDirectFileService.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFile_OK()
|
||||
{
|
||||
var bytes = new byte[1];
|
||||
_mockDirectFileService.Setup(service => service.GetFileBytes(It.IsAny<string>())).Returns(bytes);
|
||||
|
||||
// act
|
||||
var result = _controller.GetFile("\\\\fakeit\\file.csv");
|
||||
Assert.NotNull(result);
|
||||
var okResult = result as FileContentResult;
|
||||
|
||||
// assert
|
||||
Assert.NotNull(okResult);
|
||||
_mockDirectFileService.Verify(service => service.GetFileBytes(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFile_BadRequest_Pre()
|
||||
{
|
||||
var bytes = new byte[1];
|
||||
_mockDirectFileService.Setup(service => service.GetFileBytes(It.IsAny<string>())).Returns(bytes);
|
||||
|
||||
// act
|
||||
var result = _controller.GetFile(" ");
|
||||
Assert.NotNull(result);
|
||||
var badResult = result as BadRequestObjectResult;
|
||||
|
||||
// assert
|
||||
Assert.NotNull(badResult);
|
||||
Assert.Equal(400, badResult.StatusCode);
|
||||
_mockDirectFileService.Verify(service => service.GetFileBytes(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFile_BadRequest_Post()
|
||||
{
|
||||
_mockDirectFileService.Setup(service => service.GetFileBytes(It.IsAny<string>()))
|
||||
.Throws<ArgumentException>();
|
||||
|
||||
// act
|
||||
var result = _controller.GetFile("\\\\fakeit\\file.csv");
|
||||
Assert.NotNull(result);
|
||||
var badResult = result as BadRequestObjectResult;
|
||||
|
||||
// assert
|
||||
Assert.NotNull(badResult);
|
||||
Assert.Equal(400, badResult.StatusCode);
|
||||
_mockDirectFileService.Verify(service => service.GetFileBytes(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
// Post tests
|
||||
[Fact]
|
||||
public async Task MoveFilePost_OK()
|
||||
{
|
||||
// arrange
|
||||
var request = new MoveFileRequest()
|
||||
{
|
||||
SourcePath = "",
|
||||
SourceFile = "",
|
||||
DestinationPath = ""
|
||||
};
|
||||
|
||||
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>(), IsSuccessful = true };
|
||||
_mockMoveFileService.Setup(service => service.PreQueueValidation(It.IsAny<MoveFileRequest>())).Returns(validRequestResponse);
|
||||
|
||||
// act
|
||||
var result = await _controller.MoveFile(request);
|
||||
Assert.NotNull(result);
|
||||
var okResult = result as OkResult;
|
||||
|
||||
// assert
|
||||
Assert.NotNull(okResult);
|
||||
Assert.Equal(200, okResult.StatusCode);
|
||||
}
|
||||
|
||||
// Post tests
|
||||
[Fact]
|
||||
public async Task MoveFilePost_InvalidRequest()
|
||||
{
|
||||
// arrange
|
||||
var request = new MoveFileRequest() { };
|
||||
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>() { "Invalid" }, IsSuccessful = false };
|
||||
_mockMoveFileService.Setup(service => service.PreQueueValidation(It.IsAny<MoveFileRequest>())).Returns(validRequestResponse);
|
||||
// act
|
||||
var result = await _controller.MoveFile(request);
|
||||
|
||||
Assert.NotNull(result);
|
||||
var badRequest = result as BadRequestObjectResult;
|
||||
Assert.Equal(400, badRequest.StatusCode);
|
||||
Assert.Equal($"Request is invalid. Response: {String.Join("\n ", validRequestResponse.MessageStack.ToArray())}", badRequest.Value);
|
||||
}
|
||||
|
||||
// Post tests
|
||||
[Fact]
|
||||
public async Task CopyFilePost_OK()
|
||||
{
|
||||
// arrange
|
||||
var request = new CopyFileRequest()
|
||||
{
|
||||
SourcePath = "",
|
||||
SourceFile = "",
|
||||
DestinationPath = ""
|
||||
};
|
||||
|
||||
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>(), IsSuccessful = true };
|
||||
_mockCopyFileService.Setup(service => service.PreQueueValidation(It.IsAny<CopyFileRequest>())).Returns(validRequestResponse);
|
||||
|
||||
// act
|
||||
var result = await _controller.CopyFile(request);
|
||||
Assert.NotNull(result);
|
||||
var okResult = result as OkResult;
|
||||
|
||||
// assert
|
||||
Assert.NotNull(okResult);
|
||||
Assert.Equal(200, okResult.StatusCode);
|
||||
}
|
||||
|
||||
// Post tests
|
||||
[Fact]
|
||||
public async Task CopyFilePost_InvalidRequest()
|
||||
{
|
||||
// arrange
|
||||
var request = new CopyFileRequest() { };
|
||||
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>() { "Invalid" }, IsSuccessful = false };
|
||||
_mockCopyFileService.Setup(service => service.PreQueueValidation(It.IsAny<CopyFileRequest>())).Returns(validRequestResponse);
|
||||
// act
|
||||
var result = await _controller.CopyFile(request);
|
||||
|
||||
Assert.NotNull(result);
|
||||
var badRequest = result as BadRequestObjectResult;
|
||||
Assert.Equal(400, badRequest.StatusCode);
|
||||
Assert.Equal($"Request is invalid. Response: {String.Join("\n ", validRequestResponse.MessageStack.ToArray())}", badRequest.Value);
|
||||
}
|
||||
|
||||
// Post tests
|
||||
[Fact]
|
||||
public async Task UnzipPasswordProtectedFilePost_OK()
|
||||
{
|
||||
// arrange
|
||||
var request = new UnzipPasswordProtectedFileRequest()
|
||||
{
|
||||
SourceDirectory = "",
|
||||
PasswordProtectedZipFileName = "",
|
||||
Password = "",
|
||||
DestinationPath = ""
|
||||
};
|
||||
|
||||
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>(), IsSuccessful = true };
|
||||
_mockUnzipPasswordProtectedFileService.Setup(service => service.PreQueueValidation(It.IsAny<UnzipPasswordProtectedFileRequest>())).Returns(validRequestResponse);
|
||||
|
||||
// act
|
||||
var result = await _controller.UnzipPasswordProtectedFile(request);
|
||||
|
||||
// assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(200, (result as OkResult).StatusCode);
|
||||
}
|
||||
|
||||
// Post tests
|
||||
[Fact]
|
||||
public async Task UnzipPasswordProtectedFilePost_InvalidRequest()
|
||||
{
|
||||
// arrange
|
||||
var request = new UnzipPasswordProtectedFileRequest() { };
|
||||
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>() { "Invalid" }, IsSuccessful = false };
|
||||
_mockUnzipPasswordProtectedFileService.Setup(service => service.PreQueueValidation(It.IsAny<UnzipPasswordProtectedFileRequest>())).Returns(validRequestResponse);
|
||||
// act
|
||||
var result = await _controller.UnzipPasswordProtectedFile(request);
|
||||
|
||||
Assert.NotNull(result);
|
||||
var badRequest = result as BadRequestObjectResult;
|
||||
Assert.Equal(400, badRequest.StatusCode);
|
||||
Assert.Equal($"Request is invalid. Response: {String.Join("\n ", validRequestResponse.MessageStack.ToArray())}", badRequest.Value);
|
||||
}
|
||||
|
||||
// Post tests
|
||||
[Fact]
|
||||
public async Task HandleRequestResponsePost_CopyFileRequest_OK()
|
||||
{
|
||||
// arrange
|
||||
var request = new CopyFileRequest()
|
||||
{
|
||||
SourcePath = "",
|
||||
SourceFile = "",
|
||||
DestinationPath = ""
|
||||
};
|
||||
|
||||
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>(), IsSuccessful = true };
|
||||
_mockCopyFileService.Setup(service => service.PreQueueValidation(It.IsAny<CopyFileRequest>())).Returns(validRequestResponse);
|
||||
|
||||
// act
|
||||
var result = await _controller.HandleRequestResponse<CopyFileRequest>(request, FileOperationType.Copy, _mockCopyFileService.Object);
|
||||
Assert.NotNull(result);
|
||||
var okResult = result as OkResult;
|
||||
|
||||
// assert
|
||||
Assert.NotNull(okResult);
|
||||
Assert.Equal(200, okResult.StatusCode);
|
||||
}
|
||||
|
||||
// Post tests
|
||||
[Fact]
|
||||
public async Task HandleRequestResponsePost_CreateDirectoryRequest_OK()
|
||||
{
|
||||
// arrange
|
||||
var request = new CreateDirectoryRequest()
|
||||
{
|
||||
DirectoryName = "",
|
||||
DirectoryPath = ""
|
||||
};
|
||||
|
||||
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>(), IsSuccessful = true };
|
||||
_mockCreateDirectoryService.Setup(service => service.PreQueueValidation(It.IsAny<CreateDirectoryRequest>())).Returns(validRequestResponse);
|
||||
|
||||
// act
|
||||
var result = await _controller.HandleRequestResponse<CreateDirectoryRequest>(request, FileOperationType.CreateDirectory,
|
||||
_mockCreateDirectoryService.Object);
|
||||
Assert.NotNull(result);
|
||||
var okResult = result as OkResult;
|
||||
|
||||
// assert
|
||||
Assert.NotNull(okResult);
|
||||
Assert.Equal(200, okResult.StatusCode);
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using LCU.Library.Internal.Exceptions;
|
||||
using LCU.Api.Internal.FileOperations.Controllers;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Api.Internal.FileOperations.Services.QueueServices;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperationsTests.Controllers
|
||||
{
|
||||
public class IndexFileControllerTest
|
||||
{
|
||||
private IndexFileController _controller;
|
||||
private Mock<IQueueFileOperationsService> _mockQueueFileOperationsService;
|
||||
|
||||
public IndexFileControllerTest()
|
||||
{
|
||||
_mockQueueFileOperationsService = new Mock<IQueueFileOperationsService>();
|
||||
_controller = new IndexFileController(_mockQueueFileOperationsService.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BatchPdfParser_OK()
|
||||
{
|
||||
var request = new ExtractIndexFileDataRequest()
|
||||
{
|
||||
IndexFilePath = "\\test\test",
|
||||
IndexFileName = "Test.csv"
|
||||
};
|
||||
_mockQueueFileOperationsService.Setup(service => service.QueueFileOperations(It.IsAny<ExtractIndexFileDataRequest>(), It.IsAny<FileOperationType>())).Verifiable();
|
||||
|
||||
var result = await _controller.QueueExtractIndexFileData(request);
|
||||
Assert.NotNull(result);
|
||||
|
||||
var acceptedResult = result as AcceptedResult;
|
||||
Assert.NotNull(acceptedResult);
|
||||
Assert.Equal(202, acceptedResult!.StatusCode);
|
||||
_mockQueueFileOperationsService.Verify();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExtractIndexFileDataRequest_ServiceException_InternalServerError()
|
||||
{
|
||||
var request = new ExtractIndexFileDataRequest()
|
||||
{
|
||||
IndexFilePath = "\\test\test",
|
||||
IndexFileName = "Test.csv"
|
||||
};
|
||||
_mockQueueFileOperationsService.Setup(service => service.QueueFileOperations(It.IsAny<ExtractIndexFileDataRequest>(), It.IsAny<FileOperationType>()))
|
||||
.Throws(new ServiceException("Thrown From Unit Test")).Verifiable();
|
||||
|
||||
var result = await _controller.QueueExtractIndexFileData(request);
|
||||
Assert.NotNull(result);
|
||||
var statusResult = result as ObjectResult;
|
||||
Assert.NotNull(statusResult);
|
||||
Assert.Equal(500, statusResult!.StatusCode);
|
||||
_mockQueueFileOperationsService.Verify();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExtractIndexFileDataRequest_UnexpectedException_InternalServerError()
|
||||
{
|
||||
var request = new ExtractIndexFileDataRequest()
|
||||
{
|
||||
IndexFilePath = "\\test\test",
|
||||
IndexFileName = "Test.csv"
|
||||
};
|
||||
_mockQueueFileOperationsService.Setup(service => service.QueueFileOperations(It.IsAny<ExtractIndexFileDataRequest>(), It.IsAny<FileOperationType>()))
|
||||
.Throws(new Exception("Thrown From Unit Test")).Verifiable();
|
||||
|
||||
var result = await _controller.QueueExtractIndexFileData(request);
|
||||
Assert.NotNull(result);
|
||||
var statusResult = result as StatusCodeResult;
|
||||
Assert.NotNull(statusResult);
|
||||
Assert.Equal(500, statusResult!.StatusCode);
|
||||
_mockQueueFileOperationsService.Verify();
|
||||
}
|
||||
}
|
||||
}
|
||||
+330
@@ -0,0 +1,330 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using LCU.Library.Internal.Exceptions;
|
||||
using LCU.Api.Internal.FileOperations.Controllers;
|
||||
using LCU.Api.Internal.FileOperations.Services;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.HtmlToPdfModels;
|
||||
using LCU.Api.Internal.FileOperations.Services.QueueServices;
|
||||
using LCU.Api.Internal.FileOperations.Services.PdfServices;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperationsTests.Controllers
|
||||
{
|
||||
public class PdfControllerTest
|
||||
{
|
||||
private PdfController _pdfController;
|
||||
private Mock<IQueueFileOperationsService> _mockQueueFileOperationsService;
|
||||
private Mock<IPdfParserService> _mockPdfParserService;
|
||||
private Mock<IBatchPdfParserService> _mockBatchPdfParserService;
|
||||
private Mock<IDirectFileService> _mockDirectFileService;
|
||||
private Mock<IHtmlToPdfService> _mockHtmlToPdfService;
|
||||
|
||||
public PdfControllerTest()
|
||||
{
|
||||
_mockQueueFileOperationsService = new Mock<IQueueFileOperationsService>();
|
||||
_mockPdfParserService = new Mock<IPdfParserService>();
|
||||
_mockBatchPdfParserService = new Mock<IBatchPdfParserService>();
|
||||
_mockDirectFileService = new Mock<IDirectFileService>();
|
||||
_mockHtmlToPdfService = new Mock<IHtmlToPdfService>();
|
||||
_pdfController = new PdfController(_mockQueueFileOperationsService.Object,
|
||||
_mockPdfParserService.Object,
|
||||
_mockBatchPdfParserService.Object,
|
||||
_mockDirectFileService.Object,
|
||||
_mockHtmlToPdfService.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BatchPdfParser_OK()
|
||||
{
|
||||
var batchPdfParserRequest = new BatchParsePdfRequest()
|
||||
{
|
||||
RootPath = "\\test\test",
|
||||
PdfName = "Test.pdf",
|
||||
BatchPdfs = new List<BatchPdf>()
|
||||
{
|
||||
new BatchPdf
|
||||
{
|
||||
PageStart = 1,
|
||||
PageStop = 2
|
||||
}
|
||||
}
|
||||
};
|
||||
var requestResponse = new RequestResponse
|
||||
{
|
||||
IsSuccessful = true
|
||||
};
|
||||
_mockBatchPdfParserService.Setup(service => service.IsRequestValid(batchPdfParserRequest, It.IsAny<RequestResponse>()))
|
||||
.Returns(requestResponse).Verifiable();
|
||||
_mockQueueFileOperationsService.Setup(service => service.QueueFileOperations(It.IsAny<BatchParsePdfRequest>(), It.IsAny<FileOperationType>())).Verifiable();
|
||||
|
||||
var result = await _pdfController.QueueBatchPdfParser(batchPdfParserRequest);
|
||||
Assert.NotNull(result);
|
||||
|
||||
var acceptedResult = result as AcceptedResult;
|
||||
Assert.NotNull(acceptedResult);
|
||||
Assert.Equal(202, acceptedResult!.StatusCode);
|
||||
_mockBatchPdfParserService.Verify();
|
||||
_mockQueueFileOperationsService.Verify();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BatchParsePdfRequest_ServiceException_InternalServerError()
|
||||
{
|
||||
var batchPdfParserRequest = new BatchParsePdfRequest()
|
||||
{
|
||||
RootPath = "\\test\test",
|
||||
PdfName = "Test.pdf",
|
||||
BatchPdfs = new List<BatchPdf>()
|
||||
{
|
||||
new BatchPdf
|
||||
{
|
||||
PageStart = 1,
|
||||
PageStop = 2
|
||||
}
|
||||
}
|
||||
};
|
||||
var requestResponse = new RequestResponse
|
||||
{
|
||||
IsSuccessful = true
|
||||
};
|
||||
_mockBatchPdfParserService.Setup(service => service.IsRequestValid(batchPdfParserRequest, It.IsAny<RequestResponse>()))
|
||||
.Returns(requestResponse).Verifiable();
|
||||
_mockQueueFileOperationsService.Setup(service => service.QueueFileOperations(It.IsAny<BatchParsePdfRequest>(), It.IsAny<FileOperationType>()))
|
||||
.Throws(new ServiceException("Thrown From Unit Test")).Verifiable();
|
||||
|
||||
var result = await _pdfController.QueueBatchPdfParser(batchPdfParserRequest);
|
||||
Assert.NotNull(result);
|
||||
var statusResult = result as ObjectResult;
|
||||
Assert.NotNull(statusResult);
|
||||
Assert.Equal(500, statusResult!.StatusCode);
|
||||
_mockBatchPdfParserService.Verify();
|
||||
_mockQueueFileOperationsService.Verify();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BatchParsePdfRequest_UnexpectedException_InternalServerError()
|
||||
{
|
||||
var batchPdfParserRequest = new BatchParsePdfRequest()
|
||||
{
|
||||
RootPath = "\\test\test",
|
||||
PdfName = "Test.pdf",
|
||||
BatchPdfs = new List<BatchPdf>()
|
||||
{
|
||||
new BatchPdf
|
||||
{
|
||||
PageStart = 1,
|
||||
PageStop = 2
|
||||
}
|
||||
}
|
||||
};
|
||||
var requestResponse = new RequestResponse
|
||||
{
|
||||
IsSuccessful = true
|
||||
};
|
||||
_mockBatchPdfParserService.Setup(service => service.IsRequestValid(batchPdfParserRequest, It.IsAny<RequestResponse>()))
|
||||
.Returns(requestResponse).Verifiable();
|
||||
_mockQueueFileOperationsService.Setup(service => service.QueueFileOperations(It.IsAny<BatchParsePdfRequest>(), It.IsAny<FileOperationType>()))
|
||||
.Throws(new Exception("Thrown From Unit Test")).Verifiable();
|
||||
|
||||
var result = await _pdfController.QueueBatchPdfParser(batchPdfParserRequest);
|
||||
Assert.NotNull(result);
|
||||
var statusResult = result as StatusCodeResult;
|
||||
Assert.NotNull(statusResult);
|
||||
Assert.Equal(500, statusResult!.StatusCode);
|
||||
_mockBatchPdfParserService.Verify();
|
||||
_mockQueueFileOperationsService.Verify();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParsePdf_OK()
|
||||
{
|
||||
var parsePdfRequest = new ParsePdfRequest()
|
||||
{
|
||||
RootPath = "\\test\test",
|
||||
PdfName = "Test.pdf",
|
||||
PageStart = 1,
|
||||
PageStop = 2
|
||||
};
|
||||
var newPdfResponse = "test.pdf";
|
||||
_mockPdfParserService.Setup(service => service.ParsePdf(parsePdfRequest)).Returns(newPdfResponse).Verifiable();
|
||||
|
||||
var result = _pdfController.ParsePdf(parsePdfRequest);
|
||||
Assert.NotNull(result);
|
||||
|
||||
var okResult = result as OkObjectResult;
|
||||
Assert.NotNull(okResult);
|
||||
Assert.Equal(200, okResult!.StatusCode);
|
||||
_mockPdfParserService.Verify();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParsePdf_ServiceException_InternalServerError()
|
||||
{
|
||||
var parsePdfRequest = new ParsePdfRequest()
|
||||
{
|
||||
RootPath = "\\test\test",
|
||||
PdfName = "Test.pdf",
|
||||
PageStart = 1,
|
||||
PageStop = 2
|
||||
};
|
||||
_mockPdfParserService.Setup(service => service.ParsePdf(parsePdfRequest))
|
||||
.Throws(new ServiceException("Thrown From Unit Test")).Verifiable();
|
||||
|
||||
var result = _pdfController.ParsePdf(parsePdfRequest);
|
||||
Assert.NotNull(result);
|
||||
|
||||
var statusResult = result as ObjectResult;
|
||||
Assert.NotNull(statusResult);
|
||||
Assert.Equal(500, statusResult!.StatusCode);
|
||||
_mockPdfParserService.Verify();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParsePdf_UnexpectedException_InternalServerError()
|
||||
{
|
||||
var parsePdfRequest = new ParsePdfRequest()
|
||||
{
|
||||
RootPath = "\\test\test",
|
||||
PdfName = "Test.pdf",
|
||||
PageStart = 1,
|
||||
PageStop = 2
|
||||
};
|
||||
_mockPdfParserService.Setup(service => service.ParsePdf(parsePdfRequest))
|
||||
.Throws(new Exception("Thrown From Unit Test")).Verifiable();
|
||||
|
||||
var result = _pdfController.ParsePdf(parsePdfRequest);
|
||||
Assert.NotNull(result);
|
||||
|
||||
var statusResult = result as StatusCodeResult;
|
||||
Assert.NotNull(statusResult);
|
||||
Assert.Equal(500, statusResult!.StatusCode);
|
||||
_mockPdfParserService.Verify();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPdfConent_OK()
|
||||
{
|
||||
var pdfPath = "\\\\Test\\PdfPath\\test.pdf";
|
||||
var btyes = new byte[23122321];
|
||||
_mockDirectFileService.Setup(service => service.GetFileBytes(pdfPath)).Returns(btyes).Verifiable();
|
||||
|
||||
var result = _pdfController.GetPdfContent(pdfPath);
|
||||
var okResult = result as FileContentResult;
|
||||
Assert.NotNull(okResult);
|
||||
_mockDirectFileService.Verify();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPdfConent_UnexpectedException_InternalServerError()
|
||||
{
|
||||
var pdfPath = "\\\\Test\\PdfPath\\test.pdf";
|
||||
_mockDirectFileService.Setup(service => service.GetFileBytes(pdfPath))
|
||||
.Throws(new Exception("Thrown From Unit Test")).Verifiable();
|
||||
|
||||
var result = _pdfController.GetPdfContent(pdfPath);
|
||||
Assert.NotNull(result);
|
||||
|
||||
var statusResult = result as StatusCodeResult;
|
||||
Assert.NotNull(statusResult);
|
||||
Assert.Equal(500, statusResult!.StatusCode);
|
||||
_mockPdfParserService.Verify();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPdfConent_ArgumentException_InternalServerError()
|
||||
{
|
||||
var pdfPath = "\\\\Test\\PdfPath\\test.pdf";
|
||||
_mockDirectFileService.Setup(service => service.GetFileBytes(pdfPath))
|
||||
.Throws(new ArgumentException("Thrown From Unit Test")).Verifiable();
|
||||
|
||||
var result = _pdfController.GetPdfContent(pdfPath);
|
||||
Assert.NotNull(result);
|
||||
var statusResult = result as BadRequestObjectResult;
|
||||
Assert.NotNull(statusResult);
|
||||
Assert.Equal(400, statusResult!.StatusCode);
|
||||
_mockPdfParserService.Verify();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConvertHtmlToPdf_OK()
|
||||
{
|
||||
var htmlToPdfRequest = new HtmlToPdfRequest()
|
||||
{
|
||||
HtmlTemplateFilePath = "\\Test",
|
||||
BodyTagValues = new List<TagValue>
|
||||
{
|
||||
new TagValue
|
||||
{
|
||||
Tag = "Test Tag",
|
||||
Value = "Test Value"
|
||||
}
|
||||
}
|
||||
};
|
||||
var btyes = new byte[23122321];
|
||||
_mockHtmlToPdfService.Setup(service => service.ConvertHtmlToPdf(htmlToPdfRequest)).Returns(btyes).Verifiable();
|
||||
|
||||
var result = _pdfController.ConvertHtmlToPdf(htmlToPdfRequest);
|
||||
var okResult = result as FileContentResult;
|
||||
Assert.NotNull(okResult);
|
||||
_mockDirectFileService.Verify();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConvertHtmlToPdf_ServiceExceptionException_InternalServerError()
|
||||
{
|
||||
var htmlToPdfRequest = new HtmlToPdfRequest()
|
||||
{
|
||||
HtmlTemplateFilePath = "\\Test",
|
||||
BodyTagValues = new List<TagValue>
|
||||
{
|
||||
new TagValue
|
||||
{
|
||||
Tag = "Test Tag",
|
||||
Value = "Test Value"
|
||||
}
|
||||
}
|
||||
};
|
||||
_mockHtmlToPdfService.Setup(service => service.ConvertHtmlToPdf(htmlToPdfRequest))
|
||||
.Throws(new ServiceException("Thrown From Unit Test")).Verifiable();
|
||||
|
||||
var result = _pdfController.ConvertHtmlToPdf(htmlToPdfRequest);
|
||||
Assert.NotNull(result);
|
||||
|
||||
var statusResult = result as ObjectResult;
|
||||
Assert.NotNull(statusResult);
|
||||
Assert.Equal(500, statusResult!.StatusCode);
|
||||
_mockPdfParserService.Verify();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetConvertHtmlToPdf_UnexpectedExceptionException_InternalServerError()
|
||||
{
|
||||
var htmlToPdfRequest = new HtmlToPdfRequest()
|
||||
{
|
||||
HtmlTemplateFilePath = "\\Test",
|
||||
BodyTagValues = new List<TagValue>
|
||||
{
|
||||
new TagValue
|
||||
{
|
||||
Tag = "Test Tag",
|
||||
Value = "Test Value"
|
||||
}
|
||||
}
|
||||
};
|
||||
_mockHtmlToPdfService.Setup(service => service.ConvertHtmlToPdf(htmlToPdfRequest))
|
||||
.Throws(new Exception("Thrown From Unit Test")).Verifiable();
|
||||
|
||||
var result = _pdfController.ConvertHtmlToPdf(htmlToPdfRequest);
|
||||
Assert.NotNull(result);
|
||||
|
||||
var statusResult = result as StatusCodeResult;
|
||||
Assert.NotNull(statusResult);
|
||||
Assert.Equal(500, statusResult!.StatusCode);
|
||||
_mockPdfParserService.Verify();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user