Skip to content

Commit 8799427

Browse files
committed
Add CosmosDB integration
1 parent 425efb4 commit 8799427

27 files changed

+402
-110
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -402,4 +402,7 @@ ASALocalRun/
402402
.mfractor/
403403

404404
# Local History for Visual Studio
405-
.localhistory/
405+
.localhistory/
406+
407+
# Sensetive data
408+
App.config

az-appservice-dotnet.MSTest/az-appservice-dotnet.MSTest.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@
1717
<PackageReference Include="coverlet.collector" Version="3.2.0" />
1818
</ItemGroup>
1919

20+
<ItemGroup>
21+
<Folder Include="services\v1\" />
22+
</ItemGroup>
23+
2024
</Project>

az-appservice-dotnet.nUnit/services/v1/FakeFileProviderService.cs az-appservice-dotnet.nUnit/services/v1/FakeFileProviderService/GetFileObjectTest.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
namespace az_appservice_dotnet.nUnit.services.v1;
1+
namespace az_appservice_dotnet.nUnit.services.v1.FakeFileProviderService;
22

3-
public class FakeFileProviderService
3+
public class GetFileObjectTest
44
{
55
// a list of strings to hold the file paths
66
private List<String> _filePaths = new();
@@ -25,16 +25,16 @@ public void TearDown()
2525
}
2626

2727
[Test]
28-
public void FakeFileProviderService_shouldReturnFilePath()
28+
public void GetFileObject_shouldReturnFilePath()
2929
{
3030
// Arrange
31-
var sut = new az_appservice_dotnet.services.v1.FakeImageProviderService(2);
31+
var sut = new az_appservice_dotnet.services.v1.FakeImageProviderService();
3232
// Act
33-
var filePath = sut.GetFilePath();
34-
_filePaths.Add(filePath);
33+
var file = sut.GetFileObject("test", 2);
34+
_filePaths.Add(file.Path);
3535
// Assert
36-
Assert.True(File.Exists(filePath));
37-
var fileInfo = new FileInfo(filePath);
36+
Assert.True(File.Exists(file.Path));
37+
var fileInfo = new FileInfo(file.Path);
3838
Assert.True(2 * 1024 * 1024 == fileInfo.Length);
3939
}
4040
}

az-appservice-dotnet.xUnit/az-appservice-dotnet.xUnit.csproj

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<ItemGroup>
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
1515
<PackageReference Include="Moq" Version="4.20.69" />
16+
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0-rc.2.23479.6" />
1617
<PackageReference Include="xunit" Version="2.4.2" />
1718
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
1819
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@@ -28,4 +29,16 @@
2829
<ProjectReference Include="..\az-appservice-dotnet\az-appservice-dotnet.csproj" />
2930
</ItemGroup>
3031

32+
<ItemGroup>
33+
<Folder Include="routes\v1\" />
34+
</ItemGroup>
35+
36+
<ItemGroup>
37+
<Content Update="App.config">
38+
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
39+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
40+
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
41+
</Content>
42+
</ItemGroup>
43+
3144
</Project>

az-appservice-dotnet.xUnit/models/v1/LongRunningWorkloads/Progress.cs az-appservice-dotnet.xUnit/models/v1/LongRunningWorkloads/ProgressTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace az_appservice_dotnet.xUnit.models.v1.LongRunningWorkloads;
22

