|
| 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