@adobe/acc-js-sdk 1.0.7 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,729 +16,758 @@ governing permissions and limitations under the License.
16
16
  * Unit tests for the schema data objects
17
17
  *
18
18
  *********************************************************************************/
19
- const { DomUtil, XPath, XPathElement } = require('../src/domUtil.js');
19
+ const { SchemaCache } = require('../src/application.js');
20
+ const { DomUtil, XPath } = require('../src/domUtil.js');
21
+ const sdk = require('../src/index.js');
20
22
  const newSchema = require('../src/application.js').newSchema;
21
23
  const newCurrentLogin = require('../src/application.js').newCurrentLogin;
22
24
  const Mock = require('./mock.js').Mock;
23
25
 
24
- describe('Schemas', function() {
25
-
26
- describe("Root node", () => {
26
+ describe('Application', () => {
27
+ describe('Schemas', function() {
28
+
29
+ describe("Root node", () => {
30
+
31
+ it("No root node", () => {
32
+ var xml = DomUtil.parse("<schema namespace='nms' name='recipient'></schema>");
33
+ var schema = newSchema(xml);
34
+ var root = schema.root;
35
+ expect(root).toBeFalsy();
36
+ });
37
+
38
+ it("Just the root node", () => {
39
+ var xml = DomUtil.parse("<schema namespace='nms' name='recipient'><element name='recipient' label='Recipients'/></schema>");
40
+ var schema = newSchema(xml);
41
+ var root = schema.root;
42
+ expect(root).toBeTruthy();
43
+ expect(root.name).toBe("recipient");
44
+ expect(root.label).toBe("Recipients");
45
+ });
46
+
47
+ it("Duplicate root nodes", () => {
48
+ expect(() => {
49
+ var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
50
+ <element name='recipient' label='Recipients'/>
51
+ <element name='recipient' label='Recipients2'/>
52
+ </schema>`);
53
+ newSchema(xml);
54
+ }).toThrow("there's a already a node");
55
+ });
56
+
57
+ it("Should find root node", () => {
58
+ const schema = sdk.TestUtil.newSchema(`
59
+ <schema namespace="nms" name="recipient">
60
+ <element name="recipient">
61
+ <attribute name="id" type="long"/>
62
+ <attribute name="name" type="string"/>
63
+ </element>
64
+ </schema>`);
65
+ expect(schema.root).toBeTruthy();
66
+ });
27
67
 
28
- it("No root node", () => {
29
- var xml = DomUtil.parse("<schema namespace='nms' name='recipient'></schema>");
30
- var schema = newSchema(xml);
31
- var root = schema.root;
32
- expect(root).toBeFalsy();
33
68
  });
34
69
 
35
- it("Just the root node", () => {
36
- var xml = DomUtil.parse("<schema namespace='nms' name='recipient'><element name='recipient' label='Recipients'/></schema>");
37
- var schema = newSchema(xml);
38
- var root = schema.root;
39
- expect(root).toBeTruthy();
40
- expect(root.name).toBe("recipient");
41
- expect(root.label).toBe("Recipients");
42
- });
70
+ describe("isRoot", () => {
71
+ it("Shema node is not root node", () => {
72
+ var xml = DomUtil.parse("<schema namespace='nms' name='recipient'><element name='recipient' label='Recipients'/></schema>");
73
+ var schema = newSchema(xml);
74
+ expect(schema.isRoot).toBeFalsy();
75
+ });
76
+
77
+ it("Should be root node", () => {
78
+ var xml = DomUtil.parse("<schema namespace='nms' name='recipient'><element name='recipient' label='Recipients'/></schema>");
79
+ var schema = newSchema(xml);
80
+ var root = schema.root;
81
+ expect(root.isRoot).toBe(true);
82
+ });
83
+
84
+ it("Should not be root node", () => {
85
+ var xml = DomUtil.parse("<schema namespace='nms' name='recipient'><element name='lib'/><element name='recipient' label='Recipients'/></schema>");
86
+ var schema = newSchema(xml);
87
+ var lib = schema.children["lib"];
88
+ expect(lib.isRoot).toBeFalsy();
89
+ });
90
+
91
+ it("Should not be root node (second level with same name", () => {
92
+ var xml = DomUtil.parse("<schema namespace='nms' name='recipient'><element name='recipient' label='Recipients'><element name='recipient' label='Recipients (inner)'/></element></schema>");
93
+ var schema = newSchema(xml);
94
+ var root = schema.root;
95
+ var inner = root.children["recipient"];
96
+ expect(inner.label).toBe("Recipients (inner)")
97
+ expect(inner.isRoot).toBe(false);
98
+ });
99
+ })
100
+
101
+ describe("Attributes", () => {
43
102
 
44
- it("Duplicate root nodes", () => {
45
- expect(() => {
103
+ it("Should find unique attribute", () => {
46
104
  var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
47
- <element name='recipient' label='Recipients'/>
48
- <element name='recipient' label='Recipients2'/>
105
+ <element name='recipient' label='Recipients'>
106
+ <attribute name='email' type='string' length='3'/>
107
+ </element>
49
108
  </schema>`);
50
- newSchema(xml);
51
- }).toThrow("there's a already a node");
52
- });
53
-
54
- });
109
+ var schema = newSchema(xml);
110
+ var root = schema.root;
111
+ expect(root.hasChild("@email")).toBe(true);
112
+ var email = root.children["@email"];
113
+ expect(email).not.toBeNull();
114
+ expect(email.name).toBe("@email");
115
+ expect(email.type).toBe("string");
116
+ expect(email.length).toBe(3);
117
+ });
118
+
119
+ it("Should not find inexistant attribute", () => {
120
+ var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
121
+ <element name='recipient' label='Recipients'>
122
+ <attribute name='email' type='string' length='3'/>
123
+ </element>
124
+ </schema>`);
125
+ var schema = newSchema(xml);
126
+ var root = schema.root;
127
+ expect(root.hasChild("email")).toBe(false);
128
+ expect(root.hasChild("@dummy")).toBe(false);
129
+ });
55
130
 
56
- describe("isRoot", () => {
57
- it("Shema node is not root node", () => {
58
- var xml = DomUtil.parse("<schema namespace='nms' name='recipient'><element name='recipient' label='Recipients'/></schema>");
59
- var schema = newSchema(xml);
60
- expect(schema.isRoot).toBeFalsy();
131
+ it("Should not find inexistant attribute (@-syntax)", () => {
132
+ var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
133
+ <element name='recipient' label='Recipients'>
134
+ <attribute name='email' type='string' length='3'/>
135
+ </element>
136
+ </schema>`);
137
+ var schema = newSchema(xml);
138
+ var root = schema.root;
139
+ expect(root.hasChild("@email")).toBe(true);
140
+ expect(root.hasChild("email")).toBe(false);
141
+ });
61
142
  });
62
143
 
63
- it("Should be root node", () => {
64
- var xml = DomUtil.parse("<schema namespace='nms' name='recipient'><element name='recipient' label='Recipients'/></schema>");
65
- var schema = newSchema(xml);
66
- var root = schema.root;
67
- expect(root.isRoot).toBe(true);
144
+ describe("Children", () => {
145
+ it("Should browse root children", () => {
146
+ var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
147
+ <element name='recipient' label='Recipients'>
148
+ <attribute name='email' type='string' length='3'/>
149
+ </element>
150
+ <element name='lib'/>
151
+ </schema>`);
152
+ var schema = newSchema(xml);
153
+ expect(schema.childrenCount).toBe(2);
154
+ expect(schema.children["recipient"]).not.toBeNull();
155
+ expect(schema.children["lib"]).not.toBeNull();
156
+ expect(schema.children["dummy"]).toBeFalsy();
157
+ });
68
158
  });
69
159
 
70
- it("Should not be root node", () => {
71
- var xml = DomUtil.parse("<schema namespace='nms' name='recipient'><element name='lib'/><element name='recipient' label='Recipients'/></schema>");
72
- var schema = newSchema(xml);
73
- var lib = schema.children["lib"];
74
- expect(lib.isRoot).toBeFalsy();
75
- });
160
+ describe("Find node", () => {
76
161
 
77
- it("Should not be root node (second level with same name", () => {
78
- var xml = DomUtil.parse("<schema namespace='nms' name='recipient'><element name='recipient' label='Recipients'><element name='recipient' label='Recipients (inner)'/></element></schema>");
79
- var schema = newSchema(xml);
80
- var root = schema.root;
81
- var inner = root.children["recipient"];
82
- expect(inner.label).toBe("Recipients (inner)")
83
- expect(inner.isRoot).toBe(false);
162
+ it("Should find nodes", () => {
163
+ var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
164
+ <element name='recipient' label='Recipients'>
165
+ <attribute name='email' label="Email" type='string' length='3'/>
166
+ <element name='country' label="Country">
167
+ <attribute name='isoA3' label="Country name" type='string' length='3'/>
168
+ <attribute name='country-id' label="Country id" type='string' length='3'/>
169
+ <element name="terminal" label="Terminal"/>
170
+ </element>
171
+ </element>
172
+ </schema>`);
173
+ var schema = newSchema(xml);
174
+ var root = schema.root;
175
+
176
+ // Relative path
177
+ expect(root.findNode("@email").label).toBe("Email");
178
+ expect(root.findNode("country").label).toBe("Country");
179
+ expect(root.findNode("country/@isoA3").label).toBe("Country name");
180
+ expect(root.findNode("country/@country-id").label).toBe("Country id");
181
+
182
+ // Not found (defaut behavior throws)
183
+ expect(() => { root.findNode("@dummy"); }).toThrow("Unknown attribute 'dummy'");
184
+ expect(() => { root.findNode("dummy"); }).toThrow("Unknown element 'dummy'");
185
+ expect(() => { root.findNode("dummy/@dummy"); }).toThrow("Unknown element 'dummy'");
186
+ expect(() => { root.findNode("country/@dummy"); }).toThrow("Unknown attribute 'dummy'");
187
+ expect(() => { root.findNode("country/dummy/@dummy"); }).toThrow("Unknown element 'dummy'");
188
+
189
+ // Not found (mustExists=false)
190
+ expect(root.findNode("@dummy", true, false)).toBeNull();
191
+ expect(root.findNode("dummy", true, false)).toBeNull();
192
+ expect(root.findNode("dummy/@dummy", true, false)).toBeNull();
193
+ expect(root.findNode("country/@dummy", true, false)).toBeNull();
194
+
195
+ // Starting from schema
196
+ expect(schema.findNode("recipient").label).toBe('Recipients');
197
+
198
+ // Absolute path
199
+ expect(root.findNode("/@email").label).toBe("Email");
200
+ const country = root.findNode("country");
201
+ expect(country.findNode("/@email").label).toBe("Email");
202
+ expect(country.findNode("/country").label).toBe("Country");
203
+
204
+ // Self and parent
205
+ expect(country.findNode("./@isoA3").label).toBe("Country name");
206
+ expect(country.findNode("../@email").label).toBe("Email");
207
+ expect(country.findNode(".././@email").label).toBe("Email");
208
+ expect(country.findNode("./../@email").label).toBe("Email");
209
+ expect(root.findNode("./country/..").label).toBe("Recipients");
210
+
211
+ // Special cases
212
+ expect(root.findNode("").label).toBe("Recipients");
213
+ expect(root.findNode(".").label).toBe("Recipients");
214
+
215
+ // Non strict
216
+ expect(root.findNode("country/@isoA3", false, false).label).toBe("Country name");
217
+ expect(root.findNode("country/isoA3", false, false).label).toBe("Country name");
218
+ expect(country.findNode("@isoA3", false, false).label).toBe("Country name");
219
+ expect(country.findNode("isoA3", false, false).label).toBe("Country name");
220
+ expect(country.findNode("@terminal", false, false).label).toBe("Terminal");
221
+ expect(country.findNode("terminal", false, false).label).toBe("Terminal");
222
+ expect(country.findNode("@notFound", false, false)).toBeNull();
223
+ expect(country.findNode("notFound", false, false)).toBeNull();
224
+
225
+ // strict
226
+ expect(root.findNode("country/@isoA3", true, false).label).toBe("Country name");
227
+ expect(root.findNode("country/isoA3", true, false)).toBeNull();
228
+ expect(country.findNode("@isoA3", true, false).label).toBe("Country name");
229
+ expect(country.findNode("isoA3", true, false)).toBeNull();
230
+ expect(country.findNode("@terminal", true, false)).toBeNull();
231
+ expect(country.findNode("terminal", true, false).label).toBe("Terminal");
232
+ expect(country.findNode("@notFound", true, false)).toBeNull();
233
+ expect(country.findNode("notFound", true, false)).toBeNull();
234
+ });
235
+
236
+
237
+ it("Empty or absolute path requires a schema and root node", () => {
238
+
239
+ var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
240
+ <element name='profile' label='Recipients'>
241
+ <attribute name='email' label="Email" type='string' length='3'/>
242
+ <element name='country' label="Country">
243
+ <attribute name='isoA3' label="Country name" type='string' length='3'/>
244
+ <attribute name='country-id' label="Country id" type='string' length='3'/>
245
+ </element>
246
+ </element>
247
+ </schema>`);
248
+ var schemaNoRoot = newSchema(xml);
249
+ var root = schemaNoRoot.root;
250
+ expect(root).toBeUndefined();
251
+ expect(() => { schemaNoRoot.findNode("") }).toThrow("does not have a root node");
252
+ expect(() => { schemaNoRoot.findNode("/") }).toThrow("does not have a root node");
253
+
254
+ var profile = schemaNoRoot.findNode("profile");
255
+ expect(profile).toBeTruthy();
256
+ expect(profile.findNode("country/@isoA3").label).toBe("Country name");
257
+ expect(() => { profile.findNode("/country/@isoA3") }).toThrow("does not have a root node");
258
+ expect(() => { profile.findNode("") }).toThrow("does not have a root node");
259
+ });
260
+
261
+ it("Should find node by xpath", () => {
262
+ var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
263
+ <element name='recipient' label='Recipients'>
264
+ <attribute name='email' label="Email" type='string' length='3'/>
265
+ <element name='country' label="Country">
266
+ <attribute name='isoA3' label="Country name" type='string' length='3'/>
267
+ <attribute name='country-id' label="Country id" type='string' length='3'/>
268
+ </element>
269
+ </element>
270
+ </schema>`);
271
+ var schema = newSchema(xml);
272
+ var root = schema.root;
273
+
274
+ expect(root.findNode(new XPath("@email")).label).toBe("Email");
275
+ });
84
276
  });
85
- })
86
277
 
