forked from lorenzodonini/ocpp-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheartbeat_test.go
64 lines (58 loc) · 2.48 KB
/
heartbeat_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
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/mock"
"github.com/stretchr/testify/require"
"time"
)
// Test
func (suite *OcppV16TestSuite) TestHeartbeatRequestValidation() {
t := suite.T()
var requestTable = []GenericTestEntry{
{core.HeartbeatRequest{}, true},
}
ExecuteGenericTestTable(t, requestTable)
}
func (suite *OcppV16TestSuite) TestHeartbeatConfirmationValidation() {
t := suite.T()
var confirmationTable = []GenericTestEntry{
{core.HeartbeatConfirmation{CurrentTime: types.NewDateTime(time.Now())}, true},
{core.HeartbeatConfirmation{}, false},
}
ExecuteGenericTestTable(t, confirmationTable)
}
func (suite *OcppV16TestSuite) TestHeartbeatE2EMocked() {
t := suite.T()
wsId := "test_id"
messageId := defaultMessageId
wsUrl := "someUrl"
currentTime := types.NewDateTime(time.Now())
requestJson := fmt.Sprintf(`[2,"%v","%v",{}]`, messageId, core.HeartbeatFeatureName)
responseJson := fmt.Sprintf(`[3,"%v",{"currentTime":"%v"}]`, messageId, currentTime.FormatTimestamp())
heartbeatConfirmation := core.NewHeartbeatConfirmation(currentTime)
channel := NewMockWebSocket(wsId)
coreListener := MockCentralSystemCoreListener{}
coreListener.On("OnHeartbeat", mock.AnythingOfType("string"), mock.Anything).Return(heartbeatConfirmation, nil).Run(func(args mock.Arguments) {
request, ok := args.Get(1).(*core.HeartbeatRequest)
require.NotNil(t, request)
require.True(t, ok)
})
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.Heartbeat()
require.Nil(t, err)
require.NotNil(t, confirmation)
assertDateTimeEquality(t, *currentTime, *confirmation.CurrentTime)
}
func (suite *OcppV16TestSuite) TestHeartbeatInvalidEndpoint() {
messageId := defaultMessageId
heartbeatRequest := core.NewHeartbeatRequest()
requestJson := fmt.Sprintf(`[2,"%v","%v",{}]`, messageId, core.HeartbeatFeatureName)
testUnsupportedRequestFromCentralSystem(suite, heartbeatRequest, requestJson, messageId)
}