blob: 504dacf0b3b86947c01aea4eab6744f2eaf72c9e [file] [log] [blame]
James E. King, III375bfee2017-10-26 00:09:34 -04001/*
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
bforbisda1169d2018-10-28 11:27:38 -040020const ttypes = require("./gen-nodejs/JsDeepConstructorTest_types");
21const thrift = require("thrift");
22const test = require("tape");
23const bufferEquals = require("buffer-equals");
Henrique Mendonça15d90422015-06-25 22:31:41 +100024
25function serializeBinary(data) {
bforbisda1169d2018-10-28 11:27:38 -040026 let buff;
27 const transport = new thrift.TBufferedTransport(null, function(msg) {
Henrique Mendonça15d90422015-06-25 22:31:41 +100028 buff = msg;
29 });
bforbisda1169d2018-10-28 11:27:38 -040030 const prot = new thrift.TBinaryProtocol(transport);
Henrique Mendonça15d90422015-06-25 22:31:41 +100031 data.write(prot);
32 prot.flush();
33 return buff;
Henrique Mendonça15d90422015-06-25 22:31:41 +100034}
35
Henrique Mendonça15d90422015-06-25 22:31:41 +100036function deserializeBinary(serialized, type) {
bforbisda1169d2018-10-28 11:27:38 -040037 const t = new thrift.TFramedTransport(serialized);
38 const p = new thrift.TBinaryProtocol(t);
39 const data = new type();
Henrique Mendonça15d90422015-06-25 22:31:41 +100040 data.read(p);
41 return data;
42}
43
Henrique Mendonça15d90422015-06-25 22:31:41 +100044function serializeJSON(data) {
bforbisda1169d2018-10-28 11:27:38 -040045 let buff;
46 const transport = new thrift.TBufferedTransport(null, function(msg) {
Henrique Mendonça15d90422015-06-25 22:31:41 +100047 buff = msg;
48 });
bforbisda1169d2018-10-28 11:27:38 -040049 const protocol = new thrift.TJSONProtocol(transport);
Henrique Mendonça15d90422015-06-25 22:31:41 +100050 protocol.writeMessageBegin("", 0, 0);
51 data.write(protocol);
52 protocol.writeMessageEnd();
53 protocol.flush();
54 return buff;
55}
56
Henrique Mendonça15d90422015-06-25 22:31:41 +100057function deserializeJSON(serialized, type) {
bforbisda1169d2018-10-28 11:27:38 -040058 const transport = new thrift.TFramedTransport(serialized);
59 const protocol = new thrift.TJSONProtocol(transport);
Henrique Mendonça15d90422015-06-25 22:31:41 +100060 protocol.readMessageBegin();
bforbisda1169d2018-10-28 11:27:38 -040061 const data = new type();
Henrique Mendonça15d90422015-06-25 22:31:41 +100062 data.read(protocol);
63 protocol.readMessageEnd();
64 return data;
65}
66
Henrique Mendonça15d90422015-06-25 22:31:41 +100067function createThriftObj() {
Henrique Mendonça15d90422015-06-25 22:31:41 +100068 return new ttypes.Complex({
bforbisda1169d2018-10-28 11:27:38 -040069 struct_field: new ttypes.Simple({ value: "a" }),
Henrique Mendonça15d90422015-06-25 22:31:41 +100070
71 struct_list_field: [
bforbisda1169d2018-10-28 11:27:38 -040072 new ttypes.Simple({ value: "b" }),
73 new ttypes.Simple({ value: "c" })
Henrique Mendonça15d90422015-06-25 22:31:41 +100074 ],
75
76 struct_set_field: [
bforbisda1169d2018-10-28 11:27:38 -040077 new ttypes.Simple({ value: "d" }),
78 new ttypes.Simple({ value: "e" })
Henrique Mendonça15d90422015-06-25 22:31:41 +100079 ],
80
81 struct_map_field: {
bforbisda1169d2018-10-28 11:27:38 -040082 A: new ttypes.Simple({ value: "f" }),
83 B: new ttypes.Simple({ value: "g" })
Henrique Mendonça15d90422015-06-25 22:31:41 +100084 },
85
86 struct_nested_containers_field: [
87 [
88 {
89 C: [
bforbisda1169d2018-10-28 11:27:38 -040090 new ttypes.Simple({ value: "h" }),
91 new ttypes.Simple({ value: "i" })
Henrique Mendonça15d90422015-06-25 22:31:41 +100092 ]
93 }
94 ]
95 ],
96
97 struct_nested_containers_field2: {
98 D: [
99 {
bforbisda1169d2018-10-28 11:27:38 -0400100 DA: new ttypes.Simple({ value: "j" })
Henrique Mendonça15d90422015-06-25 22:31:41 +1000101 },
102 {
bforbisda1169d2018-10-28 11:27:38 -0400103 DB: new ttypes.Simple({ value: "k" })
Henrique Mendonça15d90422015-06-25 22:31:41 +1000104 }
105 ]
Nobuaki Sukegawaa400c692016-03-19 23:55:06 +0900106 },
107
108 list_of_list_field: [
bforbisda1169d2018-10-28 11:27:38 -0400109 ["l00", "l01", "l02"],
110 ["l10", "l11", "l12"],
111 ["l20", "l21", "l22"]
Nobuaki Sukegawaa400c692016-03-19 23:55:06 +0900112 ],
113
114 list_of_list_of_list_field: [
bforbisda1169d2018-10-28 11:27:38 -0400115 [
116 ["m000", "m001", "m002"],
117 ["m010", "m011", "m012"],
118 ["m020", "m021", "m022"]
119 ],
120 [
121 ["m100", "m101", "m102"],
122 ["m110", "m111", "m112"],
123 ["m120", "m121", "m122"]
124 ],
125 [
126 ["m200", "m201", "m202"],
127 ["m210", "m211", "m212"],
128 ["m220", "m221", "m222"]
129 ]
130 ]
Nobuaki Sukegawaa400c692016-03-19 23:55:06 +0900131 });
Henrique Mendonça15d90422015-06-25 22:31:41 +1000132}
133
Henrique Mendonça15d90422015-06-25 22:31:41 +1000134function createJsObj() {
Henrique Mendonça15d90422015-06-25 22:31:41 +1000135 return {
bforbisda1169d2018-10-28 11:27:38 -0400136 struct_field: { value: "a" },
Henrique Mendonça15d90422015-06-25 22:31:41 +1000137
bforbisda1169d2018-10-28 11:27:38 -0400138 struct_list_field: [{ value: "b" }, { value: "c" }],
Henrique Mendonça15d90422015-06-25 22:31:41 +1000139
bforbisda1169d2018-10-28 11:27:38 -0400140 struct_set_field: [{ value: "d" }, { value: "e" }],
Henrique Mendonça15d90422015-06-25 22:31:41 +1000141
142 struct_map_field: {
bforbisda1169d2018-10-28 11:27:38 -0400143 A: { value: "f" },
144 B: { value: "g" }
Henrique Mendonça15d90422015-06-25 22:31:41 +1000145 },
146
147 struct_nested_containers_field: [
148 [
149 {
bforbisda1169d2018-10-28 11:27:38 -0400150 C: [{ value: "h" }, { value: "i" }]
Henrique Mendonça15d90422015-06-25 22:31:41 +1000151 }
152 ]
153 ],
154
155 struct_nested_containers_field2: {
156 D: [
157 {
bforbisda1169d2018-10-28 11:27:38 -0400158 DA: { value: "j" }
Henrique Mendonça15d90422015-06-25 22:31:41 +1000159 },
160 {
bforbisda1169d2018-10-28 11:27:38 -0400161 DB: { value: "k" }
Henrique Mendonça15d90422015-06-25 22:31:41 +1000162 }
163 ]
Nobuaki Sukegawaa400c692016-03-19 23:55:06 +0900164 },
165
166 list_of_list_field: [
bforbisda1169d2018-10-28 11:27:38 -0400167 ["l00", "l01", "l02"],
168 ["l10", "l11", "l12"],
169 ["l20", "l21", "l22"]
Nobuaki Sukegawaa400c692016-03-19 23:55:06 +0900170 ],
171
172 list_of_list_of_list_field: [
bforbisda1169d2018-10-28 11:27:38 -0400173 [
174 ["m000", "m001", "m002"],
175 ["m010", "m011", "m012"],
176 ["m020", "m021", "m022"]
177 ],
178 [
179 ["m100", "m101", "m102"],
180 ["m110", "m111", "m112"],
181 ["m120", "m121", "m122"]
182 ],
183 [
184 ["m200", "m201", "m202"],
185 ["m210", "m211", "m212"],
186 ["m220", "m221", "m222"]
187 ]
188 ]
Henrique Mendonça15d90422015-06-25 22:31:41 +1000189 };
190}
191
Henrique Mendonça15d90422015-06-25 22:31:41 +1000192function assertValues(obj, assert) {
bforbisda1169d2018-10-28 11:27:38 -0400193 assert.equals(obj.struct_field.value, "a");
194 assert.equals(obj.struct_list_field[0].value, "b");
195 assert.equals(obj.struct_list_field[1].value, "c");
196 assert.equals(obj.struct_set_field[0].value, "d");
197 assert.equals(obj.struct_set_field[1].value, "e");
198 assert.equals(obj.struct_map_field.A.value, "f");
199 assert.equals(obj.struct_map_field.B.value, "g");
200 assert.equals(obj.struct_nested_containers_field[0][0].C[0].value, "h");
201 assert.equals(obj.struct_nested_containers_field[0][0].C[1].value, "i");
202 assert.equals(obj.struct_nested_containers_field2.D[0].DA.value, "j");
203 assert.equals(obj.struct_nested_containers_field2.D[1].DB.value, "k");
204 assert.equals(obj.list_of_list_field[0][0], "l00");
205 assert.equals(obj.list_of_list_field[0][1], "l01");
206 assert.equals(obj.list_of_list_field[0][2], "l02");
207 assert.equals(obj.list_of_list_field[1][0], "l10");
208 assert.equals(obj.list_of_list_field[1][1], "l11");
209 assert.equals(obj.list_of_list_field[1][2], "l12");
210 assert.equals(obj.list_of_list_field[2][0], "l20");
211 assert.equals(obj.list_of_list_field[2][1], "l21");
212 assert.equals(obj.list_of_list_field[2][2], "l22");
Nobuaki Sukegawaa400c692016-03-19 23:55:06 +0900213
bforbisda1169d2018-10-28 11:27:38 -0400214 assert.equals(obj.list_of_list_of_list_field[0][0][0], "m000");
215 assert.equals(obj.list_of_list_of_list_field[0][0][1], "m001");
216 assert.equals(obj.list_of_list_of_list_field[0][0][2], "m002");
217 assert.equals(obj.list_of_list_of_list_field[0][1][0], "m010");
218 assert.equals(obj.list_of_list_of_list_field[0][1][1], "m011");
219 assert.equals(obj.list_of_list_of_list_field[0][1][2], "m012");
220 assert.equals(obj.list_of_list_of_list_field[0][2][0], "m020");
221 assert.equals(obj.list_of_list_of_list_field[0][2][1], "m021");
222 assert.equals(obj.list_of_list_of_list_field[0][2][2], "m022");
Nobuaki Sukegawaa400c692016-03-19 23:55:06 +0900223
bforbisda1169d2018-10-28 11:27:38 -0400224 assert.equals(obj.list_of_list_of_list_field[1][0][0], "m100");
225 assert.equals(obj.list_of_list_of_list_field[1][0][1], "m101");
226 assert.equals(obj.list_of_list_of_list_field[1][0][2], "m102");
227 assert.equals(obj.list_of_list_of_list_field[1][1][0], "m110");
228 assert.equals(obj.list_of_list_of_list_field[1][1][1], "m111");
229 assert.equals(obj.list_of_list_of_list_field[1][1][2], "m112");
230 assert.equals(obj.list_of_list_of_list_field[1][2][0], "m120");
231 assert.equals(obj.list_of_list_of_list_field[1][2][1], "m121");
232 assert.equals(obj.list_of_list_of_list_field[1][2][2], "m122");
Nobuaki Sukegawaa400c692016-03-19 23:55:06 +0900233
bforbisda1169d2018-10-28 11:27:38 -0400234 assert.equals(obj.list_of_list_of_list_field[2][0][0], "m200");
235 assert.equals(obj.list_of_list_of_list_field[2][0][1], "m201");
236 assert.equals(obj.list_of_list_of_list_field[2][0][2], "m202");
237 assert.equals(obj.list_of_list_of_list_field[2][1][0], "m210");
238 assert.equals(obj.list_of_list_of_list_field[2][1][1], "m211");
239 assert.equals(obj.list_of_list_of_list_field[2][1][2], "m212");
240 assert.equals(obj.list_of_list_of_list_field[2][2][0], "m220");
241 assert.equals(obj.list_of_list_of_list_field[2][2][1], "m221");
242 assert.equals(obj.list_of_list_of_list_field[2][2][2], "m222");
Henrique Mendonça15d90422015-06-25 22:31:41 +1000243}
244
245function createTestCases(serialize, deserialize) {
bforbisda1169d2018-10-28 11:27:38 -0400246 const cases = {
247 "Serialize/deserialize should return equal object": function(assert) {
248 const tObj = createThriftObj();
249 const received = deserialize(serialize(tObj), ttypes.Complex);
250 assert.ok(tObj !== received, "not the same object");
Henrique Mendonça15d90422015-06-25 22:31:41 +1000251 assert.deepEqual(tObj, received);
252 assert.end();
253 },
254
bforbisda1169d2018-10-28 11:27:38 -0400255 "Nested structs and containers initialized from plain js objects should serialize same as if initialized from thrift objects": function(
256 assert
257 ) {
258 const tObj1 = createThriftObj();
259 const tObj2 = new ttypes.Complex(createJsObj());
Henrique Mendonça15d90422015-06-25 22:31:41 +1000260 assertValues(tObj2, assert);
bforbisda1169d2018-10-28 11:27:38 -0400261 const s1 = serialize(tObj1);
262 const s2 = serialize(tObj2);
Henrique Mendonça15d90422015-06-25 22:31:41 +1000263 assert.ok(bufferEquals(s1, s2));
264 assert.end();
265 },
266
bforbisda1169d2018-10-28 11:27:38 -0400267 "Modifications to args object should not affect constructed Thrift object": function(
268 assert
269 ) {
270 const args = createJsObj();
Henrique Mendonça15d90422015-06-25 22:31:41 +1000271 assertValues(args, assert);
272
bforbisda1169d2018-10-28 11:27:38 -0400273 const tObj = new ttypes.Complex(args);
Henrique Mendonça15d90422015-06-25 22:31:41 +1000274 assertValues(tObj, assert);
275
bforbisda1169d2018-10-28 11:27:38 -0400276 args.struct_field.value = "ZZZ";
277 args.struct_list_field[0].value = "ZZZ";
278 args.struct_list_field[1].value = "ZZZ";
279 args.struct_set_field[0].value = "ZZZ";
280 args.struct_set_field[1].value = "ZZZ";
281 args.struct_map_field.A.value = "ZZZ";
282 args.struct_map_field.B.value = "ZZZ";
283 args.struct_nested_containers_field[0][0].C[0] = "ZZZ";
284 args.struct_nested_containers_field[0][0].C[1] = "ZZZ";
285 args.struct_nested_containers_field2.D[0].DA = "ZZZ";
286 args.struct_nested_containers_field2.D[0].DB = "ZZZ";
Henrique Mendonça15d90422015-06-25 22:31:41 +1000287
288 assertValues(tObj, assert);
289 assert.end();
290 },
291
292 "nulls are ok": function(assert) {
bforbisda1169d2018-10-28 11:27:38 -0400293 const tObj = new ttypes.Complex({
Henrique Mendonça15d90422015-06-25 22:31:41 +1000294 struct_field: null,
295 struct_list_field: null,
296 struct_set_field: null,
297 struct_map_field: null,
298 struct_nested_containers_field: null,
299 struct_nested_containers_field2: null
300 });
bforbisda1169d2018-10-28 11:27:38 -0400301 const received = deserialize(serialize(tObj), ttypes.Complex);
Henrique Mendonça738143c2015-08-16 19:17:33 +1000302 assert.strictEqual(tObj.struct_field, null);
Henrique Mendonça15d90422015-06-25 22:31:41 +1000303 assert.ok(tObj !== received);
304 assert.deepEqual(tObj, received);
305 assert.end();
Henrique Mendonça738143c2015-08-16 19:17:33 +1000306 },
307
308 "Can make list with objects": function(assert) {
bforbisda1169d2018-10-28 11:27:38 -0400309 const tObj = new ttypes.ComplexList({
310 struct_list_field: [new ttypes.Complex({})]
Henrique Mendonça738143c2015-08-16 19:17:33 +1000311 });
bforbisda1169d2018-10-28 11:27:38 -0400312 const innerObj = tObj.struct_list_field[0];
313 assert.ok(innerObj instanceof ttypes.Complex);
Henrique Mendonça738143c2015-08-16 19:17:33 +1000314 assert.strictEqual(innerObj.struct_field, null);
315 assert.strictEqual(innerObj.struct_list_field, null);
316 assert.strictEqual(innerObj.struct_set_field, null);
317 assert.strictEqual(innerObj.struct_map_field, null);
318 assert.strictEqual(innerObj.struct_nested_containers_field, null);
319 assert.strictEqual(innerObj.struct_nested_containers_field2, null);
320 assert.end();
Henrique Mendonça15d90422015-06-25 22:31:41 +1000321 }
Henrique Mendonça15d90422015-06-25 22:31:41 +1000322 };
323 return cases;
324}
325
bforbisda1169d2018-10-28 11:27:38 -0400326function run(name, cases) {
Henrique Mendonça15d90422015-06-25 22:31:41 +1000327 Object.keys(cases).forEach(function(caseName) {
bforbisda1169d2018-10-28 11:27:38 -0400328 test(name + ": " + caseName, cases[caseName]);
Henrique Mendonça15d90422015-06-25 22:31:41 +1000329 });
330}
331
bforbisda1169d2018-10-28 11:27:38 -0400332run("binary", createTestCases(serializeBinary, deserializeBinary));
333run("json", createTestCases(serializeJSON, deserializeJSON));