-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JSON Marshalling support to device and entity (#45)
Both DeviceInterface and EntityInterface implementations (e.g. DeviceRemoteInterface and EntityRemoteInterface) are used as arguments in API calls and return values. When these API calls are used via interprocess communication protocols based on JSON, then the easiest way is to convert them into their addresses to be identified. This change adds support for this need.
- Loading branch information
Showing
4 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package spine | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/enbility/spine-go/model" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
func TestDeviceSuite(t *testing.T) { | ||
suite.Run(t, new(DeviceTestSuite)) | ||
} | ||
|
||
type DeviceTestSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func (s *DeviceTestSuite) Test_Device() { | ||
deviceAddress := model.AddressDeviceType("test") | ||
device := NewDevice(&deviceAddress, nil, nil) | ||
|
||
value, err := json.Marshal(device) | ||
assert.Nil(s.T(), err) | ||
assert.NotNil(s.T(), value) | ||
assert.Equal(s.T(), `"test"`, string(value)) | ||
|
||
device = NewDevice(nil, nil, nil) | ||
|
||
value, err = json.Marshal(device) | ||
assert.Nil(s.T(), err) | ||
assert.NotNil(s.T(), value) | ||
assert.Equal(s.T(), `""`, string(value)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package spine | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/enbility/spine-go/model" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
func TestEntitySuite(t *testing.T) { | ||
suite.Run(t, new(EntityTestSuite)) | ||
} | ||
|
||
type EntityTestSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func (s *EntityTestSuite) Test_Entity() { | ||
deviceAddress := model.AddressDeviceType("test") | ||
entity := NewEntity(model.EntityTypeTypeCEM, &deviceAddress, NewAddressEntityType([]uint{1, 1})) | ||
|
||
value, err := json.Marshal(entity) | ||
assert.Nil(s.T(), err) | ||
assert.NotNil(s.T(), value) | ||
assert.Equal(s.T(), `{"Device":"test","Entity":[1,1]}`, string(value)) | ||
|
||
entity = NewEntity(model.EntityTypeTypeCEM, &deviceAddress, nil) | ||
|
||
value, err = json.Marshal(entity) | ||
assert.Nil(s.T(), err) | ||
assert.NotNil(s.T(), value) | ||
assert.Equal(s.T(), `{"Device":"test","Entity":null}`, string(value)) | ||
|
||
entity = NewEntity(model.EntityTypeTypeCEM, nil, nil) | ||
|
||
value, err = json.Marshal(entity) | ||
assert.Nil(s.T(), err) | ||
assert.NotNil(s.T(), value) | ||
assert.Equal(s.T(), `{"Device":"","Entity":null}`, string(value)) | ||
} |