Skip to content

Commit

Permalink
Improve filter read requests and tests
Browse files Browse the repository at this point in the history
Make sure a filter on read requests is only send, if there really is a filter requested.
  • Loading branch information
DerAndereAndi committed Jul 5, 2024
1 parent 85d5d79 commit fb2969d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
4 changes: 2 additions & 2 deletions spine/feature_local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func (s *LocalFeatureTestSuite) BeforeTest(suiteName, testName string) {
_, s.localServerFeatureWrite = createLocalFeatures(s.localEntity, s.subFeatureType, s.serverWriteFunction)

remoteDevice := createRemoteDevice(s.localDevice, s.senderMock)
s.remoteFeature, s.remoteServerFeature = createRemoteEntityAndFeature(remoteDevice, 1, s.featureType)
s.remoteSubFeature, _ = createRemoteEntityAndFeature(remoteDevice, 2, s.subFeatureType)
s.remoteFeature, s.remoteServerFeature = createRemoteEntityAndFeature(remoteDevice, 1, s.featureType, s.function)
s.remoteSubFeature, _ = createRemoteEntityAndFeature(remoteDevice, 2, s.subFeatureType, s.serverWriteFunction)
}

func (s *LocalFeatureTestSuite) TestDeviceClassification_Functions() {
Expand Down
12 changes: 6 additions & 6 deletions spine/function_data_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,23 @@ func (r *FunctionDataCmd[T]) NotifyOrWriteCmdType(deleteSelector, partialSelecto
}

func filtersForSelectorsElements(functionType model.FunctionType, filters []model.FilterType, deleteSelector, partialSelector any, deleteElements, readElements any) []model.FilterType {
if deleteSelector != nil || deleteElements != nil {
if !util.IsNil(deleteSelector) || !util.IsNil(deleteElements) {
filter := model.FilterType{CmdControl: &model.CmdControlType{Delete: &model.ElementTagType{}}}
if deleteSelector != nil {
if !util.IsNil(deleteSelector) {
filter = addSelectorToFilter(filter, functionType, &deleteSelector)
}
if deleteElements != nil {
if !util.IsNil(deleteElements) {
filter = addElementToFilter(filter, functionType, &deleteElements)
}
filters = append(filters, filter)
}

if partialSelector != nil || readElements != nil {
if !util.IsNil(partialSelector) || !util.IsNil(readElements) {
filter := model.FilterType{CmdControl: &model.CmdControlType{Partial: &model.ElementTagType{}}}
if partialSelector != nil {
if !util.IsNil(partialSelector) {
filter = addSelectorToFilter(filter, functionType, partialSelector)
}
if readElements != nil {
if !util.IsNil(readElements != nil) {
filter = addElementToFilter(filter, functionType, readElements)
}
filters = append(filters, filter)
Expand Down
12 changes: 11 additions & 1 deletion spine/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,22 @@ func createRemoteDevice(localDevice *DeviceLocal, sender api.SenderInterface) *D
return remoteDevice
}

func createRemoteEntityAndFeature(remoteDevice *DeviceRemote, entityId uint, featureType model.FeatureTypeType) (api.FeatureRemoteInterface, api.FeatureRemoteInterface) {
func createRemoteEntityAndFeature(remoteDevice *DeviceRemote, entityId uint, featureType model.FeatureTypeType, functionType model.FunctionType) (api.FeatureRemoteInterface, api.FeatureRemoteInterface) {
remoteEntity := NewEntityRemote(remoteDevice, model.EntityTypeTypeEVSE, []model.AddressEntityType{model.AddressEntityType(entityId)})
remoteDevice.AddEntity(remoteEntity)
remoteFeature := NewFeatureRemote(remoteEntity.NextFeatureId(), remoteEntity, featureType, model.RoleTypeClient)
remoteEntity.AddFeature(remoteFeature)
remoteServerFeature := NewFeatureRemote(remoteEntity.NextFeatureId(), remoteEntity, featureType, model.RoleTypeServer)
remoteServerFeature.SetOperations([]model.FunctionPropertyType{
{
Function: util.Ptr(functionType),
PossibleOperations: &model.PossibleOperationsType{
Read: &model.PossibleOperationsReadType{
Partial: &model.ElementTagType{},
},
},
},
})
remoteEntity.AddFeature(remoteServerFeature)

return remoteFeature, remoteServerFeature
Expand Down
4 changes: 2 additions & 2 deletions spine/nodemanagement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestNodemanagement_BindingCalls(t *testing.T) {
_, serverFeature := createLocalFeatures(localEntity, featureType, "")

remoteDevice := createRemoteDevice(localDevice, senderMock)
clientFeature, _ := createRemoteEntityAndFeature(remoteDevice, bindingEntityId, featureType)
clientFeature, _ := createRemoteEntityAndFeature(remoteDevice, bindingEntityId, featureType, "")

senderMock.On("Reply", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
cmd := args.Get(2).(model.CmdType)
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestNodemanagement_SubscriptionCalls(t *testing.T) {
_, serverFeature := createLocalFeatures(localEntity, featureType, "")

remoteDevice := createRemoteDevice(localDevice, senderMock)
clientFeature, _ := createRemoteEntityAndFeature(remoteDevice, subscriptionEntityId, featureType)
clientFeature, _ := createRemoteEntityAndFeature(remoteDevice, subscriptionEntityId, featureType, "")

senderMock.On("Reply", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
cmd := args.Get(2).(model.CmdType)
Expand Down
17 changes: 17 additions & 0 deletions util/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@ package util

import (
"encoding/json"
"reflect"
)

// quick way to a struct into another
func DeepCopy[A any](source, dest A) {
byt, _ := json.Marshal(source)
_ = json.Unmarshal(byt, dest)
}

// checck if a value is nil for any type
func IsNil(data any) bool {
if data == nil {
return true
}
switch reflect.TypeOf(data).Kind() {
case reflect.Ptr,
reflect.Map,
reflect.Array,
reflect.Chan,
reflect.Slice:
return reflect.ValueOf(data).IsNil()
}
return false
}

0 comments on commit fb2969d

Please sign in to comment.