forked from DAVFoundation/dav-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mission.test.ts
299 lines (280 loc) · 9.41 KB
/
Mission.test.ts
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
import Config from './Config';
import MessageParams from './drone-charging/MessageParams';
import Bid from './Bid';
import BidParams from './drone-charging/BidParams';
import MissionParams from './drone-charging/MissionParams';
import { PriceType } from './common-enums';
import Price from './Price';
import { Observable, DavID, ID } from './common-types';
import Message from './Message';
describe('Mission class', () => {
const configuration = new Config({});
const bidParams = new BidParams({
id: 'BID_TOPIC_ID',
price: new Price('3', PriceType.flat),
vehicleId: '34',
});
const missionParams = new MissionParams({
id: 'SOURCE_ID_1',
neederDavId: 'DAV_ID',
vehicleId: 'DAV_ID',
price: new Price('100', PriceType.flat),
});
const selfId = 'selfId';
const bid = new Bid(selfId, bidParams, configuration);
const kafkaError = { msg: 'KAFKA_ERROR' };
const forContextSwitch = () => {
return new Promise((resolve, reject) => {
jest.useRealTimers();
setTimeout(resolve, 0);
jest.useFakeTimers();
});
};
describe('sendMessage method', () => {
beforeEach(() => {
jest.resetAllMocks();
jest.resetModules();
});
const kafkaMessageStreamMock = {
filterType: jest.fn(() =>
Observable.from([new MessageParams({ senderId: 'SOURCE_ID_1' })]),
),
};
it('should succeed, validate kafka mock send message', async () => {
const kafkaMock = {
sendParams: () => Promise.resolve(true),
};
jest.doMock('./Kafka', () => ({ default: kafkaMock }));
// tslint:disable-next-line:variable-name
const Mission: any = (await import('./Mission')).default;
const mission = new Mission(
selfId,
missionParams.id,
missionParams,
configuration,
);
await expect(
mission.sendMessage(new MessageParams({})),
).resolves.toBeDefined();
});
it('should fail due to kafka exception', async () => {
const kafkaMock = {
sendParams: () => Promise.reject(kafkaError),
};
jest.doMock('./Kafka', () => ({ default: kafkaMock }));
// tslint:disable-next-line:variable-name
const Mission: any = (await import('./Mission')).default;
const mission = new Mission(
selfId,
missionParams.id,
missionParams,
configuration,
);
await expect(mission.sendMessage(new MessageParams({}))).rejects.toBe(
kafkaError,
);
});
it('should call to Kafka sendParams', async () => {
const kafkaMock = {
sendParams: jest.fn(params => Promise.resolve(true)),
};
jest.doMock('./Kafka', () => ({ default: kafkaMock }));
// tslint:disable-next-line:variable-name
const Mission: any = (await import('./Mission')).default;
const mission = new Mission(
selfId,
missionParams.id,
missionParams,
configuration,
);
const messageParams = new MessageParams({});
await mission.sendMessage(messageParams);
expect(kafkaMock.sendParams).toHaveBeenCalledWith(
missionParams.id,
messageParams,
configuration,
);
});
xit('test send for consumer', async () => {
const kafkaMock = {
messages: jest.fn(() => Promise.resolve(kafkaMessageStreamMock)),
sendParams: () => Promise.resolve(true),
};
// tslint:disable-next-line:variable-name
const Mission: any = (await import('./Mission')).default;
const mission = new Mission(
missionParams.id,
null,
missionParams,
configuration,
);
const messageParams = new MessageParams({});
// TODO
});
});
describe('messages method', () => {
beforeEach(() => {
jest.resetAllMocks();
jest.resetModules();
});
it('should receive message events', async () => {
const messageParams1 = new MessageParams({ senderId: 'SOURCE_ID_1' });
const messageParams2 = new MessageParams({ senderId: 'SOURCE_ID_2' });
const messageParams3 = new MessageParams({ senderId: 'SOURCE_ID_3' });
const kafkaMessageStreamMock = {
filterType: jest.fn(() =>
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 Mission: any = (await import('./Mission')).default;
const mission = new Mission(selfId, null, missionParams, configuration);
const spy = jest.fn();
const messages = await mission.messages();
messages.subscribe(spy);
expect(spy.mock.calls.length).toBe(3);
expect(spy.mock.calls[0][0]).toEqual(
new Message(selfId, messageParams1, configuration),
);
expect(spy.mock.calls[1][0]).toEqual(
new Message(selfId, messageParams2, configuration),
);
expect(spy.mock.calls[2][0]).toEqual(
new Message(selfId, messageParams3, configuration),
);
});
xit('should receive error event', async () => {
jest.doMock('./Kafka', () => ({
default: {
paramsStream: async () =>
Observable.fromPromise(Promise.reject(kafkaError)),
},
}));
// tslint:disable-next-line:variable-name
const Mission: any = (await import('./Mission')).default;
const mission = new Mission(
missionParams.id,
missionParams,
configuration,
);
const successSpy = jest.fn();
const errorSpy = jest.fn();
const messages = await mission.messages();
messages.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);
});
});
describe('signContract method', () => {
const privateKey = 'valid private key';
const contractsMock = {
approveMission: jest.fn((dav: DavID, key: string, configParam: Config) =>
Promise.resolve(''),
),
startMission: jest.fn(
(
missionId: ID,
dav: DavID,
key: string,
vId: DavID,
price: string,
configParam: Config,
) => Promise.resolve(''),
),
};
beforeAll(() => {
jest.resetAllMocks();
jest.resetModules();
jest.doMock('./Contracts', () => ({ default: contractsMock }));
});
it('should not throw any errors when get valid input and no errors', async () => {
// tslint:disable-next-line:variable-name
const Mission: any = (await import('./Mission')).default;
const mission = new Mission(
missionParams.id,
null,
missionParams,
configuration,
);
await mission.signContract(privateKey);
expect(contractsMock.startMission).toHaveBeenCalledWith(
missionParams.id,
missionParams.neederDavId,
privateKey,
missionParams.vehicleId,
missionParams.price,
configuration,
);
});
it('should fail due to blockchain exception', async () => {
const web3Error = { msg: 'WEB3_ERROR' };
contractsMock.startMission.mockImplementation(() =>
Promise.reject(web3Error),
);
jest.doMock('./Contracts', () => ({ default: contractsMock }));
// tslint:disable-next-line:variable-name
const Mission: any = (await import('./Mission')).default;
const mission = new Mission(
missionParams.id,
null,
missionParams,
configuration,
);
await expect(mission.signContract(privateKey)).rejects.toThrow(
`Fail to sign contract ${web3Error}`,
);
});
});
describe('finalizeMission method', () => {
const WALLET_PRIVATE_KEY = 'WALLET_PRIVET_KEY';
beforeEach(() => {
jest.resetAllMocks();
jest.resetModules();
});
it('should succeed with finalize mission transaction receipt', async () => {
const transactionReceipt = { transactionHash: 'TRANSACTION_HASH' };
const contractsMock = {
finalizeMission: () => Promise.resolve(transactionReceipt),
};
jest.doMock('./Contracts', () => ({ default: contractsMock }));
// tslint:disable-next-line:variable-name
const Mission: any = (await import('./Mission')).default;
const mission = new Mission(
missionParams.id,
null,
missionParams,
configuration,
);
await expect(mission.finalizeMission(WALLET_PRIVATE_KEY)).resolves.toBe(
transactionReceipt,
);
});
it('should fail due to blockchain exception', async () => {
const web3Error = { msg: 'WEB3_ERROR' };
const contractsMock = {
finalizeMission: () => Promise.reject(web3Error),
};
jest.doMock('./Contracts', () => ({ default: contractsMock }));
// tslint:disable-next-line:variable-name
const Mission: any = (await import('./Mission')).default;
const mission = new Mission(
missionParams.id,
null,
missionParams,
configuration,
);
await expect(mission.finalizeMission(WALLET_PRIVATE_KEY)).rejects.toThrow(
`Fail to finalize mission ${web3Error}`,
);
});
});
});