Skip to content

Commit

Permalink
Reorganize tests a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
DerAndereAndi committed Jan 1, 2024
1 parent 9716649 commit 2617704
Show file tree
Hide file tree
Showing 16 changed files with 212 additions and 28 deletions.
165 changes: 165 additions & 0 deletions spine/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package spine

import (
"encoding/json"
"fmt"
"os"
"sync"
"testing"

"github.com/enbility/eebus-go/spine/model"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
)

const (
wallbox_detaileddiscoverydata_recv_reply_file_path = "./testdata/wallbox_detaileddiscoverydata_recv_reply.json"
wallbox_detaileddiscoverydata_recv_notify_file_path = "./testdata/wallbox_detaileddiscoverydata_recv_notify.json"
)

type WriteMessageHandler struct {
sentMessages [][]byte

mux sync.Mutex
}

var _ SpineDataConnection = (*WriteMessageHandler)(nil)

func (t *WriteMessageHandler) WriteSpineMessage(message []byte) {
t.mux.Lock()
defer t.mux.Unlock()

t.sentMessages = append(t.sentMessages, message)
}

func (t *WriteMessageHandler) LastMessage() []byte {
t.mux.Lock()
defer t.mux.Unlock()

if len(t.sentMessages) == 0 {
return nil
}

return t.sentMessages[len(t.sentMessages)-1]
}

func (t *WriteMessageHandler) MessageWithReference(msgCounterReference *model.MsgCounterType) []byte {
t.mux.Lock()
defer t.mux.Unlock()

var datagram model.Datagram

for _, msg := range t.sentMessages {
if err := json.Unmarshal(msg, &datagram); err != nil {
return nil
}
if datagram.Datagram.Header.MsgCounterReference == nil {
continue
}
if uint(*datagram.Datagram.Header.MsgCounterReference) != uint(*msgCounterReference) {
continue
}
if datagram.Datagram.Payload.Cmd[0].ResultData != nil {
continue
}

return msg
}

return nil
}

func (t *WriteMessageHandler) ResultWithReference(msgCounterReference *model.MsgCounterType) []byte {
t.mux.Lock()
defer t.mux.Unlock()

var datagram model.Datagram

for _, msg := range t.sentMessages {
if err := json.Unmarshal(msg, &datagram); err != nil {
return nil
}
if datagram.Datagram.Header.MsgCounterReference == nil {
continue
}
if uint(*datagram.Datagram.Header.MsgCounterReference) != uint(*msgCounterReference) {
continue
}
if datagram.Datagram.Payload.Cmd[0].ResultData == nil {
continue
}

return msg
}

return nil
}

func loadFileData(t *testing.T, fileName string) []byte {
fileData, err := os.ReadFile(fileName)
if err != nil {
t.Fatal(err)
}

return fileData
}

func checkSentData(t *testing.T, sendBytes []byte, msgSendFilePrefix string) {
msgSendExpectedBytes, err := os.ReadFile(msgSendFilePrefix + "_expected.json")
if err != nil {
t.Fatal(err)
}

msgSendActualFileName := msgSendFilePrefix + "_actual.json"
equal := jsonDatagramEqual(t, msgSendExpectedBytes, sendBytes)
if !equal {
saveJsonToFile(t, sendBytes, msgSendActualFileName)
}
assert.Truef(t, equal, "Assert equal failed! Check '%s' ", msgSendActualFileName)
}

func jsonDatagramEqual(t *testing.T, expectedJson, actualJson []byte) bool {
var actualDatagram model.Datagram
if err := json.Unmarshal(actualJson, &actualDatagram); err != nil {
t.Fatal(err)
}
var expectedDatagram model.Datagram
if err := json.Unmarshal(expectedJson, &expectedDatagram); err != nil {
t.Fatal(err)
}

less := func(a, b model.FunctionPropertyType) bool { return string(*a.Function) < string(*b.Function) }
return cmp.Equal(expectedDatagram, actualDatagram, cmpopts.SortSlices(less))
}

func saveJsonToFile(t *testing.T, data json.RawMessage, fileName string) {
jsonIndent, err := json.MarshalIndent(data, "", " ")
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(fileName, jsonIndent, os.ModePerm)
if err != nil {
t.Fatal(err)
}
}

func waitForAck(t *testing.T, msgCounterReference *model.MsgCounterType, writeHandler *WriteMessageHandler) {
var datagram model.Datagram

msg := writeHandler.ResultWithReference(msgCounterReference)
if msg == nil {
t.Fatal("acknowledge message was not sent!!")
}

if err := json.Unmarshal(msg, &datagram); err != nil {
t.Fatal(err)
}

cmd := datagram.Datagram.Payload.Cmd[0]
if cmd.ResultData != nil {
if cmd.ResultData.ErrorNumber != nil && uint(*cmd.ResultData.ErrorNumber) != uint(model.ErrorNumberTypeNoError) {
t.Fatal(fmt.Errorf("error '%d' result data received", uint(*cmd.ResultData.ErrorNumber)))
}
}
}
38 changes: 37 additions & 1 deletion spine/mocks/Sender.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package integrationtests
package spine

