Skip to content

Commit

Permalink
Add CosmosDB integration
Browse files Browse the repository at this point in the history
  • Loading branch information
dsame committed Nov 12, 2023
1 parent 425efb4 commit 8799427
Show file tree
Hide file tree
Showing 27 changed files with 402 additions and 110 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,7 @@ ASALocalRun/
.mfractor/

# Local History for Visual Studio
.localhistory/
.localhistory/

# Sensetive data
App.config
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
<PackageReference Include="coverlet.collector" Version="3.2.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="services\v1\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace az_appservice_dotnet.nUnit.services.v1;
namespace az_appservice_dotnet.nUnit.services.v1.FakeFileProviderService;

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

[Test]
public void FakeFileProviderService_shouldReturnFilePath()
public void GetFileObject_shouldReturnFilePath()
{
// Arrange
var sut = new az_appservice_dotnet.services.v1.FakeImageProviderService(2);
var sut = new az_appservice_dotnet.services.v1.FakeImageProviderService();
// Act
var filePath = sut.GetFilePath();
_filePaths.Add(filePath);
var file = sut.GetFileObject("test", 2);
_filePaths.Add(file.Path);
// Assert
Assert.True(File.Exists(filePath));
var fileInfo = new FileInfo(filePath);
Assert.True(File.Exists(file.Path));
var fileInfo = new FileInfo(file.Path);
Assert.True(2 * 1024 * 1024 == fileInfo.Length);
}
}
13 changes: 13 additions & 0 deletions az-appservice-dotnet.xUnit/az-appservice-dotnet.xUnit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0-rc.2.23479.6" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -28,4 +29,16 @@
<ProjectReference Include="..\az-appservice-dotnet\az-appservice-dotnet.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="routes\v1\" />
</ItemGroup>

<ItemGroup>
<Content Update="App.config">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace az_appservice_dotnet.xUnit.models.v1.LongRunningWorkloads;

