-
Notifications
You must be signed in to change notification settings - Fork 40
/
beneficiary_test.go
197 lines (146 loc) · 6.81 KB
/
beneficiary_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
package wire
import (
"errors"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
// mockBeneficiary creates a Beneficiary
func mockBeneficiary() *Beneficiary {
ben := NewBeneficiary()
ben.Personal.IdentificationCode = DriversLicenseNumber
ben.Personal.Identifier = "1234"
ben.Personal.Name = "Name"
ben.Personal.Address.AddressLineOne = "Address One"
ben.Personal.Address.AddressLineTwo = "Address Two"
ben.Personal.Address.AddressLineThree = "Address Three"
return ben
}
// TestMockBeneficiary validates mockBeneficiary
func TestMockBeneficiary(t *testing.T) {
ben := mockBeneficiary()
require.NoError(t, ben.Validate(), "mockBeneficiary does not validate and will break other tests")
}
// TestBeneficiaryIdentificationCodeValid validates Beneficiary IdentificationCode
func TestBeneficiaryIdentificationCodeValid(t *testing.T) {
ben := mockBeneficiary()
ben.Personal.IdentificationCode = "Baseball Card ID"
err := ben.Validate()
require.EqualError(t, err, fieldError("IdentificationCode", ErrIdentificationCode, ben.Personal.IdentificationCode).Error())
}
// TestBeneficiaryIdentifierAlphaNumeric validates Beneficiary Identifier is alphanumeric
func TestBeneficiaryIdentifierAlphaNumeric(t *testing.T) {
ben := mockBeneficiary()
ben.Personal.Identifier = "®"
err := ben.Validate()
require.EqualError(t, err, fieldError("Identifier", ErrNonAlphanumeric, ben.Personal.Identifier).Error())
}
// TestBeneficiaryNameAlphaNumeric validates Beneficiary Name is alphanumeric
func TestBeneficiaryNameAlphaNumeric(t *testing.T) {
ben := mockBeneficiary()
ben.Personal.Name = "®"
err := ben.Validate()
require.EqualError(t, err, fieldError("Name", ErrNonAlphanumeric, ben.Personal.Name).Error())
}
// TestBeneficiaryAddressLineOneAlphaNumeric validates Beneficiary AddressLineOne is alphanumeric
func TestBeneficiaryAddressLineOneAlphaNumeric(t *testing.T) {
ben := mockBeneficiary()
ben.Personal.Address.AddressLineOne = "®"
err := ben.Validate()
require.EqualError(t, err, fieldError("AddressLineOne", ErrNonAlphanumeric, ben.Personal.Address.AddressLineOne).Error())
}
// TestBeneficiaryAddressLineTwoAlphaNumeric validates Beneficiary AddressLineTwo is alphanumeric
func TestBeneficiaryAddressLineTwoAlphaNumeric(t *testing.T) {
ben := mockBeneficiary()
ben.Personal.Address.AddressLineTwo = "®"
err := ben.Validate()
require.EqualError(t, err, fieldError("AddressLineTwo", ErrNonAlphanumeric, ben.Personal.Address.AddressLineTwo).Error())
}
// TestBeneficiaryAddressLineThreeAlphaNumeric validates Beneficiary AddressLineThree is alphanumeric
func TestBeneficiaryAddressLineThreeAlphaNumeric(t *testing.T) {
ben := mockBeneficiary()
ben.Personal.Address.AddressLineThree = "®"
err := ben.Validate()
require.EqualError(t, err, fieldError("AddressLineThree", ErrNonAlphanumeric, ben.Personal.Address.AddressLineThree).Error())
}
// TestBeneficiaryIdentificationCodeWithNoIdentifier validates Beneficiary Identifier is required
// when IdentificationCode is present
func TestBeneficiaryIdentificationCodeWithNoIdentifier(t *testing.T) {
ben := mockBeneficiary()
ben.Personal.IdentificationCode = "D"
ben.Personal.Identifier = ""
err := ben.Validate()
require.EqualError(t, err, fieldError("Identifier", ErrFieldRequired).Error())
}
// TestBeneficiaryIdentifierWithNoIdentificationCode validates Beneficiary IdentificationCode
// is required when Identifier is present
func TestBeneficiaryIdentifierWithNoIdentificationCode(t *testing.T) {
ben := mockBeneficiary()
ben.Personal.IdentificationCode = ""
ben.Personal.Identifier = "1234567890ABC"
err := ben.Validate()
require.EqualError(t, err, fieldError("IdentificationCode", ErrFieldRequired).Error())
}
// TestParseBeneficiaryWrongLength parses a wrong Beneficiary record length
func TestParseBeneficiaryWrongLength(t *testing.T) {
var line = "{4200}31234 *Name *Address One *Address Two *Address Three "
r := NewReader(strings.NewReader(line))
r.line = line
err := r.parseBeneficiary()
require.EqualError(t, err, r.parseError(fieldError("AddressLineThree", ErrRequireDelimiter)).Error())
}
// TestParseBeneficiaryReaderParseError parses a wrong Beneficiary reader parse error
func TestParseBeneficiaryReaderParseError(t *testing.T) {
var line = "{4200}31234 *Na®e *Address One *Address Two *Address Three *"
r := NewReader(strings.NewReader(line))
r.line = line
err := r.parseBeneficiary()
expected := r.parseError(fieldError("Name", ErrNonAlphanumeric, "Na®e")).Error()
require.EqualError(t, err, expected)
_, err = r.Read()
expected = r.parseError(fieldError("Name", ErrNonAlphanumeric, "Na®e")).Error()
require.EqualError(t, err, expected)
}
// TestBeneficiaryTagError validates Beneficiary tag
func TestBeneficiaryTagError(t *testing.T) {
ben := mockBeneficiary()
ben.tag = "{9999}"
err := ben.Validate()
require.EqualError(t, err, fieldError("tag", ErrValidTagForType, ben.tag).Error())
}
// TestStringBeneficiaryVariableLength parses using variable length
func TestStringBeneficiaryVariableLength(t *testing.T) {
var line = "{4200}"
r := NewReader(strings.NewReader(line))
r.line = line
err := r.parseBeneficiary()
expected := r.parseError(NewTagMinLengthErr(7, len(r.line))).Error()
require.EqualError(t, err, expected)
line = "{4200}31234 *Na®e *Address One *Address Two *Address Three NNN"
r = NewReader(strings.NewReader(line))
r.line = line
err = r.parseBeneficiary()
require.ErrorContains(t, err, ErrRequireDelimiter.Error())
line = "{4200}31234*******"
r = NewReader(strings.NewReader(line))
r.line = line
err = r.parseBeneficiary()
require.ErrorContains(t, err, r.parseError(NewTagMaxLengthErr(errors.New(""))).Error())
line = "{4200}31234*****"
r = NewReader(strings.NewReader(line))
r.line = line
err = r.parseBeneficiary()
require.Equal(t, err, nil)
}
// TestStringBeneficiaryOptions validates Format() formatted according to the FormatOptions
func TestStringBeneficiaryOptions(t *testing.T) {
var line = "{4200}31234*"
r := NewReader(strings.NewReader(line))
r.line = line
err := r.parseBeneficiary()
require.Equal(t, err, nil)
ben := r.currentFEDWireMessage.Beneficiary
require.Equal(t, ben.String(), "{4200}31234 * * * * *")
require.Equal(t, ben.Format(FormatOptions{VariableLengthFields: true}), "{4200}31234*")
require.Equal(t, ben.String(), ben.Format(FormatOptions{VariableLengthFields: false}))
}