87
- describe("Attributes", () => {
88
-
89
- it("Should find unique attribute", () => {
90
- var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
91
- <element name='recipient' label='Recipients'>
92
- <attribute name='email' type='string' length='3'/>
93
- </element>
94
- </schema>`);
95
- var schema = newSchema(xml);
96
- var root = schema.root;
97
- expect(root.hasChild("@email")).toBe(true);
98
- var email = root.children["@email"];
99
- expect(email).not.toBeNull();
100
- expect(email.name).toBe("@email");
101
- expect(email.type).toBe("string");
102
- expect(email.length).toBe(3);
103
- });
278
+ describe("Enumerations", () => {
279
+ it("Should list enumerations", () => {
280
+ var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
281
+ <enumeration name="gender" basetype="byte"/>
282
+ <enumeration name="status" basetype="byte"/>
283
+ <element name='recipient' label='Recipients'></element>
284
+ </schema>`);
285
+ var schema = newSchema(xml);
286
+ var enumerations = schema.enumerations;
287
+ expect(enumerations.gender.dummy).toBeFalsy();
288
+ expect(enumerations.gender.name).toBe("gender");
289
+ expect(enumerations.status.name).toBe("status");
290
+ });
291
+
292
+ it("Should set default label", () => {
293
+ const xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
294
+ <enumeration name="gender" basetype="byte"/>
295
+ <enumeration name="status" basetype="byte" label="Status code"/>
296
+ <element name='recipient' label='Recipients'></element>
297
+ </schema>`);
298
+ const schema = newSchema(xml);
299
+ const enumerations = schema.enumerations;
300
+ expect(enumerations.gender.label).toBe("Gender");
301
+ expect(enumerations.status.label).toBe("Status code");
302
+ });
303
+
304
+ it("Should test images", () => {
305
+ var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
306
+ <enumeration name="gender" basetype="byte">
307
+ <value name="male" value="0"/>
308
+ <value name="female" value="1"/>
309
+ </enumeration>
310
+ <enumeration name="status" basetype="byte">
311
+ <value name="prospect" label="Prospect" value="0" img="xtk:prospect"/>
312
+ <value name="customer" label="Client" value="1"/>
313
+ </enumeration>
314
+ <enumeration name="status2" basetype="byte">
315
+ <value name="prospect" label="Prospect" value="0" img=""/>
316
+ <value name="customer" label="Client" value="1" img=""/>
317
+ </enumeration>
318
+ <element name='recipient' label='Recipients'>
319
+ <attribute advanced="true" desc="Recipient sex" enum="nms:recipient:gender"
320
+ label="Gender" name="gender" sqlname="iGender" type="byte"/>
321
+ </element>
322
+ </schema>`);
323
+ var schema = newSchema(xml);
324
+ var enumerations = schema.enumerations;
325
+ // no img attribute
326
+ expect(enumerations.gender.name).toBe("gender");
327
+ expect(enumerations.gender.hasImage).toBe(false);
328
+ // at least one img attribute
329
+ expect(enumerations.status.name).toBe("status");
330
+ expect(enumerations.status.hasImage).toBe(true);
331
+ // at least one img attribute
332
+ expect(enumerations.status2.name).toBe("status2");
333
+ expect(enumerations.status2.hasImage).toBe(false);
334
+ expect(schema.root.children["@gender"].enum).toBe("nms:recipient:gender");
335
+ })
336
+
337
+ it("Should list enumeration values", () => {
338
+ var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
339
+ <enumeration name="gender" basetype="byte"/>
340
+ <enumeration name="status" basetype="byte">
341
+ <value name="prospect" label="Prospect" value="0"/>
342
+ <value name="customer" label="Client" value="1"/>
343
+ </enumeration>
344
+ <element name='recipient' label='Recipients'></element>
345
+ </schema>`);
346
+ var schema = newSchema(xml);
347
+ var enumerations = schema.enumerations;
348
+ expect(enumerations.status.values.prospect.label).toBe("Prospect");
349
+ expect(enumerations.status.values.customer.label).toBe("Client");
350
+ })
351
+
352
+ it("Should set default label", () => {
353
+ const xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
354
+ <enumeration name="gender" basetype="byte"/>
355
+ <enumeration name="status" basetype="byte">
356
+ <value name="prospect" value="0"/>
357
+ <value name="customer" label="Client" value="1"/>
358
+ </enumeration>
359
+ <element name='recipient' label='Recipients'></element>
360
+ </schema>`);
361
+ const schema = newSchema(xml);
362
+ const enumerations = schema.enumerations;
363
+ const status = enumerations.status.values;
364
+ expect(status.prospect.label).toBe("Prospect");
365
+ expect(status.customer.label).toBe("Client");
366
+ });
367
+
368
+ it("Byte enumerations", () => {
369
+ var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
370
+ <enumeration basetype="byte" name="instanceType">
371
+ <value label="One-off event" name="single" value="0"/>
372
+ <value label="Reference recurrence" name="master" value="1"/>
373
+ <value label="Instance of a recurrence" name="instance" value="2"/>
374
+ <value label="Exception to a recurrence" name="exception" value="3"/>
375
+ </enumeration>
376
+ <element name='recipient' label='Recipients'></element>
377
+ </schema>`);
378
+ var schema = newSchema(xml);
379
+ var enumerations = schema.enumerations;
380
+ expect(enumerations.instanceType.values.single.label).toBe("One-off event");
381
+ expect(enumerations.instanceType.values.single.value).toBe(0);
382
+ })
104
383
 
105
- it("Should not find inexistant attribute", () => {
106
- var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
107
- <element name='recipient' label='Recipients'>
108
- <attribute name='email' type='string' length='3'/>
109
- </element>
110
- </schema>`);
111
- var schema = newSchema(xml);
112
- var root = schema.root;
113
- expect(root.hasChild("email")).toBe(false);
114
- expect(root.hasChild("@dummy")).toBe(false);
384
+ it("Should support default values", () => {
385
+ var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
386
+ <enumeration basetype="byte" name="instanceType" default="1">
387
+ <value label="One-off event" name="single" value="0"/>
388
+ <value label="Reference recurrence" name="master" value="1"/>
389
+ <value label="Instance of a recurrence" name="instance" value="2"/>
390
+ <value label="Exception to a recurrence" name="exception" value="3"/>
391
+ </enumeration>
392
+ <element name='recipient' label='Recipients'></element>
393
+ </schema>`);
394
+ var schema = newSchema(xml);
395
+ var enumerations = schema.enumerations;
396
+ expect(enumerations.instanceType.default.value).toBe(1);
397
+ });
115
398
  });
116
399
 
117
- it("Should not find inexistant attribute (@-syntax)", () => {
118
- var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
119
- <element name='recipient' label='Recipients'>
120
- <attribute name='email' type='string' length='3'/>
121
- </element>
122
- </schema>`);
123
- var schema = newSchema(xml);
124
- var root = schema.root;
125
- expect(root.hasChild("@email")).toBe(true);
126
- expect(root.hasChild("email")).toBe(false);
400
+ describe("Keys", () => {
401
+ it("Should have key", () => {
402
+ var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
403
+ <element name='recipient' label='Recipients'>
404
+ <key name="test">
405
+ <keyfield xpath="@email"/>
406
+ </key>
407
+ <attribute name='email' type='string' length='3'/>
408
+ </element>
409
+ </schema>`);
410
+ var schema = newSchema(xml);
411
+ var root = schema.root;
412
+ expect(root.keys.test).toBeTruthy();
413
+ expect(root.keys.test.fields["email"]).toBeFalsy();
414
+ expect(root.keys.test.fields["@email"]).toBeTruthy();
415
+ })
416
+
417
+ it("Should fail if keyfield does not have xpath", () => {
418
+ var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
419
+ <element name='recipient' label='Recipients'>
420
+ <key name="test">
421
+ <keyfield/>
422
+ </key>
423
+ <attribute name='email' type='string' length='3'/>
424
+ </element>
425
+ </schema>`);
426
+ expect(() => { newSchema(xml) }).toThrow("keyfield does not have an xpath attribute");
427
+ });
127
428
  });
128
- });
129
429
 
