blob: 70486bdf38c29b5a92305ae5118c16b1bee5c100 [file] [log] [blame]
Jens Geyer91d9f052024-11-23 23:45:26 +01001// 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
18using System;
19using System.Collections.Generic;
20using System.Diagnostics;
21using System.IO;
22using System.Linq;
23using System.Text;
24using System.Threading.Tasks;
25using Microsoft.VisualStudio.TestTools.UnitTesting;
26using test.ExceptionStruct;
27using Thrift.Collections;
28using Thrift.Protocol;
29using Thrift.Transport.Client;
30
31namespace 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 Geyer45b18682024-11-26 22:22:00 +010045 private static string FormatKey(int i) => $"Test {i}";
Jens Geyer91d9f052024-11-23 23:45:26 +010046
Jens Geyer45b18682024-11-26 22:22:00 +010047 private static BatchGetResponse CreateInitialData()
Jens Geyer91d9f052024-11-23 23:45:26 +010048 {
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 Geyer45b18682024-11-26 22:22:00 +010066 private static async Task<T> WriteAndReadBack<T>(T input) where T : TBase,new()
Jens Geyer91d9f052024-11-23 23:45:26 +010067 {
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 Geyer45b18682024-11-26 22:22:00 +010086 private static void VerifyIdenticalContent(BatchGetResponse left, BatchGetResponse right)
Jens Geyer91d9f052024-11-23 23:45:26 +010087 {
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}