Skip to content

Commit 2000d27

Browse files
frednestolorenzodonini
authored andcommitted
use table based tests
1 parent 7bc78e0 commit 2000d27

File tree

1 file changed

+48
-45
lines changed

1 file changed

+48
-45
lines changed

ocpp1.6_test/proto_test.go

+48-45
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ocpp16_test
22

33
import (
44
"fmt"
5+
"testing"
56

67
"github.com/lorenzodonini/ocpp-go/ocpp"
78
"github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
@@ -41,51 +42,53 @@ func (suite *OcppV16TestSuite) TestChargePointSendResponseError() {
4142
err := suite.chargePoint.Start("someUrl")
4243
require.Nil(t, err)
4344
resultChannel := make(chan error, 1)
44-
// Test 1: occurrence validation error
45-
dataTransferConfirmation := core.NewDataTransferConfirmation(core.DataTransferStatusAccepted)
46-
dataTransferConfirmation.Data = CustomData{Field1: "", Field2: 42}
47-
coreListener.On("OnDataTransfer", mock.Anything).Return(dataTransferConfirmation, nil)
48-
err = suite.centralSystem.DataTransfer(wsId, func(confirmation *core.DataTransferConfirmation, err error) {
49-
require.Nil(t, confirmation)
50-
require.Error(t, err)
51-
resultChannel <- err
52-
}, "vendor1")
53-
require.Nil(t, err)
54-
result := <-resultChannel
55-
require.IsType(t, &ocpp.Error{}, result)
56-
ocppErr = result.(*ocpp.Error)
57-
assert.Equal(t, ocppj.OccurrenceConstraintViolation, ocppErr.Code)
58-
assert.Equal(t, "Field CallResult.Payload.Data.Field1 required but not found for feature DataTransfer", ocppErr.Description)
59-
// Test 2: marshaling error
60-
dataTransferConfirmation = core.NewDataTransferConfirmation(core.DataTransferStatusAccepted)
61-
dataTransferConfirmation.Data = make(chan struct{})
62-
coreListener.ExpectedCalls = nil
63-
coreListener.On("OnDataTransfer", mock.Anything).Return(dataTransferConfirmation, nil)
64-
err = suite.centralSystem.DataTransfer(wsId, func(confirmation *core.DataTransferConfirmation, err error) {
65-
require.Nil(t, confirmation)
66-
require.Error(t, err)
67-
resultChannel <- err
68-
}, "vendor1")
69-
require.Nil(t, err)
70-
result = <-resultChannel
71-
require.IsType(t, &ocpp.Error{}, result)
72-
ocppErr = result.(*ocpp.Error)
73-
assert.Equal(t, ocppj.GenericError, ocppErr.Code)
74-
assert.Equal(t, "json: unsupported type: chan struct {}", ocppErr.Description)
75-
// Test 3: no results in callback
76-
coreListener.ExpectedCalls = nil
77-
coreListener.On("OnDataTransfer", mock.Anything).Return(nil, nil)
78-
err = suite.centralSystem.DataTransfer(wsId, func(confirmation *core.DataTransferConfirmation, err error) {
79-
require.Nil(t, confirmation)
80-
require.Error(t, err)
81-
resultChannel <- err
82-
}, "vendor1")
83-
require.Nil(t, err)
84-
result = <-resultChannel
85-
require.IsType(t, &ocpp.Error{}, result)
86-
ocppErr = result.(*ocpp.Error)
87-
assert.Equal(t, ocppj.GenericError, ocppErr.Code)
88-
assert.Equal(t, "empty confirmation to request 1234", ocppErr.Description)
45+
46+
testCases := []struct {
47+
name string
48+
confirmData interface{}
49+
expectedErr *ocpp.Error
50+
}{
51+
{
52+
name: "ocurrence validation",
53+
confirmData: CustomData{Field1: "", Field2: 42},
54+
expectedErr: &ocpp.Error{Code: ocppj.OccurrenceConstraintViolation, Description: "Field CallResult.Payload.Data.Field1 required but not found for feature DataTransfer"},
55+
},
56+
{
57+
name: "marshaling error",
58+
confirmData: make(chan struct{}),
59+
expectedErr: &ocpp.Error{Code: ocppj.GenericError, Description: "json: unsupported type: chan struct {}"},
60+
},
61+
{
62+
name: "empty confirmation",
63+
confirmData: nil,
64+
expectedErr: &ocpp.Error{Code: ocppj.GenericError, Description: "empty confirmation to request 1234"},
65+
},
66+
}
67+
68+
for _, tc := range testCases {
69+
t.Run(tc.name, func(*testing.T) {
70+
coreListener.ExpectedCalls = nil
71+
if tc.confirmData != nil {
72+
dataTransferConfirmation := core.NewDataTransferConfirmation(core.DataTransferStatusAccepted)
73+
dataTransferConfirmation.Data = tc.confirmData
74+
coreListener.On("OnDataTransfer", mock.Anything).Return(dataTransferConfirmation, nil)
75+
} else {
76+
coreListener.On("OnDataTransfer", mock.Anything).Return(nil, nil)
77+
}
78+
79+
err = suite.centralSystem.DataTransfer(wsId, func(confirmation *core.DataTransferConfirmation, err error) {
80+
require.Nil(t, confirmation)
81+
require.Error(t, err)
82+
resultChannel <- err
83+
}, "vendor1")
84+
require.Nil(t, err)
85+
result := <-resultChannel
86+
require.IsType(t, &ocpp.Error{}, result)
87+
ocppErr = result.(*ocpp.Error)
88+
assert.Equal(t, tc.expectedErr.Code, ocppErr.Code)
89+
assert.Equal(t, tc.expectedErr.Description, ocppErr.Description)
90+
})
91+
}
8992
}
9093

9194
func (suite *OcppV16TestSuite) TestCentralSystemSendResponseError() {

0 commit comments

Comments
 (0)