130
- describe("Children", () => {
131
- it("Should browse root children", () => {
132
- var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
430
+ describe("Link", () => {
431
+ it("Should have a link element", () => {
432
+ var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
433
+ <element name='recipient' label='Recipients'>
434
+ <element integrity="neutral" label="Info on the email" name="emailInfo"
435
+ target="nms:address" type="link" unbound="true">
436
+ <join xpath-dst="@address" xpath-src="@email"/>
437
+ <join xpath-dst="@dst" xpath-src="@source"/>
438
+ </element>
439
+ </element>
440
+ </schema>`);
441
+ var schema = newSchema(xml);
442
+ var link = schema.root.children["emailInfo"];
443
+ expect(link.target).toBe("nms:address");
444
+ expect(link.integrity).toBe("neutral");
445
+ expect(link.isUnbound()).toBe(true);
446
+ expect(link.joins.length).toBe(2);
447
+ expect(link.joins[0].dst).toBe("@address");
448
+ expect(link.joins[0].src).toBe("@email");
449
+
450
+ xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
133
451
  <element name='recipient' label='Recipients'>
134
- <attribute name='email' type='string' length='3'/>
452
+ <element integrity="neutral" label="Info on the email" name="emailInfo"
453
+ target="nms:address" type="link">
454
+ <join xpath-dst="@address" xpath-src="@email"/>
455
+ <join xpath-dst="@dst" xpath-src="@source"/>
456
+ </element>
135
457
  </element>
136
- <element name='lib'/>
137
458
  </schema>`);
138
- var schema = newSchema(xml);
139
- expect(schema.childrenCount).toBe(2);
140
- expect(schema.children["recipient"]).not.toBeNull();
141
- expect(schema.children["lib"]).not.toBeNull();
142
- expect(schema.children["dummy"]).toBeFalsy();
459
+ schema = newSchema(xml);
460
+ link = schema.root.children["emailInfo"];
461
+ expect(link.isUnbound()).toBe(false);
462
+ });
143
463
  });
144
- });
145
464
 
146
- describe("Find node", () => {
465
+ describe("getnodepath", () => {
147
466
 
148
- it("Should find nodes", () => {
149
467
  var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
150
- <element name='recipient' label='Recipients'>
151
- <attribute name='email' label="Email" type='string' length='3'/>
152
- <element name='country' label="Country">
153
- <attribute name='isoA3' label="Country name" type='string' length='3'/>
154
- <attribute name='country-id' label="Country id" type='string' length='3'/>
155
- <element name="terminal" label="Terminal"/>
156
- </element>
157
- </element>
158
- </schema>`);
468
+ <element name='recipient' label='Recipients'>
469
+ <attribute name='email' type='string' length='3'/>
470
+ <element name="country">
471
+ <attribute name='name'/>
472
+ </element>
473
+ </element>
474
+ </schema>`);
159
475
  var schema = newSchema(xml);
160
476
  var root = schema.root;
161
-
162
- // Relative path
163
- expect(root.findNode("@email").label).toBe("Email");
164
- expect(root.findNode("country").label).toBe("Country");
165
- expect(root.findNode("country/@isoA3").label).toBe("Country name");
166
- expect(root.findNode("country/@country-id").label).toBe("Country id");
167
-
168
- // Not found (defaut behavior throws)
169
- expect(() => { root.findNode("@dummy"); }).toThrow("Unknown attribute 'dummy'");
170
- expect(() => { root.findNode("dummy"); }).toThrow("Unknown element 'dummy'");
171
- expect(() => { root.findNode("dummy/@dummy"); }).toThrow("Unknown element 'dummy'");
172
- expect(() => { root.findNode("country/@dummy"); }).toThrow("Unknown attribute 'dummy'");
173
- expect(() => { root.findNode("country/dummy/@dummy"); }).toThrow("Unknown element 'dummy'");
174
-
175
- // Not found (mustExists=false)
176
- expect(root.findNode("@dummy", true, false)).toBeNull();
177
- expect(root.findNode("dummy", true, false)).toBeNull();
178
- expect(root.findNode("dummy/@dummy", true, false)).toBeNull();
179
- expect(root.findNode("country/@dummy", true, false)).toBeNull();
180
-
181
- // Starting from schema
182
- expect(schema.findNode("recipient").label).toBe('Recipients');
183
-
184
- // Absolute path
185
- expect(root.findNode("/@email").label).toBe("Email");
186
- const country = root.findNode("country");
187
- expect(country.findNode("/@email").label).toBe("Email");
188
- expect(country.findNode("/country").label).toBe("Country");
189
-
190
- // Self and parent
191
- expect(country.findNode("./@isoA3").label).toBe("Country name");
192
- expect(country.findNode("../@email").label).toBe("Email");
193
- expect(country.findNode(".././@email").label).toBe("Email");
194
- expect(country.findNode("./../@email").label).toBe("Email");
195
- expect(root.findNode("./country/..").label).toBe("Recipients");
196
-
197
- // Special cases
198
- expect(root.findNode("").label).toBe("Recipients");
199
- expect(root.findNode(".").label).toBe("Recipients");
200
-
201
- // Non strict
202
- expect(root.findNode("country/@isoA3", false, false).label).toBe("Country name");
203
- expect(root.findNode("country/isoA3", false, false).label).toBe("Country name");
204
- expect(country.findNode("@isoA3", false, false).label).toBe("Country name");
205
- expect(country.findNode("isoA3", false, false).label).toBe("Country name");
206
- expect(country.findNode("@terminal", false, false).label).toBe("Terminal");
207
- expect(country.findNode("terminal", false, false).label).toBe("Terminal");
208
- expect(country.findNode("@notFound", false, false)).toBeNull();
209
- expect(country.findNode("notFound", false, false)).toBeNull();
210
-
211
- // strict
212
- expect(root.findNode("country/@isoA3", true, false).label).toBe("Country name");
213
- expect(root.findNode("country/isoA3", true, false)).toBeNull();
214
- expect(country.findNode("@isoA3", true, false).label).toBe("Country name");
215
- expect(country.findNode("isoA3", true, false)).toBeNull();
216
- expect(country.findNode("@terminal", true, false)).toBeNull();
217
- expect(country.findNode("terminal", true, false).label).toBe("Terminal");
218
- expect(country.findNode("@notFound", true, false)).toBeNull();
219
- expect(country.findNode("notFound", true, false)).toBeNull();
477
+ var email = root.findNode("@email");
478
+ var country = root.findNode("country");
479
+ var name = country.findNode("@name");
480
+
481
+ it("Should support nodePath property", () => {
482
+ expect(schema.nodePath).toBe("/recipient");
483
+ expect(root.nodePath).toBe("/");
484
+ expect(email.nodePath).toBe("/@email");
485
+ expect(country.nodePath).toBe("/country");
486
+ expect(name.nodePath).toBe("/country/@name");
487
+ });
488
+
489
+ it("_getNodePath", () => {
490
+ // No parameters => absolute
491
+ expect(schema._getNodePath()._path).toBe("/recipient");
492
+ expect(root._getNodePath()._path).toBe("/");
493
+ expect(email._getNodePath()._path).toBe("/@email");
494
+ expect(country._getNodePath()._path).toBe("/country");
495
+ expect(name._getNodePath()._path).toBe("/country/@name");
496
+
497
+ // Absolute
498
+ expect(schema._getNodePath(true)._path).toBe("/recipient");
499
+ expect(root._getNodePath(true)._path).toBe("/");
500
+ expect(email._getNodePath(true)._path).toBe("/@email");
501
+ expect(country._getNodePath(true)._path).toBe("/country");
502
+ expect(name._getNodePath(true)._path).toBe("/country/@name");
503
+
504
+ // Relative
505
+ expect(schema._getNodePath(false)._path).toBe("recipient");
506
+ expect(root._getNodePath(false)._path).toBe("");
507
+ expect(email._getNodePath(false)._path).toBe("@email");
508
+ expect(country._getNodePath(false)._path).toBe("country");
509
+ expect(name._getNodePath(false)._path).toBe("country/@name");
510
+ });
220
511
  });
221
512
 
222
513
 
223
- it("Empty or absolute path requires a schema and root node", () => {
224
-
225
- var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
226
- <element name='profile' label='Recipients'>
227
- <attribute name='email' label="Email" type='string' length='3'/>
228
- <element name='country' label="Country">
229
- <attribute name='isoA3' label="Country name" type='string' length='3'/>
230
- <attribute name='country-id' label="Country id" type='string' length='3'/>
231
- </element>
514
+ describe("toString", () => {
515
+ var xml = DomUtil.parse(`<schema namespace='nms' name='recipient' label="Recipients" labelSingular="Recipient">
516
+ <element name='recipient'>
517
+ <attribute name='email' type='string' length='3'/>
518
+ <element name="country">
519
+ <attribute name='name'/>
232
520
  </element>
233
- </schema>`);
234
- var schemaNoRoot = newSchema(xml);
235
- var root = schemaNoRoot.root;
236
- expect(root).toBeUndefined();
237
- expect(() => { schemaNoRoot.findNode("") }).toThrow("does not have a root node");
238
- expect(() => { schemaNoRoot.findNode("/") }).toThrow("does not have a root node");
239
-
240
- var profile = schemaNoRoot.findNode("profile");
241
- expect(profile).toBeTruthy();
242
- expect(profile.findNode("country/@isoA3").label).toBe("Country name");
243
- expect(() => { profile.findNode("/country/@isoA3") }).toThrow("does not have a root node");
244
- expect(() => { profile.findNode("") }).toThrow("does not have a root node");
245
- });
521
+ </element>
522
+ </schema>`);
523
+
524
+ it("Should stringify schema or schema node", async () => {
525
+ var schema = newSchema(xml);
526
+ var root = schema.root;
527
+ var email = root.findNode("@email");
528
+ var country = root.findNode("country");
529
+ var name = country.findNode("@name");
530
+
531
+ expect(schema.toString()).toBe(`Recipients (recipient)
532
+ - Recipient (recipient)
533
+ - Email (@email)
534
+ - Country (country)
535
+ - Name (@name)
536
+ `);
537
+ expect(root.toString()).toBe(`Recipient (recipient)
538
+ Email (@email)
539
+ Country (country)
540
+ Name (@name)
541
+ `);
542
+ expect(email.toString()).toBe("Email (@email)\n");
543
+ expect(country.toString()).toBe("Country (country)\n Name (@name)\n");
544
+ expect(name.toString()).toBe("Name (@name)\n");
545
+ });
546
+ })
246
547
 
247
- it("Should find node by xpath", () => {
248
- var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
249
- <element name='recipient' label='Recipients'>
250
- <attribute name='email' label="Email" type='string' length='3'/>
251
- <element name='country' label="Country">
252
- <attribute name='isoA3' label="Country name" type='string' length='3'/>
253
- <attribute name='country-id' label="Country id" type='string' length='3'/>
254
- </element>
255
- </element>
256
- </schema>`);
257
- var schema = newSchema(xml);
258
- var root = schema.root;
548
+ describe("Node properties", () => {
549
+ it("Should have isSQL" , async () => {
550
+ const xml = DomUtil.parse(`<schema namespace='nms' name='recipient' label="Recipients" labelSingular="Recipient">
551
+ <element name='recipient' sqltable="NmsRecipient">
552
+ <attribute name='email' type='string' sqlname="semail"/>
553
+ <attribute name='test' type='string' xml="true"/>
554
+ </element>
555
+ </schema>`);
556
+ const schema = newSchema(xml);
557
+ expect(schema.root.isSQL).toBe(true);
558
+ let node = schema.root.findNode("@email");
559
+ expect(node.isSQL).toBe(true);
560
+ node = schema.root.findNode("@test");
561
+ expect(node.isSQL).toBe(false);
562
+ });
563
+
564
+ it("Should be memo and memo data" , async () => {
565
+ const xml = DomUtil.parse(`<schema namespace='nms' name='recipient' label="Recipients" labelSingular="Recipient">
566
+ <element name='recipient' sqltable="NmsRecipient">
567
+ <attribute name='email' type='string' sqlname="semail"/>
568
+ <attribute name='test' type='string' xml="true"/>
569
+ <attribute name='memo' type='memo'/>
570
+ <element name='data' type='memo' xml="true"/>
571
+ </element>
572
+ </schema>`);
573
+ const schema = newSchema(xml);
574
+ let node = schema.root.findNode("@email");
575
+ expect(node.isMemo).toBe(false);
576
+ expect(node.isMemoData).toBe(false);
577
+ node = schema.root.findNode("@test");
578
+ expect(node.isMemo).toBe(false);
579
+ expect(node.isMemoData).toBe(false);
580
+ node = schema.root.findNode("@memo");
581
+ expect(node.isMemo).toBe(true);
582
+ expect(node.isMemoData).toBe(false);
583
+ node = schema.root.findNode("data");
584
+ expect(node.isMemo).toBe(true);
585
+ expect(node.isMemoData).toBe(true);
586
+ });
587
+
588
+ it("Should be test isNotNull" , async () => {
589
+ const xml = DomUtil.parse(`<schema namespace='nms' name='recipient' label="Recipients" labelSingular="Recipient">
590
+ <element name='recipient'>
591
+ <attribute name='id' type='long'/>
592
+ <attribute name='b' type='byte'/>
593
+ <attribute name='s' type='short'/>
594
+ <attribute name='f' type='float'/>
595
+ <attribute name='d' type='double'/>
596
+ <attribute name='i' type='int64'/>
597
+ <attribute name='m' type='money'/>
598
+ <attribute name='p' type='percent'/>
599
+ <attribute name='t' type='time'/>
600
+ <attribute name='b0' type='boolean'/>
601
+ <attribute name='email' type='string'/>
602
+
603
+ <attribute name='snn' type='string'/>
604
+ <attribute name='snnt' type='string' notNull='true'/>
605
+ <attribute name='snnf' type='string' notNull='false'/>
606
+
607
+ <attribute name='lnn' type='long'/>
608
+ <attribute name='lnnt' type='long' notNull='true'/>
609
+ <attribute name='lnnf' type='long' notNull='false'/>
610
+ </element>
611
+ </schema>`);
612
+ const schema = newSchema(xml);
613
+ let node = schema.root.findNode("@email"); expect(node.isNotNull).toBe(false);
614
+ node = schema.root.findNode("@id"); expect(node.isNotNull).toBe(true);
615
+ node = schema.root.findNode("@b"); expect(node.isNotNull).toBe(true);
616
+ node = schema.root.findNode("@s"); expect(node.isNotNull).toBe(true);
617
+ node = schema.root.findNode("@f"); expect(node.isNotNull).toBe(true);
618
+ node = schema.root.findNode("@d"); expect(node.isNotNull).toBe(true);
619
+ node = schema.root.findNode("@i"); expect(node.isNotNull).toBe(true);
620
+ node = schema.root.findNode("@m"); expect(node.isNotNull).toBe(true);
621
+ node = schema.root.findNode("@p"); expect(node.isNotNull).toBe(true);
622
+ node = schema.root.findNode("@t"); expect(node.isNotNull).toBe(true);
623
+ node = schema.root.findNode("@b0"); expect(node.isNotNull).toBe(true);
624
+
625
+ node = schema.root.findNode("@snn"); expect(node.isNotNull).toBe(false);
626
+ node = schema.root.findNode("@snnt"); expect(node.isNotNull).toBe(true);
627
+ node = schema.root.findNode("@snnf"); expect(node.isNotNull).toBe(false);
628
+
629
+ node = schema.root.findNode("@lnn"); expect(node.isNotNull).toBe(true);
630
+ node = schema.root.findNode("@lnnt"); expect(node.isNotNull).toBe(true);
631
+ node = schema.root.findNode("@lnnf"); expect(node.isNotNull).toBe(false);
632
+ });
633
+
634
+ it("Should test user description" , async () => {
635
+ let xml = DomUtil.parse(`<schema namespace='nms' name='recipient' label="Recipients" labelSingular="Recipient">
636
+ <element name='recipient'>
637
+ </element>
638
+ </schema>`);
639
+ let schema = newSchema(xml);
640
+ expect(schema.userDescription).toBe("Recipients (recipient)");
259
641
 
260
- expect(root.findNode(new XPath("@email")).label).toBe("Email");
642
+ xml = DomUtil.parse(`<schema namespace='nms' name='recipient' label="recipient">
643
+ <element name='recipient'>
644
+ </element>
645
+ </schema>`);
646
+ schema = newSchema(xml);
647
+ expect(schema.userDescription).toBe("recipient");
648
+ });
261
649
  });
262
-
263
650
  });
264
651
 
265
- describe("Enumerations", () => {
266
- it("Should list enumerations", () => {
267
- var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
268
- <enumeration name="gender" basetype="byte"/>
269
- <enumeration name="status" basetype="byte"/>
270
- <element name='recipient' label='Recipients'></element>
271
- </schema>`);
272
- var schema = newSchema(xml);
273
- var enumerations = schema.enumerations;
274
- expect(enumerations.gender.dummy).toBeFalsy();
275
- expect(enumerations.gender.name).toBe("gender");
276
- expect(enumerations.status.name).toBe("status");
652
+ describe("CurrentLogin", () => {
653
+
654
+ it("Should create with SimpleJson", () => {
655
+ var op = newCurrentLogin({
656
+ login: "alex",
657
+ loginId: "12",
658
+ loginCS: "Alex",
659
+ timezone: "Europe/Paris",
660
+ "login-right": [
661
+ ]
662
+ })
663
+ expect(op.login).toBe("alex");
664
+ expect(op.id).toBe(12);
665
+ expect(op.computeString).toBe("Alex");
666
+ expect(op.timezone).toBe("Europe/Paris");
667
+ expect(op.rights).toEqual([]);
277
668
  })
278
669
 
279
- it("Should test images", () => {
280
- var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
281
- <enumeration name="gender" basetype="byte">
282
- <value name="male" value="0"/>
283
- <value name="female" value="1"/>
284
- </enumeration>
285
- <enumeration name="status" basetype="byte">
286
- <value name="prospect" label="Prospect" value="0" img="xtk:prospect"/>
287
- <value name="customer" label="Client" value="1"/>
288
- </enumeration>
289
- <enumeration name="status2" basetype="byte">
290
- <value name="prospect" label="Prospect" value="0" img=""/>
291
- <value name="customer" label="Client" value="1" img=""/>
292
- </enumeration>
293
- <element name='recipient' label='Recipients'>
294
- <attribute advanced="true" desc="Recipient sex" enum="nms:recipient:gender"
295
- label="Gender" name="gender" sqlname="iGender" type="byte"/>
296
- </element>
297
- </schema>`);
298
- var schema = newSchema(xml);
299
- var enumerations = schema.enumerations;
300
- // no img attribute
301
- expect(enumerations.gender.name).toBe("gender");
302
- expect(enumerations.gender.hasImage).toBe(false);
303
- // at least one img attribute
304
- expect(enumerations.status.name).toBe("status");
305
- expect(enumerations.status.hasImage).toBe(true);
306
- // at least one img attribute
307
- expect(enumerations.status2.name).toBe("status2");
308
- expect(enumerations.status2.hasImage).toBe(false);
309
- expect(schema.root.children["@gender"].enum).toBe("nms:recipient:gender");
670
+ it("Should support missing 'login-right' node", () => {
671
+ var op = newCurrentLogin({ login: "alex", loginId: "12", loginCS: "Alex" })
672
+ expect(op.rights).toEqual([]);
673
+ expect(op.hasRight("admin")).toBe(false);
310
674
  })
311
675
 
312
- it("Should list enumeration values", () => {
313
- var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
314
- <enumeration name="gender" basetype="byte"/>
315
- <enumeration name="status" basetype="byte">
316
- <value name="prospect" label="Prospect" value="0"/>
317
- <value name="customer" label="Client" value="1"/>
318
- </enumeration>
319
- <element name='recipient' label='Recipients'></element>
320
- </schema>`);
321
- var schema = newSchema(xml);
322
- var enumerations = schema.enumerations;
323
- expect(enumerations.status.values.prospect.label).toBe("Prospect");
324
- expect(enumerations.status.values.customer.label).toBe("Client");
676
+ it("Should support 'login-right' as an object", () => {
677
+ var op = newCurrentLogin({ login: "alex", loginId: "12", loginCS: "Alex", "login-right": { "right": "admin" } });
678
+ expect(op.rights).toEqual([ "admin" ]);
679
+ expect(op.hasRight("admin")).toBe(true);
325
680
  })
326
681
 
327
-
328
- it("Byte enumerations", () => {
329
- var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
330
- <enumeration basetype="byte" name="instanceType">
331
- <value label="One-off event" name="single" value="0"/>
332
- <value label="Reference recurrence" name="master" value="1"/>
333
- <value label="Instance of a recurrence" name="instance" value="2"/>
334
- <value label="Exception to a recurrence" name="exception" value="3"/>
335
- </enumeration>
336
- <element name='recipient' label='Recipients'></element>
337
- </schema>`);
338
- var schema = newSchema(xml);
339
- var enumerations = schema.enumerations;
340
- expect(enumerations.instanceType.values.single.label).toBe("One-off event");
341
- expect(enumerations.instanceType.values.single.value).toBe(0);
682
+ it("Should support 'login-right' as an object", () => {
683
+ var op = newCurrentLogin({ login: "alex", loginId: "12", loginCS: "Alex", "login-right": [ { "right": "admin" }, { "right": "delivery" } ] });
684
+ expect(op.rights).toEqual([ "admin", "delivery" ]);
685
+ expect(op.hasRight("admin")).toBe(true);
686
+ expect(op.hasRight("delivery")).toBe(true);
342
687
  })
343
688
 
344
- it("Should support default values", () => {
345
- var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
346
- <enumeration basetype="byte" name="instanceType" default="1">
347
- <value label="One-off event" name="single" value="0"/>
348
- <value label="Reference recurrence" name="master" value="1"/>
349
- <value label="Instance of a recurrence" name="instance" value="2"/>
350
- <value label="Exception to a recurrence" name="exception" value="3"/>
351
- </enumeration>
352
- <element name='recipient' label='Recipients'></element>
353
- </schema>`);
354
- var schema = newSchema(xml);
355
- var enumerations = schema.enumerations;
356
- expect(enumerations.instanceType.default.value).toBe(1);
357
- });
358
- })
359
689
 
360
- describe("Keys", () => {
361
- it("Should have key", () => {
362
- var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
363
- <element name='recipient' label='Recipients'>
364
- <key name="test">
365
- <keyfield xpath="@email"/>
366
- </key>
367
- <attribute name='email' type='string' length='3'/>
368
- </element>
369
- </schema>`);
370
- var schema = newSchema(xml);
371
- var root = schema.root;
372
- expect(root.keys.test).toBeTruthy();
373
- expect(root.keys.test.fields["email"]).toBeFalsy();
374
- expect(root.keys.test.fields["@email"]).toBeTruthy();
375
- })
376
-
377
- it("Should fail if keyfield does not have xpath", () => {
378
- var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
379
- <element name='recipient' label='Recipients'>
380
- <key name="test">
381
- <keyfield/>
382
- </key>
383
- <attribute name='email' type='string' length='3'/>
384
- </element>
385
- </schema>`);
386
- expect(() => { newSchema(xml) }).toThrow("keyfield does not have an xpath attribute");
387
- })
388
- });
389
-
390
- describe("Link", () => {
391
- it("Should have a link element", () => {
392
- var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
393
- <element name='recipient' label='Recipients'>
394
- <element integrity="neutral" label="Info on the email" name="emailInfo"
395
- target="nms:address" type="link" unbound="true">
396
- <join xpath-dst="@address" xpath-src="@email"/>
397
- <join xpath-dst="@dst" xpath-src="@source"/>
398
- </element>
399
- </element>
400
- </schema>`);
401
- var schema = newSchema(xml);
402
- var link = schema.root.children["emailInfo"];
403
- expect(link.target).toBe("nms:address");
404
- expect(link.integrity).toBe("neutral");
405
- expect(link.isUnbound()).toBe(true);
406
- expect(link.joins.length).toBe(2);
407
- expect(link.joins[0].dst).toBe("@address");
408
- expect(link.joins[0].src).toBe("@email");
409
-
410
- xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
411
- <element name='recipient' label='Recipients'>
412
- <element integrity="neutral" label="Info on the email" name="emailInfo"
413
- target="nms:address" type="link">
414
- <join xpath-dst="@address" xpath-src="@email"/>
415
- <join xpath-dst="@dst" xpath-src="@source"/>
416
- </element>
417
- </element>
418
- </schema>`);
419
- schema = newSchema(xml);
420
- link = schema.root.children["emailInfo"];
421
- expect(link.isUnbound()).toBe(false);
422
- })
423
- })
424
-
425
- describe("getnodepath", () => {
426
- var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
427
- <element name='recipient' label='Recipients'>
428
- <attribute name='email' type='string' length='3'/>
429
- <element name="country">
430
- <attribute name='name'/>
431
- </element>
432
- </element>
433
- </schema>`);
434
- var schema = newSchema(xml);
435
- var root = schema.root;
436
- var email = root.findNode("@email");
437
- var country = root.findNode("country");
438
- var name = country.findNode("@name");
439
-
440
- it("Should support nodePath property", () => {
441
- expect(schema.nodePath).toBe("/recipient");
442
- expect(root.nodePath).toBe("/");
443
- expect(email.nodePath).toBe("/@email");
444
- expect(country.nodePath).toBe("/country");
445
- expect(name.nodePath).toBe("/country/@name");
446
- });
690
+ describe("application.getSchema", () => {
691
+ it("Should return a XtkSchema object", async () => {
692
+ const client = await Mock.makeClient();
693
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
694
+ await client.NLWS.xtkSession.logon();
447
695
 
448
- it("_getNodePath", () => {
449
- // No parameters => absolute
450
- expect(schema._getNodePath()._path).toBe("/recipient");
451
- expect(root._getNodePath()._path).toBe("/");
452
- expect(email._getNodePath()._path).toBe("/@email");
453
- expect(country._getNodePath()._path).toBe("/country");
454
- expect(name._getNodePath()._path).toBe("/country/@name");
455
-
456
- // Absolute
457
- expect(schema._getNodePath(true)._path).toBe("/recipient");
458
- expect(root._getNodePath(true)._path).toBe("/");
459
- expect(email._getNodePath(true)._path).toBe("/@email");
460
- expect(country._getNodePath(true)._path).toBe("/country");
461
- expect(name._getNodePath(true)._path).toBe("/country/@name");
462
-
463
- // Relative
464
- expect(schema._getNodePath(false)._path).toBe("recipient");
465
- expect(root._getNodePath(false)._path).toBe("");
466
- expect(email._getNodePath(false)._path).toBe("@email");
467
- expect(country._getNodePath(false)._path).toBe("country");
468
- expect(name._getNodePath(false)._path).toBe("country/@name");
469
- });
470
- });
696
+ client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE);
697
+ const schema = await client.application.getSchema("xtk:session");
698
+ expect(schema.namespace).toBe("xtk");
699
+ expect(schema.name).toBe("session");
700
+ });
471
701
 
702
+ it("Should handle non-existing schemas", async () => {
703
+ const client = await Mock.makeClient();
704
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
705
+ await client.NLWS.xtkSession.logon();
472
706
 
473
- describe("toString", () => {
474
- var xml = DomUtil.parse(`<schema namespace='nms' name='recipient'>
475
- <element name='recipient' label='Recipients'>
476
- <attribute name='email' type='string' length='3'/>
477
- <element name="country">
478
- <attribute name='name'/>
479
- </element>
480
- </element>
481
- </schema>`);
482
- var schema = newSchema(xml);
483
- var root = schema.root;
484
- var email = root.findNode("@email");
485
- var country = root.findNode("country");
486
- var name = country.findNode("@name");
487
-
488
- it("Should stringify schema or schema node", () => {
489
- expect(schema.toString()).toBe(`recipient
490
- - Recipients (recipient)
491
- - @email
492
- - country
493
- - @name
494
- `);
495
- expect(root.toString()).toBe(`Recipients (recipient)
496
- @email
497
- country
498
- @name
499
- `);
500
- expect(email.toString()).toBe("@email\n");
501
- expect(country.toString()).toBe("country\n @name\n");
502
- expect(name.toString()).toBe("@name\n");
707
+ client._transport.mockReturnValueOnce(Mock.GET_MISSING_SCHEMA_RESPONSE);
708
+ const schema = await client.application.getSchema("xtk:dummy")
709
+ expect(schema).toBeNull();
710
+ })
503
711
  });
504
- })
505
-
506
- });
507
-
508
712
 
509
- describe("CurrentLogin", () => {
713
+ describe("application.hasPackage", () => {
714
+ it("Should verify if a package is installed", async () => {
715
+ const client = await Mock.makeClient();
716
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
717
+ await client.NLWS.xtkSession.logon();
718
+ expect(client.application.hasPackage("nms:core")).toBe(true);
719
+ expect(client.application.hasPackage("nms:campaign")).toBe(true);
720
+ expect(client.application.hasPackage("nms:dummy")).toBe(false);
721
+ expect(client.application.hasPackage("")).toBe(false);
722
+ expect(client.application.hasPackage(null)).toBe(false);
723
+ expect(client.application.hasPackage(undefined)).toBe(false);
724
+ })
725
+ })
726
+ });
510
727
 
511
- it("Should create with SimpleJson", () => {
512
- var op = newCurrentLogin({
513
- login: "alex",
514
- loginId: "12",
515
- loginCS: "Alex",
516
- timezone: "Europe/Paris",
517
- "login-right": [
518
- ]
728
+ describe("Application for anonymous users", () => {
729
+ it("Application objet should exist but will not have user/session info", async () => {
730
+ const client = await Mock.makeAnonymousClient();
731
+ expect(client.application).toBeNull();
732
+ await client.logon();
733
+ const application = client.application;
734
+ expect(application).not.toBeNull();
735
+ expect(application.buildNumber).toBeUndefined();
736
+ expect(application.instanceName).toBeUndefined();
737
+ expect(application.operator).toBeUndefined();
738
+ expect(application.package).toBeUndefined();
519
739
  })
520
- expect(op.login).toBe("alex");
521
- expect(op.id).toBe(12);
522
- expect(op.computeString).toBe("Alex");
523
- expect(op.timezone).toBe("Europe/Paris");
524
- expect(op.rights).toEqual([]);
525
- })
526
-
527
- it("Should support missing 'login-right' node", () => {
528
- var op = newCurrentLogin({ login: "alex", loginId: "12", loginCS: "Alex" })
529
- expect(op.rights).toEqual([]);
530
- expect(op.hasRight("admin")).toBe(false);
531
- })
532
-
533
- it("Should support 'login-right' as an object", () => {
534
- var op = newCurrentLogin({ login: "alex", loginId: "12", loginCS: "Alex", "login-right": { "right": "admin" } });
535
- expect(op.rights).toEqual([ "admin" ]);
536
- expect(op.hasRight("admin")).toBe(true);
537
- })
538
-
539
- it("Should support 'login-right' as an object", () => {
540
- var op = newCurrentLogin({ login: "alex", loginId: "12", loginCS: "Alex", "login-right": [ { "right": "admin" }, { "right": "delivery" } ] });
541
- expect(op.rights).toEqual([ "admin", "delivery" ]);
542
- expect(op.hasRight("admin")).toBe(true);
543
- expect(op.hasRight("delivery")).toBe(true);
544
- })
545
-
546
-
547
- describe("application.getSchema", () => {
548
- it("Should return a XtkSchema object", async () => {
740
+ });
741
+
742
+ describe("Schema Cache", () => {
743
+ it("Should search in empty cache", async () => {
549
744
  const client = await Mock.makeClient();
550
745
  client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
551
746
  await client.NLWS.xtkSession.logon();
747
+ const cache = new SchemaCache(client);
552
748
 
553
- client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE);
554
- const schema = await client.application.getSchema("xtk:session");
555
- expect(schema.namespace).toBe("xtk");
556
- expect(schema.name).toBe("session");
749
+ client._transport.mockReturnValueOnce(Mock.GET_XTK_QUERY_SCHEMA_RESPONSE);
750
+ const schema = await cache.getSchema("xtk:queryDef");
751
+ expect(schema.id).toBe("xtk:queryDef");
752
+
753
+ // Second call should not perform any API call
754
+ const schema2 = await cache.getSchema("xtk:queryDef");
755
+ expect(schema2.id).toBe("xtk:queryDef");
557
756
  });
558
757
 
559
- it("Should handle non-existing schemas", async () => {
758
+ it("Should support not found schemas", async () => {
560
759
  const client = await Mock.makeClient();
561
760
  client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
562
761
  await client.NLWS.xtkSession.logon();
762
+ const cache = new SchemaCache(client);
563
763
 
564
764
  client._transport.mockReturnValueOnce(Mock.GET_MISSING_SCHEMA_RESPONSE);
565
- const schema = await client.application.getSchema("xtk:dummy")
566
- expect(schema).toBeNull();
567
- })
568
- });
569
-
570
- describe("application.hasPackage", () => {
571
- it("Should verify if a package is installed", async () => {
572
- const client = await Mock.makeClient();
573
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
574
- await client.NLWS.xtkSession.logon();
575
- expect(client.application.hasPackage("nms:core")).toBe(true);
576
- expect(client.application.hasPackage("nms:campaign")).toBe(true);
577
- expect(client.application.hasPackage("nms:dummy")).toBe(false);
578
- expect(client.application.hasPackage("")).toBe(false);
579
- expect(client.application.hasPackage(null)).toBe(false);
580
- expect(client.application.hasPackage(undefined)).toBe(false);
581
- })
582
- })
583
-
765
+ const schema = await cache.getSchema("xtk:queryDef");
766
+ expect(schema).toBeFalsy();
584
767
 
585
- describe("XPath", () => {
586
-
587
- it("Should create XPath", () => {
588
- expect(new XPath("").asString()).toBe("");
589
- expect(new XPath(" ").asString()).toBe("");
590
- expect(new XPath(null).asString()).toBe("");
591
- expect(new XPath(undefined).asString()).toBe("");
592
- expect(new XPath("@name").asString()).toBe("@name");
593
- expect(new XPath("country/@name").asString()).toBe("country/@name");
594
- expect(new XPath("..").asString()).toBe("..");
595
- expect(new XPath(".").asString()).toBe(".");
596
- })
597
-
598
- it("toString", () => {
599
- expect(new XPath("").toString()).toBe("");
600
- expect(new XPath(" ").toString()).toBe("");
601
- expect(new XPath(null).toString()).toBe("");
602
- expect(new XPath(undefined).toString()).toBe("");
603
- expect(new XPath("@name").toString()).toBe("@name");
604
- expect(new XPath("country/@name").toString()).toBe("country/@name");
605
- expect(new XPath("..").toString()).toBe("..");
606
- expect(new XPath(".").toString()).toBe(".");
607
- })
608
-
609
- it("Should test empty XPath", () => {
610
- expect(new XPath("").isEmpty()).toBe(true);
611
- expect(new XPath(" ").isEmpty()).toBe(true);
612
- expect(new XPath(null).isEmpty()).toBe(true);
613
- expect(new XPath(undefined).isEmpty()).toBe(true);
614
- expect(new XPath("@name").isEmpty()).toBe(false);
615
- expect(new XPath("country/@name").isEmpty()).toBe(false);
616
- expect(new XPath("..").isEmpty()).toBe(false);
617
- expect(new XPath(".").isEmpty()).toBe(false);
618
- })
619
-
620
- it("Should test absolute XPath", () => {
621
- expect(new XPath("").isAbsolute()).toBe(false);
622
- expect(new XPath(" ").isAbsolute()).toBe(false);
623
- expect(new XPath(null).isAbsolute()).toBe(false);
624
- expect(new XPath(undefined).isAbsolute()).toBe(false);
625
- expect(new XPath("@name").isAbsolute()).toBe(false);
626
- expect(new XPath("country/@name").isAbsolute()).toBe(false);
627
- expect(new XPath("..").isAbsolute()).toBe(false);
628
- expect(new XPath(".").isAbsolute()).toBe(false);
629
- expect(new XPath("/").isAbsolute()).toBe(true);
630
- expect(new XPath("/country/@name").isAbsolute()).toBe(true);
631
- })
632
-
633
- it("Should test self XPath", () => {
634
- expect(new XPath("").isSelf()).toBe(false);
635
- expect(new XPath(" ").isSelf()).toBe(false);
636
- expect(new XPath(null).isSelf()).toBe(false);
637
- expect(new XPath(undefined).isSelf()).toBe(false);
638
- expect(new XPath("@name").isSelf()).toBe(false);
639
- expect(new XPath("country/@name").isSelf()).toBe(false);
640
- expect(new XPath("..").isSelf()).toBe(false);
641
- expect(new XPath(".").isSelf()).toBe(true);
642
- expect(new XPath("/").isSelf()).toBe(false);
643
- expect(new XPath("/country/@name").isSelf()).toBe(false);
644
- })
645
-
646
- it("Should test root XPath", () => {
647
- expect(new XPath("").isRootPath()).toBe(false);
648
- expect(new XPath(" ").isRootPath()).toBe(false);
649
- expect(new XPath(null).isRootPath()).toBe(false);
650
- expect(new XPath(undefined).isRootPath()).toBe(false);
651
- expect(new XPath("@name").isRootPath()).toBe(false);
652
- expect(new XPath("country/@name").isRootPath()).toBe(false);
653
- expect(new XPath("..").isRootPath()).toBe(false);
654
- expect(new XPath(".").isRootPath()).toBe(false);
655
- expect(new XPath("/").isRootPath()).toBe(true);
656
- expect(new XPath("/country/@name").isRootPath()).toBe(false);
657
- })
658
-
659
- it("Should return XPath elements", () => {
660
-
661
- function elements(xpath) {
662
- const result = [];
663
- const list = xpath.getElements();
664
- for (const e of list) {
665
- result.push(e._pathElement);
666
- }
667
- return result;
668
- }
669
-
670
- expect(elements(new XPath(""))).toEqual([ ]);
671
- expect(elements(new XPath(" "))).toEqual([ ]);
672
- expect(elements(new XPath(null))).toEqual([ ]);
673
- expect(elements(new XPath(undefined))).toEqual([ ]);
674
- expect(elements(new XPath("@name"))).toEqual([ "@name" ]);
675
- expect(elements(new XPath("country/@name"))).toEqual([ "country", "@name" ]);
676
- expect(elements(new XPath(".."))).toEqual([ ".." ]);
677
- expect(elements(new XPath("."))).toEqual([ "." ]);
678
- expect(elements(new XPath("/"))).toEqual([ ]);
679
- expect(elements(new XPath("/country/@name"))).toEqual([ "country", "@name" ]);
680
- })
681
-
682
- it("Should get relative path", () => {
683
- expect(new XPath("").getRelativePath().asString()).toBe("");
684
- expect(new XPath(" ").getRelativePath().asString()).toBe("");
685
- expect(new XPath(null).getRelativePath().asString()).toBe("");
686
- expect(new XPath(undefined).getRelativePath().asString()).toBe("");
687
- expect(new XPath("@name").getRelativePath().asString()).toBe("@name");
688
- expect(new XPath("country/@name").getRelativePath().asString()).toBe("country/@name");
689
- expect(new XPath("..").getRelativePath().asString()).toBe("..");
690
- expect(new XPath(".").getRelativePath().asString()).toBe(".");
691
- expect(new XPath("/").getRelativePath().asString()).toBe("");
692
- expect(new XPath("/country/@name").getRelativePath().asString()).toBe("country/@name");
693
- })
768
+ // Second call should not perform any API call
769
+ const schema2 = await cache.getSchema("xtk:queryDef");
770
+ expect(schema2).toBeNull();
771
+ });
694
772
  });
695
-
696
- describe("XPathElement", () => {
697
- it("Should create XPathElement", () => {
698
- expect(() => { new XPathElement(""); }).toThrow("Invalid empty xpath element");
699
- expect(() => { new XPathElement(" "); }).toThrow("Invalid empty xpath element");
700
- expect(() => { new XPathElement(null); }).toThrow("Invalid empty xpath element");
701
- expect(() => { new XPathElement(undefined); }).toThrow("Invalid empty xpath element");
702
- expect(new XPathElement("@name").asString()).toBe("@name");
703
- expect(new XPathElement("country").asString()).toBe("country");
704
- expect(new XPathElement("..").asString()).toBe("..");
705
- expect(new XPathElement(".").asString()).toBe(".");
706
- })
707
-
708
- it("toString", () => {
709
- expect(new XPathElement("@name").toString()).toBe("@name");
710
- expect(new XPathElement("country").toString()).toBe("country");
711
- expect(new XPathElement("..").toString()).toBe("..");
712
- expect(new XPathElement(".").toString()).toBe(".");
713
- })
714
-
715
- it("Should test if path element is self", () => {
716
- expect(new XPathElement("@name").isSelf()).toBe(false);
717
- expect(new XPathElement("country").isSelf()).toBe(false);
718
- expect(new XPathElement("..").isSelf()).toBe(false);
719
- expect(new XPathElement(".").isSelf()).toBe(true);
720
- })
721
-
722
- it("Should test if path element is the parent path (..)", () => {
723
- expect(new XPathElement("@name").isParent()).toBe(false);
724
- expect(new XPathElement("country").isParent()).toBe(false);
725
- expect(new XPathElement("..").isParent()).toBe(true);
726
- expect(new XPathElement(".").isParent()).toBe(false);
727
- })
728
- })
729
- });
730
-
731
-
732
- describe("Application for anonymous users", () => {
733
- it("Application objet should exist but will not have user/session info", async () => {
734
- const client = await Mock.makeAnonymousClient();
735
- expect(client.application).toBeNull();
736
- await client.logon();
737
- const application = client.application;
738
- expect(application).not.toBeNull();
739
- expect(application.buildNumber).toBeUndefined();
740
- expect(application.instanceName).toBeUndefined();
741
- expect(application.operator).toBeUndefined();
742
- expect(application.package).toBeUndefined();
743
- })
744
773
  });