first pass and feature add ons
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
using System.Text.Json;
|
||||
using Jibo.Cloud.Domain.Models;
|
||||
|
||||
namespace Jibo.Cloud.Tests.Fixtures;
|
||||
|
||||
internal static class ProtocolFixtureLoader
|
||||
{
|
||||
public static ProtocolFixture Load(string relativePath)
|
||||
{
|
||||
var fullPath = Path.Combine(AppContext.BaseDirectory, relativePath);
|
||||
using var document = JsonDocument.Parse(File.ReadAllText(fullPath));
|
||||
var root = document.RootElement;
|
||||
|
||||
var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
if (root.TryGetProperty("headers", out var headerElement) && headerElement.ValueKind == JsonValueKind.Object)
|
||||
{
|
||||
foreach (var property in headerElement.EnumerateObject())
|
||||
{
|
||||
headers[property.Name] = property.Value.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
var bodyText = root.TryGetProperty("body", out var bodyElement)
|
||||
? bodyElement.GetRawText()
|
||||
: string.Empty;
|
||||
|
||||
var target = headers.TryGetValue("x-amz-target", out var targetValue)
|
||||
? targetValue
|
||||
: string.Empty;
|
||||
var targetParts = target.Split('.', 2, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
return new ProtocolFixture
|
||||
{
|
||||
Name = Path.GetFileNameWithoutExtension(relativePath),
|
||||
Request = new ProtocolEnvelope
|
||||
{
|
||||
HostName = root.TryGetProperty("host", out var hostElement) ? hostElement.GetString() ?? "api.jibo.com" : "api.jibo.com",
|
||||
Method = root.TryGetProperty("method", out var methodElement) ? methodElement.GetString() ?? "POST" : "POST",
|
||||
Path = root.TryGetProperty("path", out var pathElement) ? pathElement.GetString() ?? "/" : "/",
|
||||
Headers = headers,
|
||||
ServicePrefix = targetParts.Length > 0 ? targetParts[0] : null,
|
||||
Operation = targetParts.Length > 1 ? targetParts[1] : null,
|
||||
BodyText = bodyText
|
||||
},
|
||||
ExpectedStatusCode = 200
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -19,4 +19,11 @@
|
||||
<ProjectReference Include="..\..\src\Jibo.Runtime.Abstractions\Jibo.Runtime.Abstractions.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="..\..\src\Jibo.Cloud\node\fixtures\http\*.json">
|
||||
<Link>fixtures\%(Filename)%(Extension)</Link>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -61,4 +61,82 @@ public sealed class JiboCloudProtocolServiceTests
|
||||
Assert.Equal("robot", payload.RootElement.GetProperty("subsystem").GetString());
|
||||
Assert.True(payload.RootElement.TryGetProperty("url", out _));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PutEventsAsync_ReturnsUploadUrl()
|
||||
{
|
||||
var result = await _service.DispatchAsync(new ProtocolEnvelope
|
||||
{
|
||||
HostName = "api.jibo.com",
|
||||
Method = "POST",
|
||||
ServicePrefix = "Log_20160715",
|
||||
Operation = "PutEventsAsync",
|
||||
BodyText = "{}"
|
||||
});
|
||||
|
||||
using var payload = JsonDocument.Parse(result.BodyText);
|
||||
Assert.Equal("gzip", payload.RootElement.GetProperty("contentEncoding").GetString());
|
||||
Assert.Contains("/upload/log-events", payload.RootElement.GetProperty("uploadUrl").GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MediaCreateAndGet_ReturnsCreatedItem()
|
||||
{
|
||||
var created = await _service.DispatchAsync(new ProtocolEnvelope
|
||||
{
|
||||
HostName = "api.jibo.com",
|
||||
Method = "POST",
|
||||
ServicePrefix = "Media_20160725",
|
||||
Operation = "Create",
|
||||
BodyText = """{"path":"/media/test-item","type":"image","reference":"demo"}"""
|
||||
});
|
||||
|
||||
using var createdPayload = JsonDocument.Parse(created.BodyText);
|
||||
Assert.Equal("/media/test-item", createdPayload.RootElement.GetProperty("path").GetString());
|
||||
|
||||
var fetched = await _service.DispatchAsync(new ProtocolEnvelope
|
||||
{
|
||||
HostName = "api.jibo.com",
|
||||
Method = "POST",
|
||||
ServicePrefix = "Media_20160725",
|
||||
Operation = "Get",
|
||||
BodyText = """{"paths":["/media/test-item"]}"""
|
||||
});
|
||||
|
||||
using var fetchedPayload = JsonDocument.Parse(fetched.BodyText);
|
||||
Assert.Single(fetchedPayload.RootElement.EnumerateArray());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task KeyCreateSymmetricKey_ReturnsKeyPayload()
|
||||
{
|
||||
var result = await _service.DispatchAsync(new ProtocolEnvelope
|
||||
{
|
||||
HostName = "api.jibo.com",
|
||||
Method = "POST",
|
||||
ServicePrefix = "Key_20160715",
|
||||
Operation = "CreateSymmetricKey",
|
||||
BodyText = """{"loopId":"openjibo-default-loop"}"""
|
||||
});
|
||||
|
||||
using var payload = JsonDocument.Parse(result.BodyText);
|
||||
Assert.Equal("openjibo-default-loop", payload.RootElement.GetProperty("loopId").GetString());
|
||||
Assert.False(string.IsNullOrWhiteSpace(payload.RootElement.GetProperty("key").GetString()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PersonListHolidays_ReturnsHoliday()
|
||||
{
|
||||
var result = await _service.DispatchAsync(new ProtocolEnvelope
|
||||
{
|
||||
HostName = "api.jibo.com",
|
||||
Method = "POST",
|
||||
ServicePrefix = "Person_20160715",
|
||||
Operation = "ListHolidays",
|
||||
BodyText = "{}"
|
||||
});
|
||||
|
||||
using var payload = JsonDocument.Parse(result.BodyText);
|
||||
Assert.Single(payload.RootElement.EnumerateArray());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
using Jibo.Cloud.Application.Services;
|
||||
using Jibo.Cloud.Infrastructure.Persistence;
|
||||
using Jibo.Cloud.Tests.Fixtures;
|
||||
|
||||
namespace Jibo.Cloud.Tests.Protocol;
|
||||
|
||||
public sealed class ProtocolFixtureReplayTests
|
||||
{
|
||||
private readonly JiboCloudProtocolService _service = new(new InMemoryCloudStateStore());
|
||||
|
||||
[Theory]
|
||||
[InlineData("fixtures\\create-hub-token.request.json")]
|
||||
[InlineData("fixtures\\new-robot-token.request.json")]
|
||||
public async Task FixtureRequest_ReplaysSuccessfully(string relativePath)
|
||||
{
|
||||
var fixture = ProtocolFixtureLoader.Load(relativePath);
|
||||
var result = await _service.DispatchAsync(fixture.Request);
|
||||
|
||||
Assert.Equal(fixture.ExpectedStatusCode, result.StatusCode);
|
||||
Assert.False(string.IsNullOrWhiteSpace(result.BodyText));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user