blob: 82d3a1e89597fa984fe8d5561ff0937ac2538bf1 [file] [log] [blame]
Henrique Mendonça15d90422015-06-25 22:31:41 +10001function serialize(data) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -04002 const transport = new Thrift.Transport('/service');
3 const protocol = new Thrift.Protocol(transport);
Kazuki Matsudab909a382016-02-13 19:36:09 +09004 protocol.writeMessageBegin('', 0, 0);
Henrique Mendonça15d90422015-06-25 22:31:41 +10005 data.write(protocol);
6 protocol.writeMessageEnd();
7 return transport.send_buf;
8}
9
10function deserialize(serialized, type) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -040011 const transport = new Thrift.Transport('/service');
Henrique Mendonça15d90422015-06-25 22:31:41 +100012 transport.setRecvBuffer(serialized);
Brian Forbisb5d6ea32018-08-25 23:39:29 -040013 const protocol = new Thrift.Protocol(transport);
Henrique Mendonça15d90422015-06-25 22:31:41 +100014 protocol.readMessageBegin();
Brian Forbisb5d6ea32018-08-25 23:39:29 -040015 const data = new type();
Henrique Mendonça15d90422015-06-25 22:31:41 +100016 data.read(protocol);
17 protocol.readMessageEnd();
18 return data;
19}
20
21
22function createThriftObj() {
23
24 return new Complex({
25
26 struct_field: new Simple({value: 'a'}),
27
28 struct_list_field: [
29 new Simple({value: 'b'}),
Kazuki Matsudab909a382016-02-13 19:36:09 +090030 new Simple({value: 'c'})
Henrique Mendonça15d90422015-06-25 22:31:41 +100031 ],
32
33 struct_set_field: [
34 new Simple({value: 'd'}),
Kazuki Matsudab909a382016-02-13 19:36:09 +090035 new Simple({value: 'e'})
Henrique Mendonça15d90422015-06-25 22:31:41 +100036 ],
37
38 struct_map_field: {
39 A: new Simple({value: 'f'}),
40 B: new Simple({value: 'g'})
41 },
42
43 struct_nested_containers_field: [
44 [
45 {
46 C: [
47 new Simple({value: 'h'}),
48 new Simple({value: 'i'})
49 ]
50 }
51 ]
52 ],
53
54
55 struct_nested_containers_field2: {
56 D: [
57 {
58 DA: new Simple({value: 'j'})
59 },
60 {
61 DB: new Simple({value: 'k'})
62 }
63 ]
Philip Frank55ddf192018-01-02 09:00:36 +010064 },
65
66 list_of_list_field: [
67 ['one', 'two'],
68 ['three', 'four'],
69 ['five', 'six']
70 ]
Henrique Mendonça15d90422015-06-25 22:31:41 +100071 }
72 );
73}
74
75
76function createJsObj() {
77
78 return {
79
80 struct_field: {value: 'a'},
81
82 struct_list_field: [
83 {value: 'b'},
Kazuki Matsudab909a382016-02-13 19:36:09 +090084 {value: 'c'}
Henrique Mendonça15d90422015-06-25 22:31:41 +100085 ],
86
87 struct_set_field: [
88 {value: 'd'},
Kazuki Matsudab909a382016-02-13 19:36:09 +090089 {value: 'e'}
Henrique Mendonça15d90422015-06-25 22:31:41 +100090 ],
91
92 struct_map_field: {
93 A: {value: 'f'},
94 B: {value: 'g'}
95 },
96
97 struct_nested_containers_field: [
98 [
99 {
100 C: [
101 {value: 'h'},
102 {value: 'i'}
103 ]
104 }
105 ]
106 ],
107
108 struct_nested_containers_field2: {
109 D: [
110 {
111 DA: {value: 'j'}
112 },
113 {
114 DB: {value: 'k'}
115 }
116 ]
Philip Frank55ddf192018-01-02 09:00:36 +0100117 },
118
119 list_of_list_field: [
120 ['one', 'two'],
121 ['three', 'four'],
122 ['five', 'six']
123 ]
Henrique Mendonça15d90422015-06-25 22:31:41 +1000124 };
125}
126
127
128function assertValues(obj, assert) {
129 assert.equal(obj.struct_field.value, 'a');
130 assert.equal(obj.struct_list_field[0].value, 'b');
131 assert.equal(obj.struct_list_field[1].value, 'c');
132 assert.equal(obj.struct_set_field[0].value, 'd');
133 assert.equal(obj.struct_set_field[1].value, 'e');
134 assert.equal(obj.struct_map_field.A.value, 'f');
135 assert.equal(obj.struct_map_field.B.value, 'g');
136 assert.equal(obj.struct_nested_containers_field[0][0].C[0].value, 'h');
137 assert.equal(obj.struct_nested_containers_field[0][0].C[1].value, 'i');
138 assert.equal(obj.struct_nested_containers_field2.D[0].DA.value, 'j');
139 assert.equal(obj.struct_nested_containers_field2.D[1].DB.value, 'k');
Philip Frank55ddf192018-01-02 09:00:36 +0100140 assert.equal(obj.list_of_list_field[0][0], 'one');
141 assert.equal(obj.list_of_list_field[0][1], 'two');
142 assert.equal(obj.list_of_list_field[1][0], 'three');
143 assert.equal(obj.list_of_list_field[1][1], 'four');
144 assert.equal(obj.list_of_list_field[2][0], 'five');
145 assert.equal(obj.list_of_list_field[2][1], 'six');
Henrique Mendonça15d90422015-06-25 22:31:41 +1000146}
147
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400148const cases = {
Henrique Mendonça15d90422015-06-25 22:31:41 +1000149
Kazuki Matsudab909a382016-02-13 19:36:09 +0900150 'Serialize/deserialize simple struct should return equal object': function(assert) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400151 const tObj = new Simple({value: 'a'});
152 const received = deserialize(serialize(tObj), Simple);
Henrique Mendonça15d90422015-06-25 22:31:41 +1000153 assert.ok(tObj !== received);
154 assert.deepEqual(received, tObj);
155 },
156
157
Kazuki Matsudab909a382016-02-13 19:36:09 +0900158 'Serialize/deserialize should return equal object': function(assert) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400159 const tObj = createThriftObj();
160 const received = deserialize(serialize(tObj), Complex);
Henrique Mendonça15d90422015-06-25 22:31:41 +1000161 assert.ok(tObj !== received);
162 assert.deepEqual(received, tObj);
163 },
164
Kazuki Matsudab909a382016-02-13 19:36:09 +0900165 '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 -0400166 const tObj1 = createThriftObj();
167 const tObj2 = new Complex(createJsObj());
Henrique Mendonça15d90422015-06-25 22:31:41 +1000168 assertValues(tObj2, assert);
169 assert.equal(serialize(tObj2), serialize(tObj1));
170 },
171
Kazuki Matsudab909a382016-02-13 19:36:09 +0900172 'Modifications to args object should not affect constructed Thrift object': function(assert) {
Henrique Mendonça15d90422015-06-25 22:31:41 +1000173
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400174 const args = createJsObj();
Henrique Mendonça15d90422015-06-25 22:31:41 +1000175 assertValues(args, assert);
176
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400177 const tObj = new Complex(args);
Henrique Mendonça15d90422015-06-25 22:31:41 +1000178 assertValues(tObj, assert);
179
180 args.struct_field.value = 'ZZZ';
181 args.struct_list_field[0].value = 'ZZZ';
182 args.struct_list_field[1].value = 'ZZZ';
183 args.struct_set_field[0].value = 'ZZZ';
184 args.struct_set_field[1].value = 'ZZZ';
185 args.struct_map_field.A.value = 'ZZZ';
186 args.struct_map_field.B.value = 'ZZZ';
187 args.struct_nested_containers_field[0][0].C[0] = 'ZZZ';
188 args.struct_nested_containers_field[0][0].C[1] = 'ZZZ';
189 args.struct_nested_containers_field2.D[0].DA = 'ZZZ';
190 args.struct_nested_containers_field2.D[0].DB = 'ZZZ';
191
192 assertValues(tObj, assert);
193 },
194
Kazuki Matsudab909a382016-02-13 19:36:09 +0900195 'nulls are ok': function(assert) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400196 const tObj = new Complex({
Henrique Mendonça15d90422015-06-25 22:31:41 +1000197 struct_field: null,
198 struct_list_field: null,
199 struct_set_field: null,
200 struct_map_field: null,
201 struct_nested_containers_field: null,
202 struct_nested_containers_field2: null
203 });
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400204 const received = deserialize(serialize(tObj), Complex);
Henrique Mendonça15d90422015-06-25 22:31:41 +1000205 assert.ok(tObj !== received);
206 assert.deepEqual(tObj, received);
207 }
208
209};
210
211Object.keys(cases).forEach(function(caseName) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400212 QUnit.test(caseName, cases[caseName]);
Henrique Mendonça15d90422015-06-25 22:31:41 +1000213});