|
1
|
|
|
import {mock, instance, when, verify, anything, deepEqual} from 'ts-mockito'; |
|
2
|
|
|
import {CustomerRepository} from 'src/Infrastructure/Customer/Repository/CustomerRepository'; |
|
3
|
|
|
import {DailyRateRepository} from 'src/Infrastructure/Billing/Repository/DailyRateRepository'; |
|
4
|
|
|
import {CreateDailyRateCommandHandler} from './CreateDailyRateCommandHandler'; |
|
5
|
|
|
import {CreateDailyRateCommand} from './CreateDailyRateCommand'; |
|
6
|
|
|
import {User} from 'src/Domain/User/User.entity'; |
|
7
|
|
|
import {Customer} from 'src/Domain/Customer/Customer.entity'; |
|
8
|
|
|
import {TaskRepository} from 'src/Infrastructure/Task/Repository/TaskRepository'; |
|
9
|
|
|
import {UserRepository} from 'src/Infrastructure/User/Repository/UserRepository'; |
|
10
|
|
|
import {IsDailyRateAlreadyExist} from 'src/Domain/Billing/Specification/IsDailyRateAlreadyExist'; |
|
11
|
|
|
import {UserNotFoundException} from 'src/Domain/User/Exception/UserNotFoundException'; |
|
12
|
|
|
import {CustomerNotFoundException} from 'src/Domain/Customer/Exception/CustomerNotFoundException'; |
|
13
|
|
|
import {TaskNotFoundException} from 'src/Domain/Task/Exception/TaskNotFoundException'; |
|
14
|
|
|
import {Task} from 'src/Domain/Task/Task.entity'; |
|
15
|
|
|
import {DailyRateAlreadyExistException} from 'src/Domain/Billing/Exception/DailyRateAlreadyExistException'; |
|
16
|
|
|
import {DailyRate} from 'src/Domain/Billing/DailyRate.entity'; |
|
17
|
|
|
|
|
18
|
|
|
describe('CreateDailyRateCommandHandler', () => { |
|
19
|
|
|
let dailyRateRepository: DailyRateRepository; |
|
20
|
|
|
let customerRepository: CustomerRepository; |
|
21
|
|
|
let taskRepository: TaskRepository; |
|
22
|
|
|
let userRepository: UserRepository; |
|
23
|
|
|
let isDailyRateAlreadyExist: IsDailyRateAlreadyExist; |
|
24
|
|
|
|
|
25
|
|
|
let handler: CreateDailyRateCommandHandler; |
|
26
|
|
|
|
|
27
|
|
|
const user = mock(User); |
|
28
|
|
|
const task = mock(Task); |
|
29
|
|
|
const customer = mock(Customer); |
|
30
|
|
|
|
|
31
|
|
|
const command = new CreateDailyRateCommand( |
|
32
|
|
|
100, |
|
33
|
|
|
'd36bbd74-f753-4d8f-940c-d4a6b4fd0957', |
|
34
|
|
|
'a491ccc9-df7c-4fc6-8e90-db816208f689', |
|
35
|
|
|
'3d0a282f-3b3e-4ef3-948f-5ab3cb77a04c' |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
beforeEach(() => { |
|
39
|
|
|
dailyRateRepository = mock(DailyRateRepository); |
|
40
|
|
|
customerRepository = mock(CustomerRepository); |
|
41
|
|
|
taskRepository = mock(TaskRepository); |
|
42
|
|
|
userRepository = mock(UserRepository); |
|
43
|
|
|
isDailyRateAlreadyExist = mock(IsDailyRateAlreadyExist); |
|
44
|
|
|
|
|
45
|
|
|
handler = new CreateDailyRateCommandHandler( |
|
46
|
|
|
instance(dailyRateRepository), |
|
47
|
|
|
instance(customerRepository), |
|
48
|
|
|
instance(taskRepository), |
|
49
|
|
|
instance(userRepository), |
|
50
|
|
|
instance(isDailyRateAlreadyExist) |
|
51
|
|
|
); |
|
52
|
|
|
}); |
|
53
|
|
|
|
|
54
|
|
|
it('testUserNotFound', async () => { |
|
55
|
|
|
when( |
|
56
|
|
|
userRepository.findOneById('d36bbd74-f753-4d8f-940c-d4a6b4fd0957') |
|
57
|
|
|
).thenResolve(null); |
|
58
|
|
|
|
|
59
|
|
|
try { |
|
60
|
|
|
await handler.execute(command); |
|
61
|
|
|
} catch (e) { |
|
62
|
|
|
expect(e).toBeInstanceOf(UserNotFoundException); |
|
63
|
|
|
expect(e.message).toBe('user.errors.not_found'); |
|
64
|
|
|
verify( |
|
65
|
|
|
userRepository.findOneById('d36bbd74-f753-4d8f-940c-d4a6b4fd0957') |
|
66
|
|
|
).once(); |
|
67
|
|
|
verify(customerRepository.findOneById(anything())).never(); |
|
68
|
|
|
verify(taskRepository.findOneById(anything())).never(); |
|
69
|
|
|
verify( |
|
70
|
|
|
isDailyRateAlreadyExist.isSatisfiedBy( |
|
71
|
|
|
anything(), |
|
72
|
|
|
anything(), |
|
73
|
|
|
anything() |
|
74
|
|
|
) |
|
75
|
|
|
).never(); |
|
76
|
|
|
verify(dailyRateRepository.save(anything())).never(); |
|
77
|
|
|
} |
|
78
|
|
|
}); |
|
79
|
|
|
|
|
80
|
|
|
it('testCustomerNotFound', async () => { |
|
81
|
|
|
when( |
|
82
|
|
|
userRepository.findOneById('d36bbd74-f753-4d8f-940c-d4a6b4fd0957') |
|
83
|
|
|
).thenResolve(instance(user)); |
|
84
|
|
|
when( |
|
85
|
|
|
customerRepository.findOneById('a491ccc9-df7c-4fc6-8e90-db816208f689') |
|
86
|
|
|
).thenResolve(null); |
|
87
|
|
|
|
|
88
|
|
|
try { |
|
89
|
|
|
await handler.execute(command); |
|
90
|
|
|
} catch (e) { |
|
91
|
|
|
expect(e).toBeInstanceOf(CustomerNotFoundException); |
|
92
|
|
|
expect(e.message).toBe('customer.errors.not_found'); |
|
93
|
|
|
verify( |
|
94
|
|
|
userRepository.findOneById('d36bbd74-f753-4d8f-940c-d4a6b4fd0957') |
|
95
|
|
|
).once(); |
|
96
|
|
|
verify( |
|
97
|
|
|
customerRepository.findOneById('a491ccc9-df7c-4fc6-8e90-db816208f689') |
|
98
|
|
|
).once(); |
|
99
|
|
|
verify(taskRepository.findOneById(anything())).never(); |
|
100
|
|
|
verify( |
|
101
|
|
|
isDailyRateAlreadyExist.isSatisfiedBy( |
|
102
|
|
|
anything(), |
|
103
|
|
|
anything(), |
|
104
|
|
|
anything() |
|
105
|
|
|
) |
|
106
|
|
|
).never(); |
|
107
|
|
|
verify(dailyRateRepository.save(anything())).never(); |
|
108
|
|
|
} |
|
109
|
|
|
}); |
|
110
|
|
|
|
|
111
|
|
|
it('testTaskNotFound', async () => { |
|
112
|
|
|
when( |
|
113
|
|
|
userRepository.findOneById('d36bbd74-f753-4d8f-940c-d4a6b4fd0957') |
|
114
|
|
|
).thenResolve(instance(user)); |
|
115
|
|
|
when( |
|
116
|
|
|
customerRepository.findOneById('a491ccc9-df7c-4fc6-8e90-db816208f689') |
|
117
|
|
|
).thenResolve(instance(customer)); |
|
118
|
|
|
when( |
|
119
|
|
|
taskRepository.findOneById('3d0a282f-3b3e-4ef3-948f-5ab3cb77a04c') |
|
120
|
|
|
).thenResolve(null); |
|
121
|
|
|
|
|
122
|
|
|
try { |
|
123
|
|
|
await handler.execute(command); |
|
124
|
|
|
} catch (e) { |
|
125
|
|
|
expect(e).toBeInstanceOf(TaskNotFoundException); |
|
126
|
|
|
expect(e.message).toBe('task.errors.not_found'); |
|
127
|
|
|
verify( |
|
128
|
|
|
userRepository.findOneById('d36bbd74-f753-4d8f-940c-d4a6b4fd0957') |
|
129
|
|
|
).once(); |
|
130
|
|
|
verify( |
|
131
|
|
|
customerRepository.findOneById('a491ccc9-df7c-4fc6-8e90-db816208f689') |
|
132
|
|
|
).once(); |
|
133
|
|
|
verify( |
|
134
|
|
|
taskRepository.findOneById('3d0a282f-3b3e-4ef3-948f-5ab3cb77a04c') |
|
135
|
|
|
).once(); |
|
136
|
|
|
verify( |
|
137
|
|
|
isDailyRateAlreadyExist.isSatisfiedBy( |
|
138
|
|
|
anything(), |
|
139
|
|
|
anything(), |
|
140
|
|
|
anything() |
|
141
|
|
|
) |
|
142
|
|
|
).never(); |
|
143
|
|
|
verify(dailyRateRepository.save(anything())).never(); |
|
144
|
|
|
} |
|
145
|
|
|
}); |
|
146
|
|
|
|
|
147
|
|
|
it('testDailyRateAlreadyExist', async () => { |
|
148
|
|
|
when( |
|
149
|
|
|
userRepository.findOneById('d36bbd74-f753-4d8f-940c-d4a6b4fd0957') |
|
150
|
|
|
).thenResolve(instance(user)); |
|
151
|
|
|
when( |
|
152
|
|
|
customerRepository.findOneById('a491ccc9-df7c-4fc6-8e90-db816208f689') |
|
153
|
|
|
).thenResolve(instance(customer)); |
|
154
|
|
|
when( |
|
155
|
|
|
taskRepository.findOneById('3d0a282f-3b3e-4ef3-948f-5ab3cb77a04c') |
|
156
|
|
|
).thenResolve(instance(task)); |
|
157
|
|
|
when( |
|
158
|
|
|
isDailyRateAlreadyExist.isSatisfiedBy( |
|
159
|
|
|
instance(user), |
|
160
|
|
|
instance(task), |
|
161
|
|
|
instance(customer) |
|
162
|
|
|
) |
|
163
|
|
|
).thenResolve(true); |
|
164
|
|
|
try { |
|
165
|
|
|
await handler.execute(command); |
|
166
|
|
|
} catch (e) { |
|
167
|
|
|
expect(e).toBeInstanceOf(DailyRateAlreadyExistException); |
|
168
|
|
|
expect(e.message).toBe('billing.errors.daily_rate_already_exist'); |
|
169
|
|
|
verify( |
|
170
|
|
|
userRepository.findOneById('d36bbd74-f753-4d8f-940c-d4a6b4fd0957') |
|
171
|
|
|
).once(); |
|
172
|
|
|
verify( |
|
173
|
|
|
customerRepository.findOneById('a491ccc9-df7c-4fc6-8e90-db816208f689') |
|
174
|
|
|
).once(); |
|
175
|
|
|
verify( |
|
176
|
|
|
taskRepository.findOneById('3d0a282f-3b3e-4ef3-948f-5ab3cb77a04c') |
|
177
|
|
|
).once(); |
|
178
|
|
|
verify( |
|
179
|
|
|
isDailyRateAlreadyExist.isSatisfiedBy( |
|
180
|
|
|
instance(user), |
|
181
|
|
|
instance(task), |
|
182
|
|
|
instance(customer) |
|
183
|
|
|
) |
|
184
|
|
|
).once(); |
|
185
|
|
|
verify(dailyRateRepository.save(anything())).never(); |
|
186
|
|
|
} |
|
187
|
|
|
}); |
|
188
|
|
|
|
|
189
|
|
|
it('testDailyRateSuccessfullyCreated', async () => { |
|
190
|
|
|
const savedDailyRate = mock(DailyRate); |
|
191
|
|
|
|
|
192
|
|
|
when(savedDailyRate.getId()).thenReturn( |
|
193
|
|
|
'62016719-cb62-4805-bfac-9540508ab942' |
|
194
|
|
|
); |
|
195
|
|
|
when( |
|
196
|
|
|
userRepository.findOneById('d36bbd74-f753-4d8f-940c-d4a6b4fd0957') |
|
197
|
|
|
).thenResolve(instance(user)); |
|
198
|
|
|
when( |
|
199
|
|
|
customerRepository.findOneById('a491ccc9-df7c-4fc6-8e90-db816208f689') |
|
200
|
|
|
).thenResolve(instance(customer)); |
|
201
|
|
|
when( |
|
202
|
|
|
taskRepository.findOneById('3d0a282f-3b3e-4ef3-948f-5ab3cb77a04c') |
|
203
|
|
|
).thenResolve(instance(task)); |
|
204
|
|
|
when( |
|
205
|
|
|
isDailyRateAlreadyExist.isSatisfiedBy( |
|
206
|
|
|
instance(user), |
|
207
|
|
|
instance(task), |
|
208
|
|
|
instance(customer) |
|
209
|
|
|
) |
|
210
|
|
|
).thenResolve(false); |
|
211
|
|
|
when( |
|
212
|
|
|
dailyRateRepository.save( |
|
213
|
|
|
deepEqual( |
|
214
|
|
|
new DailyRate( |
|
215
|
|
|
10000, |
|
216
|
|
|
instance(user), |
|
217
|
|
|
instance(customer), |
|
218
|
|
|
instance(task) |
|
219
|
|
|
) |
|
220
|
|
|
) |
|
221
|
|
|
) |
|
222
|
|
|
).thenResolve(instance(savedDailyRate)); |
|
223
|
|
|
|
|
224
|
|
|
expect(await handler.execute(command)).toBe( |
|
225
|
|
|
'62016719-cb62-4805-bfac-9540508ab942' |
|
226
|
|
|
); |
|
227
|
|
|
|
|
228
|
|
|
verify( |
|
229
|
|
|
userRepository.findOneById('d36bbd74-f753-4d8f-940c-d4a6b4fd0957') |
|
230
|
|
|
).once(); |
|
231
|
|
|
verify( |
|
232
|
|
|
customerRepository.findOneById('a491ccc9-df7c-4fc6-8e90-db816208f689') |
|
233
|
|
|
).once(); |
|
234
|
|
|
verify( |
|
235
|
|
|
taskRepository.findOneById('3d0a282f-3b3e-4ef3-948f-5ab3cb77a04c') |
|
236
|
|
|
).once(); |
|
237
|
|
|
verify( |
|
238
|
|
|
isDailyRateAlreadyExist.isSatisfiedBy( |
|
239
|
|
|
instance(user), |
|
240
|
|
|
instance(task), |
|
241
|
|
|
instance(customer) |
|
242
|
|
|
) |
|
243
|
|
|
).once(); |
|
244
|
|
|
verify( |
|
245
|
|
|
dailyRateRepository.save( |
|
246
|
|
|
deepEqual( |
|
247
|
|
|
new DailyRate( |
|
248
|
|
|
10000, |
|
249
|
|
|
instance(user), |
|
250
|
|
|
instance(customer), |
|
251
|
|
|
instance(task) |
|
252
|
|
|
) |
|
253
|
|
|
) |
|
254
|
|
|
) |
|
255
|
|
|
).once(); |
|
256
|
|
|
}); |
|
257
|
|
|
}); |
|
258
|
|
|
|