public class Progress
public class ProgressTest
{
[Fact]
public void Progress_shouldReturnZeroOnInit()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace az_appservice_dotnet.xUnit.services.v1.CosmoDbProcessingStateService;

[CollectionDefinition("CosmosContainer collection")]
public class ContainerCollection : ICollectionFixture<ContainerFixture>
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.Azure.Cosmos;
using Microsoft.Extensions.Configuration;

namespace az_appservice_dotnet.xUnit.services.v1.CosmoDbProcessingStateService;

public class ContainerFixture : IDisposable
{
public readonly Container Container;

public ContainerFixture()
{
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", false)
.Build();

string? endpointUri = config.GetSection("CosmosDb")["EndPointUri"];
if (endpointUri == null)
throw new Exception("App.config is missing the EndPointUri setting");

string? primaryKey = config.GetSection("CosmosDb")["PrimaryKey"];
if (primaryKey == null)
throw new Exception("App.config is missing the PrimaryKey setting");

var client = new CosmosClient(endpointUri, primaryKey, new CosmosClientOptions());
var databaseTask = client.CreateDatabaseIfNotExistsAsync("AkvTraining");
databaseTask.Wait();
var database = databaseTask.Result.Database;
var containerTask = database.CreateContainerIfNotExistsAsync("_test_States", "/taskId");
containerTask.Wait();
Container = containerTask.Result.Container;
}

public void Dispose()
{
Container.DeleteContainerAsync().Wait();
}

public az_appservice_dotnet.services.v1.CosmoDbProcessingStateService GetService()
{
return new az_appservice_dotnet.services.v1.CosmoDbProcessingStateService(Container);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Net;
using az_appservice_dotnet.services;
using Microsoft.Azure.Cosmos;

namespace az_appservice_dotnet.xUnit.services.v1.CosmoDbProcessingStateService;

[Collection("CosmosContainer collection")]
public class CreateInitialStateTest
{
readonly ContainerFixture _containerFixture;

public CreateInitialStateTest(ContainerFixture containerFixture)
{
_containerFixture = containerFixture;
}

[Fact]
public async Task Should_Create()
{
// Arrange
var sut = _containerFixture.GetService();
var taskId = 777;
var fileName = "file1.txt";
// Act
var actual = await sut.CreateInitialState(taskId, fileName);
// Assert
Assert.IsType<IProcessingStateService.State>(actual);
Assert.Equal((int)taskId, (int)actual.TaskId);
Assert.Equal(fileName, actual.FileName);

var readResponse = await _containerFixture.Container.ReadItemAsync<az_appservice_dotnet.services.v1.CosmoDbProcessingStateService.CosmosState>(actual.Id,
new PartitionKey(actual.TaskId));
Assert.Equal(HttpStatusCode.OK, readResponse.StatusCode);
var read = readResponse.Resource;
Assert.IsType<az_appservice_dotnet.services.v1.CosmoDbProcessingStateService.CosmosState>(read);
Assert.Equal(actual.Id, read.Id);
Assert.Equal((int)actual.TaskId, read.TaskId);
Assert.Equal(actual.Status, read.Status);
Assert.Equal(actual.OriginalFileUrl, read.OriginalFileUrl);
Assert.Equal(actual.ProcessedFileUrl, read.ProcessedFileUrl);
Assert.Equal(actual.FileName, read.FileName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace az_appservice_dotnet.xUnit.services.v1.LongRunningTasksService;

public class GetLongRunningTaskProgress
public class GetLongRunningTaskProgressTest
{
readonly Mock<ILongRunningWorkload> _fakeWorkload = new();
readonly Mock<ILongRunningWorkloadFactory> _fakeFactory = new();

public GetLongRunningTaskProgress()
public GetLongRunningTaskProgressTest()
{
_fakeFactory.Setup(x =>
x.Create(It.IsAny<uint>())).Returns(_fakeWorkload.Object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace az_appservice_dotnet.xUnit.services.v1.LongRunningTasksService;

public class GetLongRunningTasks
public class GetLongRunningTasksTest
{
readonly Mock<ILongRunningWorkload> _fakeWorkload = new();
readonly Mock<ILongRunningWorkloadFactory> _fakeFactory = new();

public GetLongRunningTasks()
public GetLongRunningTasksTest()
{
_fakeFactory.Setup(x =>
x.Create(It.IsAny<uint>())).Returns(_fakeWorkload.Object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace az_appservice_dotnet.xUnit.services.v1.LongRunningTasksService;

public class StartLongRunningTasksAsync
public class StartLongRunningTasksAsyncTest
{
readonly Mock<ILongRunningWorkload> _fakeWorkload = new();
readonly Mock<ILongRunningWorkloadFactory> _fakeFactory = new();

public StartLongRunningTasksAsync()
public StartLongRunningTasksAsyncTest()
{
_fakeFactory.Setup(x =>
x.Create(It.IsAny<uint>())).Returns(_fakeWorkload.Object);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace az_appservice_dotnet.xUnit.services.v1.LongRunningWorkloadFactory;

public class Create
public class CreateTest
{
[Fact]
public void Create_shouldReturnLongRunningWorkload()
Expand Down
14 changes: 7 additions & 7 deletions az-appservice-dotnet/Program.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
using System.Runtime.CompilerServices;
using az_appservice_dotnet.models;
using az_appservice_dotnet.models;
using az_appservice_dotnet.models.v1;
using az_appservice_dotnet.routing;
using az_appservice_dotnet.routing.v1;
using az_appservice_dotnet.services;
using az_appservice_dotnet.services.v1;

namespace az_appservice_dotnet;

static class Program
{
public static void Main(string[] args)
public static async Task Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<ILongRunningWorkloadFactory, LongRunningWorkloadFactory>();
builder.Services.AddSingleton<ILongRunningTasksService, LongRunningTasksService>();
builder.Services.AddSingleton<IImageProviderService, FakeImageProviderService>();
builder.Services.AddSingleton<IProcessingStateService, CosmoDbProcessingStateService>();
var app = builder.Build();

app.MapGet("/", () => "Hello World!");
app.MapGet("/", () => Results.Ok("Hello World!"));
app.MapGroup("/1")
.MapApi1(app);

app.Run();
await app.RunAsync();
}

static RouteGroupBuilder MapApi1(this RouteGroupBuilder group, WebApplication app)
{
group.MapPing();
group.MapBlobs();
group.MapImages(app.Services.GetService<IImageProviderService>());
group.MapImages();
return group;
}
}
6 changes: 5 additions & 1 deletion az-appservice-dotnet/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"CosmosDb": {
"EndpointUri": "https://akvdemo-cosmos.documents.azure.com:443/",
"PrimaryKey": "ID92pzCwWrshOmoNhP4WragyGkzkEja0MRtTbZ7iFdIjqvGRjuDD7MylAg9zl6gupI4pHS93SXqaACDbzvTRDA=="
}
}

3 changes: 3 additions & 0 deletions az-appservice-dotnet/az-appservice-dotnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@

<PropertyGroup Condition=" '$(RunConfiguration)' == 'https' " />
<PropertyGroup Condition=" '$(RunConfiguration)' == 'http' " />
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.36.0" />
</ItemGroup>
</Project>
20 changes: 0 additions & 20 deletions az-appservice-dotnet/routing/RouteGroupBuilderImages.cs

This file was deleted.

10 changes: 0 additions & 10 deletions az-appservice-dotnet/routing/RouterGroupBuilderPing.cs

This file was deleted.

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

public static class RouteGroupBuilderBlobs
{
Expand Down
20 changes: 20 additions & 0 deletions az-appservice-dotnet/routing/v1/RouteGroupBuilderImages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using az_appservice_dotnet.services;

namespace az_appservice_dotnet.routing.v1;

public static class RouteGroupBuilderImages
{
public static RouteGroupBuilder MapImages(this RouteGroupBuilder group)
{
group.MapPost("/images", Create);
group.MapGet("/images", Create);
return group;
}

private static IResult Create(IImageProviderService imageProviderService)
{
var file = imageProviderService.GetFileObject("image", 1);

return TypedResults.Created($"/images/{file.Name}", new { Name = file.Name });
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace az_appservice_dotnet;
namespace az_appservice_dotnet.routing.v1;

public static class RouteGroupBuilderTasks
{
Expand Down
20 changes: 20 additions & 0 deletions az-appservice-dotnet/routing/v1/RouterGroupBuilderPing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace az_appservice_dotnet.routing.v1;

public static class RouterGroupBuilderPing
{
public static RouteGroupBuilder MapPing(this RouteGroupBuilder group)
{
group
.MapGet("/ping", () => "pong")
.AddEndpointFilter(async (context, next) =>
{
if (!context.HttpContext.Request.Query.ContainsKey("workload"))
{
return TypedResults.UnprocessableEntity("Missing workload query parameter");
}

return await next(context);
});
return group;
}
}
Loading

0 comments on commit 8799427

Please sign in to comment.