Skip to content

Commit

Permalink
Merge pull request #103 from wokkaflokka/validation
Browse files Browse the repository at this point in the history
[validation] validate BAI2 file after parsing
  • Loading branch information
adamdecaf authored Mar 20, 2024
2 parents fffb0e4 + fe815e4 commit f815f2c
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
20 changes: 19 additions & 1 deletion cmd/bai2/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

var (
testFileName = filepath.Join("..", "..", "test", "testdata", "sample1.txt")
testFileName = filepath.Join("..", "..", "test", "testdata", "sample1.txt")
parseErrorFileName = filepath.Join("..", "..", "test", "testdata", "sample-parseError.txt")
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -63,3 +66,18 @@ func TestFormat(t *testing.T) {
t.Errorf(err.Error())
}
}

func TestPrint_ParseError(t *testing.T) {
_, err := executeCommand(rootCmd, "print", "--input", parseErrorFileName)
assert.Equal(t, err.Error(), "ERROR parsing file on line 1 (unsupported record type 00)")
}

func TestParse_ParseError(t *testing.T) {
_, err := executeCommand(rootCmd, "parse", "--input", parseErrorFileName)
assert.Equal(t, err.Error(), "ERROR parsing file on line 1 (unsupported record type 00)")
}

func TestFormat_ParseError(t *testing.T) {
_, err := executeCommand(rootCmd, "format", "--input", parseErrorFileName)
assert.Equal(t, err.Error(), "ERROR parsing file on line 1 (unsupported record type 00)")
}
10 changes: 10 additions & 0 deletions cmd/bai2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ var Print = &cobra.Command{
return err
}

err = f.Validate()
if err != nil {
return err
}

fmt.Println(f.String())
return nil
},
Expand All @@ -112,6 +117,11 @@ var Format = &cobra.Command{
return err
}

err = f.Validate()
if err != nil {
return err
}

body, ferr := json.Marshal(f)
if ferr != nil {
return ferr
Expand Down
12 changes: 12 additions & 0 deletions pkg/service/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ func print(w http.ResponseWriter, r *http.Request) {
return
}

err = f.Validate()
if err != nil {
outputError(w, http.StatusNotImplemented, err)
return
}

outputBufferToWriter(w, f)
}

Expand All @@ -101,6 +107,12 @@ func format(w http.ResponseWriter, r *http.Request) {
return
}

err = f.Validate()
if err != nil {
outputError(w, http.StatusNotImplemented, err)
return
}

outputJsonBufferToWriter(w, f)
}

Expand Down
45 changes: 44 additions & 1 deletion pkg/service/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import (
)

var (
testFileName = "sample1.txt"
testFileName = "sample1.txt"
parseErrorFileName = "sample-parseError.txt"
)

