Adding
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using LCU.Api.Internal.FTP.Controllers;
|
||||
using LCU.Api.Internal.FTP.Services;
|
||||
using LCU.Api.Internal.FTPLib.Models;
|
||||
using LCU.Api.Internal.FTP.Services.FtpOpertionServices;
|
||||
namespace LCU.Api.Internal.FTPTests.Controllers;
|
||||
|
||||
public class SftpControllerTest
|
||||
{
|
||||
private readonly SftpController controller;
|
||||
private readonly Mock<IQueueFtpService> _mockFtpService;
|
||||
private readonly Mock<IWinScpListDirectoryService> _winScpListDirectoryService;
|
||||
|
||||
public SftpControllerTest()
|
||||
{
|
||||
_mockFtpService = new Mock<IQueueFtpService>();
|
||||
_winScpListDirectoryService = new Mock<IWinScpListDirectoryService>();
|
||||
controller = new SftpController(_mockFtpService.Object, _winScpListDirectoryService.Object);
|
||||
}
|
||||
|
||||
// Post tests
|
||||
[Fact]
|
||||
public void Post_OK()
|
||||
{
|
||||
// arrange
|
||||
var request = new SftpRequest()
|
||||
{
|
||||
Protocol = ProtocolType.Sftp,
|
||||
HostName = "Test Host",
|
||||
UserName = "Test UserName",
|
||||
Password = "Test Password",
|
||||
LocalFilePath = "Test LocalFilePath",
|
||||
RemoteFilePath = "Test RemoteFilePath"
|
||||
};
|
||||
|
||||
Assert.Equal(22, request.PortNumber);
|
||||
|
||||
// act
|
||||
var result = controller.Post(request);
|
||||
Assert.NotNull(result);
|
||||
var okResult = result as OkResult;
|
||||
|
||||
// assert
|
||||
Assert.NotNull(okResult);
|
||||
Assert.Equal(200, okResult.StatusCode);
|
||||
}
|
||||
|
||||
// Post tests
|
||||
[Fact]
|
||||
public void Post_InvalidSftp_NoHost()
|
||||
{
|
||||
// arrange
|
||||
var request = new SftpRequest() { };
|
||||
|
||||
// act
|
||||
var result = controller.Post(request);
|
||||
|
||||
Assert.NotNull(result);
|
||||
var badRequest = result as BadRequestObjectResult;
|
||||
Assert.Equal(400, badRequest.StatusCode);
|
||||
Assert.Equal("Sftp request can not be created into a valid session options.", badRequest.Value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
|
||||
<PackageReference Include="Moq" Version="4.13.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\LCU.Api.Internal.FTP\LCU.Api.Internal.FTP.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,236 @@
|
||||
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<IQueueFtpService> _mockQueueFtpService;
|
||||
private Mock<IWinScpSftpUploadFileService> _mockWinScpUploadFileService;
|
||||
private Mock<IWinScpSftpDownloadFilesService> _mockWinScpDownloadFilesService;
|
||||
private Mock<ISshFtpService> _mockSshFtpService;
|
||||
|
||||
public FtpServiceTest()
|
||||
{
|
||||
var ftpApiConfiguration = new FtpApiConfiguration()
|
||||
{
|
||||
AttemptsLimit = 2
|
||||
};
|
||||
_mockQueueFtpService = new Mock<IQueueFtpService>();
|
||||
_mockWinScpUploadFileService = new Mock<IWinScpSftpUploadFileService>();
|
||||
_mockWinScpDownloadFilesService = new Mock<IWinScpSftpDownloadFilesService>();
|
||||
_mockSshFtpService = new Mock<ISshFtpService>();
|
||||
|
||||
_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<QueueRequest>())).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<QueueRequest>()), Times.Once);
|
||||
_mockWinScpDownloadFilesService.Verify(x => x.ExecuteAction(It.IsAny<QueueRequest>()), Times.Never);
|
||||
_mockSshFtpService.Verify(x => x.ExecuteAction(It.IsAny<QueueRequest>()), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PerformFtpOperation_WinScpDownloadFilesService_Ran_Returned_SuccessResponse()
|
||||
{
|
||||
var ftpRequestRequestResponse = new FtpRequestResponse()
|
||||
{
|
||||
IsSuccessful = true
|
||||
};
|
||||
_mockWinScpDownloadFilesService.Setup(x => x.ExecuteAction(It.IsAny<QueueRequest>())).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<QueueRequest>()), Times.Never);
|
||||
_mockWinScpDownloadFilesService.Verify(x => x.ExecuteAction(It.IsAny<QueueRequest>()), Times.Once);
|
||||
_mockSshFtpService.Verify(x => x.ExecuteAction(It.IsAny<QueueRequest>()), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PerformFtpOperation_SshFtpService_Ran_Returned_SuccessResponse()
|
||||
{
|
||||
var ftpRequestRequestResponse = new FtpRequestResponse()
|
||||
{
|
||||
IsSuccessful = true
|
||||
};
|
||||
_mockSshFtpService.Setup(x => x.ExecuteAction(It.IsAny<QueueRequest>())).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<QueueRequest>()), Times.Never);
|
||||
_mockWinScpDownloadFilesService.Verify(x => x.ExecuteAction(It.IsAny<QueueRequest>()), Times.Never);
|
||||
_mockSshFtpService.Verify(x => x.ExecuteAction(It.IsAny<QueueRequest>()), 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<QueueRequest>()), Times.Never);
|
||||
_mockWinScpDownloadFilesService.Verify(x => x.ExecuteAction(It.IsAny<QueueRequest>()), Times.Never);
|
||||
_mockSshFtpService.Verify(x => x.ExecuteAction(It.IsAny<QueueRequest>()), 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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<!-- additional settings used during the dotnet test execution step in the azure dev ops pipeline -->
|
||||
|
||||
<RunSettings>
|
||||
|
||||
<DataCollectionRunSettings>
|
||||
<DataCollectors>
|
||||
<DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<Configuration>
|
||||
<CodeCoverage>
|
||||
|
||||
|
||||
<CompanyNames>
|
||||
<Exclude>
|
||||
<CompanyName>.*microsoft.*</CompanyName>
|
||||
</Exclude>
|
||||
</CompanyNames>
|
||||
|
||||
</CodeCoverage>
|
||||
</Configuration>
|
||||
</DataCollector>
|
||||
|
||||
</DataCollectors>
|
||||
</DataCollectionRunSettings>
|
||||
|
||||
</RunSettings>
|
||||
Reference in New Issue
Block a user