forked from DAVFoundation/dav-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Identity.test.js
385 lines (384 loc) · 19.9 KB
/
Identity.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Config_1 = require("./Config");
const Need_1 = require("./Need");
const NeedFilterParams_1 = require("./vessel-charging/NeedFilterParams");
const NeedParams_1 = require("./vessel-charging/NeedParams");
const MissionParams_1 = require("./vessel-charging/MissionParams");
const StatusRequestMessageParams_1 = require("./vessel-charging/messages/StatusRequestMessageParams");
const BidParams_1 = require("./vessel-charging/BidParams");
const common_types_1 = require("./common-types");
const Message_1 = require("./Message");
const Mission_1 = require("./Mission");
const Bid_1 = require("./Bid");
const AxiosMock_1 = require("./mocks/AxiosMock");
describe('Identity class', () => {
const TOPIC_ID = 'TOPIC_ID';
const kafkaError = { msg: 'Kafka error' };
const davNodeError = { msg: 'Dav node error' };
const config = new Config_1.default({});
const needFilterParams = new NeedFilterParams_1.default({
location: { lat: 10, long: 10 },
radius: 1000,
});
const needParams = new NeedParams_1.default({
location: {
lat: 32.050382,
long: 34.766149,
},
});
const bidParams = new BidParams_1.default({
vehicleId: 'DAV_ID',
availableFrom: Date.now(),
price: '100',
});
const missionParams = new MissionParams_1.default({
id: 'MISSION_ID',
neederDavId: 'DAV_ID',
vehicleId: 'DAV_ID',
price: '100',
});
const forContextSwitch = () => {
return new Promise((resolve, reject) => {
jest.useRealTimers();
setTimeout(resolve, 0);
jest.useFakeTimers();
});
};
describe('publishNeed method', () => {
const kafkaMock = {
generateTopicId: jest.fn(() => TOPIC_ID),
createTopic: jest.fn(() => Promise.resolve()),
};
beforeAll(() => {
jest.doMock('./Kafka', () => ({
default: kafkaMock,
}));
jest.doMock('axios', () => ({
default: AxiosMock_1.default,
}));
});
beforeEach(() => {
jest.clearAllMocks();
});
xit('should call relevant functions and return valid need', async () => {
// tslint:disable-next-line:variable-name
const Identity = (await Promise.resolve().then(() => require('./Identity'))).default;
const identity = new Identity('id', 'davId', config);
const need = new Need_1.default(TOPIC_ID, needParams, config);
await expect(identity.publishNeed(needParams)).resolves.toEqual(need);
expect(kafkaMock.generateTopicId).toHaveBeenCalled();
expect(kafkaMock.createTopic).toHaveBeenCalledWith(TOPIC_ID, config);
expect(AxiosMock_1.default.post).toHaveBeenCalled();
expect(AxiosMock_1.default.post).toHaveBeenCalledWith(`${config.apiSeedUrls[0]}/publishNeed/${TOPIC_ID}`, needParams.serialize());
});
xit('should fail due to dav node exception', async () => {
AxiosMock_1.default.post.mockImplementation(() => Promise.reject(davNodeError));
// tslint:disable-next-line:variable-name
const Identity = (await Promise.resolve().then(() => require('./Identity'))).default;
const identity = new Identity('id', 'davId', config);
await expect(identity.publishNeed(needParams)).rejects.toThrow(`Fail to publish need: ${davNodeError}`);
});
xit('should fail due to topic creation failure', async () => {
kafkaMock.createTopic.mockImplementation(() => Promise.reject(kafkaError));
// tslint:disable-next-line:variable-name
const Identity = (await Promise.resolve().then(() => require('./Identity'))).default;
const identity = new Identity('id', 'davId', config);
await expect(identity.publishNeed(needParams)).rejects.toThrow(`Fail to create a topic: ${kafkaError}`);
expect(AxiosMock_1.default.post).not.toHaveBeenCalled();
});
});
describe('needsForType method', () => {
const needParams1 = new NeedParams_1.default({
location: {
lat: 32.050382,
long: 34.766149,
},
});
const needParams2 = new NeedParams_1.default({
location: {
lat: 32.050382,
long: 34.766149,
},
});
const needParams3 = new NeedParams_1.default({
location: {
lat: 32.050382,
long: 34.766149,
},
});
beforeAll(() => {
jest.doMock('axios', () => ({
default: AxiosMock_1.default,
}));
});
beforeEach(() => {
jest.clearAllMocks();
jest.resetAllMocks();
jest.resetModules();
});
it('should receive needs and relevant functions', async () => {
const kafkaMessageStreamMock = {
filterType: jest.fn(() => common_types_1.Observable.from([needParams1, needParams2, needParams3])),
};
const kafkaMock = {
generateTopicId: jest.fn(() => TOPIC_ID),
createTopic: jest.fn(() => Promise.resolve()),
messages: jest.fn(() => Promise.resolve(kafkaMessageStreamMock)),
};
jest.doMock('./KafkaMessageStream', () => ({
default: jest.fn().mockImplementation(() => kafkaMessageStreamMock),
}));
jest.doMock('./Kafka', () => ({ default: kafkaMock }));
// tslint:disable-next-line:variable-name
const Identity = (await Promise.resolve().then(() => require('./Identity'))).default;
const identity = new Identity('selfId', 'davId', config);
const spy = jest.fn();
const needs = await identity.needsForType(needFilterParams, NeedParams_1.default);
needs.subscribe(spy);
expect(spy.mock.calls.length).toBe(3);
expect(spy.mock.calls[0][0]).toEqual(new Need_1.default(TOPIC_ID, needParams1, config));
expect(spy.mock.calls[1][0]).toEqual(new Need_1.default(TOPIC_ID, needParams2, config));
expect(kafkaMock.generateTopicId).toHaveBeenCalled();
expect(kafkaMock.createTopic).toHaveBeenCalledWith(TOPIC_ID, config);
expect(AxiosMock_1.default.post).toHaveBeenCalledWith(`${config.apiSeedUrls[0]}/needsForType/${TOPIC_ID}`, needFilterParams.serialize());
});
// xit('should receive Kafka error event', async () => {
// kafkaMock.paramsStream.mockImplementation(() => Observable.fromPromise(Promise.reject(kafkaError)));
// // tslint:disable-next-line:variable-name
// const Identity: any = (await import('./Identity')).default;
// const identity = new Identity('selfId', 'davId', config);
// const successSpy = jest.fn();
// const errorSpy = jest.fn();
// const needs = await identity.needsForType(needFilterParams, NeedParams);
// needs.subscribe(successSpy, errorSpy);
// await forContextSwitch();
// expect(successSpy.mock.calls.length).toBe(0);
// expect(errorSpy.mock.calls.length).toBe(1);
// expect(errorSpy.mock.calls[0][0]).toBe(kafkaError);
// expect(kafkaMock.generateTopicId).toHaveBeenCalled();
// expect(kafkaMock.createTopic).toHaveBeenCalledWith(TOPIC_ID, config);
// expect(AxiosMock.post).toHaveBeenCalledWith(`${config.apiSeedUrls[0]}/needsForType/${TOPIC_ID}`, needFilterParams.serialize());
// });
it('should fail due to dav node exception', async () => {
const kafkaMock = {
generateTopicId: jest.fn(() => TOPIC_ID),
createTopic: jest.fn(() => Promise.resolve()),
};
jest.doMock('./Kafka', () => ({ default: kafkaMock }));
AxiosMock_1.default.post.mockImplementation(() => Promise.reject(davNodeError));
// tslint:disable-next-line:variable-name
const Identity = (await Promise.resolve().then(() => require('./Identity'))).default;
const identity = new Identity('selfId', 'davId', config);
await expect(identity.needsForType(needFilterParams, NeedParams_1.default)).rejects.toThrow(`Needs registration failed: ${davNodeError}`);
expect(kafkaMock.generateTopicId).toHaveBeenCalled();
expect(kafkaMock.createTopic).toHaveBeenCalledWith(TOPIC_ID, config);
expect(AxiosMock_1.default.post).toHaveBeenCalledWith(`${config.apiSeedUrls[0]}/needsForType/${TOPIC_ID}`, needFilterParams.serialize());
});
xit('should fail due to topic creation failure', async () => {
const kafkaMock = {
generateTopicId: jest.fn(() => TOPIC_ID),
createTopic: jest.fn(() => Promise.resolve()),
};
jest.doMock('./Kafka', () => ({ default: kafkaMock }));
kafkaMock.createTopic.mockImplementation(() => Promise.reject(kafkaError));
// tslint:disable-next-line:variable-name
const Identity = (await Promise.resolve().then(() => require('./Identity'))).default;
const identity = new Identity('selfId', 'davId', config);
await expect(identity.needsForType(needFilterParams, NeedParams_1.default)).rejects.toThrow(`Fail to create a topic: ${kafkaError}`);
expect(kafkaMock.generateTopicId).toHaveBeenCalled();
expect(kafkaMock.createTopic).toHaveBeenCalledWith(TOPIC_ID, config);
expect(AxiosMock_1.default.post).toHaveBeenCalledWith(`${config.apiSeedUrls[0]}/needsForType/${TOPIC_ID}`, needFilterParams.serialize());
});
});
xdescribe('missions method', () => {
const missionParams1 = new MissionParams_1.default({
id: 'MISSION_ID_1',
neederDavId: 'DAV_ID',
vehicleId: 'DAV_ID',
price: '100',
});
const missionParams2 = new MissionParams_1.default({
id: 'MISSION_ID_2',
neederDavId: 'DAV_ID',
vehicleId: 'DAV_ID',
price: '100',
});
const missionParams3 = new MissionParams_1.default({
id: 'MISSION_ID_3',
neederDavId: 'DAV_ID',
vehicleId: 'DAV_ID',
price: '100',
});
beforeEach(() => {
jest.clearAllMocks();
jest.resetAllMocks();
jest.resetModules();
jest.doMock('axios', () => ({
default: AxiosMock_1.default,
}));
});
it('should receive missions and relevant functions', async () => {
const kafkaMessageStreamMock = {
filterType: jest.fn(() => common_types_1.Observable.from([missionParams1, missionParams2, missionParams3])),
};
const kafkaMock = {
generateTopicId: jest.fn(() => TOPIC_ID),
createTopic: jest.fn(() => Promise.resolve()),
messages: jest.fn(() => Promise.resolve(kafkaMessageStreamMock)),
};
jest.doMock('./KafkaMessageStream', () => ({
default: jest.fn().mockImplementation(() => kafkaMessageStreamMock),
}));
jest.doMock('./Kafka', () => ({ default: kafkaMock }));
// tslint:disable-next-line:variable-name
const Identity = (await Promise.resolve().then(() => require('./Identity'))).default;
const identity = new Identity('selfId', 'davId', config);
const spy = jest.fn();
const missions = await identity.missions(MissionParams_1.default);
missions.subscribe(spy);
await forContextSwitch();
expect(spy.mock.calls.length).toBe(3);
expect(spy.mock.calls[0][0]).toEqual(new Mission_1.default(TOPIC_ID, missionParams1.id, missionParams1, config));
expect(spy.mock.calls[1][0]).toEqual(new Mission_1.default(TOPIC_ID, missionParams2.id, missionParams2, config));
expect(kafkaMock.generateTopicId).toHaveBeenCalled();
expect(kafkaMock.createTopic).toHaveBeenCalledWith(TOPIC_ID, config);
});
it('should receive missions with specified topicId and relevant functions', async () => {
const kafkaMessageStreamMock = {
filterType: jest.fn(() => common_types_1.Observable.from([missionParams1, missionParams2, missionParams3])),
};
const kafkaMock = {
generateTopicId: jest.fn(() => TOPIC_ID),
createTopic: jest.fn(() => Promise.resolve()),
messages: jest.fn(() => Promise.resolve(kafkaMessageStreamMock)),
};
jest.doMock('./KafkaMessageStream', () => ({
default: jest.fn().mockImplementation(() => kafkaMessageStreamMock),
}));
jest.doMock('./Kafka', () => ({ default: kafkaMock }));
const anotherTopic = 'anotherTopic';
// tslint:disable-next-line:variable-name
const Identity = (await Promise.resolve().then(() => require('./Identity'))).default;
const identity = new Identity('selfId', 'davId', config);
const spy = jest.fn();
const missions = await identity.missions(MissionParams_1.default, anotherTopic);
missions.subscribe(spy);
await forContextSwitch();
expect(spy.mock.calls.length).toBe(3);
expect(spy.mock.calls[0][0]).toEqual(new Mission_1.default(anotherTopic, missionParams1.id, missionParams1, config));
expect(spy.mock.calls[1][0]).toEqual(new Mission_1.default(anotherTopic, missionParams2.id, missionParams2, config));
expect(kafkaMock.generateTopicId).not.toHaveBeenCalled();
expect(kafkaMock.createTopic).not.toHaveBeenCalled();
});
xit('should receive Kafka error event', async () => {
// kafkaMock.paramsStream.mockImplementation(() => Promise.resolve(Observable.fromPromise(Promise.reject(kafkaError))));
// tslint:disable-next-line:variable-name
const Identity = (await Promise.resolve().then(() => require('./Identity'))).default;
const identity = new Identity('selfId', 'davId', config);
const successSpy = jest.fn();
const errorSpy = jest.fn();
const missions = await identity.missions();
missions.subscribe(successSpy, errorSpy);
await forContextSwitch();
expect(successSpy.mock.calls.length).toBe(0);
expect(errorSpy.mock.calls.length).toBe(1);
expect(errorSpy.mock.calls[0][0]).toBe(kafkaError);
});
it('should fail due to topic creation failure', async () => {
const kafkaMock = {
generateTopicId: jest.fn(() => TOPIC_ID),
createTopic: jest.fn(() => Promise.reject(kafkaError)),
messages: jest.fn(),
};
jest.doMock('./Kafka', () => ({ default: kafkaMock }));
// tslint:disable-next-line:variable-name
const Identity = (await Promise.resolve().then(() => require('./Identity'))).default;
const identity = new Identity('selfId', 'davId', config);
await forContextSwitch();
await expect(identity.missions()).rejects.toThrow(`Fail to create a topic: ${kafkaError}`);
expect(kafkaMock.generateTopicId).toHaveBeenCalled();
expect(kafkaMock.createTopic).toHaveBeenCalledWith(TOPIC_ID, config);
expect(kafkaMock.messages).not.toHaveBeenCalled();
});
});
describe('need method', () => {
it('should succeed, validate need', async () => {
// tslint:disable-next-line:variable-name
const Identity = (await Promise.resolve().then(() => require('./Identity'))).default;
const identity = new Identity('selfId', 'davId', config);
const need = identity.need('needSelfId', needParams);
expect(need).toEqual(new Need_1.default('needSelfId', needParams, config));
});
});
describe('bid method', () => {
it('should succeed, validate bid', async () => {
// tslint:disable-next-line:variable-name
const Identity = (await Promise.resolve().then(() => require('./Identity'))).default;
const identity = new Identity('selfId', 'davId', config);
const bid = identity.bid('bidId', bidParams);
expect(bid).toEqual(new Bid_1.default('bidId', bidParams, config));
});
});
describe('mission method', () => {
it('should succeed, validate mission', async () => {
// tslint:disable-next-line:variable-name
const Identity = (await Promise.resolve().then(() => require('./Identity'))).default;
const identity = new Identity('selfId', 'davId', config);
const mission = identity.mission('missionSelfId', 'missionPeerId', missionParams);
expect(mission).toEqual(new Mission_1.default('missionSelfId', 'missionPeerId', missionParams, config));
});
});
describe('messages method', () => {
beforeAll(() => {
jest.doMock('axios', () => ({
default: AxiosMock_1.default,
}));
});
beforeEach(() => {
jest.clearAllMocks();
jest.resetAllMocks();
jest.resetModules();
});
xit('should receive message events', async () => {
const messageParams1 = new StatusRequestMessageParams_1.default({ senderId: 'SOURCE_ID_1' });
const messageParams2 = new StatusRequestMessageParams_1.default({ senderId: 'SOURCE_ID_2' });
const messageParams3 = new StatusRequestMessageParams_1.default({ senderId: 'SOURCE_ID_3' });
const kafkaMessageStreamMock = {
filterType: jest.fn(() => common_types_1.Observable.from([messageParams1, messageParams2, messageParams3])),
};
const kafkaMock = {
messages: jest.fn(() => Promise.resolve(kafkaMessageStreamMock)),
};
jest.doMock('./KafkaMessageStream', () => ({
default: jest.fn().mockImplementation(() => kafkaMessageStreamMock),
}));
jest.doMock('./Kafka', () => ({ default: kafkaMock }));
// tslint:disable-next-line:variable-name
const Identity = (await Promise.resolve().then(() => require('./Identity'))).default;
const identity = new Identity('selfId', 'davId', config);
const spy = jest.fn();
const messages = await identity.messages(StatusRequestMessageParams_1.default);
messages.subscribe(spy);
expect(spy.mock.calls.length).toBe(3);
expect(spy.mock.calls[0][0]).toEqual(new Message_1.default('selfId', messageParams1, config));
expect(spy.mock.calls[1][0]).toEqual(new Message_1.default('selfId', messageParams2, config));
expect(spy.mock.calls[2][0]).toEqual(new Message_1.default('selfId', messageParams3, config));
});
xit('should receive error event', async () => {
// kafkaMock.paramsStream.mockImplementation(() => Promise.resolve(Observable.fromPromise(Promise.reject(kafkaError))));
// tslint:disable-next-line:variable-name
const Identity = (await Promise.resolve().then(() => require('./Identity'))).default;
const identity = new Identity('selfId', 'davId', config);
const successSpy = jest.fn();
const errorSpy = jest.fn();
const messages = await identity.messages();
messages.subscribe(successSpy, errorSpy);
await forContextSwitch();
expect(successSpy.mock.calls.length).toBe(0);
expect(errorSpy).toHaveBeenCalled();
expect(errorSpy.mock.calls[0][0]).toBe(kafkaError);
});
});
});
//# sourceMappingURL=Identity.test.js.map