Skip to content

Commit 3053b12

Browse files
authored
Fix adding datamodel root parameter (#38)
1 parent 702acbd commit 3053b12

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

datamodel/storage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func LoadDataModel(r io.Reader) (map[string]Parameter, error) {
8787

8888
// Add parentPath object automatically if not defined explicitly
8989
parentPath := parent(p.Path)
90-
if _, ok := values[parentPath]; !ok {
90+
if _, ok := values[parentPath]; !ok && parentPath != "" {
9191
values[parentPath] = Parameter{
9292
Path: parentPath,
9393
Object: true,

datamodel/storage_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package datamodel
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
const testDM = `Parameter,Object,Writable,Value,Type
12+
Device,true,true,,
13+
Device.DeviceInfo,true,true,,
14+
Device.DeviceInfo.Description,false,true,Residential Gateway,xsd:string
15+
Device.DeviceInfo.HardwareVersion,false,true,1.0,xsd:string
16+
Device.DeviceInfo.Manufacturer,false,true,ACME Networks,xsd:string
17+
Device.DeviceInfo.ManufacturerOUI,false,true,DECADE,xsd:string
18+
Device.DeviceInfo.ModelName,false,true,G3000E,xsd:string
19+
`
20+
21+
func TestLoadDataModel(t *testing.T) {
22+
params, err := LoadDataModel(strings.NewReader(testDM))
23+
require.NoError(t, err)
24+
25+
require.Len(t, params, 7)
26+
assert.Equal(t, "Residential Gateway", params["Device.DeviceInfo.Description"].Value)
27+
assert.Equal(t, "1.0", params["Device.DeviceInfo.HardwareVersion"].Value)
28+
assert.Equal(t, "ACME Networks", params["Device.DeviceInfo.Manufacturer"].Value)
29+
assert.Equal(t, "DECADE", params["Device.DeviceInfo.ManufacturerOUI"].Value)
30+
assert.Equal(t, "G3000E", params["Device.DeviceInfo.ModelName"].Value)
31+
}
32+
33+
func TestLoadingGenerators(t *testing.T) {
34+
dmsrc := `Parameter,Object,Writable,Value,Type
35+
Device.Foo,false,false,"randomWalk(startValue=50, minValue=0, maxValue=100, step=0) as xsd:int",sim:generator
36+
`
37+
params, err := LoadDataModel(strings.NewReader(dmsrc))
38+
require.NoError(t, err)
39+
dm := New(newState().WithDefaults(params))
40+
41+
t.Run("GetValue", func(t *testing.T) {
42+
p, ok := dm.GetValue("Device.Foo")
43+
assert.True(t, ok)
44+
assert.Equal(t, "50", p.Encode().Value.Value)
45+
assert.Equal(t, "xsd:int", p.Encode().Value.Type)
46+
})
47+
t.Run("GetValues", func(t *testing.T) {
48+
pp, ok := dm.GetValues("Device.Foo")
49+
assert.True(t, ok)
50+
require.Len(t, pp, 1)
51+
assert.Equal(t, "50", pp[0].Encode().Value.Value)
52+
assert.Equal(t, "xsd:int", pp[0].Encode().Value.Type)
53+
})
54+
t.Run("GetAll", func(t *testing.T) {
55+
pp, ok := dm.GetAll("Device.")
56+
assert.True(t, ok)
57+
require.Len(t, pp, 1)
58+
assert.Equal(t, "50", pp[0].Encode().Value.Value)
59+
assert.Equal(t, "xsd:int", pp[0].Encode().Value.Type)
60+
})
61+
}

0 commit comments

Comments
 (0)