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

     1  package spine
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/enbility/spine-go/api"
     7  	"github.com/enbility/spine-go/model"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/suite"
    10  )
    11  
    12  const (
    13  	nm_detaileddiscoverydata_send_read_file_prefix     = "./testdata/nm_detaileddiscoverydata_send_read"
    14  	nm_detaileddiscoverydata_recv_read_file_path       = "./testdata/nm_detaileddiscoverydata_recv_read.json"
    15  	nm_detaileddiscoverydata_send_reply_file_prefix    = "./testdata/nm_detaileddiscoverydata_send_reply"
    16  	nm_detaileddiscoverydata_recv_read_ack_file_path   = "./testdata/nm_detaileddiscoverydata_recv_read_ack.json"
    17  	nm_subscriptionRequestCall_recv_call_file_path     = "./testdata/nm_subscriptionRequestCall_recv_call.json"
    18  	nm_subscriptionRequestCall_send_result_file_prefix = "./testdata/nm_subscriptionRequestCall_send_result"
    19  	nm_destinationListData_recv_read_file_path         = "./testdata/nm_destinationListData_recv_read.json"
    20  	nm_destinationListData_send_reply_file_prefix      = "./testdata/nm_destinationListData_send_reply"
    21  )
    22  
    23  func TestNodeManagementSuite(t *testing.T) {
    24  	suite.Run(t, new(NodeManagementSuite))
    25  }
    26  
    27  type NodeManagementSuite struct {
    28  	suite.Suite
    29  	sut api.DeviceLocalInterface
    30  
    31  	remoteSki string
    32  
    33  	writeHandler *WriteMessageHandler
    34  	remoteDevice api.DeviceRemoteInterface
    35  }
    36  
    37  func (s *NodeManagementSuite) BeforeTest(suiteName, testName string) {
    38  	s.sut = NewDeviceLocal("TestBrandName", "TestDeviceModel", "TestSerialNumber", "TestDeviceCode",
    39  		"TestDeviceAddress", model.DeviceTypeTypeEnergyManagementSystem, model.NetworkManagementFeatureSetTypeSmart)
    40  	s.remoteSki = "TestRemoteSki"
    41  
    42  	s.writeHandler = &WriteMessageHandler{}
    43  	_ = s.sut.SetupRemoteDevice(s.remoteSki, s.writeHandler)
    44  	s.remoteDevice = s.sut.RemoteDeviceForSki(s.remoteSki)
    45  }
    46  
    47  func (s *NodeManagementSuite) TestDetailedDiscovery_SendRead() {
    48  	// Act (see BeforeTest)
    49  
    50  	// Assert
    51  	sendBytes := s.writeHandler.LastMessage()
    52  	checkSentData(s.T(), sendBytes, nm_detaileddiscoverydata_send_read_file_prefix)
    53  }
    54  
    55  func (s *NodeManagementSuite) TestDetailedDiscovery_SendReply() {
    56  	// Act
    57  	msgCounter, _ := s.remoteDevice.HandleSpineMesssage(loadFileData(s.T(), nm_detaileddiscoverydata_recv_read_file_path))
    58  
    59  	// Assert
    60  	sendBytes := s.writeHandler.MessageWithReference(msgCounter)
    61  	checkSentData(s.T(), sendBytes, nm_detaileddiscoverydata_send_reply_file_prefix)
    62  }
    63  
    64  func (s *NodeManagementSuite) TestDetailedDiscovery_RecvReply() {
    65  	// Act
    66  	_, _ = s.remoteDevice.HandleSpineMesssage(loadFileData(s.T(), wallbox_detaileddiscoverydata_recv_reply_file_path))
    67  
    68  	// Assert
    69  	remoteDevice := s.sut.RemoteDeviceForSki(s.remoteSki)
    70  	assert.NotNil(s.T(), remoteDevice)
    71  	assert.Equal(s.T(), model.DeviceTypeTypeChargingStation, *remoteDevice.DeviceType())
    72  	assert.Equal(s.T(), model.NetworkManagementFeatureSetTypeSmart, *remoteDevice.FeatureSet())
    73  
    74  	rEntities := remoteDevice.Entities()
    75  	assert.Equal(s.T(), 2, len(rEntities))
    76  	di := rEntities[DeviceInformationEntityId]
    77  	assert.NotNil(s.T(), di)
    78  	assert.Equal(s.T(), model.EntityTypeTypeDeviceInformation, di.EntityType())
    79  
    80  	diFeatures := di.Features()
    81  	assert.Equal(s.T(), 2, len(diFeatures))
    82  
    83  	nm := diFeatures[0]
    84  	assert.Equal(s.T(), NodeManagementFeatureId, uint(*nm.Address().Feature))
    85  	assert.Equal(s.T(), model.FeatureTypeTypeNodeManagement, nm.Type())
    86  	assert.Equal(s.T(), model.RoleTypeSpecial, nm.Role())
    87  	assert.Equal(s.T(), 8, len(nm.Operations()))
    88  
    89  	dc := diFeatures[1]
    90  	assert.Equal(s.T(), 1, int(*dc.Address().Feature))
    91  	assert.Equal(s.T(), model.FeatureTypeTypeDeviceClassification, dc.Type())
    92  	assert.Equal(s.T(), model.RoleTypeServer, dc.Role())
    93  	assert.Equal(s.T(), 1, len(dc.Operations()))
    94  
    95  	evse := rEntities[1]
    96  	assert.NotNil(s.T(), evse)
    97  	assert.Equal(s.T(), model.EntityTypeTypeEVSE, evse.EntityType())
    98  
    99  	evseFeatures := evse.Features()
   100  	assert.Equal(s.T(), 3, len(evseFeatures))
   101  
   102  	evsedc := evseFeatures[0]
   103  	assert.Equal(s.T(), 1, int(*evsedc.Address().Feature))
   104  	assert.Equal(s.T(), model.FeatureTypeTypeDeviceClassification, evsedc.Type())
   105  	assert.Equal(s.T(), model.RoleTypeClient, evsedc.Role())
   106  	assert.Equal(s.T(), 0, len(evsedc.Operations()))
   107  
   108  	evsedd := evseFeatures[1]
   109  	assert.Equal(s.T(), 2, int(*evsedd.Address().Feature))
   110  	assert.Equal(s.T(), model.FeatureTypeTypeDeviceDiagnosis, evsedd.Type())
   111  	assert.Equal(s.T(), model.RoleTypeClient, evsedd.Role())
   112  	assert.Equal(s.T(), 0, len(evsedd.Operations()))
   113  
   114  	evseec := evseFeatures[2]
   115  	assert.Equal(s.T(), 3, int(*evseec.Address().Feature))
   116  	assert.Equal(s.T(), model.FeatureTypeTypeElectricalConnection, evseec.Type())
   117  	assert.Equal(s.T(), model.RoleTypeServer, evseec.Role())
   118  	assert.Equal(s.T(), 0, len(evseec.Operations()))
   119  }
   120  
   121  func (s *NodeManagementSuite) TestDetailedDiscovery_RecvNotifyAdded() {
   122  	_, _ = s.remoteDevice.HandleSpineMesssage(loadFileData(s.T(), wallbox_detaileddiscoverydata_recv_reply_file_path))
   123  
   124  	// Act
   125  	msgCounter, _ := s.remoteDevice.HandleSpineMesssage(loadFileData(s.T(), wallbox_detaileddiscoverydata_recv_notify_file_path))
   126  	waitForAck(s.T(), msgCounter, s.writeHandler)
   127  
   128  	// Assert
   129  	remoteDevice := s.sut.RemoteDeviceForSki(s.remoteSki)
   130  	assert.NotNil(s.T(), remoteDevice)
   131  	assert.Equal(s.T(), model.DeviceTypeTypeChargingStation, *remoteDevice.DeviceType())
   132  	assert.Equal(s.T(), model.NetworkManagementFeatureSetTypeSmart, *remoteDevice.FeatureSet())
   133  
   134  	rEntities := remoteDevice.Entities()
   135  	if assert.Equal(s.T(), 3, len(rEntities)) {
   136  		{
   137  			di := rEntities[DeviceInformationEntityId]
   138  			assert.NotNil(s.T(), di)
   139  			assert.Equal(s.T(), model.EntityTypeTypeDeviceInformation, di.EntityType())
   140  			assert.Equal(s.T(), 2, len(di.Features()))
   141  		}
   142  		{
   143  			evse := rEntities[1]
   144  			assert.NotNil(s.T(), evse)
   145  			assert.Equal(s.T(), model.EntityTypeTypeEVSE, evse.EntityType())
   146  			assert.Equal(s.T(), 3, len(evse.Features()))
   147  		}
   148  		{
   149  			ev := rEntities[2]
   150  			assert.NotNil(s.T(), ev)
   151  			assert.Equal(s.T(), model.EntityTypeTypeEV, ev.EntityType())
   152  			assert.Equal(s.T(), 10, len(ev.Features()))
   153  		}
   154  	}
   155  
   156  	// Act
   157  	msgCounter, _ = s.remoteDevice.HandleSpineMesssage(loadFileData(s.T(), wallbox_detaileddiscoverydata_recv_notify_remove_file_path))
   158  	waitForAck(s.T(), msgCounter, s.writeHandler)
   159  
   160  	// Assert
   161  	rEntities = remoteDevice.Entities()
   162  	if assert.Equal(s.T(), 2, len(rEntities)) {
   163  		{
   164  			di := rEntities[DeviceInformationEntityId]
   165  			assert.NotNil(s.T(), di)
   166  			assert.Equal(s.T(), model.EntityTypeTypeDeviceInformation, di.EntityType())
   167  			assert.Equal(s.T(), 2, len(di.Features()))
   168  		}
   169  		{
   170  			evse := rEntities[1]
   171  			assert.NotNil(s.T(), evse)
   172  			assert.Equal(s.T(), model.EntityTypeTypeEVSE, evse.EntityType())
   173  			assert.Equal(s.T(), 3, len(evse.Features()))
   174  		}
   175  	}
   176  }
   177  
   178  func (s *NodeManagementSuite) TestDetailedDiscovery_RecvNotifyFullAdded() {
   179  	_, _ = s.remoteDevice.HandleSpineMesssage(loadFileData(s.T(), wallbox_detaileddiscoverydata_recv_reply_full_file_path))
   180  
   181  	// Act
   182  	_, _ = s.remoteDevice.HandleSpineMesssage(loadFileData(s.T(), wallbox_detaileddiscoverydata_recv_notify_full_file_path))
   183  
   184  	// Assert
   185  	remoteDevice := s.sut.RemoteDeviceForSki(s.remoteSki)
   186  	assert.NotNil(s.T(), remoteDevice)
   187  	assert.Equal(s.T(), model.DeviceTypeTypeChargingStation, *remoteDevice.DeviceType())
   188  	assert.Equal(s.T(), model.NetworkManagementFeatureSetTypeSmart, *remoteDevice.FeatureSet())
   189  
   190  	rEntities := remoteDevice.Entities()
   191  	if assert.Equal(s.T(), 3, len(rEntities)) {
   192  		{
   193  			di := rEntities[DeviceInformationEntityId]
   194  			assert.NotNil(s.T(), di)
   195  			assert.Equal(s.T(), model.EntityTypeTypeDeviceInformation, di.EntityType())
   196  			assert.Equal(s.T(), 1, len(di.Features()))
   197  		}
   198  		{
   199  			evse := rEntities[1]
   200  			assert.NotNil(s.T(), evse)
   201  			assert.Equal(s.T(), model.EntityTypeTypeEVSE, evse.EntityType())
   202  			assert.Equal(s.T(), 3, len(evse.Features()))
   203  		}
   204  		{
   205  			ev := rEntities[2]
   206  			assert.NotNil(s.T(), ev)
   207  			assert.Equal(s.T(), model.EntityTypeTypeEV, ev.EntityType())
   208  			assert.Equal(s.T(), 6, len(ev.Features()))
   209  		}
   210  	}
   211  
   212  	// Act
   213  	_, _ = s.remoteDevice.HandleSpineMesssage(loadFileData(s.T(), wallbox_detaileddiscoverydata_recv_notify_remove_full_file_path))
   214  
   215  	// Assert
   216  	rEntities = remoteDevice.Entities()
   217  	if assert.Equal(s.T(), 2, len(rEntities)) {
   218  		{
   219  			di := rEntities[DeviceInformationEntityId]
   220  			assert.NotNil(s.T(), di)
   221  			assert.Equal(s.T(), model.EntityTypeTypeDeviceInformation, di.EntityType())
   222  			assert.Equal(s.T(), 1, len(di.Features()))
   223  		}
   224  		{
   225  			evse := rEntities[1]
   226  			assert.NotNil(s.T(), evse)
   227  			assert.Equal(s.T(), model.EntityTypeTypeEVSE, evse.EntityType())
   228  			assert.Equal(s.T(), 3, len(evse.Features()))
   229  		}
   230  	}
   231  }
   232  
   233  func (s *NodeManagementSuite) TestDetailedDiscovery_SendReplyWithAcknowledge() {
   234  	// Act
   235  	msgCounter, _ := s.remoteDevice.HandleSpineMesssage(loadFileData(s.T(), nm_detaileddiscoverydata_recv_read_ack_file_path))
   236  
   237  	// Assert
   238  	sentReply := s.writeHandler.MessageWithReference(msgCounter)
   239  	checkSentData(s.T(), sentReply, nm_detaileddiscoverydata_send_reply_file_prefix)
   240  	// on successful reply, no result should be sent
   241  }
   242  
   243  func (s *NodeManagementSuite) TestSubscriptionRequestCall_BeforeDetailedDiscovery() {
   244  	// Act
   245  	msgCounter, _ := s.remoteDevice.HandleSpineMesssage(loadFileData(s.T(), nm_subscriptionRequestCall_recv_call_file_path))
   246  
   247  	// Assert
   248  	sentResult := s.writeHandler.ResultWithReference(msgCounter)
   249  	checkSentData(s.T(), sentResult, nm_subscriptionRequestCall_send_result_file_prefix)
   250  
   251  	remoteDevice := s.sut.RemoteDeviceForSki(s.remoteSki)
   252  	subscriptionsForDevice := s.sut.SubscriptionManager().Subscriptions(remoteDevice)
   253  	assert.Equal(s.T(), 1, len(subscriptionsForDevice))
   254  	subscriptionsOnFeature := s.sut.SubscriptionManager().SubscriptionsOnFeature(*NodeManagementAddress(s.sut.Address()))
   255  	assert.Equal(s.T(), 1, len(subscriptionsOnFeature))
   256  }
   257  
   258  func (s *NodeManagementSuite) TestDestinationList_SendReply() {
   259  	// Act
   260  	msgCounter, _ := s.remoteDevice.HandleSpineMesssage(loadFileData(s.T(), nm_destinationListData_recv_read_file_path))
   261  
   262  	// Assert
   263  	sendBytes := s.writeHandler.MessageWithReference(msgCounter)
   264  	checkSentData(s.T(), sendBytes, nm_destinationListData_send_reply_file_prefix)
   265  }