This repository was archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_test.go
More file actions
65 lines (53 loc) · 1.63 KB
/
api_test.go
File metadata and controls
65 lines (53 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package gateway
import (
"testing"
"github.com/freeconf/restconf/callhome"
"github.com/freeconf/restconf/device"
"github.com/freeconf/yang/fc"
"github.com/freeconf/yang/nodeutil"
"github.com/freeconf/yang/source"
)
func newTestApp(t *testing.T) (*LocalRegistrar, *device.Local) {
fc.DebugLog(true)
ypath := source.Dir("./yang")
local := device.New(ypath)
proto := func(address string) (device.Device, error) {
return local, nil
}
app := NewLocalRegistrar(proto)
InstallRegistrar(app, local)
return app, local
}
func TestGatewayApi(t *testing.T) {
app, dev := newTestApp(t)
app.RegisterDevice("x", "z")
api, err := dev.Browser("fc-gateway")
fc.AssertEqual(t, nil, err)
actual, err := nodeutil.WriteJSON(api.Root())
fc.AssertEqual(t, nil, err)
fc.AssertEqual(t, `{"fc-gateway:registration":[{"deviceId":"x","address":"z"}]}`, actual)
}
func TestCallHomeApi(t *testing.T) {
gw, dev := newTestApp(t)
// gateway to receive call-home requests
fc.AssertEqual(t, nil, InstallRegistrar(gw, dev))
serverCount := 0
gw.OnRegister(func(r Registration) {
serverCount++
})
fc.AssertEqual(t, nil, dev.Add("fc-call-home-server", CallHomeServer(gw)))
// using the client driver to make a calll home request to gateway
caller := callhome.New(gw.proto)
options := caller.Options()
options.DeviceId = "x"
options.Address = "gw"
options.LocalAddress = "device"
clientCount := 0
caller.OnRegister(func(d device.Device, update callhome.RegisterUpdate) {
clientCount++
})
fc.AssertEqual(t, nil, caller.ApplyOptions(options))
fc.AssertEqual(t, 1, clientCount)
fc.AssertEqual(t, 1, gw.RegistrationCount())
fc.AssertEqual(t, 1, serverCount)
}