forked from lorenzodonini/ocpp-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_configuration_test.go
101 lines (94 loc) · 5.87 KB
/
get_configuration_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
package ocpp16_test
import (
"fmt"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
// Test
func (suite *OcppV16TestSuite) TestGetConfigurationRequestValidation() {
t := suite.T()
var requestTable = []GenericTestEntry{
{core.GetConfigurationRequest{Key: []string{"key1", "key2"}}, true},
{core.GetConfigurationRequest{Key: []string{"key1", "key2", "key3", "key4", "key5", "key6"}}, true},
{core.GetConfigurationRequest{Key: []string{"key1", "key2", "key2"}}, false},
{core.GetConfigurationRequest{}, true},
{core.GetConfigurationRequest{Key: []string{}}, true},
{core.GetConfigurationRequest{Key: []string{">50................................................"}}, false},
}
ExecuteGenericTestTable(t, requestTable)
}
func (suite *OcppV16TestSuite) TestGetConfigurationConfirmationValidation() {
t := suite.T()
value1 := "value1"
value2 := "value2"
longValue := ">500................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................."
var confirmationTable = []GenericTestEntry{
{core.GetConfigurationConfirmation{ConfigurationKey: []core.ConfigurationKey{{Key: "key1", Readonly: true, Value: &value1}}}, true},
{core.GetConfigurationConfirmation{ConfigurationKey: []core.ConfigurationKey{{Key: "key1", Readonly: true, Value: &value1}, {Key: "key2", Readonly: false, Value: &value2}}}, true},
{core.GetConfigurationConfirmation{ConfigurationKey: []core.ConfigurationKey{{Key: "key1", Readonly: true, Value: &value1}}, UnknownKey: []string{"keyX"}}, true},
{core.GetConfigurationConfirmation{ConfigurationKey: []core.ConfigurationKey{{Key: "key1", Readonly: false, Value: &value1}}, UnknownKey: []string{"keyX", "keyY"}}, true},
{core.GetConfigurationConfirmation{UnknownKey: []string{"keyX"}}, true},
{core.GetConfigurationConfirmation{UnknownKey: []string{">50................................................"}}, false},
{core.GetConfigurationConfirmation{ConfigurationKey: []core.ConfigurationKey{{Key: ">50................................................", Readonly: true, Value: &value1}}}, false},
{core.GetConfigurationConfirmation{ConfigurationKey: []core.ConfigurationKey{{Key: "key1", Readonly: true, Value: &longValue}}}, false},
//{ocpp16.GetConfigurationConfirmation{ConfigurationKey: []ocpp16.ConfigurationKey{{Key: "key1", Readonly: true, Value: "value1"}, {Key: "key1", Readonly: false, Value: "value2"}}}, false},
}
//TODO: additional test cases TBD. See get_configuration.go
ExecuteGenericTestTable(t, confirmationTable)
}
func (suite *OcppV16TestSuite) TestGetConfigurationE2EMocked() {
t := suite.T()
wsId := "test_id"
messageId := defaultMessageId
wsUrl := "someUrl"
key1 := "key1"
key2 := "key2"
someValue := "someValue"
someOtherValue := "someOtherValue"
requestKeys := []string{key1, key2}
resultKey1 := core.ConfigurationKey{Key: key1, Readonly: true, Value: &someValue}
resultKey2 := core.ConfigurationKey{Key: key1, Readonly: true, Value: &someOtherValue}
resultKeys := []core.ConfigurationKey{resultKey1, resultKey2}
unknownKeys := []string{"keyX", "keyY"}
requestJson := fmt.Sprintf(`[2,"%v","%v",{"key":["%v","%v"]}]`, messageId, core.GetConfigurationFeatureName, key1, key2)
responseJson := fmt.Sprintf(`[3,"%v",{"configurationKey":[{"key":"%v","readonly":%v,"value":"%v"},{"key":"%v","readonly":%v,"value":"%v"}],"unknownKey":["%v","%v"]}]`, messageId, resultKey1.Key, resultKey1.Readonly, *resultKey1.Value, resultKey2.Key, resultKey2.Readonly, *resultKey2.Value, unknownKeys[0], unknownKeys[1])
getConfigurationConfirmation := core.NewGetConfigurationConfirmation(resultKeys)
getConfigurationConfirmation.UnknownKey = unknownKeys
channel := NewMockWebSocket(wsId)
coreListener := &MockChargePointCoreListener{}
coreListener.On("OnGetConfiguration", mock.Anything).Return(getConfigurationConfirmation, nil).Run(func(args mock.Arguments) {
request, ok := args.Get(0).(*core.GetConfigurationRequest)
require.NotNil(t, request)
require.True(t, ok)
require.Len(t, request.Key, len(requestKeys))
assert.Equal(t, requestKeys, request.Key)
})
setupDefaultCentralSystemHandlers(suite, nil, expectedCentralSystemOptions{clientId: wsId, rawWrittenMessage: []byte(requestJson), forwardWrittenMessage: true})
setupDefaultChargePointHandlers(suite, coreListener, expectedChargePointOptions{serverUrl: wsUrl, clientId: wsId, createChannelOnStart: true, channel: channel, rawWrittenMessage: []byte(responseJson), forwardWrittenMessage: true})
// Run Test
suite.centralSystem.Start(8887, "somePath")
err := suite.chargePoint.Start(wsUrl)
assert.Nil(t, err)
resultChannel := make(chan bool, 1)
err = suite.centralSystem.GetConfiguration(wsId, func(confirmation *core.GetConfigurationConfirmation, err error) {
require.Nil(t, err)
require.NotNil(t, confirmation)
assert.Equal(t, unknownKeys, confirmation.UnknownKey)
assert.Equal(t, resultKeys, confirmation.ConfigurationKey)
resultChannel <- true
}, requestKeys)
require.Nil(t, err)
result := <-resultChannel
assert.True(t, result)
}
func (suite *OcppV16TestSuite) TestGetConfigurationInvalidEndpoint() {
messageId := defaultMessageId
key1 := "key1"
key2 := "key2"
requestKeys := []string{key1, key2}
getConfigurationRequest := core.NewGetConfigurationRequest(requestKeys)
requestJson := fmt.Sprintf(`[2,"%v","%v",{"key":["%v","%v"]}]`, messageId, core.GetConfigurationFeatureName, key1, key2)
testUnsupportedRequestFromChargePoint(suite, getConfigurationRequest, requestJson, messageId)
}