-
Notifications
You must be signed in to change notification settings - Fork 151
/
batchTRC_test.go
442 lines (392 loc) · 14.2 KB
/
batchTRC_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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Licensed to The Moov Authors under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. The Moov Authors licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package ach
import (
"testing"
"github.com/moov-io/base"
"github.com/stretchr/testify/require"
)
// mockBatchTRCHeader creates a BatchTRC BatchHeader
func mockBatchTRCHeader() *BatchHeader {
bh := NewBatchHeader()
bh.ServiceClassCode = DebitsOnly
bh.StandardEntryClassCode = TRC
bh.CompanyName = "Payee Name"
bh.CompanyIdentification = "121042882"
bh.CompanyEntryDescription = TRC
bh.ODFIIdentification = "12104288"
return bh
}
// mockTRCEntryDetail creates a BatchTRC EntryDetail
func mockTRCEntryDetail() *EntryDetail {
entry := NewEntryDetail()
entry.TransactionCode = CheckingDebit
entry.SetRDFI("231380104")
entry.DFIAccountNumber = "744-5678-99"
entry.Amount = 25000
entry.SetCheckSerialNumber("123456789")
entry.SetProcessControlField("CHECK1")
entry.SetItemResearchNumber("182726")
entry.SetItemTypeIndicator("01")
entry.SetTraceNumber(mockBatchTRCHeader().ODFIIdentification, 1)
entry.Category = CategoryForward
return entry
}
// mockBatchTRC creates a BatchTRC
func mockBatchTRC(t testing.TB) *BatchTRC {
mockBatch := NewBatchTRC(mockBatchTRCHeader())
mockBatch.AddEntry(mockTRCEntryDetail())
require.NoError(t, mockBatch.Create())
return mockBatch
}
// mockBatchTRCHeaderCredit creates a BatchTRC BatchHeader
func mockBatchTRCHeaderCredit() *BatchHeader {
bh := NewBatchHeader()
bh.ServiceClassCode = DebitsOnly
bh.StandardEntryClassCode = TRC
bh.CompanyName = "Payee Name"
bh.CompanyIdentification = "121042882"
bh.CompanyEntryDescription = TRC
bh.ODFIIdentification = "12104288"
return bh
}
// mockTRCEntryDetailCredit creates a TRC EntryDetail with a credit entry
func mockTRCEntryDetailCredit() *EntryDetail {
entry := NewEntryDetail()
entry.TransactionCode = CheckingCredit
entry.SetRDFI("231380104")
entry.DFIAccountNumber = "744-5678-99"
entry.Amount = 25000
entry.SetCheckSerialNumber("123456789")
entry.SetProcessControlField("CHECK1")
entry.SetItemResearchNumber("182726")
entry.SetTraceNumber(mockBatchTRCHeader().ODFIIdentification, 1)
entry.Category = CategoryForward
return entry
}
// mockBatchTRCCredit creates a BatchTRC with a Credit entry
func mockBatchTRCCredit() *BatchTRC {
mockBatch := NewBatchTRC(mockBatchTRCHeaderCredit())
mockBatch.AddEntry(mockTRCEntryDetailCredit())
return mockBatch
}
// testBatchTRCHeader creates a BatchTRC BatchHeader
func testBatchTRCHeader(t testing.TB) {
batch, _ := NewBatch(mockBatchTRCHeader())
err, ok := batch.(*BatchTRC)
if !ok {
t.Errorf("Expecting BatchTRC got %T", err)
}
}
// TestBatchTRCHeader tests validating BatchTRC BatchHeader
func TestBatchTRCHeader(t *testing.T) {
testBatchTRCHeader(t)
}
// BenchmarkBatchTRCHeader benchmarks validating BatchTRC BatchHeader
func BenchmarkBatchTRCHeader(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchTRCHeader(b)
}
}
// testBatchTRCCreate validates BatchTRC create
func testBatchTRCCreate(t testing.TB) {
mockBatch := mockBatchTRC(t)
if err := mockBatch.Create(); err != nil {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchTRCCreate tests validating BatchTRC create
func TestBatchTRCCreate(t *testing.T) {
testBatchTRCCreate(t)
}
// BenchmarkBatchTRCCreate benchmarks validating BatchTRC create
func BenchmarkBatchTRCCreate(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchTRCCreate(b)
}
}
// testBatchTRCStandardEntryClassCode validates BatchTRC create for an invalid StandardEntryClassCode
func testBatchTRCStandardEntryClassCode(t testing.TB) {
mockBatch := mockBatchTRC(t)
mockBatch.Header.StandardEntryClassCode = WEB
err := mockBatch.Create()
if !base.Match(err, ErrBatchSECType) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchTRCStandardEntryClassCode tests validating BatchTRC create for an invalid StandardEntryClassCode
func TestBatchTRCStandardEntryClassCode(t *testing.T) {
testBatchTRCStandardEntryClassCode(t)
}
// BenchmarkBatchTRCStandardEntryClassCode benchmarks validating BatchTRC create for an invalid StandardEntryClassCode
func BenchmarkBatchTRCStandardEntryClassCode(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchTRCStandardEntryClassCode(b)
}
}
// testBatchTRCServiceClassCodeEquality validates service class code equality
func testBatchTRCServiceClassCodeEquality(t testing.TB) {
mockBatch := mockBatchTRC(t)
mockBatch.GetControl().ServiceClassCode = MixedDebitsAndCredits
err := mockBatch.Validate()
if !base.Match(err, NewErrBatchHeaderControlEquality(220, MixedDebitsAndCredits)) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchTRCServiceClassCodeEquality tests validating service class code equality
func TestBatchTRCServiceClassCodeEquality(t *testing.T) {
testBatchTRCServiceClassCodeEquality(t)
}
// BenchmarkBatchTRCServiceClassCodeEquality benchmarks validating service class code equality
func BenchmarkBatchTRCServiceClassCodeEquality(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchTRCServiceClassCodeEquality(b)
}
}
// testBatchTRCMixedCreditsAndDebits validates BatchTRC create for an invalid MixedCreditsAndDebits
func testBatchTRCMixedCreditsAndDebits(t testing.TB) {
mockBatch := mockBatchTRC(t)
mockBatch.Header.ServiceClassCode = MixedDebitsAndCredits
err := mockBatch.Validate()
if !base.Match(err, NewErrBatchHeaderControlEquality(MixedDebitsAndCredits, 225)) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchTRCMixedCreditsAndDebits tests validating BatchTRC create for an invalid MixedCreditsAndDebits
func TestBatchTRCMixedCreditsAndDebits(t *testing.T) {
testBatchTRCMixedCreditsAndDebits(t)
}
// BenchmarkBatchTRCMixedCreditsAndDebits benchmarks validating BatchTRC create for an invalid MixedCreditsAndDebits
func BenchmarkBatchTRCMixedCreditsAndDebits(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchTRCMixedCreditsAndDebits(b)
}
}
// testBatchTRCCreditsOnly validates BatchTRC create for an invalid CreditsOnly
func testBatchTRCCreditsOnly(t testing.TB) {
mockBatch := mockBatchTRC(t)
mockBatch.Header.ServiceClassCode = CreditsOnly
err := mockBatch.Validate()
if !base.Match(err, NewErrBatchHeaderControlEquality(CreditsOnly, 225)) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchTRCCreditsOnly tests validating BatchTRC create for an invalid CreditsOnly
func TestBatchTRCCreditsOnly(t *testing.T) {
testBatchTRCCreditsOnly(t)
}
// BenchmarkBatchTRCCreditsOnly benchmarks validating BatchTRC create for an invalid CreditsOnly
func BenchmarkBatchTRCCreditsOnly(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchTRCCreditsOnly(b)
}
}
// testBatchTRCAutomatedAccountingAdvices validates BatchTRC create for an invalid AutomatedAccountingAdvices
func testBatchTRCAutomatedAccountingAdvices(t testing.TB) {
mockBatch := mockBatchTRC(t)
mockBatch.Header.ServiceClassCode = AutomatedAccountingAdvices
err := mockBatch.Validate()
if !base.Match(err, NewErrBatchHeaderControlEquality(AutomatedAccountingAdvices, 225)) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchTRCAutomatedAccountingAdvices tests validating BatchTRC create for an invalid AutomatedAccountingAdvices
func TestBatchTRCAutomatedAccountingAdvices(t *testing.T) {
testBatchTRCAutomatedAccountingAdvices(t)
}
// BenchmarkBatchTRCAutomatedAccountingAdvices benchmarks validating BatchTRC create for an invalid AutomatedAccountingAdvices
func BenchmarkBatchTRCAutomatedAccountingAdvices(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchTRCAutomatedAccountingAdvices(b)
}
}
// testBatchTRCCheckSerialNumber validates BatchTRC CheckSerialNumber is not mandatory
func testBatchTRCCheckSerialNumber(t testing.TB) {
mockBatch := mockBatchTRC(t)
// modify CheckSerialNumber / IdentificationNumber to nothing
mockBatch.GetEntries()[0].SetCheckSerialNumber("")
err := mockBatch.Validate()
// no error expected
if !base.Match(err, nil) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchTRCCheckSerialNumber tests validating BatchTRC
// CheckSerialNumber / IdentificationNumber is a mandatory field
func TestBatchTRCCheckSerialNumber(t *testing.T) {
testBatchTRCCheckSerialNumber(t)
}
// BenchmarkBatchTRCCheckSerialNumber benchmarks validating BatchTRC
// CheckSerialNumber / IdentificationNumber is a mandatory field
func BenchmarkBatchTRCCheckSerialNumber(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchTRCCheckSerialNumber(b)
}
}
// testBatchTRCTransactionCode validates BatchTRC TransactionCode is not a credit
func testBatchTRCTransactionCode(t testing.TB) {
mockBatch := mockBatchTRCCredit()
err := mockBatch.Create()
if !base.Match(err, ErrBatchDebitOnly) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchTRCTransactionCode tests validating BatchTRC TransactionCode is not a credit
func TestBatchTRCTransactionCode(t *testing.T) {
testBatchTRCTransactionCode(t)
}
// BenchmarkBatchTRCTransactionCode benchmarks validating BatchTRC TransactionCode is not a credit
func BenchmarkBatchTRCTransactionCode(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchTRCTransactionCode(b)
}
}
// testBatchTRCAddendaCount validates BatchTRC Addenda count
func testBatchTRCAddendaCount(t testing.TB) {
mockBatch := mockBatchTRC(t)
mockBatch.Entries[0].AddendaRecordIndicator = 1
mockBatch.GetEntries()[0].AddAddenda05(mockAddenda05())
err := mockBatch.Create()
if !base.Match(err, ErrBatchAddendaCategory) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchTRCAddendaCount tests validating BatchTRC Addenda count
func TestBatchTRCAddendaCount(t *testing.T) {
testBatchTRCAddendaCount(t)
}
// BenchmarkBatchTRCAddendaCount benchmarks validating BatchTRC Addenda count
func BenchmarkBatchTRCAddendaCount(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchTRCAddendaCount(b)
}
}
// testBatchTRCInvalidBuild validates an invalid batch build
func testBatchTRCInvalidBuild(t testing.TB) {
mockBatch := mockBatchTRC(t)
mockBatch.GetHeader().ServiceClassCode = 3
err := mockBatch.Create()
if !base.Match(err, ErrServiceClass) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchTRCInvalidBuild tests validating an invalid batch build
func TestBatchTRCInvalidBuild(t *testing.T) {
testBatchTRCInvalidBuild(t)
}
// BenchmarkBatchTRCInvalidBuild benchmarks validating an invalid batch build
func BenchmarkBatchTRCInvalidBuild(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchTRCInvalidBuild(b)
}
}
// TestBatchTRCAddendum98 validates Addenda98 returns an error
func TestBatchTRCAddendum98(t *testing.T) {
mockBatch := NewBatchTRC(mockBatchTRCHeader())
mockBatch.AddEntry(mockTRCEntryDetail())
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)
}
}
// TestBatchTRCAddendum99 validates Addenda99 returns an error
func TestBatchTRCAddendum99(t *testing.T) {
mockBatch := NewBatchTRC(mockBatchTRCHeader())
mockBatch.AddEntry(mockTRCEntryDetail())
mockAddenda99 := mockAddenda99()
mockAddenda99.TypeCode = "05"
mockBatch.GetEntries()[0].Addenda99 = mockAddenda99
err := mockBatch.Create()
if !base.Match(err, ErrAddendaTypeCode) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchTRCAddendum99Category validates Addenda99 returns an error
func TestBatchTRCAddendum99Category(t *testing.T) {
mockBatch := NewBatchTRC(mockBatchTRCHeader())
mockBatch.AddEntry(mockTRCEntryDetail())
mockAddenda99 := mockAddenda99()
mockBatch.Entries[0].AddendaRecordIndicator = 1
mockBatch.GetEntries()[0].Category = CategoryForward
mockBatch.GetEntries()[0].Addenda99 = mockAddenda99
err := mockBatch.Create()
if !base.Match(err, ErrBatchAddendaCategory) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchTRCProcessControlField returns an error if ProcessControlField is not defined.
func TestBatchTRCProcessControlField(t *testing.T) {
mockBatch := NewBatchTRC(mockBatchTRCHeader())
mockBatch.AddEntry(mockTRCEntryDetail())
mockBatch.GetEntries()[0].SetProcessControlField("")
err := mockBatch.Create()
if !base.Match(err, ErrFieldRequired) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchTRCItemResearchNumber returns an error if ItemResearchNumber is not defined.
func TestBatchTRCItemResearchNumber(t *testing.T) {
mockBatch := NewBatchTRC(mockBatchTRCHeader())
mockBatch.AddEntry(mockTRCEntryDetail())
mockBatch.GetEntries()[0].IndividualName = ""
mockBatch.GetEntries()[0].SetProcessControlField("CHECK1")
mockBatch.GetEntries()[0].SetItemResearchNumber("")
err := mockBatch.Create()
if !base.Match(err, ErrFieldRequired) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchTRCItemTypeIndicator returns an error if ItemTypeIndicator is not 01.
func TestBatchTRCItemTypeIndicator(t *testing.T) {
mockBatch := NewBatchTRC(mockBatchTRCHeader())
mockBatch.AddEntry(mockTRCEntryDetail())
if err := mockBatch.Create(); err != nil {
t.Errorf("%T: %s", err, err)
}
if mockBatch.GetEntries()[0].ItemTypeIndicator() != "01" {
t.Error("ItemTypeIndicator does not validate")
}
}
// testBatchTRCMixedDebitsAndCreditsServiceClassCodeEquality validates service class code
func testBatchTRCMixedDebitsAndCreditsServiceClassCode(t testing.TB) {
mockBatch := mockBatchTRC(t)
mockBatch.GetControl().ServiceClassCode = MixedDebitsAndCredits
mockBatch.Header.ServiceClassCode = MixedDebitsAndCredits
err := mockBatch.Validate()
if err != nil {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchTRCMixedDebitsAndCreditsServiceClassCodeEquality tests validating service class code
func TestBatchTRCMixedDebitsAndCreditsServiceClassCode(t *testing.T) {
testBatchTRCMixedDebitsAndCreditsServiceClassCode(t)
}