forked from moov-io/ach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batchENR_test.go
392 lines (348 loc) · 11.9 KB
/
batchENR_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
// 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"
)
// mockBatchENRHeader creates a ENR batch header
func mockBatchENRHeader() *BatchHeader {
bh := NewBatchHeader()
bh.ServiceClassCode = CreditsOnly
bh.CompanyName = "Name on Account"
bh.CompanyIdentification = "231380104"
bh.StandardEntryClassCode = ENR
bh.CompanyEntryDescription = "AUTOENROLL"
bh.ODFIIdentification = "23138010"
return bh
}
// mockENREntryDetail creates a ENR entry detail
func mockENREntryDetail() *EntryDetail {
entry := NewEntryDetail()
entry.TransactionCode = CheckingPrenoteCredit
entry.SetRDFI("031300012")
entry.DFIAccountNumber = "744-5678-99"
entry.Amount = 0
entry.SetOriginalTraceNumber("031300010000001")
entry.SetReceivingCompany("Best. #1")
entry.SetTraceNumber("23138010", 1)
addenda := NewAddenda05()
addenda.PaymentRelatedInformation = `21*12200004*3*123987654321*777777777*DOE*JOHN*1\`
entry.AddAddenda05(addenda)
entry.AddendaRecordIndicator = 1
return entry
}
// mockBatchENR creates a ENR batch
func mockBatchENR(t testing.TB) *BatchENR {
t.Helper()
batch := NewBatchENR(mockBatchENRHeader())
batch.AddEntry(mockENREntryDetail())
if err := batch.Create(); err != nil {
t.Fatalf("Unexpected error building batch: %s\n", err)
}
return batch
}
// testBatchENRHeader creates a ENR batch header
func testBatchENRHeader(t testing.TB) {
batch, _ := NewBatch(mockBatchENRHeader())
_, ok := batch.(*BatchENR)
if !ok {
t.Error("Expecting BatchENR")
}
}
// TestBatchENRHeader tests creating a ENR batch header
func TestBatchENRHeader(t *testing.T) {
testBatchENRHeader(t)
}
// BenchmarkBatchENRHeader benchmark creating a ENR batch header
func BenchmarkBatchENRHeader(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchENRHeader(b)
}
}
// testBatchENRAddendumCount batch control ENR must have 1-9999 Addenda05 records
func testBatchENRAddendumCount(t testing.TB) {
mockBatch := mockBatchENR(t)
// Adding a second addenda to the mock entry
mockBatch.GetEntries()[0].AddAddenda05(mockAddenda05())
if err := mockBatch.Create(); err != nil {
t.Errorf("Adding addenda is allowed: %v", err)
}
if err := mockBatch.Validate(); err != nil {
t.Errorf("Adding addendas is allowed: %v", err)
}
}
// TestBatchENRAddendumCount tests batch control ENR can only have one addendum per entry detail
func TestBatchENRAddendumCount(t *testing.T) {
testBatchENRAddendumCount(t)
}
// BenchmarkBatchENRAddendumCount benchmarks batch control ENR can only have one addendum per entry detail
func BenchmarkBatchENRAddendumCount(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchENRAddendumCount(b)
}
}
// TestBatchENRAddendum98 validates Addenda05 returns an error
func TestBatchENRAddendum98(t *testing.T) {
mockBatch := NewBatchENR(mockBatchENRHeader())
mockBatch.AddEntry(mockENREntryDetail())
err := mockBatch.Create()
// TODO: are we expecting there to be an error here?
if !base.Match(err, nil) {
t.Errorf("%T: %s", err, err)
}
}
// testBatchENRCompanyEntryDescription validates CompanyEntryDescription
func testBatchENRCompanyEntryDescription(t testing.TB) {
mockBatch := mockBatchENR(t)
mockBatch.Header.CompanyEntryDescription = "bad"
err := mockBatch.Create()
if !base.Match(err, ErrBatchCompanyEntryDescriptionAutoenroll) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchENRCompanyEntryDescription tests validating receiving company / Individual name is a mandatory field
func TestBatchENRCompanyEntryDescription(t *testing.T) {
testBatchENRCompanyEntryDescription(t)
}
// BenchmarkBatchENRCompanyEntryDescription benchmarks validating receiving company / Individual name is a mandatory field
func BenchmarkBatchENRCompanyEntryDescription(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchENRCompanyEntryDescription(b)
}
}
// testBatchENRAddendaTypeCode validates addenda type code is 05
func testBatchENRAddendaTypeCode(t testing.TB) {
mockBatch := mockBatchENR(t)
mockBatch.GetEntries()[0].Addenda05[0].TypeCode = "98"
err := mockBatch.Validate()
if !base.Match(err, ErrAddendaTypeCode) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchENRAddendaTypeCode tests validating addenda type code is 05
func TestBatchENRAddendaTypeCode(t *testing.T) {
testBatchENRAddendaTypeCode(t)
}
// BenchmarkBatchENRAddendaTypeCod benchmarks validating addenda type code is 05
func BenchmarkBatchENRAddendaTypeCode(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchENRAddendaTypeCode(b)
}
}
// testBatchENRSEC validates that the standard entry class code is ENR for batchENR
func testBatchENRSEC(t testing.TB) {
mockBatch := mockBatchENR(t)
mockBatch.Header.StandardEntryClassCode = ACK
err := mockBatch.Validate()
if !base.Match(err, ErrBatchSECType) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchENRSEC tests validating that the standard entry class code is ENR for batchENR
func TestBatchENRSEC(t *testing.T) {
testBatchENRSEC(t)
}
// BenchmarkBatchENRSEC benchmarks validating that the standard entry class code is ENR for batch ENR
func BenchmarkBatchENRSEC(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchENRSEC(b)
}
}
// testBatchENRAddendaCount validates batch ENR addenda count
func testBatchENRAddendaCount(t testing.TB) {
mockBatch := mockBatchENR(t)
mockBatch.GetEntries()[0].AddAddenda05(mockAddenda05())
err := mockBatch.Create()
// TODO: are we expecting there to be no errors here?
if !base.Match(err, nil) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchENRAddendaCount tests validating batch ENR addenda count
func TestBatchENRAddendaCount(t *testing.T) {
testBatchENRAddendaCount(t)
}
// BenchmarkBatchENRAddendaCount benchmarks validating batch ENR addenda count
func BenchmarkBatchENRAddendaCount(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchENRAddendaCount(b)
}
}
// testBatchENRServiceClassCode validates ServiceClassCode
func testBatchENRServiceClassCode(t testing.TB) {
mockBatch := mockBatchENR(t)
mockBatch.GetHeader().ServiceClassCode = 0
err := mockBatch.Create()
if !base.Match(err, ErrConstructor) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchENRServiceClassCode tests validating ServiceClassCode
func TestBatchENRServiceClassCode(t *testing.T) {
testBatchENRServiceClassCode(t)
}
// BenchmarkBatchENRServiceClassCode benchmarks validating ServiceClassCode
func BenchmarkBatchENRServiceClassCode(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchENRServiceClassCode(b)
}
}
// TestBatchENRAmount validates Amount
func TestBatchENRAmount(t *testing.T) {
mockBatch := mockBatchENR(t)
mockBatch.GetEntries()[0].Amount = 25000
err := mockBatch.Create()
if !base.Match(err, ErrBatchAmountNonZero) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchENRTransactionCode validates TransactionCode
func TestBatchENRTransactionCode(t *testing.T) {
mockBatch := mockBatchENR(t)
mockBatch.GetEntries()[0].TransactionCode = CheckingReturnNOCCredit
err := mockBatch.Create()
if !base.Match(err, ErrBatchTransactionCode) {
t.Errorf("%T: %s", err, err)
}
}
func TestBatchENR__PaymentInformation(t *testing.T) {
batch := mockBatchENR(t)
if err := batch.Validate(); err != nil {
t.Fatal(err)
}
addenda05 := batch.GetEntries()[0].Addenda05[0]
info, err := ParseENRPaymentInformation(addenda05)
if err != nil {
t.Fatal(err)
}
if v := info.TransactionCode; v != CheckingReturnNOCCredit {
t.Errorf("TransactionCode: %d", v)
}
if v := info.RDFIIdentification; v != "12200004" {
t.Errorf("RDFIIdentification: %s", v)
}
if v := info.CheckDigit; v != "3" {
t.Errorf("CheckDigit: %s", v)
}
if v := info.DFIAccountNumber; v != "123987654321" {
t.Errorf("DFIAccountNumber: %s", v)
}
if v := info.IndividualIdentification; v != "777777777" {
t.Errorf("IndividualIdentification: %s", v)
}
if v := info.IndividualName; v != "JOHN DOE" {
t.Errorf("IndividualName: %s", v)
}
if v := info.EnrolleeClassificationCode; v != "1" {
t.Errorf("EnrolleeClassificationCode: %v", v)
}
addenda05 = NewAddenda05()
t.Run("Nacha Examples", func(t *testing.T) {
// Consumer
input := `22*12200004*3*123987654321*777777777*DOE*JOHN*0\`
expected := &ENRPaymentInformation{
TransactionCode: 22,
RDFIIdentification: "12200004",
CheckDigit: "3",
DFIAccountNumber: "123987654321",
IndividualIdentification: "777777777",
IndividualName: "JOHN DOE",
EnrolleeClassificationCode: "0",
}
addenda05.PaymentRelatedInformation = input
parsed, err := ParseENRPaymentInformation(addenda05)
require.NoError(t, err)
require.Equal(t, expected, parsed)
require.Equal(t, input, parsed.String())
// Consumer
input = `27*12200004*3*123987654321*777777777*DOE*JOHN*A\`
addenda05.PaymentRelatedInformation = input
parsed, err = ParseENRPaymentInformation(addenda05)
require.NoError(t, err)
expected = &ENRPaymentInformation{
TransactionCode: 27,
RDFIIdentification: "12200004",
CheckDigit: "3",
DFIAccountNumber: "123987654321",
IndividualIdentification: "777777777",
IndividualName: "JOHN DOE",
EnrolleeClassificationCode: "A",
}
require.Equal(t, expected, parsed)
require.Equal(t, input, parsed.String())
// Company
input = `27*12200004*3*987654321123*876543210*ABCELECTRONICIN*DUSTRIE*B\`
addenda05.PaymentRelatedInformation = input
parsed, err = ParseENRPaymentInformation(addenda05)
require.NoError(t, err)
expected = &ENRPaymentInformation{
TransactionCode: 27,
RDFIIdentification: "12200004",
CheckDigit: "3",
DFIAccountNumber: "987654321123",
IndividualIdentification: "876543210",
IndividualName: "ABCELECTRONICINDUSTRIE",
EnrolleeClassificationCode: "B",
}
require.Equal(t, expected, parsed)
require.Equal(t, input, parsed.String())
// Company
input = `27*12200004*3*987654321123*876543210*ELECTRIC**B\`
addenda05.PaymentRelatedInformation = input
parsed, err = ParseENRPaymentInformation(addenda05)
require.NoError(t, err)
expected = &ENRPaymentInformation{
TransactionCode: 27,
RDFIIdentification: "12200004",
CheckDigit: "3",
DFIAccountNumber: "987654321123",
IndividualIdentification: "876543210",
IndividualName: "ELECTRIC",
EnrolleeClassificationCode: "B",
}
require.Equal(t, expected, parsed)
require.Equal(t, input, parsed.String())
})
}
// TestBatchENRValidTranCodeForServiceClassCode validates a transactionCode based on ServiceClassCode
func TestBatchENRValidTranCodeForServiceClassCode(t *testing.T) {
mockBatch := mockBatchENR(t)
mockBatch.GetHeader().ServiceClassCode = DebitsOnly
err := mockBatch.Create()
if !base.Match(err, NewErrBatchServiceClassTranCode(DebitsOnly, 22)) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchENRAddenda02 validates BatchENR cannot have Addenda02
func TestBatchENRAddenda02(t *testing.T) {
mockBatch := mockBatchENR(t)
mockBatch.Entries[0].AddendaRecordIndicator = 1
mockBatch.GetEntries()[0].Addenda02 = mockAddenda02()
err := mockBatch.Create()
if !base.Match(err, ErrBatchAddendaCategory) {
t.Errorf("%T: %s", err, err)
}
}