Skip to content

Commit 9480529

Browse files
committed
update tests
1 parent 9e91ed0 commit 9480529

File tree

10 files changed

+203
-37
lines changed

10 files changed

+203
-37
lines changed

FileSystem.Adapters.Sftp/src/SftpAdapter.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Net.Sockets;
56
using System.Text;
67
using System.Threading;
78
using System.Threading.Tasks;
@@ -188,7 +189,7 @@ public override async Task<byte[]> ReadFileAsync(string path, CancellationToken
188189
using var fileStream = client.OpenRead(PrependRootPath(path));
189190
var fileContents = new byte[fileStream.Length];
190191

191-
await fileStream.ReadAsync(fileContents, 0, (int) fileStream.Length, cancellationToken);
192+
var _ = await fileStream.ReadAsync(fileContents, 0, (int) fileStream.Length, cancellationToken);
192193

193194
return fileContents;
194195
}
@@ -268,6 +269,21 @@ private static Exception Exception(Exception exception)
268269
return new ConnectionException(sshConnectionException);
269270
}
270271

272+
if (exception is SocketException socketException)
273+
{
274+
return new ConnectionException(socketException);
275+
}
276+
277+
if (exception is SshAuthenticationException sshAuthenticationException)
278+
{
279+
return new ConnectionException(sshAuthenticationException);
280+
}
281+
282+
if (exception is ProxyException proxyException)
283+
{
284+
return new ConnectionException(proxyException);
285+
}
286+
271287
return new AdapterRuntimeException(exception);
272288
}
273289
}

FileSystem/src/Adapters/LocalAdapter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public override async Task<byte[]> ReadFileAsync(string path, CancellationToken
157157
using var fileStream = new FileStream(PrependRootPath(path), FileMode.Open);
158158
var fileContents = new byte[fileStream.Length];
159159

160-
await fileStream.ReadAsync(fileContents, 0, (int) fileStream.Length, cancellationToken);
160+
var _ = await fileStream.ReadAsync(fileContents, 0, (int) fileStream.Length, cancellationToken);
161161

162162
return fileContents;
163163
}

Tests/Tests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121

2222
<ItemGroup>
2323
<ProjectReference Include="..\FileSystem\FileSystem.csproj" />
24+
<ProjectReference Include="..\FileSystem.Adapters.AmazonS3\FileSystem.Adapters.AmazonS3.csproj" />
25+
<ProjectReference Include="..\FileSystem.Adapters.AzureBlobStorage\FileSystem.Adapters.AzureBlobStorage.csproj" />
26+
<ProjectReference Include="..\FileSystem.Adapters.AzureFileStorage\FileSystem.Adapters.AzureFileStorage.csproj" />
27+
<ProjectReference Include="..\FileSystem.Adapters.Dropbox\FileSystem.Adapters.Dropbox.csproj" />
28+
<ProjectReference Include="..\FileSystem.Adapters.MicrosoftOneDrive\FileSystem.Adapters.MicrosoftOneDrive.csproj" />
29+
<ProjectReference Include="..\FileSystem.Adapters.Sftp\FileSystem.Adapters.Sftp.csproj" />
2430
</ItemGroup>
2531

