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
20//This is the server side Node test handler for the standard
21//  Apache Thrift test service.
22
23const es6Mode = process.argv.includes('--es6');
24const genFolder = es6Mode ? 'gen-nodejs-es6' : 'gen-nodejs';
25const ttypes = require(`./${genFolder}/ThriftTest_types`);
26const TException = require('../../nodejs/lib/thrift').TException;
27const Int64 = require('node-int64');
28
29exports.ThriftTestHandler = {
30  testVoid: function(result) {
31    console.log('testVoid()');
32    result(null);
33  },
34  testString: function(thing, result) {
35    console.log('testString(\'' + thing + '\')');
36    result(null, thing);
37  },
38  testByte: function(thing, result) {
39    console.log('testByte(' + thing + ')');
40    result(null, thing);
41  },
42  testI32: function(thing, result) {
43    console.log('testI32(' + thing + ')');
44    result(null, thing);
45  },
46  testI64: function(thing, result) {
47    console.log('testI64(' + thing + ')');
48    result(null, thing);
49  },
50  testDouble: function(thing, result) {
51    console.log('testDouble(' + thing + ')');
52    result(null, thing);
53  },
54  testBinary: function(thing, result) {
55    console.log('testBinary(\'' + thing + '\')');
56    result(null, thing);
57  },
58  testStruct: function(thing, result) {
59    console.log('testStruct(');
60    console.log(thing);
61    console.log(')');
62    result(null, thing);
63  },
64  testNest: function(nest, result) {
65    console.log('testNest(');
66    console.log(nest);
67    console.log(')');
68    result(null, nest);
69  },
70  testMap: function(thing, result) {
71    console.log('testMap(');
72    console.log(thing);
73    console.log(')');
74    result(null, thing);
75  },
76  testStringMap: function(thing, result) {
77    console.log('testStringMap(');
78    console.log(thing);
79    console.log(')');
80    result(null, thing);
81  },
82  testSet: function(thing, result) {
83    console.log('testSet(');
84    console.log(thing);
85    console.log(')');
86    result(null, thing);
87  },
88  testList: function(thing, result) {
89    console.log('testList(');
90    console.log(thing);
91    console.log(')');
92    result(null, thing);
93  },
94  testEnum: function(thing, result) {
95    console.log('testEnum(' + thing + ')');
96    result(null, thing);
97  },
98  testTypedef: function(thing, result) {
99    console.log('testTypedef(' + thing + ')');
100    result(null, thing);
101  },
102  testMapMap: function(hello, result) {
103    console.log('testMapMap(' + hello + ')');
104
105    const mapmap = [];
106    const pos = [];
107    const neg = [];
108    for (let i = 1; i < 5; i++) {
109      pos[i] = i;
110      neg[-i] = -i;
111    }
112    mapmap[4] = pos;
113    mapmap[-4] = neg;
114
115    result(null, mapmap);
116  },
117  testInsanity: function(argument, result) {
118    console.log('testInsanity(');
119    console.log(argument);
120    console.log(')');
121
122    const hello = new ttypes.Xtruct();
123    hello.string_thing = 'Hello2';
124    hello.byte_thing = 2;
125    hello.i32_thing = 2;
126    hello.i64_thing = new Int64(2);
127
128    const goodbye = new ttypes.Xtruct();
129    goodbye.string_thing = 'Goodbye4';
130    goodbye.byte_thing = 4;
131    goodbye.i32_thing = 4;
132    goodbye.i64_thing = new Int64(4);
133
134    const crazy = new ttypes.Insanity();
135    crazy.userMap = [];
136    crazy.userMap[ttypes.Numberz.EIGHT] = 8;
137    crazy.userMap[ttypes.Numberz.FIVE] = 5;
138    crazy.xtructs = [goodbye, hello];
139
140    const first_map = [];
141    const second_map = [];
142
143    first_map[ttypes.Numberz.TWO] = crazy;
144    first_map[ttypes.Numberz.THREE] = crazy;
145
146    const looney = new ttypes.Insanity();
147    second_map[ttypes.Numberz.SIX] = looney;
148
149    const insane = [];
150    insane[1] = first_map;
151    insane[2] = second_map;
152
153    console.log('insane result:');
154    console.log(insane);
155    result(null, insane);
156  },
157  testMulti: function(arg0, arg1, arg2, arg3, arg4, arg5, result) {
158    console.log('testMulti()');
159
160    const hello = new ttypes.Xtruct();
161    hello.string_thing = 'Hello2';
162    hello.byte_thing = arg0;
163    hello.i32_thing = arg1;
164    hello.i64_thing = arg2;
165    result(null, hello);
166  },
167  testException: function(arg, result) {
168    console.log('testException(' + arg + ')');
169    if (arg === 'Xception') {
170      const x = new ttypes.Xception();
171      x.errorCode = 1001;
172      x.message = arg;
173      result(x);
174    } else if (arg === 'TException') {
175      result(new TException(arg));
176    } else {
177      result(null);
178    }
179  },
180  testMultiException: function(arg0, arg1, result) {
181    console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
182    if (arg0 === ('Xception')) {
183      const x = new ttypes.Xception();
184      x.errorCode = 1001;
185      x.message = 'This is an Xception';
186      result(x);
187    } else if (arg0 === ('Xception2')) {
188      const x2 = new ttypes.Xception2();
189      x2.errorCode = 2002;
190      x2.struct_thing = new ttypes.Xtruct();
191      x2.struct_thing.string_thing = 'This is an Xception2';
192      result(x2);
193    }
194
195    const res = new ttypes.Xtruct();
196    res.string_thing = arg1;
197    result(null, res);
198  },
199  testOneway: function(sleepFor, result) {
200    console.log('testOneway(' + sleepFor + ') => JavaScript (like Rust) never sleeps!');
201  }
202};   //ThriftTestSvcHandler
203