-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathbundleControl_test.go
169 lines (149 loc) · 5.28 KB
/
bundleControl_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
// Copyright 2020 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package imagecashletter
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
)
// mockBundleControl creates a BundleControl
func mockBundleControl() *BundleControl {
bc := NewBundleControl()
bc.BundleItemsCount = 7
bc.BundleTotalAmount = 100000 // 1000.00
bc.MICRValidTotalAmount = 100000 // 1000.00
bc.BundleImagesCount = 1
bc.UserField = ""
bc.CreditTotalIndicator = 0
return bc
}
// TestMockBundleControl creates an BundleControl
func TestMockBundleControl(t *testing.T) {
bc := mockBundleControl()
require.NoError(t, bc.Validate())
require.Equal(t, "70", bc.recordType)
require.Equal(t, 7, bc.BundleItemsCount)
require.Equal(t, 100000, bc.BundleTotalAmount)
require.Equal(t, 100000, bc.MICRValidTotalAmount)
require.Equal(t, 1, bc.BundleImagesCount)
require.Empty(t, bc.UserField)
require.Equal(t, 0, bc.CreditTotalIndicator)
}
// TestParseBundleControl parses a known BundleControl record string
func TestParseBundleControl(t *testing.T) {
var line = "70000100000010000000000000000000000 0 "
r := NewReader(strings.NewReader(line))
r.line = line
clh := mockCashLetterHeader()
r.addCurrentCashLetter(NewCashLetter(clh))
bh := mockBundleHeader()
r.currentCashLetter.AddBundle(NewBundle(bh))
r.addCurrentBundle(NewBundle(bh))
require.NoError(t, r.parseBundleControl())
record := r.currentCashLetter.currentBundle.BundleControl
require.Equal(t, "70", record.recordType)
require.Equal(t, "0001", record.BundleItemsCountField())
require.Equal(t, "000000100000", record.BundleTotalAmountField())
require.Equal(t, "000000000000", record.MICRValidTotalAmountField())
require.Equal(t, "00000", record.BundleImagesCountField())
require.Equal(t, " ", record.UserFieldField())
require.Equal(t, "0", record.CreditTotalIndicatorField())
require.Equal(t, " ", record.reservedField())
}
func TestParseBundleControlError(t *testing.T) {
bc := NewBundleControl()
bc.Parse(" invalid line ")
require.Equal(t, 0, bc.BundleItemsCount)
require.Equal(t, 0, bc.BundleTotalAmount)
require.Equal(t, 0, bc.MICRValidTotalAmount)
require.Equal(t, 0, bc.BundleImagesCount)
require.Empty(t, bc.UserField)
require.Equal(t, 0, bc.CreditTotalIndicator)
}
// testBCString validates that a known parsed BundleControl can be return to a string of the same value
func testBCString(t testing.TB) {
var line = "70000100000010000000000000000000000 0 "
r := NewReader(strings.NewReader(line))
r.line = line
clh := mockCashLetterHeader()
r.addCurrentCashLetter(NewCashLetter(clh))
bh := mockBundleHeader()
r.currentCashLetter.AddBundle(NewBundle(bh))
r.addCurrentBundle(NewBundle(bh))
require.NoError(t, r.parseBundleControl())
record := r.currentCashLetter.currentBundle.BundleControl
require.Equal(t, line, record.String())
}
// TestBCString tests validating that a known parsed BundleControl can be return to a string of the same value
func TestBCString(t *testing.T) {
testBCString(t)
}
// BenchmarkBCString benchmarks validating that a known parsed BundleControl can be return to a string of the same value
func BenchmarkBCString(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBCString(b)
}
}
// TestBCRecordType validation
func TestBCRecordType(t *testing.T) {
bc := mockBundleControl()
bc.recordType = "00"
err := bc.Validate()
var fieldErr *FieldError
require.ErrorAs(t, err, &fieldErr)
require.Equal(t, "recordType", fieldErr.FieldName)
}
// TestBCUserField validation
func TestBCUserFieldI(t *testing.T) {
bc := mockBundleControl()
bc.UserField = "®©"
err := bc.Validate()
var fieldErr *FieldError
require.ErrorAs(t, err, &fieldErr)
require.Equal(t, "UserField", fieldErr.FieldName)
}
// TestBCCreditTotalIndicator validation
func TestBCCreditTotalIndicator(t *testing.T) {
bc := mockBundleControl()
bc.CreditTotalIndicator = 9
err := bc.Validate()
var fieldErr *FieldError
require.ErrorAs(t, err, &fieldErr)
require.Equal(t, "CreditTotalIndicator", fieldErr.FieldName)
}
// TestBCFieldInclusionRecordType validates FieldInclusion
func TestBCFieldInclusionRecordType(t *testing.T) {
bc := mockBundleControl()
bc.recordType = ""
err := bc.Validate()
var fieldErr *FieldError
require.ErrorAs(t, err, &fieldErr)
require.Equal(t, "recordType", fieldErr.FieldName)
}
// TestFieldInclusionBundleItemsCount validates FieldInclusion
func TestFieldInclusionBundleItemsCount(t *testing.T) {
bc := mockBundleControl()
bc.BundleItemsCount = 0
err := bc.Validate()
var fieldErr *FieldError
require.ErrorAs(t, err, &fieldErr)
require.Equal(t, "BundleItemsCount", fieldErr.FieldName)
}
// TestFieldInclusionBundleTotalAmount validates FieldInclusion
func TestFieldInclusionBundleTotalAmount(t *testing.T) {
bc := mockBundleControl()
bc.BundleTotalAmount = 0
err := bc.Validate()
var fieldErr *FieldError
require.ErrorAs(t, err, &fieldErr)
require.Equal(t, "BundleTotalAmount", fieldErr.FieldName)
}
// TestBundleControlRuneCountInString validates RuneCountInString
func TestBundleControlRuneCountInString(t *testing.T) {
bc := NewBundleControl()
var line = "70"
bc.Parse(line)
require.Equal(t, 0, bc.BundleItemsCount)
}