forked from lorenzodonini/ocpp-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboot_notification_test.go
93 lines (87 loc) · 6.08 KB
/
boot_notification_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
package ocpp16_test
import (
"fmt"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"time"
)
// Tests
func (suite *OcppV16TestSuite) TestBootNotificationRequestValidation() {
t := suite.T()
var requestTable = []GenericTestEntry{
{core.BootNotificationRequest{ChargePointModel: "test", ChargePointVendor: "test"}, true},
{core.BootNotificationRequest{ChargeBoxSerialNumber: "test", ChargePointModel: "test", ChargePointSerialNumber: "number", ChargePointVendor: "test", FirmwareVersion: "version", Iccid: "test", Imsi: "test"}, true},
{core.BootNotificationRequest{ChargeBoxSerialNumber: "test", ChargePointSerialNumber: "number", ChargePointVendor: "test", FirmwareVersion: "version", Iccid: "test", Imsi: "test"}, false},
{core.BootNotificationRequest{ChargeBoxSerialNumber: "test", ChargePointModel: "test", ChargePointSerialNumber: "number", FirmwareVersion: "version", Iccid: "test", Imsi: "test"}, false},
{core.BootNotificationRequest{ChargeBoxSerialNumber: ">25.......................", ChargePointModel: "test", ChargePointVendor: "test"}, false},
{core.BootNotificationRequest{ChargePointModel: ">20..................", ChargePointVendor: "test"}, false},
{core.BootNotificationRequest{ChargePointModel: "test", ChargePointSerialNumber: ">25.......................", ChargePointVendor: "test"}, false},
{core.BootNotificationRequest{ChargePointModel: "test", ChargePointVendor: ">20.................."}, false},
{core.BootNotificationRequest{ChargePointModel: "test", ChargePointVendor: "test", FirmwareVersion: ">50................................................"}, false},
{core.BootNotificationRequest{ChargePointModel: "test", ChargePointVendor: "test", Iccid: ">20.................."}, false},
{core.BootNotificationRequest{ChargePointModel: "test", ChargePointVendor: "test", Imsi: ">20.................."}, false},
{core.BootNotificationRequest{ChargePointModel: "test", ChargePointVendor: "test", MeterSerialNumber: ">25......................."}, false},
{core.BootNotificationRequest{ChargePointModel: "test", ChargePointVendor: "test", MeterType: ">25......................."}, false},
}
ExecuteGenericTestTable(t, requestTable)
}
func (suite *OcppV16TestSuite) TestBootNotificationConfirmationValidation() {
t := suite.T()
var confirmationTable = []GenericTestEntry{
{core.BootNotificationConfirmation{CurrentTime: types.NewDateTime(time.Now()), Interval: 60, Status: core.RegistrationStatusAccepted}, true},
{core.BootNotificationConfirmation{CurrentTime: types.NewDateTime(time.Now()), Interval: 60, Status: core.RegistrationStatusPending}, true},
{core.BootNotificationConfirmation{CurrentTime: types.NewDateTime(time.Now()), Interval: 60, Status: core.RegistrationStatusRejected}, true},
{core.BootNotificationConfirmation{CurrentTime: types.NewDateTime(time.Now()), Status: core.RegistrationStatusAccepted}, true},
{core.BootNotificationConfirmation{CurrentTime: types.NewDateTime(time.Now()), Interval: -1, Status: core.RegistrationStatusRejected}, false},
{core.BootNotificationConfirmation{CurrentTime: types.NewDateTime(time.Now()), Interval: 60, Status: "invalidRegistrationStatus"}, false},
{core.BootNotificationConfirmation{CurrentTime: types.NewDateTime(time.Now()), Interval: 60}, false},
{core.BootNotificationConfirmation{Interval: 60, Status: core.RegistrationStatusAccepted}, false},
}
ExecuteGenericTestTable(t, confirmationTable)
}
func (suite *OcppV16TestSuite) TestBootNotificationE2EMocked() {
t := suite.T()
wsId := "test_id"
messageId := "1234"
wsUrl := "someUrl"
interval := 60
chargePointModel := "model1"
chargePointVendor := "ABL"
registrationStatus := core.RegistrationStatusAccepted
currentTime := types.NewDateTime(time.Now())
requestJson := fmt.Sprintf(`[2,"%v","%v",{"chargePointModel":"%v","chargePointVendor":"%v"}]`, messageId, core.BootNotificationFeatureName, chargePointModel, chargePointVendor)
responseJson := fmt.Sprintf(`[3,"%v",{"currentTime":"%v","interval":%v,"status":"%v"}]`, messageId, currentTime.FormatTimestamp(), interval, registrationStatus)
bootNotificationConfirmation := core.NewBootNotificationConfirmation(currentTime, interval, registrationStatus)
channel := NewMockWebSocket(wsId)
coreListener := MockCentralSystemCoreListener{}
coreListener.On("OnBootNotification", mock.AnythingOfType("string"), mock.Anything).Return(bootNotificationConfirmation, nil).Run(func(args mock.Arguments) {
request, ok := args.Get(1).(*core.BootNotificationRequest)
require.True(t, ok)
require.NotNil(t, request)
assert.Equal(t, chargePointModel, request.ChargePointModel)
assert.Equal(t, chargePointVendor, request.ChargePointVendor)
})
setupDefaultCentralSystemHandlers(suite, coreListener, expectedCentralSystemOptions{clientId: wsId, rawWrittenMessage: []byte(responseJson), forwardWrittenMessage: true})
setupDefaultChargePointHandlers(suite, nil, expectedChargePointOptions{serverUrl: wsUrl, clientId: wsId, createChannelOnStart: true, channel: channel, rawWrittenMessage: []byte(requestJson), forwardWrittenMessage: true})
// Run test
suite.centralSystem.Start(8887, "somePath")
err := suite.chargePoint.Start(wsUrl)
require.Nil(t, err)
confirmation, err := suite.chargePoint.BootNotification(chargePointModel, chargePointVendor)
require.Nil(t, err)
require.NotNil(t, confirmation)
assert.Equal(t, registrationStatus, confirmation.Status)
assert.Equal(t, interval, confirmation.Interval)
assertDateTimeEquality(t, *currentTime, *confirmation.CurrentTime)
}
func (suite *OcppV16TestSuite) TestBootNotificationInvalidEndpoint() {
messageId := defaultMessageId
chargePointModel := "model1"
chargePointVendor := "ABL"
bootNotificationRequest := core.NewBootNotificationRequest(chargePointModel, chargePointVendor)
requestJson := fmt.Sprintf(`[2,"%v","%v",{"chargePointModel":"%v","chargePointVendor":"%v"}]`, messageId, core.BootNotificationFeatureName, chargePointModel, chargePointVendor)
testUnsupportedRequestFromCentralSystem(suite, bootNotificationRequest, requestJson, messageId)
}