2632
</Project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Amazon;
4+
using Amazon.S3;
5+
using Amazon.S3.Model;
6+
using Moq;
7+
using SharpGrip.FileSystem.Adapters.AmazonS3;
8+
using SharpGrip.FileSystem.Models;
9+
using Xunit;
10+
11+
namespace Tests.FileSystem.Adapters.AmazonS3;
12+
13+
public class AmazonS3AdapterTest
14+
{
15+
[Fact]
16+
public void Test_Instantiation()
17+
{
18+
var amazonS3Client = new Mock<AmazonS3Client>("awsAccessKeyId", "awsSecretAccessKey", RegionEndpoint.USEast2);
19+
var amazonS3Adapter = new AmazonS3Adapter("prefix", "/root-path", amazonS3Client.Object, "bucket");
20+
21+
Assert.Equal("prefix", amazonS3Adapter.Prefix);
22+
Assert.Equal("/root-path", amazonS3Adapter.RootPath);
23+
}
24+
25+
[Fact]
26+
public async Task Test_Get_File_Async()
27+
{
28+
var amazonS3Client = new Mock<AmazonS3Client>("awsAccessKeyId", "awsSecretAccessKey", RegionEndpoint.USEast2);
29+
var amazonS3Adapter = new AmazonS3Adapter("prefix-1", "/root-path-1", amazonS3Client.Object, "bucket-1");
30+
31+
var getObjectResponse = new Mock<GetObjectResponse>();
32+
33+
getObjectResponse.SetupAllProperties();
34+
getObjectResponse.Object.Key = "test.txt";
35+
getObjectResponse.Object.ContentLength = 1;
36+
getObjectResponse.Object.LastModified = new DateTime(1970, 1, 1);
37+
38+
amazonS3Client.Setup(o => o.GetObjectAsync("bucket-1", "/root-path-1\\test.txt", default)).ReturnsAsync(getObjectResponse.Object);
39+
40+
var fileModel = new FileModel
41+
{
42+
Name = "test.txt",
43+
Path = "test.txt",
44+
Length = 1,
45+
LastModifiedDateTime = new DateTime(1970, 1, 1)
46+
};
47+
48+
var result = await amazonS3Adapter.GetFileAsync("test.txt");
49+
50+
Assert.Equal(fileModel.Name, result.Name);
51+
Assert.Equal(fileModel.Path, result.Path);
52+
Assert.Equal(fileModel.Length, result.Length);
53+
Assert.Equal(fileModel.LastModifiedDateTime, result.LastModifiedDateTime);
54+
}
55+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Azure.Storage.Blobs;
2+
using Moq;
3+
using SharpGrip.FileSystem.Adapters.AzureBlobStorage;
4+
using Xunit;
5+
6+
namespace Tests.FileSystem.Adapters.AzureBlobStorage;
7+
8+
public class AzureBlobStorageAdapterTest
9+
{
10+
[Fact]
11+
public void Test_Instantiation()
12+
{
13+
var blobContainerClient = new Mock<BlobContainerClient>();
14+
var azureBlobStorageAdapter = new AzureBlobStorageAdapter("prefix", "/root-path", blobContainerClient.Object);
15+
16+
Assert.Equal("prefix", azureBlobStorageAdapter.Prefix);
17+
Assert.Equal("/root-path", azureBlobStorageAdapter.RootPath);
18+
}
19+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace Tests.FileSystem.Adapters.AzureFileStorage;
2+
3+
public class AzureFileStorageAdapterTest
4+
{
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace Tests.FileSystem.Adapters.Dropbox;
2+
3+
public class DropboxAdapterTest
4+
{
5+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Threading.Tasks;
2+
using Moq;
3+
using Renci.SshNet;
4+
using SharpGrip.FileSystem.Adapters.Sftp;
5+
using SharpGrip.FileSystem.Exceptions;
6+
using Xunit;
7+
8+
namespace Tests.FileSystem.Adapters.Sftp;
9+
10+
public class SftpAdapterTest
11+
{
12+
[Fact]
13+
public void Test_Instantiation()
14+
{
15+
var sftpClient = new Mock<SftpClient>("hostName", "userName", "password");
16+
var sftpAdapter = new SftpAdapter("prefix", "/root-path", sftpClient.Object);
17+
18+
Assert.Equal("prefix", sftpAdapter.Prefix);
19+
Assert.Equal("/root-path", sftpAdapter.RootPath);
20+
}
21+
22+
[Fact]
23+
public void Test_Connect()
24+
{
25+
var sftpClient = new Mock<SftpClient>("hostName", "userName", "password");
26+
var sftpAdapter = new SftpAdapter("prefix-1", "/root-path-1", sftpClient.Object);
27+
28+
Assert.Throws<ConnectionException>(() => sftpAdapter.Connect());
29+
}
30+
31+
[Fact]
32+
public async Task Test_Get_File_Async()
33+
{
34+
var sftpClient = new Mock<SftpClient>("hostName", "userName", "password");
35+
var sftpAdapter = new SftpAdapter("prefix-1", "/root-path-1", sftpClient.Object);
36+
37+
await Assert.ThrowsAsync<ConnectionException>(async () => await sftpAdapter.GetFileAsync("test.txt"));
38+
}
39+
}

Tests/src/FileSystem/FileSystemTest.cs

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
using System.Collections.Generic;
2-
using System.Threading;
3-
using System.Threading.Tasks;
4-
using Moq;
52
using SharpGrip.FileSystem.Adapters;
63
using SharpGrip.FileSystem.Exceptions;
7-
using SharpGrip.FileSystem.Models;
84
using Xunit;
95

106
namespace Tests.FileSystem
@@ -60,46 +56,20 @@ public void Test_Instantiation()
6056
public void Test_Get_Adapter()
6157
{
6258
var fileSystem = new SharpGrip.FileSystem.FileSystem();
63-
var localAdapter = new LocalAdapter("test", "/");
59+
var localAdapter = new LocalAdapter("prefix-1", "/");
6460

6561
Assert.Equal(0, fileSystem.Adapters.Count);
66-
Assert.Throws<NoAdaptersRegisteredException>(() => fileSystem.GetAdapter("test"));
62+
Assert.Throws<NoAdaptersRegisteredException>(() => fileSystem.GetAdapter("prefix-1"));
6763

6864
fileSystem.Adapters.Add(localAdapter);
6965

7066
Assert.Equal(1, fileSystem.Adapters.Count);
71-
Assert.Equal(localAdapter, fileSystem.GetAdapter("test"));
72-
Assert.Throws<AdapterNotFoundException>(() => fileSystem.GetAdapter("test-test"));
67+
Assert.Equal(localAdapter, fileSystem.GetAdapter("prefix-1"));
68+
Assert.Throws<AdapterNotFoundException>(() => fileSystem.GetAdapter("prefix-2"));
7369

7470
fileSystem.Adapters.Add(localAdapter);
7571
Assert.Equal(2, fileSystem.Adapters.Count);
76-
Assert.Throws<DuplicateAdapterPrefixException>(() => fileSystem.GetAdapter("test"));
77-
}
78-
79-
[Fact]
80-
public void Test_Get_File()
81-
{
82-
var fileSystem = new SharpGrip.FileSystem.FileSystem();
83-
84-
var localAdapter = new Mock<LocalAdapter>("test1", "/");
85-
localAdapter.SetupAllProperties();
86-
87-
var fileModel = new FileModel
88-
{
89-
Name = "test"
90-
};
91-
92-
localAdapter.Setup(o => o.GetFileAsync("test.txt", CancellationToken.None)).Returns(Task.FromResult<IFile>(fileModel));
93-
94-
var adapters = new List<IAdapter>
95-
{
96-
localAdapter.Object
97-
};
98-
99-
fileSystem.Adapters = adapters;
100-
101-
Assert.True(fileSystem.DirectoryExists("test1://test.txt"));
102-
Assert.Equal(fileModel, fileSystem.GetFileAsync("test1://test.txt", CancellationToken.None).Result);
72+
Assert.Throws<DuplicateAdapterPrefixException>(() => fileSystem.GetAdapter("prefix-3"));
10373
}
10474
}
10575
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Threading.Tasks;
2+
using SharpGrip.FileSystem.Adapters;
3+
using SharpGrip.FileSystem.Exceptions;
4+
using Xunit;
5+
6+
namespace Tests.FileSystem
7+
{
8+
public class LocalAdapterTest
9+
{
10+
[Fact]
11+
public void Test_Instantiation()
12+
{
13+
var localAdapter = new LocalAdapter("prefix", "/root-path");
14+
15+
Assert.Equal("prefix", localAdapter.Prefix);
16+
Assert.Equal("/root-path", localAdapter.RootPath);
17+
}
18+
19+
[Fact]
20+
public async Task Test_Get_File_Async()
21+
{
22+
var localAdapter = new LocalAdapter("prefix-1", "/root-path-1");
23+
24+
await Assert.ThrowsAsync<FileNotFoundException>(async () => await localAdapter.GetFileAsync("prefix-1://test.txt"));
25+
}
26+
27+
[Fact]
28+
public async Task Test_Get_Directory_Async()
29+
{
30+
var localAdapter = new LocalAdapter("prefix-1", "/root-path-1");
31+
32+
await Assert.ThrowsAsync<DirectoryNotFoundException>(async () => await localAdapter.GetDirectoryAsync("prefix-1://test"));
33+
}
34+
35+
[Fact]
36+
public async Task Test_Get_Files_Async()
37+
{
38+
var localAdapter = new LocalAdapter("prefix-1", "/root-path-1");
39+
40+
await Assert.ThrowsAsync<DirectoryNotFoundException>(async () => await localAdapter.GetFilesAsync("prefix-1://test"));
41+
}
42+
43+
[Fact]
44+
public async Task Test_Get_Directories_Async()
45+
{
46+
var localAdapter = new LocalAdapter("prefix-1", "/root-path-1");
47+
48+
await Assert.ThrowsAsync<DirectoryNotFoundException>(async () => await localAdapter.GetDirectoriesAsync("prefix-1://test"));
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)