Skip to content

Commit 151f802

Browse files
committed
Fakes
1 parent 38376ff commit 151f802

8 files changed

+279
-3
lines changed

Directory.Build.props

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1414
<PackageReadmeFile>README.md</PackageReadmeFile>
1515
<Product>Managed Code - Storage</Product>
16-
<Version>2.0.18</Version>
17-
<PackageVersion>2.0.18</PackageVersion>
16+
<Version>2.1.0</Version>
17+
<PackageVersion>2.1.0</PackageVersion>
1818
</PropertyGroup>
1919
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
2020
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
@@ -28,6 +28,6 @@
2828
<None Include="$(SolutionDir)\README.md" Pack="true" Visible="false" PackagePath="\"/>
2929
</ItemGroup>
3030
<ItemGroup>
31-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
31+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
3232
</ItemGroup>
3333
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Amazon.S3;
2+
using ManagedCode.Storage.Aws;
3+
using ManagedCode.Storage.FileSystem;
4+
using ManagedCode.Storage.FileSystem.Options;
5+
6+
namespace ManagedCode.Storage.TestFakes;
7+
8+
public class FakeAWSStorage : FileSystemStorage, IAWSStorage
9+
{
10+
public FakeAWSStorage() : base(new FileSystemStorageOptions())
11+
{
12+
}
13+
14+
public IAmazonS3 StorageClient { get; }
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using Azure.Storage.Files.DataLake;
2+
using ManagedCode.Communication;
3+
using ManagedCode.Storage.AzureDataLake;
4+
using ManagedCode.Storage.AzureDataLake.Options;
5+
using ManagedCode.Storage.FileSystem;
6+
using ManagedCode.Storage.FileSystem.Options;
7+
8+
namespace ManagedCode.Storage.TestFakes;
9+
10+
public class FakeAzureDataLakeStorage : FileSystemStorage, IAzureDataLakeStorage
11+
{
12+
public FakeAzureDataLakeStorage() : base(new FileSystemStorageOptions())
13+
{
14+
}
15+
16+
public DataLakeFileSystemClient StorageClient { get; }
17+
18+
public Task<Result> CreateDirectoryAsync(string directory, CancellationToken cancellationToken = default)
19+
{
20+
if (Directory.Exists(directory))
21+
{
22+
Task.FromResult(Result.Fail());
23+
}
24+
25+
try
26+
{
27+
Directory.CreateDirectory(directory);
28+
return Task.FromResult(Result.Succeed());
29+
}
30+
catch (Exception e)
31+
{
32+
return Task.FromResult(Result.Fail(e));
33+
}
34+
}
35+
36+
public Task<Result> RenameDirectory(string directory, string newDirectory, CancellationToken cancellationToken = default)
37+
{
38+
if (!Directory.Exists(directory))
39+
{
40+
Task.FromResult(Result.Fail());
41+
}
42+
43+
try
44+
{
45+
Directory.Move(directory, newDirectory);
46+
return Task.FromResult(Result.Succeed());
47+
}
48+
catch (Exception e)
49+
{
50+
return Task.FromResult(Result.Fail(e));
51+
}
52+
}
53+
54+
public Task<Result<Stream>> OpenWriteStreamAsync(OpenWriteStreamOptions options, CancellationToken cancellationToken = default)
55+
{
56+
if (!File.Exists(options.FullPath))
57+
{
58+
return Task.FromResult(Result<Stream>.Fail());
59+
}
60+
61+
try
62+
{
63+
return Task.FromResult(Result<Stream>.Succeed(File.OpenWrite(options.FullPath)));
64+
}
65+
catch (Exception e)
66+
{
67+
return Task.FromResult(Result<Stream>.Fail(e));
68+
}
69+
}
70+
71+
public Task<Result<Stream>> OpenReadStreamAsync(OpenReadStreamOptions options, CancellationToken cancellationToken = default)
72+
{
73+
if (!File.Exists(options.FullPath))
74+
{
75+
return Task.FromResult(Result<Stream>.Fail());
76+
}
77+
78+
try
79+
{
80+
return Task.FromResult(Result<Stream>.Succeed(File.OpenRead(options.FullPath)));
81+
}
82+
catch (Exception e)
83+
{
84+
return Task.FromResult(Result<Stream>.Fail(e));
85+
}
86+
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Azure.Storage.Blobs;
2+
using ManagedCode.Communication;
3+
using ManagedCode.Storage.Azure;
4+
using ManagedCode.Storage.FileSystem;
5+
using ManagedCode.Storage.FileSystem.Options;
6+
7+
namespace ManagedCode.Storage.TestFakes;
8+
9+
public class FakeAzureStorage : FileSystemStorage, IAzureStorage
10+
{
11+
public FakeAzureStorage() : base(new FileSystemStorageOptions())
12+
{
13+
}
14+
15+
public BlobContainerClient StorageClient { get; }
16+
17+
public Task<Result<Stream>> OpenReadStreamAsync(string fileName, CancellationToken cancellationToken = default)
18+
{
19+
if (!File.Exists(fileName))
20+
{
21+
return Task.FromResult(Result<Stream>.Fail());
22+
}
23+
24+
try
25+
{
26+
return Task.FromResult(Result<Stream>.Succeed(File.OpenRead(fileName)));
27+
}
28+
catch (Exception e)
29+
{
30+
return Task.FromResult(Result<Stream>.Fail(e));
31+
}
32+
}
33+
34+
public Task<Result<Stream>> OpenWriteStreamAsync(string fileName, CancellationToken cancellationToken = default)
35+
{
36+
if (!File.Exists(fileName))
37+
{
38+
return Task.FromResult(Result<Stream>.Fail());
39+
}
40+
41+
try
42+
{
43+
return Task.FromResult(Result<Stream>.Succeed(File.OpenWrite(fileName)));
44+
}
45+
catch (Exception e)
46+
{
47+
return Task.FromResult(Result<Stream>.Fail(e));
48+
}
49+
}
50+
51+
public Stream GetBlobStream(string fileName, bool userBuffer = true, int bufferSize = BlobStream.DefaultBufferSize)
52+
{
53+
return File.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Google.Cloud.Storage.V1;
2+
using ManagedCode.Storage.FileSystem;
3+
using ManagedCode.Storage.FileSystem.Options;
4+
using ManagedCode.Storage.Gcp;
5+
6+
namespace ManagedCode.Storage.TestFakes;
7+
8+
public class FakeGCPStorage : FileSystemStorage, IGCPStorage
9+
{
10+
public FakeGCPStorage() : base(new FileSystemStorageOptions())
11+
{
12+
}
13+
14+
public StorageClient StorageClient { get; }
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsPackable>true</IsPackable>
8+
</PropertyGroup>
9+
10+
<!--NuGet-->
11+
<PropertyGroup>
12+
<Title>ManagedCode.Storage.TestFakes</Title>
13+
<PackageId>ManagedCode.Storage.TestFakes</PackageId>
14+
<Description>Fake implementaions for units tests</Description>
15+
<PackageTags>managedcode, aws, gcp, azure storage, cloud, asp.net, file, upload, download</PackageTags>
16+
</PropertyGroup>
17+
<ItemGroup>
18+
<ProjectReference Include="..\ManagedCode.Storage.Aws\ManagedCode.Storage.Aws.csproj"/>
19+
<ProjectReference Include="..\ManagedCode.Storage.AzureDataLake\ManagedCode.Storage.AzureDataLake.csproj"/>
20+
<ProjectReference Include="..\ManagedCode.Storage.Azure\ManagedCode.Storage.Azure.csproj"/>
21+
<ProjectReference Include="..\ManagedCode.Storage.Core\ManagedCode.Storage.Core.csproj"/>
22+
<ProjectReference Include="..\ManagedCode.Storage.FileSystem\ManagedCode.Storage.FileSystem.csproj"/>
23+
<ProjectReference Include="..\ManagedCode.Storage.Gcp\ManagedCode.Storage.Gcp.csproj"/>
24+
</ItemGroup>
25+
26+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using ManagedCode.Storage.Aws;
2+
using ManagedCode.Storage.Azure;
3+
using ManagedCode.Storage.AzureDataLake;
4+
using ManagedCode.Storage.Core;
5+
using ManagedCode.Storage.Gcp;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.DependencyInjection.Extensions;
8+
9+
namespace ManagedCode.Storage.TestFakes;
10+
11+
public static class MockCollectionExtensions
12+
{
13+
public static IServiceCollection ReplaceAWSStorageAsDefault(this IServiceCollection serviceCollection)
14+
{
15+
serviceCollection.ReplaceAWSStorage();
16+
serviceCollection.AddScoped<IStorage, FakeAWSStorage>();
17+
return serviceCollection;
18+
}
19+
20+
public static IServiceCollection ReplaceAWSStorage(this IServiceCollection serviceCollection)
21+
{
22+
serviceCollection.RemoveAll<IAWSStorage>();
23+
serviceCollection.RemoveAll<AWSStorage>();
24+
serviceCollection.AddScoped<IAWSStorage, FakeAWSStorage>();
25+
return serviceCollection;
26+
}
27+
28+
public static IServiceCollection ReplaceAzureDataLakeStorage(this IServiceCollection serviceCollection)
29+
{
30+
serviceCollection.RemoveAll<IAzureDataLakeStorage>();
31+
serviceCollection.RemoveAll<AzureDataLakeStorage>();
32+
serviceCollection.AddScoped<IAzureDataLakeStorage, FakeAzureDataLakeStorage>();
33+
return serviceCollection;
34+
}
35+
36+
public static IServiceCollection ReplaceAzureDataLakeStorageAsDefault(this IServiceCollection serviceCollection)
37+
{
38+
serviceCollection.ReplaceAzureDataLakeStorage();
39+
serviceCollection.AddScoped<IStorage, FakeAzureDataLakeStorage>();
40+
return serviceCollection;
41+
}
42+
43+
public static IServiceCollection ReplaceAzureStorage(this IServiceCollection serviceCollection)
44+
{
45+
serviceCollection.RemoveAll<IAzureStorage>();
46+
serviceCollection.RemoveAll<FakeAzureStorage>();
47+
serviceCollection.AddScoped<IAzureStorage, FakeAzureStorage>();
48+
return serviceCollection;
49+
}
50+
51+
public static IServiceCollection ReplaceAzureStorageAsDefault(this IServiceCollection serviceCollection)
52+
{
53+
serviceCollection.ReplaceAzureStorage();
54+
serviceCollection.AddScoped<IStorage, FakeAzureStorage>();
55+
return serviceCollection;
56+
}
57+
58+
public static IServiceCollection ReplaceGCPStorageAsDefault(this IServiceCollection serviceCollection)
59+
{
60+
serviceCollection.ReplaceGCPStorage();
61+
serviceCollection.AddScoped<IStorage, FakeAzureStorage>();
62+
return serviceCollection;
63+
}
64+
65+
public static IServiceCollection ReplaceGCPStorage(this IServiceCollection serviceCollection)
66+
{
67+
serviceCollection.RemoveAll<IGCPStorage>();
68+
serviceCollection.RemoveAll<GCPStorage>();
69+
serviceCollection.AddScoped<IGCPStorage, FakeGCPStorage>();
70+
return serviceCollection;
71+
}
72+
}

ManagedCode.Storage.sln

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiSample", "Samples\Web
2323
EndProject
2424
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ManagedCode.Storage.AzureDataLake", "ManagedCode.Storage.AzureDataLake\ManagedCode.Storage.AzureDataLake.csproj", "{4D4D2AC7-923D-4219-9BC9-341FBA7FE690}"
2525
EndProject
26+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ManagedCode.Storage.TestFakes", "ManagedCode.Storage.TestFakes\ManagedCode.Storage.TestFakes.csproj", "{7190B548-4BE9-4EF6-B55F-8432757AEAD5}"
27+
EndProject
2628
Global
2729
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2830
Debug|Any CPU = Debug|Any CPU
@@ -65,6 +67,10 @@ Global
6567
{4D4D2AC7-923D-4219-9BC9-341FBA7FE690}.Debug|Any CPU.Build.0 = Debug|Any CPU
6668
{4D4D2AC7-923D-4219-9BC9-341FBA7FE690}.Release|Any CPU.ActiveCfg = Release|Any CPU
6769
{4D4D2AC7-923D-4219-9BC9-341FBA7FE690}.Release|Any CPU.Build.0 = Release|Any CPU
70+
{7190B548-4BE9-4EF6-B55F-8432757AEAD5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
71+
{7190B548-4BE9-4EF6-B55F-8432757AEAD5}.Debug|Any CPU.Build.0 = Debug|Any CPU
72+
{7190B548-4BE9-4EF6-B55F-8432757AEAD5}.Release|Any CPU.ActiveCfg = Release|Any CPU
73+
{7190B548-4BE9-4EF6-B55F-8432757AEAD5}.Release|Any CPU.Build.0 = Release|Any CPU
6874
EndGlobalSection
6975
GlobalSection(SolutionProperties) = preSolution
7076
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)