@fincity/kirun-js 1.4.14 → 1.5.2

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.
@@ -0,0 +1,226 @@
1
+ import { AdditionalPropertiesType, ArraySchemaType, HybridRepository } from '../../../../../src';
2
+ import { Schema } from '../../../../../src/engine/json/schema/Schema';
3
+ import { SchemaType } from '../../../../../src/engine/json/schema/type/SchemaType';
4
+ import { TypeUtil } from '../../../../../src/engine/json/schema/type/TypeUtil';
5
+ import { SchemaValidationException } from '../../../../../src/engine/json/schema/validator/exception/SchemaValidationException';
6
+ import { SchemaValidator } from '../../../../../src/engine/json/schema/validator/SchemaValidator';
7
+ import { KIRunSchemaRepository } from '../../../../../src/engine/repository/KIRunSchemaRepository';
8
+
9
+ test('filter condition with ref schema', () => {
10
+ let filterOperator: Schema = Schema.ofString('filterOperator')
11
+ .setNamespace('Test')
12
+ .setEnums(['EQUALS', 'LESS_THAN', 'GREATER_THAN', 'LESS_THAN_EQUAL']);
13
+
14
+ let filterCondition: Schema = Schema.ofObject('filterCondition')
15
+ .setNamespace('Test')
16
+ .setProperties(
17
+ new Map<string, Schema>([
18
+ ['negate', Schema.ofBoolean('negate')],
19
+ ['filterConditionOperator', Schema.ofRef('Test.filterOperator')],
20
+ ['field', Schema.ofString('field')],
21
+ ['value', Schema.ofAny('value')],
22
+ ['toValue', Schema.ofAny('toValue')],
23
+ [
24
+ 'multiValue',
25
+ Schema.ofArray('multiValue').setItems(
26
+ new ArraySchemaType().setSingleSchema(Schema.ofAny('singleType')),
27
+ ),
28
+ ],
29
+ ['isValue', Schema.ofBoolean('isValue')],
30
+ ['isToValue', Schema.ofBoolean('isToValue')],
31
+ ]),
32
+ );
33
+
34
+ var schemaMap = new Map<string, Schema>([
35
+ ['filterOperator', filterOperator],
36
+ ['filterCondition', filterCondition],
37
+ ]);
38
+
39
+ const repo = new HybridRepository<Schema>(
40
+ {
41
+ find(namespace: string, name: string): Schema | undefined {
42
+ if (namespace !== 'Test') return undefined;
43
+ return schemaMap.get(name);
44
+ },
45
+ },
46
+ new KIRunSchemaRepository(),
47
+ );
48
+
49
+ var tempOb = {
50
+ field: 'a.b.c.d',
51
+ value: 'surendhar',
52
+ filterConditionOperator: 'LESS_THAN',
53
+ negate: true,
54
+ isValue: false,
55
+ };
56
+
57
+ expect(SchemaValidator.validate([], filterCondition, repo, tempOb)).toBe(tempOb);
58
+ });
59
+
60
+ test('complex condition with ref schema', () => {
61
+ let complexOperator: Schema = Schema.ofString('complexOperator')
62
+ .setNamespace('Test')
63
+ .setEnums(['AND', 'OR']);
64
+
65
+ // let arraySchema: Schema = Schema.ofArray('conditions', Schema.ofRef('#'));
66
+ let arraySchema: Schema = Schema.ofArray('conditions', Schema.ofRef('Test.complexCondition'));
67
+
68
+ let complexCondition: Schema = Schema.ofObject('complexCondition')
69
+ .setNamespace('Test')
70
+ .setProperties(
71
+ new Map<string, Schema>([
72
+ ['negate', Schema.ofBoolean('negate')],
73
+ ['complexConditionOperator', Schema.ofRef('Test.complexOperator')],
74
+ ['conditions', arraySchema],
75
+ ]),
76
+ );
77
+
78
+ var schemaMap = new Map<string, Schema>([
79
+ ['complexOperator', complexOperator],
80
+ ['complexCondition', complexCondition],
81
+ ]);
82
+
83
+ const repo = new HybridRepository<Schema>(
84
+ {
85
+ find(namespace: string, name: string): Schema | undefined {
86
+ if (namespace !== 'Test') return undefined;
87
+ return schemaMap.get(name);
88
+ },
89
+ },
90
+ new KIRunSchemaRepository(),
91
+ );
92
+
93
+ let ja: any[] = [];
94
+
95
+ let mjob = {
96
+ conditions: [...ja],
97
+ negate: true,
98
+ complexConditionOperator: 'AND',
99
+ };
100
+
101
+ let njob = {
102
+ conditions: [...ja],
103
+ negate: true,
104
+ complexConditionOperator: 'OR',
105
+ };
106
+
107
+ ja.push(mjob);
108
+ ja.push(njob);
109
+
110
+ let bjob = {
111
+ conditions: [...ja],
112
+ negate: true,
113
+ complexConditionOperator: 'AND',
114
+ };
115
+
116
+ expect(SchemaValidator.validate([], complexCondition, repo, bjob)).toBe(bjob);
117
+ });
118
+
119
+ test('filter complex condition with ref schema', () => {
120
+ let filterOperator: Schema = Schema.ofString('filterOperator')
121
+ .setNamespace('Test')
122
+ .setEnums(['EQUALS', 'LESS_THAN', 'GREATER_THAN', 'LESS_THAN_EQUAL', 'IN'])
123
+ .setDefaultValue('EQUALS');
124
+
125
+ let filterCondition: Schema = Schema.ofObject('filterCondition')
126
+ .setNamespace('Test')
127
+ .setProperties(
128
+ new Map<string, Schema>([
129
+ ['negate', Schema.ofBoolean('negate').setDefaultValue(false)],
130
+ ['operator', Schema.ofRef('Test.filterOperator')],
131
+ ['field', Schema.ofString('field')],
132
+ ['value', Schema.ofAny('value')],
133
+ ['toValue', Schema.ofAny('toValue')],
134
+ [
135
+ 'multiValue',
136
+ Schema.ofArray('multiValue').setItems(
137
+ new ArraySchemaType().setSingleSchema(Schema.ofAny('singleType')),
138
+ ),
139
+ ],
140
+ ['isValue', Schema.ofBoolean('isValue').setDefaultValue(false)],
141
+ ['isToValue', Schema.ofBoolean('isToValue').setDefaultValue(false)],
142
+ ]),
143
+ )
144
+ .setRequired(['operator', 'field'])
145
+ .setAdditionalProperties(new AdditionalPropertiesType().setBooleanValue(false));
146
+
147
+ let complexOperator: Schema = Schema.ofString('complexOperator')
148
+ .setNamespace('Test')
149
+ .setEnums(['AND', 'OR']);
150
+
151
+ let arraySchema: Schema = Schema.ofArray(
152
+ 'conditions',
153
+ new Schema().setAnyOf([Schema.ofRef('#'), Schema.ofRef('Test.FilterCondition')]),
154
+ );
155
+
156
+ let complexCondition: Schema = Schema.ofObject('complexCondition')
157
+ .setNamespace('Test')
158
+ .setProperties(
159
+ new Map<string, Schema>([
160
+ ['conditions', arraySchema],
161
+ ['negate', Schema.ofBoolean('negate').setDefaultValue(false)],
162
+ ['operator', Schema.ofRef('Test.complexOperator')],
163
+ ]),
164
+ )
165
+ .setRequired(['conditions', 'operator'])
166
+ .setAdditionalProperties(new AdditionalPropertiesType().setBooleanValue(false));
167
+
168
+ var schemaMap = new Map<string, Schema>([
169
+ ['filterOperator', filterOperator],
170
+ ['filterCondition', filterCondition],
171
+ ['complexOperator', complexOperator],
172
+ ['complexCondition', complexCondition],
173
+ ]);
174
+
175
+ const repo = new HybridRepository<Schema>(
176
+ {
177
+ find(namespace: string, name: string): Schema | undefined {
178
+ if (namespace !== 'Test') return undefined;
179
+ return schemaMap.get(name);
180
+ },
181
+ },
182
+ new KIRunSchemaRepository(),
183
+ );
184
+
185
+ var tempOb = {
186
+ field: 'a.b.c.d',
187
+ value: 'surendhar',
188
+ operator: 'LESS_THAN',
189
+ negate: true,
190
+ isValue: false,
191
+ };
192
+
193
+ var tempOb1 = {
194
+ ...tempOb,
195
+ operator: 'GREATER_THAN',
196
+ isValue: true,
197
+ };
198
+
199
+ var tempOb2 = {
200
+ field: 'k.lm.mno',
201
+ operator: 'IN',
202
+ multiValue: [1, 2, 3, 4, 5, 5, 6, 7],
203
+ negate: true,
204
+ };
205
+ var ja: any[] = [];
206
+
207
+ ja.push(tempOb);
208
+ ja.push(tempOb1);
209
+ ja.push(tempOb2);
210
+
211
+ var mjob = {
212
+ conditions: [],
213
+ negate: false,
214
+ operator: 'AND',
215
+ };
216
+
217
+ var bjob = {
218
+ conditions: [mjob],
219
+ negate: true,
220
+ operator: 'OR',
221
+ };
222
+
223
+ var res = SchemaValidator.validate([], complexCondition, repo, bjob);
224
+
225
+ expect(res).toBe(bjob);
226
+ });
@@ -0,0 +1,214 @@
1
+ import { KIRuntime } from '../../../src/engine/runtime/KIRuntime';
2
+ import { FunctionDefinition } from '../../../src/engine/model/FunctionDefinition';
3
+ import { FunctionExecutionParameters } from '../../../src/engine/runtime/FunctionExecutionParameters';
4
+ import { KIRunFunctionRepository } from '../../../src/engine/repository/KIRunFunctionRepository';
5
+ import { KIRunSchemaRepository } from '../../../src/engine/repository/KIRunSchemaRepository';
6
+ import { Namespaces } from '../../../src/engine/namespaces/Namespaces';
7
+
8
+ test('KIRuntime Without Gen Event Definition 1', async () => {
9
+ var def = {
10
+ name: 'getAppData',
11
+ namespace: 'UIApp',
12
+ parameters: {
13
+ a: { parameterName: 'a', schema: { name: 'INTEGER', type: 'INTEGER' } },
14
+ b: { parameterName: 'b', schema: { name: 'INTEGER', type: 'INTEGER' } },
15
+ c: { parameterName: 'c', schema: { name: 'INTEGER', type: 'INTEGER' } },
16
+ },
17
+ steps: {
18
+ add: {
19
+ statementName: 'add',
20
+ namespace: Namespaces.MATH,
21
+ name: 'Add',
22
+ parameterMap: {
23
+ value: {
24
+ one: { key: 'one', type: 'EXPRESSION', expression: 'Arguments.a' },
25
+ two: { key: 'two', type: 'EXPRESSION', expression: '10 + 1' },
26
+ three: { key: 'three', type: 'EXPRESSION', expression: 'Arguments.c' },
27
+ },
28
+ },
29
+ },
30
+ print: {
31
+ statementName: 'print',
32
+ namespace: Namespaces.SYSTEM,
33
+ name: 'Print',
34
+ parameterMap: {
35
+ values: {
36
+ one: {
37
+ key: 'one',
38
+ type: 'EXPRESSION',
39
+ expression: 'Steps.add.output.value',
40
+ order: 2,
41
+ },
42
+ abc: {
43
+ key: 'abc',
44
+ type: 'VALUE',
45
+ value: 'Nothing muchh....',
46
+ order: 1,
47
+ },
48
+ },
49
+ },
50
+ },
51
+ },
52
+ };
53
+
54
+ const fd = FunctionDefinition.from(def);
55
+
56
+ const test = (console.log = jest.fn().mockImplementation(() => {}));
57
+
58
+ await new KIRuntime(fd, false).execute(
59
+ new FunctionExecutionParameters(
60
+ new KIRunFunctionRepository(),
61
+ new KIRunSchemaRepository(),
62
+ ).setArguments(
63
+ new Map([
64
+ ['a', 7],
65
+ ['b', 11],
66
+ ['c', 13],
67
+ ]),
68
+ ),
69
+ );
70
+ expect(test.mock.calls[0][0]).toBe('Nothing muchh....');
71
+ expect(test.mock.calls[0][1]).toBe(31);
72
+ });
73
+
74
+ test('KIRuntime Without Gen Event Definition 2', async () => {
75
+ var def = {
76
+ name: 'getAppData',
77
+ namespace: 'UIApp',
78
+ parameters: {
79
+ a: { parameterName: 'a', schema: { name: 'INTEGER', type: 'INTEGER' } },
80
+ b: { parameterName: 'b', schema: { name: 'INTEGER', type: 'INTEGER' } },
81
+ c: { parameterName: 'c', schema: { name: 'INTEGER', type: 'INTEGER' } },
82
+ },
83
+ events: {
84
+ output: {
85
+ name: 'output',
86
+ parameters: {},
87
+ },
88
+ },
89
+ steps: {
90
+ add: {
91
+ statementName: 'add',
92
+ namespace: Namespaces.MATH,
93
+ name: 'Add',
94
+ parameterMap: {
95
+ value: {
96
+ one: { key: 'one', type: 'EXPRESSION', expression: 'Arguments.a' },
97
+ two: { key: 'two', type: 'EXPRESSION', expression: '10 + 1' },
98
+ three: { key: 'three', type: 'EXPRESSION', expression: 'Arguments.c' },
99
+ },
100
+ },
101
+ },
102
+ print: {
103
+ statementName: 'print',
104
+ namespace: Namespaces.SYSTEM,
105
+ name: 'Print',
106
+ parameterMap: {
107
+ values: {
108
+ one: {
109
+ key: 'one',
110
+ type: 'EXPRESSION',
111
+ expression: 'Steps.add.output.value',
112
+ order: 2,
113
+ },
114
+ abc: {
115
+ key: 'abc',
116
+ type: 'VALUE',
117
+ value: 'Something muchh....',
118
+ order: 1,
119
+ },
120
+ },
121
+ },
122
+ },
123
+ },
124
+ };
125
+
126
+ const fd = FunctionDefinition.from(def);
127
+
128
+ const test = (console.log = jest.fn().mockImplementation(() => {}));
129
+
130
+ await new KIRuntime(fd, false).execute(
131
+ new FunctionExecutionParameters(
132
+ new KIRunFunctionRepository(),
133
+ new KIRunSchemaRepository(),
134
+ ).setArguments(
135
+ new Map([
136
+ ['a', 7],
137
+ ['b', 11],
138
+ ['c', 13],
139
+ ]),
140
+ ),
141
+ );
142
+ expect(test.mock.calls[0][0]).toBe('Something muchh....');
143
+ expect(test.mock.calls[0][1]).toBe(31);
144
+ });
145
+
146
+ test('KIRuntime With Definition 1', async () => {
147
+ var def = {
148
+ name: 'getAppData',
149
+ namespace: 'UIApp',
150
+ parameters: {
151
+ a: { parameterName: 'a', schema: { name: 'INTEGER', type: 'INTEGER' } },
152
+ b: { parameterName: 'b', schema: { name: 'INTEGER', type: 'INTEGER' } },
153
+ c: { parameterName: 'c', schema: { name: 'INTEGER', type: 'INTEGER' } },
154
+ },
155
+ events: {
156
+ output: {
157
+ name: 'output',
158
+ parameters: { additionResult: { name: 'additionResult', type: 'INTEGER' } },
159
+ },
160
+ },
161
+ steps: {
162
+ add: {
163
+ statementName: 'add',
164
+ namespace: Namespaces.MATH,
165
+ name: 'Add',
166
+ parameterMap: {
167
+ value: {
168
+ one: { key: 'one', type: 'EXPRESSION', expression: 'Arguments.a' },
169
+ two: { key: 'two', type: 'EXPRESSION', expression: '10 + 1' },
170
+ three: { key: 'three', type: 'EXPRESSION', expression: 'Arguments.c' },
171
+ },
172
+ },
173
+ },
174
+ print: {
175
+ statementName: 'print',
176
+ namespace: Namespaces.SYSTEM,
177
+ name: 'Print',
178
+ parameterMap: {
179
+ values: {
180
+ one: {
181
+ key: 'one',
182
+ type: 'EXPRESSION',
183
+ expression: 'Steps.add.output.value',
184
+ order: 2,
185
+ },
186
+ abc: {
187
+ key: 'abc',
188
+ type: 'VALUE',
189
+ value: 'Something muchh....',
190
+ order: 1,
191
+ },
192
+ },
193
+ },
194
+ },
195
+ },
196
+ };
197
+
198
+ const fd = FunctionDefinition.from(def);
199
+
200
+ const fep = new FunctionExecutionParameters(
201
+ new KIRunFunctionRepository(),
202
+ new KIRunSchemaRepository(),
203
+ ).setArguments(
204
+ new Map([
205
+ ['a', 7],
206
+ ['b', 11],
207
+ ['c', 13],
208
+ ]),
209
+ );
210
+
211
+ const runtime = new KIRuntime(fd, false);
212
+
213
+ await expect(() => runtime.execute(fep)).rejects.toThrowError();
214
+ });