-
Notifications
You must be signed in to change notification settings - Fork 43
/
ArrayConverterTests.cs
156 lines (132 loc) · 5.63 KB
/
ArrayConverterTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Abblix OIDC Server Library
// Copyright (c) Abblix LLP. All rights reserved.
//
// DISCLAIMER: This software is provided 'as-is', without any express or implied
// warranty. Use at your own risk. Abblix LLP is not liable for any damages
// arising from the use of this software.
//
// LICENSE RESTRICTIONS: This code may not be modified, copied, or redistributed
// in any form outside of the official GitHub repository at:
// https://github.com/Abblix/OIDC.Server. All development and modifications
// must occur within the official repository and are managed solely by Abblix LLP.
//
// Unauthorized use, modification, or distribution of this software is strictly
// prohibited and may be subject to legal action.
//
// For full licensing terms, please visit:
//
// https://oidc.abblix.com/license
//
// CONTACT: For license inquiries or permissions, contact Abblix LLP at
// info@abblix.com
using Abblix.Utils.Json;
namespace Abblix.Utils.UnitTests;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Xunit;
/// <summary>
/// Unit tests for the ArrayConverter<TElement, TConverter> class.
/// These tests cover the serialization and deserialization of arrays using a custom JSON converter for individual elements.
/// </summary>
public class ArrayConverterTests
{
/// <summary>
/// A custom JSON converter for string elements used for testing the ArrayConverter.
/// </summary>
private class CustomElementConverter : JsonConverter<string>
{
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.GetString();
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
=> writer.WriteStringValue(value);
}
/// <summary>
/// Tests the deserialization of a null JSON token.
/// Ensures that the converter returns null when the JSON token is null.
/// </summary>
[Fact]
public void Read_NullToken_ReturnsNull()
{
const string json = "null";
var reader = new Utf8JsonReader(System.Text.Encoding.UTF8.GetBytes(json));
reader.Read();
var converter = new ArrayConverter<string, CustomElementConverter>();
var result = converter.Read(ref reader, typeof(string[]), new JsonSerializerOptions());
Assert.Null(result);
}
/// <summary>
/// Tests the deserialization of an empty JSON array.
/// Ensures that the converter returns an empty array when the JSON array is empty.
/// </summary>
[Fact]
public void Read_EmptyArray_ReturnsEmptyArray()
{
const string json = "[]";
var reader = new Utf8JsonReader(System.Text.Encoding.UTF8.GetBytes(json));
reader.Read();
var converter = new ArrayConverter<string, CustomElementConverter>();
var result = converter.Read(ref reader, typeof(string[]), new JsonSerializerOptions());
Assert.NotNull(result);
Assert.Empty(result);
}
/// <summary>
/// Tests the deserialization of a JSON array with string elements.
/// Ensures that the converter returns an array with the correct elements.
/// </summary>
[Fact]
public void Read_ArrayWithElements_ReturnsArray()
{
const string json = "[\"one\", \"two\", \"three\"]";
var reader = new Utf8JsonReader(System.Text.Encoding.UTF8.GetBytes(json));
reader.Read();
var converter = new ArrayConverter<string, CustomElementConverter>();
var result = converter.Read(ref reader, typeof(string[]), new JsonSerializerOptions());
Assert.Equal(new[] { "one", "two", "three" }, result);
}
/// <summary>
/// Tests the serialization of a null array.
/// Ensures that the converter writes null to the JSON output when the array is null.
/// </summary>
[Fact]
public void Write_NullValue_WritesNull()
{
var converter = new ArrayConverter<string, CustomElementConverter>();
var options = new JsonSerializerOptions();
var memoryStream = new MemoryStream();
using (var writer = new Utf8JsonWriter(memoryStream))
converter.Write(writer, null, options);
var json = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
Assert.Equal("null", json);
}
/// <summary>
/// Tests the serialization of an empty array.
/// Ensures that the converter writes an empty JSON array when the array is empty.
/// </summary>
[Fact]
public void Write_EmptyArray_WritesEmptyArray()
{
var converter = new ArrayConverter<string, CustomElementConverter>();
var options = new JsonSerializerOptions();
var memoryStream = new MemoryStream();
using (var writer = new Utf8JsonWriter(memoryStream))
converter.Write(writer, [], options);
var json = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
Assert.Equal("[]", json);
}
/// <summary>
/// Tests the serialization of an array with string elements.
/// Ensures that the converter writes the correct JSON array when the array contains elements.
/// </summary>
[Fact]
public void Write_ArrayWithElements_WritesArray()
{
var converter = new ArrayConverter<string, CustomElementConverter>();
var options = new JsonSerializerOptions();
var memoryStream = new MemoryStream();
using (var writer = new Utf8JsonWriter(memoryStream))
converter.Write(writer, ["one", "two", "three"], options);
var json = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
Assert.Equal("[\"one\",\"two\",\"three\"]", json);
}
}