-
Notifications
You must be signed in to change notification settings - Fork 13
/
NUEntity.test.js
133 lines (123 loc) · 6.08 KB
/
NUEntity.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import NUAttribute from './NUAttribute';
import NUEntity from './NUEntity';
import NUValidator from './NUValidator';
import { Enum } from 'enumify';
class NUAddressRangeIPTypeEnum extends Enum {}
NUAddressRangeIPTypeEnum.initEnum(['DUALSTACK', 'IPV4', 'IPV6']);
class CustomValidator1 extends NUValidator {
validate(entity, attrObj, formValues) {
const { attr5, attr4 } = formValues;
if (attr5 && !attr4) {
return 'attr4 must be set when attr5 is true';
}
return undefined;
}
}
class CustomValidator2 extends NUValidator {
validate(entity, attrObj, formValues) {
const { attr3, attr5 } = formValues;
if (attr5 && attr3 !== 'DUALSTACK') {
return 'attr3 must be DUALSTACK when attr5 is true';
}
return undefined;
}
}
class MyEntity extends NUEntity {
static attributeDescriptors = {
...NUEntity.attributeDescriptors,
attr1: new NUAttribute({ localName: 'attr1', remoteName: 'ATTR1', attributeType: NUAttribute.ATTR_TYPE_STRING, isCreateOnly: true }),
attr2: new NUAttribute({ localName: 'attr2', attributeType: NUAttribute.ATTR_TYPE_STRING }),
attr3: new NUAttribute({ localName: 'attr3', remoteName: 'ATTR3', attributeType: NUAttribute.ATTR_TYPE_ENUM, isRequired: true, choices: [NUAddressRangeIPTypeEnum.DUALSTACK, NUAddressRangeIPTypeEnum.IPV4, NUAddressRangeIPTypeEnum.IPV6] }),
attr4: new NUAttribute({ localName: 'attr4', attributeType: NUAttribute.ATTR_TYPE_STRING, minLength: 3, maxLength: 6 }),
attr5: new NUAttribute({ localName: 'attr5', attributeType: NUAttribute.ATTR_TYPE_BOOLEAN }),
attr6: new NUAttribute({ localName: 'attr6', attributeType: NUAttribute.ATTR_TYPE_INTEGER }),
attr7: new NUAttribute({ localName: 'attr7', attributeType: NUAttribute.ATTR_TYPE_FLOAT }),
attr8: new NUAttribute({ localName: 'attr8', attributeType: NUAttribute.ATTR_TYPE_LIST, subType: NUAttribute.ATTR_TYPE_ENUM, choices: [NUAddressRangeIPTypeEnum.DUALSTACK, NUAddressRangeIPTypeEnum.IPV4, NUAddressRangeIPTypeEnum.IPV6] }),
attr9: new NUAttribute({ localName: 'attr9', attributeType: NUAttribute.ATTR_TYPE_LIST, subType: NUAttribute.ATTR_TYPE_FLOAT }),
attr11: new NUAttribute({ localName: 'attr11', attributeType: NUAttribute.ATTR_TYPE_STRING, isReadOnly:true }),
}
constructor() {
super();
this.defineProperties({
attr1: null,
attr2: null,
});
this.registerValidator(new CustomValidator1('customValidator1'));
this.registerValidator(new CustomValidator2('customValidator2'));
}
get RESTName() {
return 'childentity';
}
}
// test validate mandatory attribute, choice, min length, max length
it('attribute validations', () => {
const myEntity = new MyEntity();
myEntity.ID = 'xyz123';
let isValid = myEntity.isValid(myEntity);
const errors = myEntity.validationErrors;
expect(isValid).toEqual(false);
expect(errors.get('attr3').description).toEqual('This value is mandatory');
myEntity.attr3 = 'IPV7';
isValid = myEntity.isValid(myEntity);
expect(isValid).toEqual(false);
expect(errors.get('attr3').description).toEqual('Allowed values are DUALSTACK,IPV4,IPV6, but value provided is IPV7');
myEntity.attr3 = 'IPV6';
isValid = myEntity.isValid(myEntity);
expect(errors.get('attr3')).toEqual(undefined);
expect(isValid).toEqual(true);
myEntity.attr4 = 'ab';
isValid = myEntity.isValid(myEntity);
expect(isValid).toEqual(false);
expect(errors.get('attr4').description).toEqual('Minimum length should be 3, but is 2');
myEntity.attr4 = 'abcdefgh';
isValid = myEntity.isValid(myEntity);
expect(isValid).toEqual(false);
expect(errors.get('attr4').description).toEqual('Maximum length should be 6, but is 8');
myEntity.attr4 = 123;
isValid = myEntity.isValid(myEntity);
expect(isValid).toEqual(false);
expect(errors.get('attr4').description).toEqual('Data type should be string');
myEntity.attr4 = 'abcdef';
isValid = myEntity.isValid(myEntity);
expect(isValid).toEqual(true);
expect(errors.get('attr4')).toEqual(undefined);
myEntity.attr8 = [1, 2, 3];
isValid = myEntity.isValid(myEntity);
expect(isValid).toEqual(false);
expect(errors.get('attr8').description).toEqual('Allowed values are DUALSTACK,IPV4,IPV6, but value provided is 1');
myEntity.attr8 = [NUAddressRangeIPTypeEnum.DUALSTACK.name , NUAddressRangeIPTypeEnum.IPV4.name];
isValid = myEntity.isValid(myEntity);
expect(isValid).toEqual(true);
expect(errors.get('attr8')).toEqual(undefined);
myEntity.attr9 = ["abc", "def"];
isValid = myEntity.isValid(myEntity);
expect(isValid).toEqual(false);
expect(errors.get('attr9').description).toEqual('Data type should be float');
myEntity.attr9 = [1, 2.22, 3.56, 4];
isValid = myEntity.isValid(myEntity);
expect(isValid).toEqual(true);
expect(errors.get('attr9')).toEqual(undefined);
expect(isValid).toEqual(true);
});
it('custom validations', () => {
const myEntity = new MyEntity();
myEntity.ID = 'xyz123';
myEntity.attr3 = 'IPV4';
myEntity.attr7 = 123.45;
let isValid = myEntity.isValid({});
myEntity.attr5 = true;
isValid = myEntity.isValid(myEntity);
const errors = myEntity.validationErrors;
expect(isValid).toEqual(false);
expect(errors.get('customValidator1')).toEqual('attr4 must be set when attr5 is true');
expect(errors.get('customValidator2')).toEqual('attr3 must be DUALSTACK when attr5 is true');
myEntity.attr4 = 'value4';
myEntity.attr3 = 'DUALSTACK';
isValid = myEntity.isValid(myEntity);
expect(isValid).toEqual(true);
expect(myEntity.constructor.attributeDescriptors.attr1.isCreateOnly).toEqual(true);
expect(myEntity.constructor.attributeDescriptors.attr1.isEditable).toEqual(false);
expect(myEntity.constructor.attributeDescriptors.attr2.isEditable).toEqual(true);
expect(myEntity.constructor.attributeDescriptors.attr11.isReadOnly).toEqual(true);
expect(myEntity.constructor.attributeDescriptors.attr11.isEditable).toEqual(false);
});