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

     1  package integrationtests
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/enbility/spine-go/api"
     8  	"github.com/enbility/spine-go/model"
     9  	"github.com/enbility/spine-go/spine"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/suite"
    12  )
    13  
    14  const (
    15  	m_subscriptionRequestCall_recv_result_file_path = "./testdata/m_subscriptionRequestCall_recv_result.json"
    16  	m_descriptionListData_recv_reply_file_path      = "./testdata/m_descriptionListData_recv_reply.json"
    17  	m_measurementListData_recv_notify_file_path     = "./testdata/m_measurementListData_recv_notify.json"
    18  )
    19  
    20  func TestMeasurementSuite(t *testing.T) {
    21  	suite.Run(t, new(MeasurementSuite))
    22  }
    23  
    24  type MeasurementSuite struct {
    25  	suite.Suite
    26  	sut api.DeviceLocalInterface
    27  
    28  	remoteSki string
    29  
    30  	remoteDevice api.DeviceRemoteInterface
    31  	writeHandler *WriteMessageHandler
    32  }
    33  
    34  func (s *MeasurementSuite) SetupSuite() {
    35  }
    36  
    37  func (s *MeasurementSuite) BeforeTest(suiteName, testName string) {
    38  	s.sut, s.remoteSki, s.remoteDevice, s.writeHandler = beforeTest(2, model.FeatureTypeTypeMeasurement, model.RoleTypeClient)
    39  	initialCommunication(s.T(), s.remoteDevice, s.writeHandler)
    40  }
    41  
    42  func (s *MeasurementSuite) AfterTest(suiteName, testName string) {
    43  }
    44  
    45  func (s *MeasurementSuite) TestDescriptionList_Recv() {
    46  	// Act
    47  	msgCounter, _ := s.remoteDevice.HandleSpineMesssage(loadFileData(s.T(), m_descriptionListData_recv_reply_file_path))
    48  	waitForAck(s.T(), msgCounter, s.writeHandler)
    49  
    50  	// Assert
    51  	remoteDevice := s.sut.RemoteDeviceForSki(s.remoteSki)
    52  	assert.NotNil(s.T(), remoteDevice)
    53  
    54  	mFeature := remoteDevice.FeatureByEntityTypeAndRole(
    55  		remoteDevice.Entity(spine.NewAddressEntityType([]uint{1, 1})),
    56  		model.FeatureTypeTypeMeasurement,
    57  		model.RoleTypeServer)
    58  	assert.NotNil(s.T(), mFeature)
    59  
    60  	fdata := mFeature.DataCopy(model.FunctionTypeMeasurementDescriptionListData)
    61  	if !assert.NotNil(s.T(), fdata) {
    62  		return
    63  	}
    64  	data := fdata.(*model.MeasurementDescriptionListDataType)
    65  
    66  	if !assert.Equal(s.T(), 3, len(data.MeasurementDescriptionData)) {
    67  		return
    68  	}
    69  
    70  	item1 := data.MeasurementDescriptionData[0]
    71  	assert.Equal(s.T(), 1, int(*item1.MeasurementId))
    72  	assert.Equal(s.T(), string(model.MeasurementTypeTypeCurrent), string(*item1.MeasurementType))
    73  	assert.Equal(s.T(), string(model.CommodityTypeTypeElectricity), string(*item1.CommodityType))
    74  	assert.Equal(s.T(), string(model.UnitOfMeasurementTypeA), string(*item1.Unit))
    75  	assert.Equal(s.T(), string(model.ScopeTypeTypeACCurrent), string(*item1.ScopeType))
    76  }
    77  
    78  func (s *MeasurementSuite) TestMeasurementList_Recv() {
    79  	// Act
    80  	msgCounter, _ := s.remoteDevice.HandleSpineMesssage(loadFileData(s.T(), m_measurementListData_recv_notify_file_path))
    81  	waitForAck(s.T(), msgCounter, s.writeHandler)
    82  
    83  	// Assert
    84  	remoteDevice := s.sut.RemoteDeviceForSki(s.remoteSki)
    85  	assert.NotNil(s.T(), remoteDevice)
    86  
    87  	mFeature := remoteDevice.FeatureByEntityTypeAndRole(
    88  		remoteDevice.Entity(spine.NewAddressEntityType([]uint{1, 1})),
    89  		model.FeatureTypeTypeMeasurement,
    90  		model.RoleTypeServer)
    91  	assert.NotNil(s.T(), mFeature)
    92  
    93  	fdata := mFeature.DataCopy(model.FunctionTypeMeasurementListData)
    94  	if !assert.NotNil(s.T(), fdata) {
    95  		return
    96  	}
    97  	data := fdata.(*model.MeasurementListDataType)
    98  
    99  	if !assert.Equal(s.T(), 3, len(data.MeasurementData)) {
   100  		return
   101  	}
   102  
   103  	item1 := data.MeasurementData[0]
   104  	assert.Equal(s.T(), 1, int(*item1.MeasurementId))
   105  	assert.Equal(s.T(), string(model.MeasurementValueTypeTypeValue), string(*item1.ValueType))
   106  	assert.Equal(s.T(), 5.0, item1.Value.GetValue())
   107  	timestamp, err := item1.Timestamp.GetDateTimeType().GetTime()
   108  	assert.Nil(s.T(), err)
   109  	compareTimestamp := time.Date(
   110  		2022, 11, 19, 15, 21, 50, 3000000, time.UTC)
   111  	assert.Equal(s.T(), compareTimestamp, timestamp)
   112  	assert.Equal(s.T(), string(model.MeasurementValueSourceTypeMeasuredValue), string(*item1.ValueSource))
   113  }
   114  
   115  func (s *MeasurementSuite) TestMeasurementByScope_Recv() {
   116  	// Act
   117  	msgCounter, _ := s.remoteDevice.HandleSpineMesssage(loadFileData(s.T(), m_descriptionListData_recv_reply_file_path))
   118  	waitForAck(s.T(), msgCounter, s.writeHandler)
   119  
   120  	// Act
   121  	msgCounter, _ = s.remoteDevice.HandleSpineMesssage(loadFileData(s.T(), m_measurementListData_recv_notify_file_path))
   122  	waitForAck(s.T(), msgCounter, s.writeHandler)
   123  
   124  	// Assert
   125  	remoteDevice := s.sut.RemoteDeviceForSki(s.remoteSki)
   126  	assert.NotNil(s.T(), remoteDevice)
   127  
   128  	mFeature := remoteDevice.FeatureByEntityTypeAndRole(
   129  		remoteDevice.Entity(spine.NewAddressEntityType([]uint{1, 1})),
   130  		model.FeatureTypeTypeMeasurement,
   131  		model.RoleTypeServer)
   132  	assert.NotNil(s.T(), mFeature)
   133  
   134  	fdata := mFeature.DataCopy(model.FunctionTypeMeasurementDescriptionListData)
   135  	if !assert.NotNil(s.T(), fdata) {
   136  		return
   137  	}
   138  	descData := fdata.(*model.MeasurementDescriptionListDataType)
   139  
   140  	if !assert.Equal(s.T(), 3, len(descData.MeasurementDescriptionData)) {
   141  		return
   142  	}
   143  
   144  	fdata = mFeature.DataCopy(model.FunctionTypeMeasurementListData)
   145  	if !assert.NotNil(s.T(), fdata) {
   146  		return
   147  	}
   148  	mData := fdata.(*model.MeasurementListDataType)
   149  
   150  	if !assert.Equal(s.T(), 3, len(mData.MeasurementData)) {
   151  		return
   152  	}
   153  
   154  	item1 := mData.MeasurementData[0]
   155  	assert.Equal(s.T(), 1, int(*item1.MeasurementId))
   156  	assert.Equal(s.T(), string(model.MeasurementValueTypeTypeValue), string(*item1.ValueType))
   157  	assert.Equal(s.T(), 5.0, item1.Value.GetValue())
   158  	timestamp, err := item1.Timestamp.GetDateTimeType().GetTime()
   159  	assert.Nil(s.T(), err)
   160  	compareTimestamp := time.Date(
   161  		2022, 11, 19, 15, 21, 50, 3000000, time.UTC)
   162  	assert.Equal(s.T(), compareTimestamp, timestamp)
   163  	assert.Equal(s.T(), string(model.MeasurementValueSourceTypeMeasuredValue), string(*item1.ValueSource))
   164  }