github.com/enbility/spine-go@v0.7.0/spine/nodemanagement_test.go (about)

     1  package spine
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/enbility/spine-go/api"
     8  	"github.com/enbility/spine-go/mocks"
     9  	"github.com/enbility/spine-go/model"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/mock"
    12  )
    13  
    14  func TestNodemanagement_BindingCalls(t *testing.T) {
    15  	const bindingEntityId uint = 1
    16  	const featureType = model.FeatureTypeTypeLoadControl
    17  
    18  	senderMock := mocks.NewSenderInterface(t)
    19  
    20  	localDevice, localEntity := createLocalDeviceAndEntity(bindingEntityId)
    21  	_, serverFeature := createLocalFeatures(localEntity, featureType, "")
    22  
    23  	remoteDevice := createRemoteDevice(localDevice, "ski", senderMock)
    24  	clientFeature, _ := createRemoteEntityAndFeature(remoteDevice, bindingEntityId, featureType, "")
    25  
    26  	senderMock.On("Reply", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
    27  		cmd := args.Get(2).(model.CmdType)
    28  		assert.Equal(t, 1, len(cmd.NodeManagementBindingData.BindingEntry))
    29  		assert.True(t, reflect.DeepEqual(cmd.NodeManagementBindingData.BindingEntry[0].ClientAddress, clientFeature.Address()))
    30  		assert.True(t, reflect.DeepEqual(cmd.NodeManagementBindingData.BindingEntry[0].ServerAddress, serverFeature.Address()))
    31  	}).Return(nil).Once()
    32  
    33  	requestMsg := api.Message{
    34  		Cmd: model.CmdType{
    35  			NodeManagementBindingRequestCall: NewNodeManagementBindingRequestCallType(
    36  				clientFeature.Address(), serverFeature.Address(), featureType),
    37  		},
    38  		CmdClassifier: model.CmdClassifierTypeCall,
    39  		FeatureRemote: clientFeature,
    40  	}
    41  
    42  	sut := NewNodeManagement(0, serverFeature.Entity())
    43  
    44  	// Act
    45  	err := sut.HandleMessage(&requestMsg)
    46  	if assert.Nil(t, err) {
    47  		dataMsg := api.Message{
    48  			Cmd: model.CmdType{
    49  				NodeManagementBindingData: &model.NodeManagementBindingDataType{},
    50  			},
    51  			CmdClassifier: model.CmdClassifierTypeCall,
    52  			FeatureRemote: clientFeature,
    53  		}
    54  		err = sut.HandleMessage(&dataMsg)
    55  		assert.Nil(t, err)
    56  	}
    57  
    58  	senderMock.On("Reply", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
    59  		cmd := args.Get(2).(model.CmdType)
    60  		assert.Equal(t, 0, len(cmd.NodeManagementBindingData.BindingEntry))
    61  	}).Return(nil).Once()
    62  
    63  	deleteMsg := api.Message{
    64  		Cmd: model.CmdType{
    65  			NodeManagementBindingDeleteCall: NewNodeManagementBindingDeleteCallType(
    66  				clientFeature.Address(), serverFeature.Address()),
    67  		},
    68  		CmdClassifier: model.CmdClassifierTypeCall,
    69  		FeatureRemote: clientFeature,
    70  	}
    71  
    72  	// Act
    73  	err = sut.HandleMessage(&deleteMsg)
    74  	if assert.Nil(t, err) {
    75  		dataMsg := api.Message{
    76  			Cmd: model.CmdType{
    77  				NodeManagementBindingData: &model.NodeManagementBindingDataType{},
    78  			},
    79  			CmdClassifier: model.CmdClassifierTypeCall,
    80  			FeatureRemote: clientFeature,
    81  		}
    82  		err = sut.HandleMessage(&dataMsg)
    83  		assert.Nil(t, err)
    84  	}
    85  }
    86  
    87  func TestNodemanagement_SubscriptionCalls(t *testing.T) {
    88  	const subscriptionEntityId uint = 1
    89  	const featureType = model.FeatureTypeTypeDeviceClassification
    90  
    91  	senderMock := mocks.NewSenderInterface(t)
    92  
    93  	localDevice, localEntity := createLocalDeviceAndEntity(subscriptionEntityId)
    94  	_, serverFeature := createLocalFeatures(localEntity, featureType, "")
    95  
    96  	remoteDevice := createRemoteDevice(localDevice, "ski", senderMock)
    97  	clientFeature, _ := createRemoteEntityAndFeature(remoteDevice, subscriptionEntityId, featureType, "")
    98  
    99  	senderMock.On("Reply", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
   100  		cmd := args.Get(2).(model.CmdType)
   101  		assert.Equal(t, 1, len(cmd.NodeManagementSubscriptionData.SubscriptionEntry))
   102  		assert.True(t, reflect.DeepEqual(cmd.NodeManagementSubscriptionData.SubscriptionEntry[0].ClientAddress, clientFeature.Address()))
   103  		assert.True(t, reflect.DeepEqual(cmd.NodeManagementSubscriptionData.SubscriptionEntry[0].ServerAddress, serverFeature.Address()))
   104  	}).Return(nil).Once()
   105  
   106  	requestMsg := api.Message{
   107  		Cmd: model.CmdType{
   108  			NodeManagementSubscriptionRequestCall: NewNodeManagementSubscriptionRequestCallType(
   109  				clientFeature.Address(), serverFeature.Address(), featureType),
   110  		},
   111  		CmdClassifier: model.CmdClassifierTypeCall,
   112  		FeatureRemote: clientFeature,
   113  	}
   114  
   115  	sut := NewNodeManagement(0, serverFeature.Entity())
   116  
   117  	// Act
   118  	err := sut.HandleMessage(&requestMsg)
   119  	if assert.Nil(t, err) {
   120  		dataMsg := api.Message{
   121  			Cmd: model.CmdType{
   122  				NodeManagementSubscriptionData: &model.NodeManagementSubscriptionDataType{},
   123  			},
   124  			CmdClassifier: model.CmdClassifierTypeCall,
   125  			FeatureRemote: clientFeature,
   126  		}
   127  		err = sut.HandleMessage(&dataMsg)
   128  		assert.Nil(t, err)
   129  	}
   130  
   131  	senderMock.On("Reply", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
   132  		cmd := args.Get(2).(model.CmdType)
   133  		assert.Equal(t, 0, len(cmd.NodeManagementSubscriptionData.SubscriptionEntry))
   134  	}).Return(nil).Once()
   135  
   136  	deleteMsg := api.Message{
   137  		Cmd: model.CmdType{
   138  			NodeManagementSubscriptionDeleteCall: NewNodeManagementSubscriptionDeleteCallType(
   139  				clientFeature.Address(), serverFeature.Address()),
   140  		},
   141  		CmdClassifier: model.CmdClassifierTypeCall,
   142  		FeatureRemote: clientFeature,
   143  	}
   144  
   145  	// Act
   146  	err = sut.HandleMessage(&deleteMsg)
   147  	if assert.Nil(t, err) {
   148  		dataMsg := api.Message{
   149  			Cmd: model.CmdType{
   150  				NodeManagementSubscriptionData: &model.NodeManagementSubscriptionDataType{},
   151  			},
   152  			CmdClassifier: model.CmdClassifierTypeCall,
   153  			FeatureRemote: clientFeature,
   154  		}
   155  		err = sut.HandleMessage(&dataMsg)
   156  		assert.Nil(t, err)
   157  	}
   158  }