import (
"testing"
"time"

"github.com/enbility/eebus-go/spine"
"github.com/enbility/eebus-go/spine/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
Expand All @@ -28,19 +27,19 @@ func TestNodeManagementSuite(t *testing.T) {

type NodeManagementSuite struct {
suite.Suite
sut *spine.DeviceLocalImpl
sut *DeviceLocalImpl

remoteSki string

readHandler spine.SpineDataProcessing
readHandler SpineDataProcessing
writeHandler *WriteMessageHandler
}

func (s *NodeManagementSuite) SetupSuite() {
}

func (s *NodeManagementSuite) BeforeTest(suiteName, testName string) {
s.sut = spine.NewDeviceLocalImpl("TestBrandName", "TestDeviceModel", "TestSerialNumber", "TestDeviceCode",
s.sut = NewDeviceLocalImpl("TestBrandName", "TestDeviceModel", "TestSerialNumber", "TestDeviceCode",
"TestDeviceAddress", model.DeviceTypeTypeEnergyManagementSystem, model.NetworkManagementFeatureSetTypeSmart, time.Second*4)
s.remoteSki = "TestRemoteSki"

Expand Down Expand Up @@ -81,15 +80,15 @@ func (s *NodeManagementSuite) TestDetailedDiscovery_RecvReply() {

rEntities := remoteDevice.Entities()
assert.Equal(s.T(), 2, len(rEntities))
di := rEntities[spine.DeviceInformationEntityId]
di := rEntities[DeviceInformationEntityId]
assert.NotNil(s.T(), di)
assert.Equal(s.T(), model.EntityTypeTypeDeviceInformation, di.EntityType())

diFeatures := di.Features()
assert.Equal(s.T(), 2, len(diFeatures))

nm := diFeatures[0]
assert.Equal(s.T(), spine.NodeManagementFeatureId, uint(*nm.Address().Feature))
assert.Equal(s.T(), NodeManagementFeatureId, uint(*nm.Address().Feature))
assert.Equal(s.T(), model.FeatureTypeTypeNodeManagement, nm.Type())
assert.Equal(s.T(), model.RoleTypeSpecial, nm.Role())
assert.Equal(s.T(), 8, len(nm.Operations()))
Expand Down Expand Up @@ -143,7 +142,7 @@ func (s *NodeManagementSuite) TestDetailedDiscovery_RecvNotifyAdded() {
rEntities := remoteDevice.Entities()
if assert.Equal(s.T(), 3, len(rEntities)) {
{
di := rEntities[spine.DeviceInformationEntityId]
di := rEntities[DeviceInformationEntityId]
assert.NotNil(s.T(), di)
assert.Equal(s.T(), model.EntityTypeTypeDeviceInformation, di.EntityType())
assert.Equal(s.T(), 2, len(di.Features()))
Expand Down Expand Up @@ -185,7 +184,7 @@ func (s *NodeManagementSuite) TestSubscriptionRequestCall_BeforeDetailedDiscover
remoteDevice := s.sut.RemoteDeviceForSki(s.remoteSki)
subscriptionsForDevice := s.sut.SubscriptionManager().Subscriptions(remoteDevice)
assert.Equal(s.T(), 1, len(subscriptionsForDevice))
subscriptionsOnFeature := s.sut.SubscriptionManager().SubscriptionsOnFeature(*spine.NodeManagementAddress(s.sut.Address()))
subscriptionsOnFeature := s.sut.SubscriptionManager().SubscriptionsOnFeature(*NodeManagementAddress(s.sut.Address()))
assert.Equal(s.T(), 1, len(subscriptionsOnFeature))
}

Expand Down
20 changes: 2 additions & 18 deletions spine/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package spine_test

import (
"encoding/json"
"sync"
"testing"

"github.com/enbility/eebus-go/spine"
Expand All @@ -11,23 +10,8 @@ import (
"github.com/stretchr/testify/assert"
)

type WriteMessageHandler struct {
sentMessage []byte

mux sync.Mutex
}

var _ spine.SpineDataConnection = (*WriteMessageHandler)(nil)

func (t *WriteMessageHandler) WriteSpineMessage(message []byte) {
t.mux.Lock()
defer t.mux.Unlock()

t.sentMessage = message
}

func TestSender_Notify_MsgCounter(t *testing.T) {
temp := &WriteMessageHandler{}
temp := &spine.WriteMessageHandler{}
sut := spine.NewSender(temp)

senderAddress := featureAddressType(1, spine.NewEntityAddressType("Sender", []uint{1}))
Expand All @@ -44,7 +28,7 @@ func TestSender_Notify_MsgCounter(t *testing.T) {
assert.NoError(t, err)
expectedMsgCounter := 2 //because Notify was called twice

sentBytes := temp.sentMessage
sentBytes := temp.LastMessage()
var sentDatagram model.Datagram
assert.NoError(t, json.Unmarshal(sentBytes, &sentDatagram))
assert.Equal(t, expectedMsgCounter, int(*sentDatagram.Datagram.Header.MsgCounter))
Expand Down

0 comments on commit 2617704

Please sign in to comment.