forked from moov-io/ach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batchCCD_test.go
298 lines (264 loc) · 9.17 KB
/
batchCCD_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
// 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"
)
// mockBatchCCDHeader creates a CCD batch header
func mockBatchCCDHeader() *BatchHeader {
bh := NewBatchHeader()
bh.ServiceClassCode = DebitsOnly
bh.StandardEntryClassCode = CCD
bh.CompanyName = "Your Company, inc"
bh.CompanyIdentification = "121042882"
bh.CompanyEntryDescription = "Vndr Pay"
bh.ODFIIdentification = "121042882"
return bh
}
// mockCCDEntryDetail creates a CCD entry detail
func mockCCDEntryDetail() *EntryDetail {
entry := NewEntryDetail()
entry.TransactionCode = CheckingDebit
entry.SetRDFI("231380104")
entry.DFIAccountNumber = "744-5678-99"
entry.Amount = 5000000
entry.IdentificationNumber = "location #23"
entry.SetReceivingCompany("Best Co. #23")
entry.SetTraceNumber(mockBatchCCDHeader().ODFIIdentification, 1)
entry.DiscretionaryData = "S"
return entry
}
// mockBatchCCD creates a CCD batch
func mockBatchCCD(t testing.TB) *BatchCCD {
mockBatch := NewBatchCCD(mockBatchCCDHeader())
mockBatch.AddEntry(mockCCDEntryDetail())
mockBatch.GetEntries()[0].AddAddenda05(mockAddenda05())
mockBatch.Entries[0].AddendaRecordIndicator = 1
require.NoError(t, mockBatch.Create())
return mockBatch
}
// testBatchCCDHeader creates a CCD batch header
func testBatchCCDHeader(t testing.TB) {
batch, _ := NewBatch(mockBatchCCDHeader())
_, ok := batch.(*BatchCCD)
if !ok {
t.Error("Expecting BatchCCD")
}
}
// TestBatchCCDHeader tests creating a CCD batch header
func TestBatchCCDHeader(t *testing.T) {
testBatchCCDHeader(t)
}
// BenchmarkBatchCCDHeader benchmark creating a CCD batch header
func BenchmarkBatchCCDHeader(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCCDHeader(b)
}
}
// testBatchCCDAddendumCount batch control CCD can only have one addendum per entry detail
func testBatchCCDAddendumCount(t testing.TB) {
mockBatch := mockBatchCCD(t)
// Adding a second addenda to the mock entry
mockBatch.GetEntries()[0].AddAddenda05(mockAddenda05())
err := mockBatch.Validate()
if !base.Match(err, NewErrBatchCalculatedControlEquality(3, 2)) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCCDAddendumCount tests batch control CCD can only have one addendum per entry detail
func TestBatchCCDAddendumCount(t *testing.T) {
testBatchCCDAddendumCount(t)
}
// BenchmarkBatchCCDAddendumCount benchmarks batch control CCD can only have one addendum per entry detail
func BenchmarkBatchCCDAddendumCount(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCCDAddendumCount(b)
}
}
// TestBatchCCDAddendum98 validates Addenda98 returns an error
func TestBatchCCDAddendum98(t *testing.T) {
mockBatch := NewBatchCCD(mockBatchCCDHeader())
mockBatch.AddEntry(mockCCDEntryDetail())
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)
}
}
// TestBatchCCDAddendum99 validates Addenda99 returns an error
func TestBatchCCDAddendum99(t *testing.T) {
mockBatch := NewBatchCCD(mockBatchCCDHeader())
mockBatch.AddEntry(mockCCDEntryDetail())
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)
}
}
// testBatchCCDReceivingCompanyName validates Receiving company / Individual name is a mandatory field
func testBatchCCDReceivingCompanyName(t testing.TB) {
mockBatch := mockBatchCCD(t)
// modify the Individual name / receiving company to nothing
mockBatch.GetEntries()[0].SetReceivingCompany("")
err := mockBatch.Validate()
if !base.Match(err, ErrConstructor) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCCDReceivingCompanyName tests validating receiving company / Individual name is a mandatory field
func TestBatchCCDReceivingCompanyName(t *testing.T) {
testBatchCCDReceivingCompanyName(t)
}
// BenchmarkBatchCCDReceivingCompanyName benchmarks validating receiving company / Individual name is a mandatory field
func BenchmarkBatchCCDReceivingCompanyName(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCCDReceivingCompanyName(b)
}
}
// testBatchCCDAddendaTypeCode validates addenda type code is 05
func testBatchCCDAddendaTypeCode(t testing.TB) {
mockBatch := mockBatchCCD(t)
mockBatch.GetEntries()[0].Addenda05[0].TypeCode = "07"
err := mockBatch.Validate()
if !base.Match(err, ErrAddendaTypeCode) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCCDAddendaTypeCode tests validating addenda type code is 05
func TestBatchCCDAddendaTypeCode(t *testing.T) {
testBatchCCDAddendaTypeCode(t)
}
// BenchmarkBatchCCDAddendaTypeCod benchmarks validating addenda type code is 05
func BenchmarkBatchCCDAddendaTypeCode(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCCDAddendaTypeCode(b)
}
}
// testBatchCCDSEC validates that the standard entry class code is CCD for batchCCD
func testBatchCCDSEC(t testing.TB) {
mockBatch := mockBatchCCD(t)
mockBatch.Header.StandardEntryClassCode = RCK
err := mockBatch.Validate()
if !base.Match(err, ErrBatchSECType) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCCDSEC tests validating that the standard entry class code is CCD for batchCCD
func TestBatchCCDSEC(t *testing.T) {
testBatchCCDSEC(t)
}
// BenchmarkBatchCCDSEC benchmarks validating that the standard entry class code is CCD for batch CCD
func BenchmarkBatchCCDSEC(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCCDSEC(b)
}
}
// testBatchCCDAddendaCount validates batch CCD addenda count
func testBatchCCDAddendaCount(t testing.TB) {
mockBatch := mockBatchCCD(t)
mockBatch.GetEntries()[0].AddAddenda05(mockAddenda05())
err := mockBatch.Create()
if !base.Match(err, NewErrBatchAddendaCount(0, 1)) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCCDAddendaCount tests validating batch CCD addenda count
func TestBatchCCDAddendaCount(t *testing.T) {
testBatchCCDAddendaCount(t)
}
// BenchmarkBatchCCDAddendaCount benchmarks validating batch CCD addenda count
func BenchmarkBatchCCDAddendaCount(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCCDAddendaCount(b)
}
}
// testBatchCCDCreate creates a batch CCD
func testBatchCCDCreate(t testing.TB) {
mockBatch := mockBatchCCD(t)
// Batch Header information is required to Create a batch.
mockBatch.GetHeader().ServiceClassCode = 0
err := mockBatch.Create()
if !base.Match(err, ErrConstructor) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCCDCreate Test creating a batch CCD
func TestBatchCCDCreate(t *testing.T) {
testBatchCCDCreate(t)
}
// BenchmarkBatchCCDCreate benchmark creating a batch CCD
func BenchmarkBatchCCDCreate(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCCDCreate(b)
}
}
// testBatchCCDReceivingCompanyField validates CCDReceivingCompanyField
// underlying IndividualName
func testBatchCCDReceivingCompanyField(t testing.TB) {
mockBatch := mockBatchCCD(t)
ts := mockBatch.Entries[0].ReceivingCompanyField()
if ts != "Best Co. #23 " {
t.Error("Receiving Company Field is invalid")
}
}
// TestBatchCCDReceivingCompanyField tests validating CCDReceivingCompanyField
// underlying IndividualName
func TestBatchCCDReceivingCompanyFieldField(t *testing.T) {
testBatchCCDReceivingCompanyField(t)
}
// BenchmarkBatchCCDReceivingCompanyField benchmarks validating CCDReceivingCompanyField
// underlying IndividualName
func BenchmarkBatchCCDReceivingCompanyField(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchCCDReceivingCompanyField(b)
}
}
// TestBatchCCDValidTranCodeForServiceClassCode validates a transactionCode based on ServiceClassCode
func TestBatchCCDValidTranCodeForServiceClassCode(t *testing.T) {
mockBatch := mockBatchCCD(t)
mockBatch.GetHeader().ServiceClassCode = CreditsOnly
err := mockBatch.Create()
if !base.Match(err, NewErrBatchServiceClassTranCode(CreditsOnly, 27)) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchCCDAddenda02 validates BatchCCD cannot have Addenda02
func TestBatchCCDAddenda02(t *testing.T) {
mockBatch := mockBatchCCD(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)
}
}