Jens Geyer | 91d9f05 | 2024-11-23 23:45:26 +0100 | [diff] [blame] | 1 | // Licensed to the Apache Software Foundation(ASF) under one |
| 2 | // or more contributor license agreements.See the NOTICE file |
| 3 | // distributed with this work for additional information |
| 4 | // regarding copyright ownership.The ASF licenses this file |
| 5 | // to you under the Apache License, Version 2.0 (the |
| 6 | // "License"); you may not use this file except in compliance |
| 7 | // with the License. You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, |
| 12 | // software distributed under the License is distributed on an |
| 13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | // KIND, either express or implied. See the License for the |
| 15 | // specific language governing permissions and limitations |
| 16 | // under the License. |
| 17 | |
| 18 | using System; |
| 19 | using System.Collections.Generic; |
| 20 | using System.Diagnostics; |
| 21 | using System.IO; |
| 22 | using System.Linq; |
| 23 | using System.Text; |
| 24 | using System.Threading.Tasks; |
| 25 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 26 | using test.ExceptionStruct; |
| 27 | using Thrift.Collections; |
| 28 | using Thrift.Protocol; |
| 29 | using Thrift.Transport.Client; |
| 30 | |
| 31 | namespace Thrift.Tests.DataModel |
| 32 | { |
| 33 | // ReSharper disable once InconsistentNaming |
| 34 | [TestClass] |
| 35 | public class ExceptionAsStructTests |
| 36 | { |
| 37 | [TestMethod] |
| 38 | public async Task Test_Serialize_Deserialize() |
| 39 | { |
| 40 | var initial = CreateInitialData(); |
| 41 | var checking = await WriteAndReadBack(initial); |
| 42 | VerifyIdenticalContent(checking, initial); |
| 43 | } |
| 44 | |
Jens Geyer | 45b1868 | 2024-11-26 22:22:00 +0100 | [diff] [blame] | 45 | private static string FormatKey(int i) => $"Test {i}"; |
Jens Geyer | 91d9f05 | 2024-11-23 23:45:26 +0100 | [diff] [blame] | 46 | |
Jens Geyer | 45b1868 | 2024-11-26 22:22:00 +0100 | [diff] [blame] | 47 | private static BatchGetResponse CreateInitialData() |
Jens Geyer | 91d9f05 | 2024-11-23 23:45:26 +0100 | [diff] [blame] | 48 | { |
| 49 | var initial = new BatchGetResponse() |
| 50 | { |
| 51 | Errors = [], |
| 52 | Responses = [], |
| 53 | }; |
| 54 | |
| 55 | var i = 0; |
| 56 | initial.Errors.Add(FormatKey(++i), new() { Error = ErrorCode.GenericError }); |
| 57 | initial.Errors.Add(FormatKey(++i), new() { Error = ErrorCode.InvalidData }); |
| 58 | initial.Errors.Add(FormatKey(++i), new() { Error = ErrorCode.ServerOverload }); |
| 59 | initial.Responses.Add(FormatKey(++i), new() { Id = FormatKey(i), Data = [0x00, 0x11, 0x22] }); |
| 60 | initial.Responses.Add(FormatKey(++i), new() { Id = FormatKey(i), Data = [0x45, 0x56, 0x64] }); |
| 61 | initial.Responses.Add(FormatKey(++i), new() { Id = FormatKey(i), Data = [0x78, 0x9a, 0xbc] }); |
| 62 | |
| 63 | return initial; |
| 64 | } |
| 65 | |
Jens Geyer | 45b1868 | 2024-11-26 22:22:00 +0100 | [diff] [blame] | 66 | private static async Task<T> WriteAndReadBack<T>(T input) where T : TBase,new() |
Jens Geyer | 91d9f05 | 2024-11-23 23:45:26 +0100 | [diff] [blame] | 67 | { |
| 68 | var stream = new MemoryStream(); |
| 69 | var config = new TConfiguration(); |
| 70 | |
| 71 | // write data |
| 72 | var trans = new TStreamTransport(null, stream, config); |
| 73 | var proto = new TCompactProtocol(trans); |
| 74 | await input.WriteAsync(proto, default); |
| 75 | |
| 76 | // read data |
| 77 | stream.Position = 0; |
| 78 | trans = new TStreamTransport(stream, null, config); |
| 79 | proto = new TCompactProtocol(trans); |
| 80 | var output = new T(); |
| 81 | await output.ReadAsync(proto, default); |
| 82 | |
| 83 | return output; |
| 84 | } |
| 85 | |
Jens Geyer | 45b1868 | 2024-11-26 22:22:00 +0100 | [diff] [blame] | 86 | private static void VerifyIdenticalContent(BatchGetResponse left, BatchGetResponse right) |
Jens Geyer | 91d9f05 | 2024-11-23 23:45:26 +0100 | [diff] [blame] | 87 | { |
| 88 | // Errors |
| 89 | Assert.IsNotNull(left.Errors); |
| 90 | Assert.IsNotNull(right.Errors); |
| 91 | Assert.AreEqual(left.Errors.Count, right.Errors.Count); |
| 92 | foreach(var key in left.Errors.Keys) |
| 93 | { |
| 94 | Assert.AreEqual(left.Errors[key].Error, right.Errors[key].Error); |
| 95 | } |
| 96 | |
| 97 | // Responses |
| 98 | Assert.IsNotNull(left.Responses); |
| 99 | Assert.IsNotNull(right.Responses); |
| 100 | Assert.AreEqual(left.Responses.Count, right.Responses.Count); |
| 101 | foreach (var key in left.Responses.Keys) |
| 102 | { |
| 103 | Assert.AreEqual(left.Responses[key].Id, right.Responses[key].Id); |
| 104 | |
| 105 | var leftData = left.Responses[key].Data; |
| 106 | var rightData = right.Responses[key].Data; |
| 107 | Assert.IsNotNull(leftData); |
| 108 | Assert.IsNotNull(rightData); |
| 109 | Assert.AreEqual(leftData.Length, rightData.Length); |
| 110 | for (var index = 0; index < leftData.Length; ++index) |
| 111 | Assert.AreEqual(leftData[index], rightData[index]); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | } |
| 116 | } |