|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.Net; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Text; |
| 7 | +using System.Text.Json; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 10 | + |
| 11 | +namespace Azure.DataApiBuilder.Service.Tests.SqlTests.RestApiTests |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Tests for SQL Server native JSON column support via REST endpoints (read and write). |
| 15 | + /// DAB does nothing special for a JSON column - it is written from the request payload and |
| 16 | + /// read back with no custom handling, new scalar, or format. On read, SQL Server's |
| 17 | + /// FOR JSON PATH projection (which DAB uses to shape every result) inlines a native json |
| 18 | + /// column as a nested JSON value, so the metadata surfaces at the REST boundary as a JSON |
| 19 | + /// object rather than an escaped string. Writes still supply the payload as a JSON string, |
| 20 | + /// exactly as a normal string column would be written. |
| 21 | + /// Assertions compare the returned metadata semantically so they are robust to any |
| 22 | + /// whitespace / key-order normalization the engine applies to the JSON type. |
| 23 | + /// NOTE: The native JSON data type requires SQL Server 2025 / Azure SQL. |
| 24 | + /// </summary> |
| 25 | + [TestClass, TestCategory(TestCategory.MSSQL)] |
| 26 | + public class MsSqlRestJsonTypesTests : SqlTestBase |
| 27 | + { |
| 28 | + private const string JSON_TYPE_REST_PATH = "api/Profile"; |
| 29 | + |
| 30 | + [ClassInitialize] |
| 31 | + public static async Task SetupAsync(TestContext context) |
| 32 | + { |
| 33 | + DatabaseEngine = TestCategory.MSSQL; |
| 34 | + await InitializeTestFixture(); |
| 35 | + } |
| 36 | + |
| 37 | + #region Read Tests |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// GET /api/Profile - Verify the whole collection (5 seeded rows) is returned and that |
| 41 | + /// each metadata value renders either as a native JSON object payload or null (row 5). |
| 42 | + /// </summary> |
| 43 | + [TestMethod] |
| 44 | + public async Task GetJsonTypeList() |
| 45 | + { |
| 46 | + HttpResponseMessage response = await HttpClient.GetAsync(JSON_TYPE_REST_PATH); |
| 47 | + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); |
| 48 | + |
| 49 | + JsonElement items = JsonDocument.Parse(await response.Content.ReadAsStringAsync()) |
| 50 | + .RootElement.GetProperty("value"); |
| 51 | + Assert.AreEqual(5, items.GetArrayLength(), "Expected the 5 seeded profile rows."); |
| 52 | + |
| 53 | + // Rows 1-4 carry a JSON payload (inlined as a native JSON object); row 5 is null. |
| 54 | + foreach (JsonElement record in items.EnumerateArray()) |
| 55 | + { |
| 56 | + JsonValueKind metadataKind = record.GetProperty("metadata").ValueKind; |
| 57 | + Assert.IsTrue( |
| 58 | + metadataKind is JsonValueKind.Object or JsonValueKind.Null, |
| 59 | + $"Expected metadata to be a JSON object or null, but was {metadataKind}."); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// GET /api/Profile/id/1 - Verify a simple object payload round-trips (verbatim value-equivalence). |
| 65 | + /// </summary> |
| 66 | + [TestMethod] |
| 67 | + public async Task GetJsonTypeByPrimaryKey() |
| 68 | + { |
| 69 | + JsonElement metadata = ParseMetadata(await GetRecordByIdAsync(1)); |
| 70 | + Assert.AreEqual("admin", metadata.GetProperty("role").GetString()); |
| 71 | + Assert.AreEqual(3, metadata.GetProperty("tier").GetInt32()); |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// GET /api/Profile/id/5 - Verify a SQL NULL metadata value is rendered as JSON null. |
| 76 | + /// </summary> |
| 77 | + [TestMethod] |
| 78 | + public async Task GetJsonTypeWithNull() |
| 79 | + { |
| 80 | + JsonElement record = await GetRecordByIdAsync(5); |
| 81 | + Assert.AreEqual(JsonValueKind.Null, record.GetProperty("metadata").ValueKind); |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// GET /api/Profile/id/2 - Verify an array-bearing payload is preserved. |
| 86 | + /// </summary> |
| 87 | + [TestMethod] |
| 88 | + public async Task GetJsonTypeWithArrayPayload() |
| 89 | + { |
| 90 | + JsonElement metadata = ParseMetadata(await GetRecordByIdAsync(2)); |
| 91 | + JsonElement tags = metadata.GetProperty("tags"); |
| 92 | + Assert.AreEqual(JsonValueKind.Array, tags.ValueKind); |
| 93 | + Assert.AreEqual(3, tags.GetArrayLength()); |
| 94 | + Assert.AreEqual("a", tags[0].GetString()); |
| 95 | + Assert.AreEqual("b", tags[1].GetString()); |
| 96 | + Assert.AreEqual("c", tags[2].GetString()); |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// GET /api/Profile/id/3 - Verify a deeply nested object payload is preserved. |
| 101 | + /// </summary> |
| 102 | + [TestMethod] |
| 103 | + public async Task GetJsonTypeWithNestedPayload() |
| 104 | + { |
| 105 | + JsonElement metadata = ParseMetadata(await GetRecordByIdAsync(3)); |
| 106 | + Assert.IsTrue(metadata.GetProperty("nested").GetProperty("key").GetProperty("deep").GetBoolean()); |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// GET /api/Profile/id/4 - Verify unicode (including a multi-byte emoji) round-trips intact. |
| 111 | + /// </summary> |
| 112 | + [TestMethod] |
| 113 | + public async Task GetJsonTypeWithUnicode() |
| 114 | + { |
| 115 | + JsonElement metadata = ParseMetadata(await GetRecordByIdAsync(4)); |
| 116 | + Assert.AreEqual("éü😀", metadata.GetProperty("unicode").GetString()); |
| 117 | + } |
| 118 | + |
| 119 | + #endregion |
| 120 | + |
| 121 | + #region Write Tests |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// POST /api/Profile - Verify a new record with a JSON payload can be inserted, the value |
| 125 | + /// echoes back, and it is persisted (read-back). Also covers inserting a null payload. |
| 126 | + /// </summary> |
| 127 | + [DataTestMethod] |
| 128 | + [DataRow("{ \"metadata\": \"{\\\"role\\\":\\\"guest\\\"}\" }", false, DisplayName = "Insert profile with valid JSON object")] |
| 129 | + [DataRow("{ \"metadata\": null }", true, DisplayName = "Insert profile with null metadata")] |
| 130 | + public async Task InsertJsonType(string requestBody, bool expectNull) |
| 131 | + { |
| 132 | + HttpResponseMessage postResponse = await HttpClient.PostAsync( |
| 133 | + JSON_TYPE_REST_PATH, |
| 134 | + new StringContent(requestBody, Encoding.UTF8, "application/json")); |
| 135 | + Assert.AreEqual(HttpStatusCode.Created, postResponse.StatusCode); |
| 136 | + |
| 137 | + JsonElement postElement = JsonDocument.Parse(await postResponse.Content.ReadAsStringAsync()) |
| 138 | + .RootElement.GetProperty("value")[0]; |
| 139 | + int newId = postElement.GetProperty("id").GetInt32(); |
| 140 | + |
| 141 | + JsonElement readBack = await GetRecordByIdAsync(newId); |
| 142 | + if (expectNull) |
| 143 | + { |
| 144 | + Assert.AreEqual(JsonValueKind.Null, readBack.GetProperty("metadata").ValueKind); |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + Assert.AreEqual("guest", ParseMetadata(readBack).GetProperty("role").GetString()); |
| 149 | + } |
| 150 | + |
| 151 | + await DeleteProfile(newId); |
| 152 | + } |
| 153 | + |
| 154 | + /// <summary> |
| 155 | + /// PUT /api/Profile/id/1 - Verify a full update replaces the metadata payload, then restore it. |
| 156 | + /// </summary> |
| 157 | + [TestMethod] |
| 158 | + public async Task PutJsonType_Update() |
| 159 | + { |
| 160 | + try |
| 161 | + { |
| 162 | + HttpResponseMessage response = await HttpClient.PutAsync( |
| 163 | + $"{JSON_TYPE_REST_PATH}/id/1", |
| 164 | + new StringContent("{ \"metadata\": \"{\\\"role\\\":\\\"owner\\\"}\" }", Encoding.UTF8, "application/json")); |
| 165 | + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); |
| 166 | + Assert.AreEqual("owner", ParseMetadata(await GetRecordByIdAsync(1)).GetProperty("role").GetString()); |
| 167 | + } |
| 168 | + finally |
| 169 | + { |
| 170 | + // Restore original value so shared row 1 is left intact for other tests. |
| 171 | + HttpResponseMessage restore = await HttpClient.PutAsync( |
| 172 | + $"{JSON_TYPE_REST_PATH}/id/1", |
| 173 | + new StringContent("{ \"metadata\": \"{\\\"role\\\":\\\"admin\\\",\\\"tier\\\":3}\" }", Encoding.UTF8, "application/json")); |
| 174 | + Assert.AreEqual(HttpStatusCode.OK, restore.StatusCode); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + /// <summary> |
| 179 | + /// PATCH /api/Profile/id/1 - Verify a partial update sets a new metadata payload, then restore it. |
| 180 | + /// </summary> |
| 181 | + [TestMethod] |
| 182 | + public async Task PatchJsonType_Update() |
| 183 | + { |
| 184 | + try |
| 185 | + { |
| 186 | + HttpResponseMessage response = await HttpClient.PatchAsync( |
| 187 | + $"{JSON_TYPE_REST_PATH}/id/1", |
| 188 | + new StringContent("{ \"metadata\": \"{\\\"role\\\":\\\"editor\\\"}\" }", Encoding.UTF8, "application/json")); |
| 189 | + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); |
| 190 | + Assert.AreEqual("editor", ParseMetadata(await GetRecordByIdAsync(1)).GetProperty("role").GetString()); |
| 191 | + } |
| 192 | + finally |
| 193 | + { |
| 194 | + // Restore original value so shared row 1 is left intact for other tests. |
| 195 | + HttpResponseMessage restore = await HttpClient.PutAsync( |
| 196 | + $"{JSON_TYPE_REST_PATH}/id/1", |
| 197 | + new StringContent("{ \"metadata\": \"{\\\"role\\\":\\\"admin\\\",\\\"tier\\\":3}\" }", Encoding.UTF8, "application/json")); |
| 198 | + Assert.AreEqual(HttpStatusCode.OK, restore.StatusCode); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + /// <summary> |
| 203 | + /// PATCH /api/Profile/id/2 - Verify metadata can be cleared to null. |
| 204 | + /// </summary> |
| 205 | + [TestMethod] |
| 206 | + public async Task PatchJsonType_ToNull() |
| 207 | + { |
| 208 | + try |
| 209 | + { |
| 210 | + HttpResponseMessage response = await HttpClient.PatchAsync( |
| 211 | + $"{JSON_TYPE_REST_PATH}/id/2", |
| 212 | + new StringContent("{ \"metadata\": null }", Encoding.UTF8, "application/json")); |
| 213 | + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); |
| 214 | + Assert.AreEqual(JsonValueKind.Null, (await GetRecordByIdAsync(2)).GetProperty("metadata").ValueKind); |
| 215 | + } |
| 216 | + finally |
| 217 | + { |
| 218 | + // Restore original array payload so shared row 2 is left intact for other tests. |
| 219 | + HttpResponseMessage restore = await HttpClient.PutAsync( |
| 220 | + $"{JSON_TYPE_REST_PATH}/id/2", |
| 221 | + new StringContent("{ \"metadata\": \"{\\\"tags\\\":[\\\"a\\\",\\\"b\\\",\\\"c\\\"]}\" }", Encoding.UTF8, "application/json")); |
| 222 | + Assert.AreEqual(HttpStatusCode.OK, restore.StatusCode); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + #endregion |
| 227 | + |
| 228 | + #region Helpers |
| 229 | + |
| 230 | + /// <summary> |
| 231 | + /// DELETE /api/Profile/id/{id} - Verify a record can be deleted. |
| 232 | + /// </summary> |
| 233 | + private static async Task DeleteProfile(int id) |
| 234 | + { |
| 235 | + HttpResponseMessage deleteResponse = await HttpClient.DeleteAsync($"{JSON_TYPE_REST_PATH}/id/{id}"); |
| 236 | + Assert.AreEqual(HttpStatusCode.NoContent, deleteResponse.StatusCode); |
| 237 | + } |
| 238 | + |
| 239 | + /// <summary> |
| 240 | + /// Fetches a single Profile record by its primary key and returns the record element. |
| 241 | + /// </summary> |
| 242 | + private static async Task<JsonElement> GetRecordByIdAsync(int id) |
| 243 | + { |
| 244 | + HttpResponseMessage response = await HttpClient.GetAsync($"{JSON_TYPE_REST_PATH}/id/{id}"); |
| 245 | + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); |
| 246 | + string body = await response.Content.ReadAsStringAsync(); |
| 247 | + return JsonDocument.Parse(body).RootElement.GetProperty("value")[0].Clone(); |
| 248 | + } |
| 249 | + |
| 250 | + /// <summary> |
| 251 | + /// Returns the metadata field as a JSON element. DAB applies no special handling to a JSON |
| 252 | + /// column, so SQL Server's FOR JSON PATH projection inlines it as a native JSON object at the |
| 253 | + /// REST boundary. This helper asserts the value is a JSON object and returns it for inspection. |
| 254 | + /// </summary> |
| 255 | + private static JsonElement ParseMetadata(JsonElement record) |
| 256 | + { |
| 257 | + JsonElement metadata = record.GetProperty("metadata"); |
| 258 | + Assert.AreEqual( |
| 259 | + JsonValueKind.Object, |
| 260 | + metadata.ValueKind, |
| 261 | + "A native JSON column is inlined as a JSON object at the REST boundary via FOR JSON PATH."); |
| 262 | + |
| 263 | + return metadata.Clone(); |
| 264 | + } |
| 265 | + |
| 266 | + #endregion |
| 267 | + } |
| 268 | +} |
0 commit comments