forked from nuagenetworks/js-bambou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNUService.test.js
197 lines (173 loc) · 5.91 KB
/
NUService.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import NUAttribute from './NUAttribute';
import NUEntity from './NUEntity';
import NURootEntity from './NURootEntity';
import NUService from './NUService';
import ServiceClassRegistry from './ServiceClassRegistry';
jest.mock('./NURESTConnection');
class RESTUser extends NURootEntity {
static attributeDescriptors = {
...NURootEntity.attributeDescriptors,
attr1: new NUAttribute({ localName: 'attr1', attributeType: NUAttribute.ATTR_TYPE_STRING }),
attr2: new NUAttribute({ localName: 'attr2', remoteName: 'ATTR2', attributeType: NUAttribute.ATTR_TYPE_STRING }),
}
constructor() {
super();
this.defineProperties({
attr1: null,
attr2: null,
});
}
get RESTName() {
return 'me';
}
get resourceName() {
return this.RESTName;
}
}
class Parent extends NUEntity {
static attributeDescriptors = {
...NUEntity.attributeDescriptors,
parentAttr1: new NUAttribute({ localName: 'parentAttr1', remoteName: 'parentATTR1', attributeType: NUAttribute.ATTR_TYPE_STRING }),
}
constructor() {
super();
this.defineProperties({
parentAttr1: null,
});
}
get RESTName() {
return 'parententity';
}
get resourceName() {
return 'parententities';
}
}
class Child extends NUEntity {
static attributeDescriptors = {
...NUEntity.attributeDescriptors,
attr1: new NUAttribute({ localName: 'attr1', remoteName: 'ATTR1', attributeType: NUAttribute.ATTR_TYPE_STRING }),
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: ['A', 'B', 'C'] }),
attr4: new NUAttribute({ localName: 'attr4', attributeType: NUAttribute.ATTR_TYPE_STRING, minLength: 3, maxLength: 6 }),
}
constructor() {
super();
this.defineProperties({
attr1: null,
attr2: null,
});
}
get RESTName() {
return 'childentity';
}
get resourceName() {
return 'childentities';
}
}
ServiceClassRegistry.register(Parent);
ServiceClassRegistry.register(Child);
const VSDService = new NUService({
rootURL: 'https://135.227.177.144:8443/nuage/api/v4_0',
headers: {
headerAuthorization: 'Authorization',
headerPage: 'Page',
headerPageSize: 'PageSize',
headerFilter: 'Filter',
headerFilterType: 'FilterType',
headerOrderBy: 'OrderBy',
headerCount: 'Count',
headerMessage:'Message'
}
});
VSDService.addCustomHeader('X-Nuage-Organization', 'csp');
VSDService.addCustomHeader('Content-type', 'application/json');
VSDService.pageSize = 3;
// test login
it('login', () => {
const userEntity = new RESTUser();
userEntity.userName = 'csproot';
userEntity.password = 'csproot';
return VSDService.fetch(userEntity).then((response) => {
expect(response.attr1).toEqual('abcd');
expect(response.attr2).toEqual('efgh');
expect(response.APIKey).toEqual('396bbbe7-3aae-424a-9d4b-5d2202ce6a4c');
});
});
// test fetch
it('fetch', () => {
const child = new Child();
child.ID = 'xyz123';
return VSDService.fetch(child).then((response) => {
expect(response.ID).toEqual('def123');
expect(response.attr1).toEqual('AC0098777');
expect(response.attr2).toEqual('1485301116777');
});
});
// test fetchall
it('fetchall', () => {
const parentEntity = new Parent();
parentEntity.ID = 'd8cf28fa-e6d5-4779-8229-e45192ef763b';
return VSDService.fetchAll({resourceName: new Child().resourceName, parentEntity}).then((response) => {
expect(response.data[1].ID).toEqual('xyz456');
expect(response.data[1].attr1).toEqual('AC0098766');
expect(response.data[1].attr2).toEqual('1485302226000');
});
});
// test fetchall by page, filter, and orderBy
it('fetchall by page, filter, and orderBy', () => {
const parentEntity = new Parent();
parentEntity.ID = 'd8cf28fa-e6d5-4779-8229-e45192ef763b';
return VSDService.fetchAll({
resourceName: new Child().resourceName,
parentEntity,
page: 3,
filter: 'value < 100',
orderBy: 'value ASC'
}).then((response) => {
expect(response.data[2].ID).toEqual('xyz789');
expect(response.data[2].attr1).toEqual('AC0098767');
expect(response.data[2].attr2).toEqual('1485303336000');
expect(response.headers.count).toEqual(10);
expect(response.headers.filter).toEqual('value < 100');
expect(response.headers.orderBy).toEqual('value ASC');
expect(response.headers.page).toEqual(3);
expect(response.headers.pageSize).toEqual(3);
});
});
// test update
it('update', () => {
const child = new Child();
child.ID = 'xyz123';
child.attr1 = 'TEST123';
return VSDService.update(child).then((response) => {
expect(response.data).toEqual('');
});
});
// test create
it('create', () => {
const parent = new Parent();
parent.ID = 'd8cf28fa-e6d5-4779-8229-e45192ef763b';
const child = new Child();
child.ID = 'emp789';
return VSDService.create(child, parent).then((response) => {
expect(response.ID).toEqual('emp789');
expect(response.attr1).toEqual('AC00EMP789');
expect(response.attr2).toEqual('1481113336000');
});
});
// test delete
it('delete', () => {
const child = new Child();
child.ID = 'xyz123';
return VSDService.delete(child).then((response) => {
expect(response.data).toEqual('');
});
});
// test count
it('count', () => {
const parentEntity = new Parent();
parentEntity.ID = 'parent123';
return VSDService.count({resourceName: new Child().resourceName, parentEntity, filter: 'value < 123'}).then((response) => {
expect(response).toEqual(30);
});
});