forked from moov-io/ach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batchCIE_test.go
426 lines (374 loc) · 13.3 KB
/
batchCIE_test.go
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright 2018 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package ach
import (
"testing"
"github.com/moov-io/base"
)
// mockBatchCIEHeader creates a BatchCIE BatchHeader
func mockBatchCIEHeader() *BatchHeader {
bh := NewBatchHeader()
bh.ServiceClassCode = CreditsOnly
bh.StandardEntryClassCode = CIE
bh.CompanyName = "Payee Name"
bh.CompanyIdentification = "121042882"
bh.CompanyEntryDescription = "ACH CIE"
bh.ODFIIdentification = "12104288"
return bh
}
// mockCIEEntryDetail creates a BatchCIE EntryDetail
func mockCIEEntryDetail() *EntryDetail {
entry := NewEntryDetail()
entry.TransactionCode = CheckingCredit
entry.SetRDFI("231380104")
entry.DFIAccountNumber = "744-5678-99"
entry.Amount = 25000
entry.IdentificationNumber = "45689033"
entry.IndividualName = "Receiver Account Name"
entry.SetTraceNumber(mockBatchCIEHeader().ODFIIdentification, 1)
entry.DiscretionaryData = "01"
entry.Category = CategoryForward
return entry
}
// mockBatchCIE creates a BatchCIE
func mockBatchCIE() *BatchCIE {
mockBatch := NewBatchCIE(mockBatchCIEHeader())
mockBatch.AddEntry(mockCIEEntryDetail())
mockBatch.GetEntries()[0].AddAddenda05(mockAddenda05())
mockBatch.Entries[0].AddendaRecordIndicator = 1
if err := mockBatch.Create(); err != nil {
panic(err)
}
return mockBatch
}
// testBatchCIEHeader creates a BatchCIE BatchHeader
func testBatchCIEHeader(t testing.TB) {
batch, _ := NewBatch(mockBatchCIEHeader())
err, ok := batch.(*BatchCIE)
if !ok {
t.Errorf("Expecting BatchCIE got %T", err)
}
}
// TestBatchCIEHeader tests validating BatchCIE BatchHeader
func TestBatchCIEHeader(t *testing.T) {
testBatchCIEHeader(t)
}
// BenchmarkBatchCIEHeader benchmarks validating BatchCIE BatchHeader
func BenchmarkBatchCIEHeader(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCIEHeader(b)
}
}
// testBatchCIECreate validates BatchCIE create
func testBatchCIECreate(t testing.TB) {
mockBatch := mockBatchCIE()
if err := mockBatch.Create(); err != nil {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCIECreate tests validating BatchCIE create
func TestBatchCIECreate(t *testing.T) {
testBatchCIECreate(t)
}
// BenchmarkBatchCIECreate benchmarks validating BatchCIE create
func BenchmarkBatchCIECreate(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCIECreate(b)
}
}
// testBatchCIEStandardEntryClassCode validates BatchCIE create for an invalid StandardEntryClassCode
func testBatchCIEStandardEntryClassCode(t testing.TB) {
mockBatch := mockBatchCIE()
mockBatch.Header.StandardEntryClassCode = WEB
err := mockBatch.Create()
if !base.Match(err, ErrBatchSECType) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCIEStandardEntryClassCode tests validating BatchCIE create for an invalid StandardEntryClassCode
func TestBatchCIEStandardEntryClassCode(t *testing.T) {
testBatchCIEStandardEntryClassCode(t)
}
// BenchmarkBatchCIEStandardEntryClassCode benchmarks validating BatchCIE create for an invalid StandardEntryClassCode
func BenchmarkBatchCIEStandardEntryClassCode(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCIEStandardEntryClassCode(b)
}
}
// testBatchCIEServiceClassCodeEquality validates service class code equality
func testBatchCIEServiceClassCodeEquality(t testing.TB) {
mockBatch := mockBatchCIE()
mockBatch.GetControl().ServiceClassCode = MixedDebitsAndCredits
err := mockBatch.Validate()
if !base.Match(err, NewErrBatchHeaderControlEquality(220, MixedDebitsAndCredits)) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCIEServiceClassCodeEquality tests validating service class code equality
func TestBatchCIEServiceClassCodeEquality(t *testing.T) {
testBatchCIEServiceClassCodeEquality(t)
}
// BenchmarkBatchCIEServiceClassCodeEquality benchmarks validating service class code equality
func BenchmarkBatchCIEServiceClassCodeEquality(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCIEServiceClassCodeEquality(b)
}
}
// testBatchCIEMixedCreditsAndDebits validates BatchCIE create for an invalid MixedCreditsAndDebits
func testBatchCIEMixedCreditsAndDebits(t testing.TB) {
mockBatch := mockBatchCIE()
mockBatch.Header.ServiceClassCode = MixedDebitsAndCredits
err := mockBatch.Validate()
if !base.Match(err, NewErrBatchHeaderControlEquality(MixedDebitsAndCredits, 220)) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCIEMixedCreditsAndDebits tests validating BatchCIE create for an invalid MixedCreditsAndDebits
func TestBatchCIEMixedCreditsAndDebits(t *testing.T) {
testBatchCIEMixedCreditsAndDebits(t)
}
// BenchmarkBatchCIEMixedCreditsAndDebits benchmarks validating BatchCIE create for an invalid MixedCreditsAndDebits
func BenchmarkBatchCIEMixedCreditsAndDebits(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCIEMixedCreditsAndDebits(b)
}
}
// testBatchCIEDebitsOnly validates BatchCIE create for an invalid DebitsOnly
func testBatchCIEDebitsOnly(t testing.TB) {
mockBatch := mockBatchCIE()
mockBatch.Header.ServiceClassCode = DebitsOnly
err := mockBatch.Validate()
if !base.Match(err, NewErrBatchHeaderControlEquality(DebitsOnly, 220)) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCIEDebitsOnly tests validating BatchCIE create for an invalid DebitsOnly
func TestBatchCIEDebitsOnly(t *testing.T) {
testBatchCIEDebitsOnly(t)
}
// BenchmarkBatchCIEDebitsOnly benchmarks validating BatchCIE create for an invalid DebitsOnly
func BenchmarkBatchCIEDebitsOnly(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCIEDebitsOnly(b)
}
}
// testBatchCIEAutomatedAccountingAdvices validates BatchCIE create for an invalid AutomatedAccountingAdvices
func testBatchCIEAutomatedAccountingAdvices(t testing.TB) {
mockBatch := mockBatchCIE()
mockBatch.Header.ServiceClassCode = AutomatedAccountingAdvices
err := mockBatch.Validate()
if !base.Match(err, NewErrBatchHeaderControlEquality(AutomatedAccountingAdvices, 220)) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCIEAutomatedAccountingAdvices tests validating BatchCIE create for an invalid AutomatedAccountingAdvices
func TestBatchCIEAutomatedAccountingAdvices(t *testing.T) {
testBatchCIEAutomatedAccountingAdvices(t)
}
// BenchmarkBatchCIEAutomatedAccountingAdvices benchmarks validating BatchCIE create for an invalid AutomatedAccountingAdvices
func BenchmarkBatchCIEAutomatedAccountingAdvices(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCIEAutomatedAccountingAdvices(b)
}
}
// testBatchCIETransactionCode validates BatchCIE TransactionCode is not a debit
func testBatchCIETransactionCode(t testing.TB) {
mockBatch := mockBatchCIE()
mockBatch.GetEntries()[0].TransactionCode = CheckingDebit
err := mockBatch.Create()
if !base.Match(err, ErrBatchDebitOnly) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCIETransactionCode tests validating BatchCIE TransactionCode is not a credit
func TestBatchCIETransactionCode(t *testing.T) {
testBatchCIETransactionCode(t)
}
// BenchmarkBatchCIETransactionCode benchmarks validating BatchCIE TransactionCode is not a credit
func BenchmarkBatchCIETransactionCode(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCIETransactionCode(b)
}
}
// testBatchCIEAddendaCount validates BatchCIE Addendum count of 2
func testBatchCIEAddendaCount(t testing.TB) {
mockBatch := mockBatchCIE()
mockBatch.GetEntries()[0].AddAddenda05(mockAddenda05())
err := mockBatch.Create()
if !base.Match(err, NewErrBatchRequiredAddendaCount(2, 1)) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCIEAddendaCount tests validating BatchCIE Addendum count of 2
func TestBatchCIEAddendaCount(t *testing.T) {
testBatchCIEAddendaCount(t)
}
// BenchmarkBatchCIEAddendaCount benchmarks validating BatchCIE Addendum count of 2
func BenchmarkBatchCIEAddendaCount(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCIEAddendaCount(b)
}
}
// testBatchCIEAddendaCountZero validates Addendum count of 0
func testBatchCIEAddendaCountZero(t testing.TB) {
mockBatch := NewBatchCIE(mockBatchCIEHeader())
mockBatch.AddEntry(mockCIEEntryDetail())
err := mockBatch.Create()
if !base.Match(err, NewErrBatchRequiredAddendaCount(0, 1)) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCIEAddendaCountZero tests validating Addendum count of 0
func TestBatchCIEAddendaCountZero(t *testing.T) {
testBatchCIEAddendaCountZero(t)
}
// BenchmarkBatchCIEAddendaCountZero benchmarks validating Addendum count of 0
func BenchmarkBatchCIEAddendaCountZero(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCIEAddendaCountZero(b)
}
}
// testBatchCIEInvalidAddendum validates Addendum must be Addenda05
func testBatchCIEInvalidAddendum(t testing.TB) {
mockBatch := NewBatchCIE(mockBatchCIEHeader())
mockBatch.AddEntry(mockCIEEntryDetail())
mockBatch.GetEntries()[0].Addenda02 = mockAddenda02()
mockBatch.Entries[0].AddendaRecordIndicator = 1
err := mockBatch.Create()
if !base.Match(err, NewErrBatchRequiredAddendaCount(0, 1)) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCIEInvalidAddendum tests validating Addendum must be Addenda05
func TestBatchCIEInvalidAddendum(t *testing.T) {
testBatchCIEInvalidAddendum(t)
}
// BenchmarkBatchCIEInvalidAddendum benchmarks validating Addendum must be Addenda05
func BenchmarkBatchCIEInvalidAddendum(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCIEInvalidAddendum(b)
}
}
// testBatchCIEInvalidAddenda validates Addendum must be Addenda05 with record type 7
func testBatchCIEInvalidAddenda(t testing.TB) {
mockBatch := NewBatchCIE(mockBatchCIEHeader())
mockBatch.AddEntry(mockCIEEntryDetail())
addenda05 := mockAddenda05()
addenda05.recordType = "63"
mockBatch.GetEntries()[0].AddAddenda05(addenda05)
mockBatch.Entries[0].AddendaRecordIndicator = 1
err := mockBatch.Create()
if !base.Match(err, NewErrRecordType(7)) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCIEInvalidAddenda tests validating Addendum must be Addenda05 with record type 7
func TestBatchCIEInvalidAddenda(t *testing.T) {
testBatchCIEInvalidAddenda(t)
}
// BenchmarkBatchCIEInvalidAddenda benchmarks validating Addendum must be Addenda05 with record type 7
func BenchmarkBatchCIEInvalidAddenda(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCIEInvalidAddenda(b)
}
}
// testBatchCIEInvalidBuild validates an invalid batch build
func testBatchCIEInvalidBuild(t testing.TB) {
mockBatch := mockBatchCIE()
mockBatch.GetHeader().recordType = "3"
err := mockBatch.Create()
if !base.Match(err, NewErrRecordType(5)) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCIEInvalidBuild tests validating an invalid batch build
func TestBatchCIEInvalidBuild(t *testing.T) {
testBatchCIEInvalidBuild(t)
}
// BenchmarkBatchCIEInvalidBuild benchmarks validating an invalid batch build
func BenchmarkBatchCIEInvalidBuild(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCIEInvalidBuild(b)
}
}
// testBatchCIECardTransactionType validates BatchCIE create for an invalid CardTransactionType
func testBatchCIECardTransactionType(t testing.TB) {
mockBatch := mockBatchCIE()
mockBatch.GetEntries()[0].DiscretionaryData = "555"
err := mockBatch.Validate()
// TODO: are we not expecting any errors here?
if !base.Match(err, nil) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCIECardTransactionType tests validating BatchCIE create for an invalid CardTransactionType
func TestBatchCIECardTransactionType(t *testing.T) {
testBatchCIECardTransactionType(t)
}
// BenchmarkBatchCIECardTransactionType benchmarks validating BatchCIE create for an invalid CardTransactionType
func BenchmarkBatchCIECardTransactionType(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCIECardTransactionType(b)
}
}
// TestBatchCIEAddendum98 validates Addenda98 returns an error
func TestBatchCIEAddendum98(t *testing.T) {
mockBatch := NewBatchCIE(mockBatchCIEHeader())
mockBatch.AddEntry(mockCIEEntryDetail())
mockAddenda98 := mockAddenda98()
mockAddenda98.TypeCode = "05"
mockBatch.GetEntries()[0].Category = CategoryNOC
mockBatch.GetEntries()[0].Addenda98 = mockAddenda98
err := mockBatch.Create()
if !base.Match(err, ErrAddendaTypeCode) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCIEAddendum99 validates Addenda99 returns an error
func TestBatchCIEAddendum99(t *testing.T) {
mockBatch := NewBatchCIE(mockBatchCIEHeader())
mockBatch.AddEntry(mockCIEEntryDetail())
mockAddenda99 := mockAddenda99()
mockAddenda99.TypeCode = "05"
mockBatch.GetEntries()[0].Category = CategoryReturn
mockBatch.GetEntries()[0].Addenda99 = mockAddenda99
err := mockBatch.Create()
if !base.Match(err, ErrAddendaTypeCode) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCIEAddenda validates no more than 1 addenda record per entry detail record can exist
func TestBatchCIEAddenda(t *testing.T) {
mockBatch := mockBatchCIE()
// mock batch already has one addenda. Creating two addenda should error
mockBatch.GetEntries()[0].AddAddenda05(mockAddenda05())
err := mockBatch.Create()
if !base.Match(err, NewErrBatchRequiredAddendaCount(2, 1)) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCIEAddenda02 validates BatchCIE cannot have Addenda02
func TestBatchCIEAddenda02(t *testing.T) {
mockBatch := mockBatchCIE()
mockBatch.Entries[0].AddendaRecordIndicator = 1
mockBatch.GetEntries()[0].Addenda02 = mockAddenda02()
err := mockBatch.Create()
if !base.Match(err, ErrBatchAddendaCategory) {
t.Errorf("%T: %s", err, err)
}
}