Jens Geyer | 72a714e | 2025-08-26 22:12:07 +0200 | [diff] [blame^] | 1 | /* |
| 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ça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 20 | function serialize(data) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 21 | const transport = new Thrift.Transport('/service'); |
| 22 | const protocol = new Thrift.Protocol(transport); |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 23 | protocol.writeMessageBegin('', 0, 0); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 24 | data.write(protocol); |
| 25 | protocol.writeMessageEnd(); |
| 26 | return transport.send_buf; |
| 27 | } |
| 28 | |
| 29 | function deserialize(serialized, type) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 30 | const transport = new Thrift.Transport('/service'); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 31 | transport.setRecvBuffer(serialized); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 32 | const protocol = new Thrift.Protocol(transport); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 33 | protocol.readMessageBegin(); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 34 | const data = new type(); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 35 | data.read(protocol); |
| 36 | protocol.readMessageEnd(); |
| 37 | return data; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | function 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 Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 49 | new Simple({value: 'c'}) |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 50 | ], |
| 51 | |
| 52 | struct_set_field: [ |
| 53 | new Simple({value: 'd'}), |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 54 | new Simple({value: 'e'}) |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 55 | ], |
| 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 Frank | 55ddf19 | 2018-01-02 09:00:36 +0100 | [diff] [blame] | 83 | }, |
| 84 | |
| 85 | list_of_list_field: [ |
| 86 | ['one', 'two'], |
| 87 | ['three', 'four'], |
| 88 | ['five', 'six'] |
| 89 | ] |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 90 | } |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | function createJsObj() { |
| 96 | |
| 97 | return { |
| 98 | |
| 99 | struct_field: {value: 'a'}, |
| 100 | |
| 101 | struct_list_field: [ |
| 102 | {value: 'b'}, |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 103 | {value: 'c'} |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 104 | ], |
| 105 | |
| 106 | struct_set_field: [ |
| 107 | {value: 'd'}, |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 108 | {value: 'e'} |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 109 | ], |
| 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 Frank | 55ddf19 | 2018-01-02 09:00:36 +0100 | [diff] [blame] | 136 | }, |
| 137 | |
| 138 | list_of_list_field: [ |
| 139 | ['one', 'two'], |
| 140 | ['three', 'four'], |
| 141 | ['five', 'six'] |
| 142 | ] |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 143 | }; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | function 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 Frank | 55ddf19 | 2018-01-02 09:00:36 +0100 | [diff] [blame] | 159 | 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ça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 165 | } |
| 166 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 167 | const cases = { |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 168 | |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 169 | 'Serialize/deserialize simple struct should return equal object': function(assert) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 170 | const tObj = new Simple({value: 'a'}); |
| 171 | const received = deserialize(serialize(tObj), Simple); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 172 | assert.ok(tObj !== received); |
| 173 | assert.deepEqual(received, tObj); |
| 174 | }, |
| 175 | |
| 176 | |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 177 | 'Serialize/deserialize should return equal object': function(assert) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 178 | const tObj = createThriftObj(); |
| 179 | const received = deserialize(serialize(tObj), Complex); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 180 | assert.ok(tObj !== received); |
| 181 | assert.deepEqual(received, tObj); |
| 182 | }, |
| 183 | |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 184 | 'Nested structs and containers initialized from plain js objects should serialize same as if initialized from thrift objects': function(assert) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 185 | const tObj1 = createThriftObj(); |
| 186 | const tObj2 = new Complex(createJsObj()); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 187 | assertValues(tObj2, assert); |
| 188 | assert.equal(serialize(tObj2), serialize(tObj1)); |
| 189 | }, |
| 190 | |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 191 | 'Modifications to args object should not affect constructed Thrift object': function(assert) { |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 192 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 193 | const args = createJsObj(); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 194 | assertValues(args, assert); |
| 195 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 196 | const tObj = new Complex(args); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 197 | 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 Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 214 | 'nulls are ok': function(assert) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 215 | const tObj = new Complex({ |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 216 | 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 Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 223 | const received = deserialize(serialize(tObj), Complex); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 224 | assert.ok(tObj !== received); |
| 225 | assert.deepEqual(tObj, received); |
| 226 | } |
| 227 | |
| 228 | }; |
| 229 | |
| 230 | Object.keys(cases).forEach(function(caseName) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 231 | QUnit.test(caseName, cases[caseName]); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 232 | }); |