type HandlersTest struct {
Expand Down Expand Up @@ -161,3 +162,45 @@ func (suite *HandlersTest) TestFormat() {
assert.Equal(suite.T(), recorder.Body.String(), `{"sender":"0004","receiver":"12345","fileCreatedDate":"060321","fileCreatedTime":"0829","fileIdNumber":"001","physicalRecordLength":80,"blockSize":1,"versionNumber":2,"fileControlTotal":"+00000000001280000","numberOfGroups":1,"numberOfRecords":27,"Groups":[{"receiver":"12345","originator":"0004","groupStatus":1,"asOfDate":"060317","currencyCode":"CAD","groupControlTotal":"+00000000001280000","numberOfAccounts":2,"numberOfRecords":25,"Accounts":[{"accountNumber":"10200123456","currencyCode":"CAD","summaries":[{"TypeCode":"040","Amount":"+000000000000","ItemCount":0,"FundsType":{}},{"TypeCode":"045","Amount":"+000000000000","ItemCount":0,"FundsType":{}},{"TypeCode":"100","Amount":"000000000208500","ItemCount":3,"FundsType":{"type_code":"V","date":"060316"}},{"TypeCode":"400","Amount":"000000000208500","ItemCount":8,"FundsType":{"type_code":"V","date":"060316"}}],"accountControlTotal":"+00000000000834000","numberRecords":14,"Details":[{"TypeCode":"409","Amount":"000000000002500","FundsType":{"type_code":"V","date":"060316"},"BankReferenceNumber":"","CustomerReferenceNumber":"","Text":"RETURNED CHEQUE "},{"TypeCode":"409","Amount":"000000000090000","FundsType":{"type_code":"V","date":"060316"},"BankReferenceNumber":"","CustomerReferenceNumber":"","Text":"RTN-UNKNOWN "},{"TypeCode":"409","Amount":"000000000000500","FundsType":{"type_code":"V","date":"060316"},"BankReferenceNumber":"","CustomerReferenceNumber":"","Text":"RTD CHQ SERVICE CHRG"},{"TypeCode":"108","Amount":"000000000203500","FundsType":{"type_code":"V","date":"060316"},"BankReferenceNumber":"","CustomerReferenceNumber":"","Text":"TFR 1020 0345678 "},{"TypeCode":"108","Amount":"000000000002500","FundsType":{"type_code":"V","date":"060316"},"BankReferenceNumber":"","CustomerReferenceNumber":"","Text":"MACLEOD MALL "},{"TypeCode":"108","Amount":"000000000002500","FundsType":{"type_code":"V","date":"060316"},"BankReferenceNumber":"","CustomerReferenceNumber":"","Text":"MASCOUCHE QUE "},{"TypeCode":"409","Amount":"000000000020000","FundsType":{"type_code":"V","date":"060316"},"BankReferenceNumber":"","CustomerReferenceNumber":"","Text":"1000 ISLANDS MALL "},{"TypeCode":"409","Amount":"000000000090000","FundsType":{"type_code":"V","date":"060316"},"BankReferenceNumber":"","CustomerReferenceNumber":"","Text":"PENHORA MALL "},{"TypeCode":"409","Amount":"000000000002000","FundsType":{"type_code":"V","date":"060316"},"BankReferenceNumber":"","CustomerReferenceNumber":"","Text":"CAPILANO MALL "},{"TypeCode":"409","Amount":"000000000002500","FundsType":{"type_code":"V","date":"060316"},"BankReferenceNumber":"","CustomerReferenceNumber":"","Text":"GALERIES LA CAPITALE"},{"TypeCode":"409","Amount":"000000000001000","FundsType":{"type_code":"V","date":"060316"},"BankReferenceNumber":"","CustomerReferenceNumber":"","Text":"PLAZA ROCK FOREST "}]},{"accountNumber":"10200123456","currencyCode":"CAD","summaries":[{"TypeCode":"040","Amount":"+000000000000","ItemCount":0,"FundsType":{}},{"TypeCode":"045","Amount":"+000000000000","ItemCount":0,"FundsType":{}},{"TypeCode":"100","Amount":"000000000111500","ItemCount":2,"FundsType":{"type_code":"V","date":"060317"}},{"TypeCode":"400","Amount":"000000000111500","ItemCount":4,"FundsType":{"type_code":"V","date":"060317"}}],"accountControlTotal":"+00000000000446000","numberRecords":9,"Details":[{"TypeCode":"108","Amount":"000000000011500","FundsType":{"type_code":"V","date":"060317"},"BankReferenceNumber":"","CustomerReferenceNumber":"","Text":"TFR 1020 0345678 "},{"TypeCode":"108","Amount":"000000000100000","FundsType":{"type_code":"V","date":"060317"},"BankReferenceNumber":"","CustomerReferenceNumber":"","Text":"MONTREAL "},{"TypeCode":"409","Amount":"000000000100000","FundsType":{"type_code":"V","date":"060317"},"BankReferenceNumber":"","CustomerReferenceNumber":"","Text":"GRANDFALL NB "},{"TypeCode":"409","Amount":"000000000009000","FundsType":{"type_code":"V","date":"060317"},"BankReferenceNumber":"","CustomerReferenceNumber":"","Text":"HAMILTON ON "},{"TypeCode":"409","Amount":"000000000002000","FundsType":{"type_code":"V","date":"060317"},"BankReferenceNumber":"","CustomerReferenceNumber":"","Text":"WOODSTOCK NB "},{"TypeCode":"409","Amount":"000000000000500","FundsType":{"type_code":"V","date":"060317"},"BankReferenceNumber":"","CustomerReferenceNumber":"","Text":"GALERIES RICHELIEU "}]}]}]}
`)
}

func (suite *HandlersTest) TestPrint_ParseError() {
writer, body := suite.getWriter(parseErrorFileName)
err := writer.Close()
assert.Equal(suite.T(), nil, err)

recorder, request := suite.makeRequest(http.MethodPost, "/print", body.String())
request.Header.Set("Content-Type", writer.FormDataContentType())

suite.testServer.ServeHTTP(recorder, request)
assert.Equal(suite.T(), http.StatusBadRequest, recorder.Code)
assert.Equal(suite.T(), recorder.Body.String(), `{"error":"ERROR parsing file on line 1 (unsupported record type 00)"}
`)
}

func (suite *HandlersTest) TestParse_ParseError() {
writer, body := suite.getWriter(parseErrorFileName)
err := writer.Close()
assert.Equal(suite.T(), nil, err)

recorder, request := suite.makeRequest(http.MethodPost, "/parse", body.String())
request.Header.Set("Content-Type", writer.FormDataContentType())

suite.testServer.ServeHTTP(recorder, request)
assert.Equal(suite.T(), http.StatusBadRequest, recorder.Code)
assert.Equal(suite.T(), recorder.Body.String(), `{"error":"ERROR parsing file on line 1 (unsupported record type 00)"}
`)
}

func (suite *HandlersTest) TestFormat_ParseError() {
writer, body := suite.getWriter(parseErrorFileName)
err := writer.Close()
assert.Equal(suite.T(), nil, err)

recorder, request := suite.makeRequest(http.MethodPost, "/format", body.String())
request.Header.Set("Content-Type", writer.FormDataContentType())

suite.testServer.ServeHTTP(recorder, request)
assert.Equal(suite.T(), http.StatusBadRequest, recorder.Code)
assert.Equal(suite.T(), recorder.Body.String(), `{"error":"ERROR parsing file on line 1 (unsupported record type 00)"}
`)
}
1 change: 1 addition & 0 deletions test/testdata/sample-parseError.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
00,0004,1/2/345,06/0321,0829,001,80,1,2/

0 comments on commit f815f2c

Please sign in to comment.