forked from lorenzodonini/ocpp-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_charging_profile_test.go
136 lines (129 loc) · 6.81 KB
/
set_charging_profile_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
package ocpp16_test
import (
"fmt"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/smartcharging"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
// Test
func (suite *OcppV16TestSuite) TestSetChargingProfileRequestValidation() {
t := suite.T()
chargingSchedule := types.NewChargingSchedule(types.ChargingRateUnitWatts, types.NewChargingSchedulePeriod(0, 10.0))
chargingProfile := types.NewChargingProfile(1, 1, types.ChargingProfilePurposeChargePointMaxProfile, types.ChargingProfileKindAbsolute, chargingSchedule)
var requestTable = []GenericTestEntry{
{smartcharging.SetChargingProfileRequest{ConnectorId: 1, ChargingProfile: chargingProfile}, true},
{smartcharging.SetChargingProfileRequest{ChargingProfile: chargingProfile}, true},
{smartcharging.SetChargingProfileRequest{}, false},
{smartcharging.SetChargingProfileRequest{ConnectorId: 1}, false},
{smartcharging.SetChargingProfileRequest{ConnectorId: -1, ChargingProfile: chargingProfile}, false},
}
ExecuteGenericTestTable(t, requestTable)
}
func (suite *OcppV16TestSuite) TestSetChargingProfileConfirmationValidation() {
t := suite.T()
var confirmationTable = []GenericTestEntry{
{smartcharging.SetChargingProfileConfirmation{Status: smartcharging.ChargingProfileStatusAccepted}, true},
{smartcharging.SetChargingProfileConfirmation{Status: "invalidChargingProfileStatus"}, false},
{smartcharging.SetChargingProfileConfirmation{}, false},
}
ExecuteGenericTestTable(t, confirmationTable)
}
func (suite *OcppV16TestSuite) TestSetChargingProfileE2EMocked() {
t := suite.T()
wsId := "test_id"
messageId := defaultMessageId
wsUrl := "someUrl"
connectorId := 1
chargingProfileId := 1
stackLevel := 1
chargingProfilePurpose := types.ChargingProfilePurposeChargePointMaxProfile
chargingProfileKind := types.ChargingProfileKindAbsolute
chargingRateUnit := types.ChargingRateUnitWatts
startPeriod := 0
limit := 10.0
status := smartcharging.ChargingProfileStatusAccepted
chargingSchedule := types.NewChargingSchedule(chargingRateUnit, types.NewChargingSchedulePeriod(startPeriod, limit))
chargingProfile := types.NewChargingProfile(chargingProfileId, stackLevel, chargingProfilePurpose, chargingProfileKind, chargingSchedule)
requestJson := fmt.Sprintf(`[2,"%v","%v",{"connectorId":%v,"csChargingProfiles":{"chargingProfileId":%v,"stackLevel":%v,"chargingProfilePurpose":"%v","chargingProfileKind":"%v","chargingSchedule":{"chargingRateUnit":"%v","chargingSchedulePeriod":[{"startPeriod":%v,"limit":%v}]}}}]`,
messageId,
smartcharging.SetChargingProfileFeatureName,
connectorId,
chargingProfileId,
stackLevel,
chargingProfilePurpose,
chargingProfileKind,
chargingRateUnit,
startPeriod,
limit)
responseJson := fmt.Sprintf(`[3,"%v",{"status":"%v"}]`, messageId, status)
SetChargingProfileConfirmation := smartcharging.NewSetChargingProfileConfirmation(status)
channel := NewMockWebSocket(wsId)
smartChargingListener := MockChargePointSmartChargingListener{}
smartChargingListener.On("OnSetChargingProfile", mock.Anything).Return(SetChargingProfileConfirmation, nil).Run(func(args mock.Arguments) {
request, ok := args.Get(0).(*smartcharging.SetChargingProfileRequest)
require.True(t, ok)
require.NotNil(t, request)
assert.Equal(t, connectorId, request.ConnectorId)
assert.Equal(t, chargingProfileId, request.ChargingProfile.ChargingProfileId)
assert.Equal(t, chargingProfileKind, request.ChargingProfile.ChargingProfileKind)
assert.Equal(t, chargingProfilePurpose, request.ChargingProfile.ChargingProfilePurpose)
assert.Equal(t, types.RecurrencyKindType(""), request.ChargingProfile.RecurrencyKind)
assert.Equal(t, stackLevel, request.ChargingProfile.StackLevel)
assert.Equal(t, 0, request.ChargingProfile.TransactionId)
assert.Nil(t, request.ChargingProfile.ValidFrom)
assert.Nil(t, request.ChargingProfile.ValidTo)
assert.Equal(t, chargingRateUnit, request.ChargingProfile.ChargingSchedule.ChargingRateUnit)
assert.Nil(t, request.ChargingProfile.ChargingSchedule.MinChargingRate)
assert.Nil(t, request.ChargingProfile.ChargingSchedule.Duration)
assert.Nil(t, request.ChargingProfile.ChargingSchedule.StartSchedule)
require.Len(t, request.ChargingProfile.ChargingSchedule.ChargingSchedulePeriod, 1)
assert.Equal(t, limit, request.ChargingProfile.ChargingSchedule.ChargingSchedulePeriod[0].Limit)
assert.Equal(t, startPeriod, request.ChargingProfile.ChargingSchedule.ChargingSchedulePeriod[0].StartPeriod)
assert.Nil(t, request.ChargingProfile.ChargingSchedule.ChargingSchedulePeriod[0].NumberPhases)
})
setupDefaultCentralSystemHandlers(suite, nil, expectedCentralSystemOptions{clientId: wsId, rawWrittenMessage: []byte(requestJson), forwardWrittenMessage: true})
setupDefaultChargePointHandlers(suite, nil, expectedChargePointOptions{serverUrl: wsUrl, clientId: wsId, createChannelOnStart: true, channel: channel, rawWrittenMessage: []byte(responseJson), forwardWrittenMessage: true})
suite.chargePoint.SetSmartChargingHandler(smartChargingListener)
// Run Test
suite.centralSystem.Start(8887, "somePath")
err := suite.chargePoint.Start(wsUrl)
require.Nil(t, err)
resultChannel := make(chan bool, 1)
err = suite.centralSystem.SetChargingProfile(wsId, func(confirmation *smartcharging.SetChargingProfileConfirmation, err error) {
require.Nil(t, err)
require.NotNil(t, confirmation)
assert.Equal(t, status, confirmation.Status)
resultChannel <- true
}, connectorId, chargingProfile)
require.Nil(t, err)
result := <-resultChannel
assert.True(t, result)
}
func (suite *OcppV16TestSuite) TestSetChargingProfileInvalidEndpoint() {
messageId := defaultMessageId
connectorId := 1
chargingProfileId := 1
stackLevel := 1
chargingProfilePurpose := types.ChargingProfilePurposeChargePointMaxProfile
chargingProfileKind := types.ChargingProfileKindAbsolute
chargingRateUnit := types.ChargingRateUnitWatts
startPeriod := 0
limit := 10.0
chargingSchedule := types.NewChargingSchedule(chargingRateUnit, types.NewChargingSchedulePeriod(startPeriod, limit))
chargingProfile := types.NewChargingProfile(chargingProfileId, stackLevel, chargingProfilePurpose, chargingProfileKind, chargingSchedule)
requestJson := fmt.Sprintf(`[2,"%v","%v",{"connectorId":%v,"csChargingProfiles":{"chargingProfileId":%v,"stackLevel":%v,"chargingProfilePurpose":"%v","chargingProfileKind":"%v","chargingSchedule":{"chargingRateUnit":"%v","chargingSchedulePeriod":[{"startPeriod":%v,"limit":%v}]}}}]`,
messageId,
smartcharging.SetChargingProfileFeatureName,
connectorId,
chargingProfileId,
stackLevel,
chargingProfilePurpose,
chargingProfileKind,
chargingRateUnit,
startPeriod,
limit)
SetChargingProfileRequest := smartcharging.NewSetChargingProfileRequest(connectorId, chargingProfile)
testUnsupportedRequestFromChargePoint(suite, SetChargingProfileRequest, requestJson, messageId)
}