3-
public class Progress
3+
public class ProgressTest
44
{
55
[Fact]
66
public void Progress_shouldReturnZeroOnInit()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace az_appservice_dotnet.xUnit.services.v1.CosmoDbProcessingStateService;
2+
3+
[CollectionDefinition("CosmosContainer collection")]
4+
public class ContainerCollection : ICollectionFixture<ContainerFixture>
5+
{
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Microsoft.Azure.Cosmos;
2+
using Microsoft.Extensions.Configuration;
3+
4+
namespace az_appservice_dotnet.xUnit.services.v1.CosmoDbProcessingStateService;
5+
6+
public class ContainerFixture : IDisposable
7+
{
8+
public readonly Container Container;
9+
10+
public ContainerFixture()
11+
{
12+
IConfiguration config = new ConfigurationBuilder()
13+
.AddJsonFile("appsettings.json", false)
14+
.Build();
15+
16+
string? endpointUri = config.GetSection("CosmosDb")["EndPointUri"];
17+
if (endpointUri == null)
18+
throw new Exception("App.config is missing the EndPointUri setting");
19+
20+
string? primaryKey = config.GetSection("CosmosDb")["PrimaryKey"];
21+
if (primaryKey == null)
22+
throw new Exception("App.config is missing the PrimaryKey setting");
23+
24+
var client = new CosmosClient(endpointUri, primaryKey, new CosmosClientOptions());
25+
var databaseTask = client.CreateDatabaseIfNotExistsAsync("AkvTraining");
26+
databaseTask.Wait();
27+
var database = databaseTask.Result.Database;
28+
var containerTask = database.CreateContainerIfNotExistsAsync("_test_States", "/taskId");
29+
containerTask.Wait();
30+
Container = containerTask.Result.Container;
31+
}
32+
33+
public void Dispose()
34+
{
35+
Container.DeleteContainerAsync().Wait();
36+
}
37+
38+
public az_appservice_dotnet.services.v1.CosmoDbProcessingStateService GetService()
39+
{
40+
return new az_appservice_dotnet.services.v1.CosmoDbProcessingStateService(Container);
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Net;
2+
using az_appservice_dotnet.services;
3+
using Microsoft.Azure.Cosmos;
4+
5+
namespace az_appservice_dotnet.xUnit.services.v1.CosmoDbProcessingStateService;
6+
7+
[Collection("CosmosContainer collection")]
8+
public class CreateInitialStateTest
9+
{
10+
readonly ContainerFixture _containerFixture;
11+
12+
public CreateInitialStateTest(ContainerFixture containerFixture)
13+
{
14+
_containerFixture = containerFixture;
15+
}
16+
17+
[Fact]
18+
public async Task Should_Create()
19+
{
20+
// Arrange
21+
var sut = _containerFixture.GetService();
22+
var taskId = 777;
23+
var fileName = "file1.txt";
24+
// Act
25+
var actual = await sut.CreateInitialState(taskId, fileName);
26+
// Assert
27+
Assert.IsType<IProcessingStateService.State>(actual);
28+
Assert.Equal((int)taskId, (int)actual.TaskId);
29+
Assert.Equal(fileName, actual.FileName);
30+
31+
var readResponse = await _containerFixture.Container.ReadItemAsync<az_appservice_dotnet.services.v1.CosmoDbProcessingStateService.CosmosState>(actual.Id,
32+
new PartitionKey(actual.TaskId));
33+
Assert.Equal(HttpStatusCode.OK, readResponse.StatusCode);
34+
var read = readResponse.Resource;
35+
Assert.IsType<az_appservice_dotnet.services.v1.CosmoDbProcessingStateService.CosmosState>(read);
36+
Assert.Equal(actual.Id, read.Id);
37+
Assert.Equal((int)actual.TaskId, read.TaskId);
38+
Assert.Equal(actual.Status, read.Status);
39+
Assert.Equal(actual.OriginalFileUrl, read.OriginalFileUrl);
40+
Assert.Equal(actual.ProcessedFileUrl, read.ProcessedFileUrl);
41+
Assert.Equal(actual.FileName, read.FileName);
42+
}
43+
44+
}

az-appservice-dotnet.xUnit/services/v1/LongRunningTasksService/GetLongRunningTaskProgress.cs az-appservice-dotnet.xUnit/services/v1/LongRunningTasksService/GetLongRunningTaskProgressTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace az_appservice_dotnet.xUnit.services.v1.LongRunningTasksService;
66

7-
public class GetLongRunningTaskProgress
7+
public class GetLongRunningTaskProgressTest
88
{
99
readonly Mock<ILongRunningWorkload> _fakeWorkload = new();
1010
readonly Mock<ILongRunningWorkloadFactory> _fakeFactory = new();
1111

12-
public GetLongRunningTaskProgress()
12+
public GetLongRunningTaskProgressTest()
1313
{
1414
_fakeFactory.Setup(x =>
1515
x.Create(It.IsAny<uint>())).Returns(_fakeWorkload.Object);

az-appservice-dotnet.xUnit/services/v1/LongRunningTasksService/GetLongRunningTasks.cs az-appservice-dotnet.xUnit/services/v1/LongRunningTasksService/GetLongRunningTasksTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
namespace az_appservice_dotnet.xUnit.services.v1.LongRunningTasksService;
55

6-
public class GetLongRunningTasks
6+
public class GetLongRunningTasksTest
77
{
88
readonly Mock<ILongRunningWorkload> _fakeWorkload = new();
99
readonly Mock<ILongRunningWorkloadFactory> _fakeFactory = new();
1010

11-
public GetLongRunningTasks()
11+
public GetLongRunningTasksTest()
1212
{
1313
_fakeFactory.Setup(x =>
1414
x.Create(It.IsAny<uint>())).Returns(_fakeWorkload.Object);

az-appservice-dotnet.xUnit/services/v1/LongRunningTasksService/StartLongRunningTasksAsync.cs az-appservice-dotnet.xUnit/services/v1/LongRunningTasksService/StartLongRunningTasksAsyncTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace az_appservice_dotnet.xUnit.services.v1.LongRunningTasksService;
66

7-
public class StartLongRunningTasksAsync
7+
public class StartLongRunningTasksAsyncTest
88
{
99
readonly Mock<ILongRunningWorkload> _fakeWorkload = new();
1010
readonly Mock<ILongRunningWorkloadFactory> _fakeFactory = new();
1111

12-
public StartLongRunningTasksAsync()
12+
public StartLongRunningTasksAsyncTest()
1313
{
1414
_fakeFactory.Setup(x =>
1515
x.Create(It.IsAny<uint>())).Returns(_fakeWorkload.Object);

az-appservice-dotnet.xUnit/services/v1/LongRunningWorkloadFactory/Create.cs az-appservice-dotnet.xUnit/services/v1/LongRunningWorkloadFactory/CreateTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace az_appservice_dotnet.xUnit.services.v1.LongRunningWorkloadFactory;
22

3-
public class Create
3+
public class CreateTest
44
{
55
[Fact]
66
public void Create_shouldReturnLongRunningWorkload()

az-appservice-dotnet/Program.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
using System.Runtime.CompilerServices;
2-
using az_appservice_dotnet.models;
1+
using az_appservice_dotnet.models;
32
using az_appservice_dotnet.models.v1;
4-
using az_appservice_dotnet.routing;
3+
using az_appservice_dotnet.routing.v1;
54
using az_appservice_dotnet.services;
65
using az_appservice_dotnet.services.v1;
76

87
namespace az_appservice_dotnet;
98

109
static class Program
1110
{
12-
public static void Main(string[] args)
11+
public static async Task Main(string[] args)
1312
{
1413
var builder = WebApplication.CreateBuilder(args);
1514
builder.Services.AddSingleton<ILongRunningWorkloadFactory, LongRunningWorkloadFactory>();
1615
builder.Services.AddSingleton<ILongRunningTasksService, LongRunningTasksService>();
1716
builder.Services.AddSingleton<IImageProviderService, FakeImageProviderService>();
17+
builder.Services.AddSingleton<IProcessingStateService, CosmoDbProcessingStateService>();
1818
var app = builder.Build();
1919

20-
app.MapGet("/", () => "Hello World!");
20+
app.MapGet("/", () => Results.Ok("Hello World!"));
2121
app.MapGroup("/1")
2222
.MapApi1(app);
2323

24-
app.Run();
24+
await app.RunAsync();
2525
}
2626

2727
static RouteGroupBuilder MapApi1(this RouteGroupBuilder group, WebApplication app)
2828
{
2929
group.MapPing();
3030
group.MapBlobs();
31-
group.MapImages(app.Services.GetService<IImageProviderService>());
31+
group.MapImages();
3232
return group;
3333
}
3434
}

az-appservice-dotnet/appsettings.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
"Microsoft.AspNetCore": "Warning"
66
}
77
},
8-
"AllowedHosts": "*"
8+
"AllowedHosts": "*",
9+
"CosmosDb": {
10+
"EndpointUri": "https://akvdemo-cosmos.documents.azure.com:443/",
11+
"PrimaryKey": "ID92pzCwWrshOmoNhP4WragyGkzkEja0MRtTbZ7iFdIjqvGRjuDD7MylAg9zl6gupI4pHS93SXqaACDbzvTRDA=="
12+
}
913
}
1014

az-appservice-dotnet/az-appservice-dotnet.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@
99

1010
<PropertyGroup Condition=" '$(RunConfiguration)' == 'https' " />
1111
<PropertyGroup Condition=" '$(RunConfiguration)' == 'http' " />
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.36.0" />
14+
</ItemGroup>
1215
</Project>

az-appservice-dotnet/routing/RouteGroupBuilderImages.cs

-20
This file was deleted.

az-appservice-dotnet/routing/RouterGroupBuilderPing.cs

-10
This file was deleted.

az-appservice-dotnet/routing/RouteGroupBuilderBlobs.cs az-appservice-dotnet/routing/v1/RouteGroupBuilderBlobs.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace az_appservice_dotnet;
1+
namespace az_appservice_dotnet.routing.v1;
22

33
public static class RouteGroupBuilderBlobs
44
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using az_appservice_dotnet.services;
2+
3+
namespace az_appservice_dotnet.routing.v1;
4+
5+
public static class RouteGroupBuilderImages
6+
{
7+
public static RouteGroupBuilder MapImages(this RouteGroupBuilder group)
8+
{
9+
group.MapPost("/images", Create);
10+
group.MapGet("/images", Create);
11+
return group;
12+
}
13+
14+
private static IResult Create(IImageProviderService imageProviderService)
15+
{
16+
var file = imageProviderService.GetFileObject("image", 1);
17+
18+
return TypedResults.Created($"/images/{file.Name}", new { Name = file.Name });
19+
}
20+
}

az-appservice-dotnet/routing/RouteGroupBuilderTasks.cs az-appservice-dotnet/routing/v1/RouteGroupBuilderTasks.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace az_appservice_dotnet;
1+
namespace az_appservice_dotnet.routing.v1;
22

33
public static class RouteGroupBuilderTasks
44
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace az_appservice_dotnet.routing.v1;
2+
3+
public static class RouterGroupBuilderPing
4+
{
5+
public static RouteGroupBuilder MapPing(this RouteGroupBuilder group)
6+
{
7+
group
8+
.MapGet("/ping", () => "pong")
9+
.AddEndpointFilter(async (context, next) =>
10+
{
11+
if (!context.HttpContext.Request.Query.ContainsKey("workload"))
12+
{
13+
return TypedResults.UnprocessableEntity("Missing workload query parameter");
14+
}
15+
16+
return await next(context);
17+
});
18+
return group;
19+
}
20+
}

0 commit comments

Comments
 (0)