blob: 4bde56135603c31059491fc26e0ff6280c888f63 [file] [log] [blame]
Jens Geyer72a714e2025-08-26 22:12:07 +02001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Henrique Mendonça15d90422015-06-25 22:31:41 +100020function serialize(data) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -040021 const transport = new Thrift.Transport('/service');
22 const protocol = new Thrift.Protocol(transport);
Kazuki Matsudab909a382016-02-13 19:36:09 +090023 protocol.writeMessageBegin('', 0, 0);
Henrique Mendonça15d90422015-06-25 22:31:41 +100024 data.write(protocol);
25 protocol.writeMessageEnd();
26 return transport.send_buf;
27}
28
29function deserialize(serialized, type) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -040030 const transport = new Thrift.Transport('/service');
Henrique Mendonça15d90422015-06-25 22:31:41 +100031 transport.setRecvBuffer(serialized);
Brian Forbisb5d6ea32018-08-25 23:39:29 -040032 const protocol = new Thrift.Protocol(transport);
Henrique Mendonça15d90422015-06-25 22:31:41 +100033 protocol.readMessageBegin();
Brian Forbisb5d6ea32018-08-25 23:39:29 -040034 const data = new type();
Henrique Mendonça15d90422015-06-25 22:31:41 +100035 data.read(protocol);
36 protocol.readMessageEnd();
37 return data;
38}
39
40
41function createThriftObj() {
42
43 return new Complex({
44
45 struct_field: new Simple({value: 'a'}),
46
47 struct_list_field: [
48 new Simple({value: 'b'}),
Kazuki Matsudab909a382016-02-13 19:36:09 +090049 new Simple({value: 'c'})
Henrique Mendonça15d90422015-06-25 22:31:41 +100050 ],
51
52 struct_set_field: [
53 new Simple({value: 'd'}),
Kazuki Matsudab909a382016-02-13 19:36:09 +090054 new Simple({value: 'e'})
Henrique Mendonça15d90422015-06-25 22:31:41 +100055 ],
56
57 struct_map_field: {
58 A: new Simple({value: 'f'}),
59 B: new Simple({value: 'g'})
60 },
61
62 struct_nested_containers_field: [
63 [
64 {
65 C: [
66 new Simple({value: 'h'}),
67 new Simple({value: 'i'})
68 ]
69 }
70 ]
71 ],
72
73
74 struct_nested_containers_field2: {
75 D: [
76 {
77 DA: new Simple({value: 'j'})
78 },
79 {
80 DB: new Simple({value: 'k'})
81 }
82 ]
Philip Frank55ddf192018-01-02 09:00:36 +010083 },
84
85 list_of_list_field: [
86 ['one', 'two'],
87 ['three', 'four'],
88 ['five', 'six']
89 ]
Henrique Mendonça15d90422015-06-25 22:31:41 +100090 }
91 );
92}
93
94
95function createJsObj() {
96
97 return {
98
99 struct_field: {value: 'a'},
100
101 struct_list_field: [
102 {value: 'b'},
Kazuki Matsudab909a382016-02-13 19:36:09 +0900103 {value: 'c'}
Henrique Mendonça15d90422015-06-25 22:31:41 +1000104 ],
105
106 struct_set_field: [
107 {value: 'd'},
Kazuki Matsudab909a382016-02-13 19:36:09 +0900108 {value: 'e'}
Henrique Mendonça15d90422015-06-25 22:31:41 +1000109 ],
110
111 struct_map_field: {
112 A: {value: 'f'},
113 B: {value: 'g'}
114 },
115
116 struct_nested_containers_field: [
117 [
118 {
119 C: [
120 {value: 'h'},
121 {value: 'i'}
122 ]
123 }
124 ]
125 ],
126
127 struct_nested_containers_field2: {
128 D: [
129 {
130 DA: {value: 'j'}
131 },
132 {
133 DB: {value: 'k'}
134 }
135 ]
Philip Frank55ddf192018-01-02 09:00:36 +0100136 },
137
138 list_of_list_field: [
139 ['one', 'two'],
140 ['three', 'four'],
141 ['five', 'six']
142 ]
Henrique Mendonça15d90422015-06-25 22:31:41 +1000143 };
144}
145
146
147function assertValues(obj, assert) {
148 assert.equal(obj.struct_field.value, 'a');
149 assert.equal(obj.struct_list_field[0].value, 'b');
150 assert.equal(obj.struct_list_field[1].value, 'c');
151 assert.equal(obj.struct_set_field[0].value, 'd');
152 assert.equal(obj.struct_set_field[1].value, 'e');
153 assert.equal(obj.struct_map_field.A.value, 'f');
154 assert.equal(obj.struct_map_field.B.value, 'g');
155 assert.equal(obj.struct_nested_containers_field[0][0].C[0].value, 'h');
156 assert.equal(obj.struct_nested_containers_field[0][0].C[1].value, 'i');
157 assert.equal(obj.struct_nested_containers_field2.D[0].DA.value, 'j');
158 assert.equal(obj.struct_nested_containers_field2.D[1].DB.value, 'k');
Philip Frank55ddf192018-01-02 09:00:36 +0100159 assert.equal(obj.list_of_list_field[0][0], 'one');
160 assert.equal(obj.list_of_list_field[0][1], 'two');
161 assert.equal(obj.list_of_list_field[1][0], 'three');
162 assert.equal(obj.list_of_list_field[1][1], 'four');
163 assert.equal(obj.list_of_list_field[2][0], 'five');
164 assert.equal(obj.list_of_list_field[2][1], 'six');
Henrique Mendonça15d90422015-06-25 22:31:41 +1000165}
166
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400167const cases = {
Henrique Mendonça15d90422015-06-25 22:31:41 +1000168
Kazuki Matsudab909a382016-02-13 19:36:09 +0900169 'Serialize/deserialize simple struct should return equal object': function(assert) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400170 const tObj = new Simple({value: 'a'});
171 const received = deserialize(serialize(tObj), Simple);
Henrique Mendonça15d90422015-06-25 22:31:41 +1000172 assert.ok(tObj !== received);
173 assert.deepEqual(received, tObj);
174 },
175
176
Kazuki Matsudab909a382016-02-13 19:36:09 +0900177 'Serialize/deserialize should return equal object': function(assert) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400178 const tObj = createThriftObj();
179 const received = deserialize(serialize(tObj), Complex);
Henrique Mendonça15d90422015-06-25 22:31:41 +1000180 assert.ok(tObj !== received);
181 assert.deepEqual(received, tObj);
182 },
183
Kazuki Matsudab909a382016-02-13 19:36:09 +0900184 'Nested structs and containers initialized from plain js objects should serialize same as if initialized from thrift objects': function(assert) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400185 const tObj1 = createThriftObj();
186 const tObj2 = new Complex(createJsObj());
Henrique Mendonça15d90422015-06-25 22:31:41 +1000187 assertValues(tObj2, assert);
188 assert.equal(serialize(tObj2), serialize(tObj1));
189 },
190
Kazuki Matsudab909a382016-02-13 19:36:09 +0900191 'Modifications to args object should not affect constructed Thrift object': function(assert) {
Henrique Mendonça15d90422015-06-25 22:31:41 +1000192
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400193 const args = createJsObj();
Henrique Mendonça15d90422015-06-25 22:31:41 +1000194 assertValues(args, assert);
195
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400196 const tObj = new Complex(args);
Henrique Mendonça15d90422015-06-25 22:31:41 +1000197 assertValues(tObj, assert);
198
199 args.struct_field.value = 'ZZZ';
200 args.struct_list_field[0].value = 'ZZZ';
201 args.struct_list_field[1].value = 'ZZZ';
202 args.struct_set_field[0].value = 'ZZZ';
203 args.struct_set_field[1].value = 'ZZZ';
204 args.struct_map_field.A.value = 'ZZZ';
205 args.struct_map_field.B.value = 'ZZZ';
206 args.struct_nested_containers_field[0][0].C[0] = 'ZZZ';
207 args.struct_nested_containers_field[0][0].C[1] = 'ZZZ';
208 args.struct_nested_containers_field2.D[0].DA = 'ZZZ';
209 args.struct_nested_containers_field2.D[0].DB = 'ZZZ';
210
211 assertValues(tObj, assert);
212 },
213
Kazuki Matsudab909a382016-02-13 19:36:09 +0900214 'nulls are ok': function(assert) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400215 const tObj = new Complex({
Henrique Mendonça15d90422015-06-25 22:31:41 +1000216 struct_field: null,
217 struct_list_field: null,
218 struct_set_field: null,
219 struct_map_field: null,
220 struct_nested_containers_field: null,
221 struct_nested_containers_field2: null
222 });
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400223 const received = deserialize(serialize(tObj), Complex);
Henrique Mendonça15d90422015-06-25 22:31:41 +1000224 assert.ok(tObj !== received);
225 assert.deepEqual(tObj, received);
226 }
227
228};
229
230Object.keys(cases).forEach(function(caseName) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400231 QUnit.test(caseName, cases[caseName]);
Henrique Mendonça15d90422015-06-25 22:31:41 +1000232});