Skip to content

Commit

Permalink
Added Manager tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xBlaz3kx committed Jul 23, 2024
1 parent de3885f commit fd2d8e1
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 35 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.1
replace github.com/lorenzodonini/ocpp-go v0.18.0 => github.com/ChargePi/ocpp-go v0.18.1

require (
github.com/agrison/go-commons-lang v0.0.0-20240106075236-2e001e6401ef
github.com/lorenzodonini/ocpp-go v0.18.0
github.com/samber/lo v1.46.0
github.com/sirupsen/logrus v1.9.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/ChargePi/ocpp-go v0.18.1 h1:Il2g9EVknUpK5qnRV/jOk+QET1/s9cDyLGkUDIUct
github.com/ChargePi/ocpp-go v0.18.1/go.mod h1:ZynYDWGw6CslG3vyPuucLsy6AyE+h3XXYlr39jhNiQY=
github.com/Shopify/toxiproxy v2.1.4+incompatible h1:TKdv8HiTLgE5wdJuEML90aBgNWsokNbMijUGhmcoBJc=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/agrison/go-commons-lang v0.0.0-20240106075236-2e001e6401ef h1:KkznClyESbRaLmRo7Oam4vv5L4oknDK+mixJ9mypl6E=
github.com/agrison/go-commons-lang v0.0.0-20240106075236-2e001e6401ef/go.mod h1:u+Zwm0OKtJAGx+DXcmp2NNwZ0GKtV80ipbF/uhKhQdw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
9 changes: 9 additions & 0 deletions ocpp_v16/manager.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package ocpp_v16

import (
"errors"
"fmt"
"sync"

"github.com/agrison/go-commons-lang/stringUtils"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
"github.com/samber/lo"
)

var ErrKeyCannotBeEmpty = errors.New("key cannot be empty")

type (
KeyValidator func(Key Key, value *string) bool
OnUpdateHandler func(value *string) error
Expand Down Expand Up @@ -159,6 +163,11 @@ func (m *ManagerV16) ValidateKey(key Key, value *string) error {

// OnUpdateKey registers a function to call after a specific key has been updated.
func (m *ManagerV16) OnUpdateKey(key Key, handler OnUpdateHandler) error {
if stringUtils.IsEmpty(key.String()) {
return ErrKeyCannotBeEmpty
}

// Validate that the key exists
_, err := m.ocppConfig.GetConfigurationValue(key.String())
if err != nil {
return err
Expand Down
207 changes: 172 additions & 35 deletions ocpp_v16/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,201 @@ import (
"github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/localauth"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/smartcharging"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/types"
"github.com/samber/lo"
"github.com/stretchr/testify/suite"
)

type ConfigurationManagerTestSuite struct {
suite.Suite
config Config
manager Manager
manager *ManagerV16
}

func (s *ConfigurationManagerTestSuite) SetupTest() {
val := strings.Join(
[]string{
string(types.MeasurandEnergyActiveExportInterval),
string(types.MeasurandCurrentExport),
string(types.MeasurandVoltage),
},
",")

val1 := "60"

s.config = Config{
Version: 1,
Keys: []core.ConfigurationKey{
{
Key: HeartbeatInterval.String(),
Readonly: false,
Value: &val1,
}, {
Key: ChargeProfileMaxStackLevel.String(),
Readonly: false,
Value: nil,
},
{
Key: MeterValuesSampledData.String(),
Readonly: false,
Value: &val,
},
}}

var err error
s.manager, err = NewV16ConfigurationManager(s.config, core.ProfileName, smartcharging.ProfileName, localauth.ProfileName)
profiles := []string{core.ProfileName, smartcharging.ProfileName, localauth.ProfileName}
configuration, err := DefaultConfigurationFromProfiles(profiles...)
s.Require().NoError(err)

s.manager, err = NewV16ConfigurationManager(*configuration, profiles...)
s.Assert().NoError(err)
}

func (s *ConfigurationManagerTestSuite) TestNewV16ConfigurationManager() {
configuration, err := DefaultConfigurationFromProfiles(core.ProfileName)
s.Assert().NoError(err)

// Valid configuration
manager, err := NewV16ConfigurationManager(*configuration, core.ProfileName)
s.Assert().NoError(err)
s.Assert().NotNil(manager)

// Invalid configuration
manager, err = NewV16ConfigurationManager(NewEmptyConfiguration(), core.ProfileName)
s.Assert().Error(err)
s.Assert().Nil(manager)
}

func (s *ConfigurationManagerTestSuite) TestGetConfiguration() {
// todo
}

func (s *ConfigurationManagerTestSuite) TestUpdateConfiguration() {
// todo
// Key found
err := s.manager.UpdateKey(HeartbeatInterval, lo.ToPtr("123"))
s.Assert().NoError(err)
value, err := s.manager.GetConfigurationValue(HeartbeatInterval)
s.Assert().NoError(err)
s.Assert().NotNil(value)
s.Assert().Equal("123", *value)

err = s.manager.UpdateKey(HeartbeatInterval, nil)
s.Assert().NoError(err)
s.Assert().Nil(value)

// Key not found
err = s.manager.UpdateKey("ExampleKey", lo.ToPtr("exampleValue"))
s.Assert().Error(err)

err = s.manager.UpdateKey("", lo.ToPtr("exampleValue"))
s.Assert().Error(err)

err = s.manager.UpdateKey("", nil)
s.Assert().Error(err)
}

func (s *ConfigurationManagerTestSuite) TestGetConfigurationValue() {
// Tested in the UpdateConfiguration test
}

func (s *ConfigurationManagerTestSuite) TestOnUpdateKey() {
numExecutions := 0

err := s.manager.OnUpdateKey(HeartbeatInterval, func(value *string) error {
numExecutions++
return nil
})
_ = s.manager.UpdateKey(HeartbeatInterval, lo.ToPtr("exampleValue"))
_ = s.manager.UpdateKey(HeartbeatInterval, lo.ToPtr("exampleValue"))
_ = s.manager.UpdateKey(HeartbeatInterval, lo.ToPtr("exampleValue"))
s.Assert().NoError(err)
s.Assert().Equal(3, numExecutions)

err = s.manager.OnUpdateKey("ExampleKey", func(value *string) error {
return nil
})
s.Assert().Error(err)

err = s.manager.UpdateKey("ExampleKey", lo.ToPtr("exampleValue"))
s.Assert().Error(err)

err = s.manager.OnUpdateKey("", func(value *string) error {
numExecutions++
return nil
})
s.Assert().Error(err)

err = s.manager.UpdateKey("", lo.ToPtr("exampleValue"))
s.Assert().Error(err)
s.Assert().Equal(3, numExecutions)
}

func (s *ConfigurationManagerTestSuite) TestSetConfiguration() {
// todo
}

func (s *ConfigurationManagerTestSuite) TestValidateKey() {

s.manager.RegisterCustomKeyValidator(func(key Key, value *string) bool {
switch key {
case HeartbeatInterval:
if value == nil {
return false
}
return true
case LocalAuthListEnabled:
if value == nil {
return false
}

if strings.ToUpper(*value) == "TRUE" || strings.ToUpper(*value) == "FALSE" {
return true
}

return false
default:
return false
}
})

err := s.manager.ValidateKey(HeartbeatInterval, lo.ToPtr("123"))
s.Assert().NoError(err)
// Should fail - invalid value
err = s.manager.ValidateKey(HeartbeatInterval, nil)
s.Assert().Error(err)

err = s.manager.ValidateKey(LocalAuthListEnabled, lo.ToPtr("true"))
s.Assert().NoError(err)

err = s.manager.ValidateKey(LocalAuthListEnabled, lo.ToPtr("false"))
s.Assert().NoError(err)

// Should fail - invalid value
err = s.manager.ValidateKey(LocalAuthListEnabled, nil)
s.Assert().Error(err)

err = s.manager.ValidateKey(LocalAuthListEnabled, lo.ToPtr("aaaaa"))
s.Assert().Error(err)

// Should fail - invalid key
err = s.manager.ValidateKey("ABCD", lo.ToPtr("aaaaa"))
s.Assert().Error(err)

err = s.manager.ValidateKey("ABCD", nil)
s.Assert().Error(err)
}

func (s *ConfigurationManagerTestSuite) TestRegisterCustomKeyValidator() {
exampleKey := Key("ExampleKey")
numExecutions := 0

s.manager.RegisterCustomKeyValidator(func(key Key, value *string) bool {
numExecutions++
return exampleKey == key && value != nil && *value == "exampleValue"
})

_ = s.manager.UpdateKey(exampleKey, lo.ToPtr("exampleValue"))
s.Assert().Equal(1, numExecutions)
}

func (s *ConfigurationManagerTestSuite) TestGetMandatoryKeys() {
configuration, err := DefaultConfigurationFromProfiles(core.ProfileName)
s.Assert().NoError(err)

// Valid configuration
manager, err := NewV16ConfigurationManager(*configuration, core.ProfileName)
s.Assert().NoError(err)

s.ElementsMatch(MandatoryCoreKeys, manager.GetMandatoryKeys())

configuration, err = DefaultConfigurationFromProfiles(core.ProfileName, localauth.ProfileName)
s.Assert().NoError(err)
manager, err = NewV16ConfigurationManager(*configuration, core.ProfileName, localauth.ProfileName)
s.Assert().NoError(err)

keys := append(MandatoryCoreKeys, MandatoryLocalAuthKeys...)
s.ElementsMatch(keys, manager.GetMandatoryKeys())
}

func (s *ConfigurationManagerTestSuite) TestSetMandatoryKeys() {
configuration, err := DefaultConfigurationFromProfiles(core.ProfileName)
s.Assert().NoError(err)

// Valid configuration
manager, err := NewV16ConfigurationManager(*configuration, core.ProfileName)
s.Assert().NoError(err)
s.Assert().NotNil(manager)

}

func TestConfigurationManager(t *testing.T) {
suite.Run(t, new(ConfigurationManagerTestSuite))
}

0 comments on commit fd2d8e1

Please sign in to comment.