using Moq; using Xunit; using Newtonsoft.Json; using LCU.Api.Internal.FTP.Configuration; using LCU.Api.Internal.FTP.Services; using LCU.Api.Internal.FTP.Services.FtpOpertionServices; using LCU.Api.Internal.FTPLib.Models; using LCU.Api.Internal.FTPLib.Models.Enums; namespace LCU.Api.Internal.FTPTests.Services; public class FtpServiceTest { private readonly FtpService _ftpService; private Mock _mockQueueFtpService; private Mock _mockWinScpUploadFileService; private Mock _mockWinScpDownloadFilesService; private Mock _mockSshFtpService; public FtpServiceTest() { var ftpApiConfiguration = new FtpApiConfiguration() { AttemptsLimit = 2 }; _mockQueueFtpService = new Mock(); _mockWinScpUploadFileService = new Mock(); _mockWinScpDownloadFilesService = new Mock(); _mockSshFtpService = new Mock(); _ftpService = new FtpService(_mockQueueFtpService.Object, _mockWinScpUploadFileService.Object, _mockWinScpDownloadFilesService.Object, _mockSshFtpService.Object, ftpApiConfiguration); } [Fact] public void Basic_Construct_Test() { Assert.NotNull(_ftpService); } [Fact] public void PerformFtpOperation_WinScpUploadFileService_Ran_Returned_SuccessResponse() { var ftpRequestRequestResponse = new FtpRequestResponse() { IsSuccessful = true }; _mockWinScpUploadFileService.Setup(x => x.ExecuteAction(It.IsAny())).Returns(ftpRequestRequestResponse); var request = new SftpUploadFileRequest() { SessionInformation = new SessionInformation() { Protocol = 0, HostName = "TestHostName", PortNumber = 22, UserName = "TestUserName", Password = "TestPassword", SshHostKeyFingerprint = "TestSshHostKeyFingerprint" }, LocalFileName = "TestLocalFileName", LocalFilePath = "\\TestLocalFilePath\\", RemoteFilePath = "\\TestRemoteFilePath\\" }; var queueRequest = new QueueRequest() { Request = JsonConvert.SerializeObject(request), FtpType = FtpType.WinScpSftpUpload }; var result = _ftpService.PerformFtpOperation(queueRequest); Assert.True(result.IsSuccessful); _mockWinScpUploadFileService.Verify(x => x.ExecuteAction(It.IsAny()), Times.Once); _mockWinScpDownloadFilesService.Verify(x => x.ExecuteAction(It.IsAny()), Times.Never); _mockSshFtpService.Verify(x => x.ExecuteAction(It.IsAny()), Times.Never); } [Fact] public void PerformFtpOperation_WinScpDownloadFilesService_Ran_Returned_SuccessResponse() { var ftpRequestRequestResponse = new FtpRequestResponse() { IsSuccessful = true }; _mockWinScpDownloadFilesService.Setup(x => x.ExecuteAction(It.IsAny())).Returns(ftpRequestRequestResponse); var request = new SftpDownloadFileRequest() { SessionInformation = new SessionInformation() { Protocol = 0, HostName = "TestHostName", PortNumber = 22, UserName = "TestUserName", Password = "TestPassword", SshHostKeyFingerprint = "TestSshHostKeyFingerprint" }, LocalFilePath = "\\TestLocalFilePath\\", RemoteFilePath = "\\TestRemoteFilePath\\", RemoteFileName = "TestRemoteFileName" }; var queueRequest = new QueueRequest() { Request = JsonConvert.SerializeObject(request), FtpType = FtpType.WinScpSftpDownload }; var result = _ftpService.PerformFtpOperation(queueRequest); Assert.True(result.IsSuccessful); _mockWinScpUploadFileService.Verify(x => x.ExecuteAction(It.IsAny()), Times.Never); _mockWinScpDownloadFilesService.Verify(x => x.ExecuteAction(It.IsAny()), Times.Once); _mockSshFtpService.Verify(x => x.ExecuteAction(It.IsAny()), Times.Never); } [Fact] public void PerformFtpOperation_SshFtpService_Ran_Returned_SuccessResponse() { var ftpRequestRequestResponse = new FtpRequestResponse() { IsSuccessful = true }; _mockSshFtpService.Setup(x => x.ExecuteAction(It.IsAny())).Returns(ftpRequestRequestResponse); var request = new SshFtpRequest() { Protocol = 0, HostName = "TestHostName", PortNumber = 22, UserName = "TestUserName", Password = "TestPassword", LocalFileData = new byte[1], RemoteFilePath = "\\TestRemoteFilePath\\" }; var queueRequest = new QueueRequest() { Request = JsonConvert.SerializeObject(request), FtpType = FtpType.SshFtp }; var result = _ftpService.PerformFtpOperation(queueRequest); Assert.True(result.IsSuccessful); _mockWinScpUploadFileService.Verify(x => x.ExecuteAction(It.IsAny()), Times.Never); _mockWinScpDownloadFilesService.Verify(x => x.ExecuteAction(It.IsAny()), Times.Never); _mockSshFtpService.Verify(x => x.ExecuteAction(It.IsAny()), Times.Once); } [Fact] public void PerformFtpOperation_Exception_NoType_Returned_FailResponse() { var request = new SshFtpRequest() { Protocol = 0, HostName = "TestHostName", PortNumber = 22, UserName = "TestUserName", Password = "TestPassword", LocalFileData = new byte[1], RemoteFilePath = "\\TestRemoteFilePath\\" }; var queueRequest = new QueueRequest() { Request = JsonConvert.SerializeObject(request) }; var result = _ftpService.PerformFtpOperation(queueRequest); Assert.False(result.IsSuccessful); _mockWinScpUploadFileService.Verify(x => x.ExecuteAction(It.IsAny()), Times.Never); _mockWinScpDownloadFilesService.Verify(x => x.ExecuteAction(It.IsAny()), Times.Never); _mockSshFtpService.Verify(x => x.ExecuteAction(It.IsAny()), Times.Never); } [Fact] public void ShouldPerformFtpRequestAgain_AttempsLessThenLimit_True() { var request = new QueueRequest() { Attempts = 0 }; var ftpRequestRequestResponse = new FtpRequestResponse() { IsSuccessful = false }; var result = _ftpService.ShouldPerformFtpRequestAgain(request, ftpRequestRequestResponse); Assert.True(result); } [Fact] public void ShouldPerformFtpRequestAgain_AttempsGreaterThenLimit_False() { var request = new QueueRequest() { Attempts = 3 }; var ftpRequestRequestResponse = new FtpRequestResponse() { IsSuccessful = false }; var result = _ftpService.ShouldPerformFtpRequestAgain(request, ftpRequestRequestResponse); Assert.False(result); } [Fact] public void ShouldPerformFtpRequestAgain_SuccessfullyFtpRequest_False() { var request = new QueueRequest() { Attempts = 1 }; var ftpRequestRequestResponse = new FtpRequestResponse() { IsSuccessful = true }; var result = _ftpService.ShouldPerformFtpRequestAgain(request, ftpRequestRequestResponse); Assert.False(result); } }