-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IHS-88 IHS-89 Change message format (#90)
* change NewImage and CategorizedNewImage models * pass original image name * upd ci action
- Loading branch information
Showing
12 changed files
with
210 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
src/ImageHosting.Storage.Domain/Messages/CategorizedNewImage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
using System.Text.Json.Serialization; | ||
using ImageHosting.Storage.Domain.ValueTypes; | ||
|
||
namespace ImageHosting.Storage.Domain.Messages; | ||
|
||
public class CategorizedNewImage | ||
{ | ||
[JsonPropertyName("imageId")] public required ImageId ImageId { get; init; } | ||
[JsonPropertyName("image")] public required NewImage Image { get; init; } | ||
[JsonPropertyName("categories")] public required Dictionary<string, double> Categories { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,11 @@ | ||
using ImageHosting.Storage.Domain.ValueTypes; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace ImageHosting.Storage.Domain.Messages; | ||
|
||
public class NewImage | ||
{ | ||
[JsonPropertyName("bucket")] public UserId Bucket { get; set; } | ||
[JsonIgnore] public ImageId ImageId { get; set; } | ||
|
||
[JsonPropertyName("prefix")] public string Prefix => ImageId.ToString(); | ||
[JsonPropertyName("image")] public string ImageName => "original.jpg"; | ||
public required UserId Bucket { get; init; } | ||
public string Prefix => ImageId.ToString(); | ||
public required string Name { get; init; } | ||
public required ImageId ImageId { get; init; } | ||
} |
77 changes: 77 additions & 0 deletions
77
src/ImageHosting.Storage.Domain/Serialization/NewImageJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using ImageHosting.Storage.Domain.Messages; | ||
using ImageHosting.Storage.Domain.ValueTypes; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace ImageHosting.Storage.Domain.Serialization; | ||
|
||
public class NewImageJsonConverter : JsonConverter<NewImage> | ||
{ | ||
public override NewImage Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType is not JsonTokenType.StartObject) | ||
throw new JsonException("Expected start of object"); | ||
|
||
UserId? bucket = null; | ||
ImageId? prefix = null; | ||
string? name = null; | ||
|
||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType is JsonTokenType.EndObject) | ||
break; | ||
|
||
if (reader.TokenType is JsonTokenType.PropertyName) | ||
{ | ||
var propertyName = reader.GetString(); | ||
reader.Read(); // Move to the value | ||
|
||
switch (propertyName) | ||
{ | ||
case "bucket": | ||
bucket = JsonSerializer.Deserialize<UserId>(ref reader, options); | ||
break; | ||
|
||
case "prefix": | ||
prefix = JsonSerializer.Deserialize<ImageId>(ref reader, options); | ||
break; | ||
|
||
case "name": | ||
name = reader.GetString(); | ||
break; | ||
|
||
default: | ||
throw new JsonException($"Unexpected property {propertyName}"); | ||
} | ||
} | ||
} | ||
|
||
if (!bucket.HasValue || name == null || !prefix.HasValue) | ||
{ | ||
throw new JsonException("Missing required properties"); | ||
} | ||
|
||
return new NewImage | ||
{ | ||
Bucket = bucket.Value, | ||
Name = name, | ||
ImageId = prefix.Value, | ||
}; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, NewImage value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
|
||
writer.WritePropertyName("bucket"); | ||
JsonSerializer.Serialize(writer, value.Bucket, options); | ||
|
||
writer.WritePropertyName("prefix"); | ||
writer.WriteStringValue(value.Prefix); | ||
|
||
writer.WritePropertyName("name"); | ||
writer.WriteStringValue(value.Name); | ||
|
||
writer.WriteEndObject(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
tests/ImageHosting.Storage.UnitTests/Extensions/ObjectExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Reflection; | ||
using Xunit.Abstractions; | ||
|
||
namespace ImageHosting.Storage.UnitTests.Extensions; | ||
|
||
public static class ObjectExtensions | ||
{ | ||
public static void PrintProperties(this ITestOutputHelper outputHelper, object? obj) | ||
{ | ||
if (obj == null) | ||
{ | ||
outputHelper.WriteLine("null"); | ||
return; | ||
} | ||
|
||
var type = obj.GetType(); | ||
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); | ||
|
||
foreach (var property in properties) | ||
{ | ||
var value = property.GetValue(obj) ?? "null"; | ||
outputHelper.WriteLine($"{property.Name}: {value}"); | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
tests/ImageHosting.Storage.UnitTests/Serialization/NewImageJsonConverterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
namespace ImageHosting.Storage.UnitTests.Serialization; | ||
|
||
using ImageHosting.Storage.Domain.Messages; | ||
using ImageHosting.Storage.Domain.Serialization; | ||
using ImageHosting.Storage.Domain.ValueTypes; | ||
using ImageHosting.Storage.UnitTests.Extensions; | ||
using System.Text.Json; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
public class NewImageJsonConverterTests | ||
{ | ||
private readonly ITestOutputHelper _testOutputHelper; | ||
private readonly JsonSerializerOptions _jsonSerializerOptions; | ||
|
||
public NewImageJsonConverterTests(ITestOutputHelper testOutputHelper) | ||
{ | ||
_testOutputHelper = testOutputHelper; | ||
_jsonSerializerOptions = new JsonSerializerOptions | ||
{ | ||
Converters = { new NewImageJsonConverter() }, | ||
WriteIndented = false | ||
}; | ||
} | ||
|
||
[Fact] | ||
public void Serialize_NewImage_returns_correct_JSON() | ||
{ | ||
var newImage = new NewImage | ||
{ | ||
Bucket = new UserId(Guid.Empty), | ||
ImageId = new ImageId(Guid.Empty), | ||
Name = "image.jpg", | ||
}; | ||
|
||
var json = JsonSerializer.Serialize(newImage, _jsonSerializerOptions); | ||
|
||
_testOutputHelper.WriteLine("JSON string: {0}", json); | ||
json.Should().Contain("\"bucket\":\"00000000-0000-0000-0000-000000000000\""); | ||
json.Should().Contain("\"prefix\":\"00000000-0000-0000-0000-000000000000\""); | ||
json.Should().Contain("\"name\":\"image.jpg\""); | ||
json.Should().NotContain("imageId"); // Ensure ImageId is ignored | ||
} | ||
|
||
[Fact] | ||
public void Deserialize_valid_JSON_returns_NewImage() | ||
{ | ||
var json = @"{ | ||
""bucket"": ""00000000-0000-0000-0000-000000000000"", | ||
""prefix"": ""00000000-0000-0000-0000-000000000000"", | ||
""name"": ""image.jpg"" | ||
}"; | ||
|
||
var newImage = JsonSerializer.Deserialize<NewImage>(json, _jsonSerializerOptions); | ||
|
||
_testOutputHelper.PrintProperties(newImage); | ||
Assert.NotNull(newImage); | ||
Assert.Equal(new UserId(Guid.Empty), newImage.Bucket); | ||
Assert.Equal("image.jpg", newImage.Name); | ||
Assert.Equal(new ImageId(Guid.Empty), newImage.ImageId); | ||
} | ||
|
||
[Fact] | ||
public void Deserialize_invalid_JSON_throws_JSON_exception() | ||
{ | ||
// Missing required prefix field | ||
var invalidJson = @"{ | ||
""bucket"": ""00000000-0000-0000-0000-000000000000"", | ||
""image"": ""image.jpg"" | ||
}"; | ||
|
||
_testOutputHelper.WriteLine("JSON string: {0}", invalidJson); | ||
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<NewImage>(invalidJson, _jsonSerializerOptions)); | ||
} | ||
} |