forked from zubairhamed/betwixt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmocks.go
336 lines (267 loc) · 6.87 KB
/
mocks.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// List of mocks used for testing
package betwixt
import (
"errors"
"time"
"github.com/zubairhamed/canopus"
)
type TestDeviceObject struct {
Model ObjectDefinition
currentTime time.Time
utcOffset string
timeZone string
}
func (o *TestDeviceObject) OnExecute(instanceId int, resourceId int, req Lwm2mRequest) Lwm2mResponse {
return Changed()
}
func (o *TestDeviceObject) OnCreate(instanceId int, resourceId int, req Lwm2mRequest) Lwm2mResponse {
return Created()
}
func (o *TestDeviceObject) OnDelete(instanceId int, req Lwm2mRequest) Lwm2mResponse {
return Deleted()
}
func (o *TestDeviceObject) OnRead(instanceId int, resourceId int, req Lwm2mRequest) Lwm2mResponse {
if resourceId == -1 {
// Read Object Instance
} else {
// Read Resource Instance
var val Value
// resource := o.Model.GetResource(resourceId)
switch resourceId {
case 0:
val = String("Open Mobile Alliance")
break
case 1:
val = String("Lightweight M2M Client")
break
case 2:
val = String("345000123")
break
case 3:
val = String("1.0")
break
case 6:
// val, _ = values.TlvPayloadFromIntResource(resource, []int{1, 5})
break
case 7:
// val, _ = values.TlvPayloadFromIntResource(resource, []int{3800, 5000})
break
case 8:
// val, _ = values.TlvPayloadFromIntResource(resource, []int{125, 900})
break
case 9:
val = Integer(100)
break
case 10:
val = Integer(15)
break
case 11:
// val, _ = values.TlvPayloadFromIntResource(resource, []int{0})
break
case 13:
val = Time(o.currentTime)
break
case 14:
val = String(o.utcOffset)
break
case 15:
val = String(o.timeZone)
break
case 16:
val = String(string(BINDINGMODE_UDP))
break
default:
break
}
return Content(val)
}
return NotFound()
}
func (o *TestDeviceObject) OnWrite(instanceId int, resourceId int, req Lwm2mRequest) Lwm2mResponse {
val := req.GetMessage().Payload
switch resourceId {
case 13:
break
case 14:
o.utcOffset = val.String()
break
case 15:
o.timeZone = val.String()
break
default:
return NotFound()
}
return Changed()
}
func (o *TestDeviceObject) Reboot() Value {
return Empty()
}
func (o *TestDeviceObject) FactoryReset() Value {
return Empty()
}
func (o *TestDeviceObject) ResetErrorCode() string {
return ""
}
func NewTestDeviceObject(def ObjectDefinition) *TestDeviceObject {
return &TestDeviceObject{
Model: def,
currentTime: time.Unix(1367491215, 0),
utcOffset: "+02:00",
timeZone: "+02:00",
}
}
type MockServerStatistics struct {
}
func (s *MockServerStatistics) IncrementCoapRequestsCount() {
}
func (s *MockServerStatistics) GetRequestsCount() int {
return 0
}
func NewMockServer() Server {
return &MockServer{
stats: &MockServerStatistics{},
}
}
type MockServer struct {
stats ServerStatistics
coapServer canopus.CoapServer
}
func (server *MockServer) Start() {
}
func (server *MockServer) UseRegistry(reg Registry) {
}
func (server *MockServer) On(e EventType, fn FnEvent) {
}
func (server *MockServer) GetClients() map[string]RegisteredClient {
return make(map[string]RegisteredClient)
}
func (server *MockServer) GetStats() ServerStatistics {
return server.stats
}
func (server *MockServer) GetCoapServer() canopus.CoapServer {
return server.coapServer
}
func (server *MockServer) GetClient(id string) RegisteredClient {
return nil
}
func NewMockRegistry(s ...ObjectSource) Registry {
reg := &MockRegistry{}
reg.sources = []ObjectSource{}
for _, o := range s {
reg.Register(o)
}
return reg
}
type MockRegistry struct {
sources []ObjectSource
}
func (r *MockRegistry) GetDefinition(t LWM2MObjectType) ObjectDefinition {
return nil
}
func (m *MockRegistry) Register(s ObjectSource) {
s.Initialize()
m.sources = append(m.sources, s)
}
func (r *MockRegistry) GetMandatory() []ObjectDefinition {
mandatory := []ObjectDefinition{}
for _, s := range r.sources {
objs := s.GetObjects()
for _, o := range objs {
if o.IsMandatory() {
mandatory = append(mandatory, o)
}
}
}
return mandatory
}
func (m *MockRegistry) GetDefinitions() []ObjectDefinition {
defs := []ObjectDefinition{}
for _, s := range m.sources {
if s != nil {
for _, v := range s.GetObjects() {
defs = append(defs, v)
}
}
}
return defs
}
func NewMockObject(t LWM2MObjectType, enabler ObjectEnabler, reg Registry) Object {
def := reg.GetDefinition(t)
return &MockObject{
definition: def,
typeId: t,
enabler: enabler,
instances: make(map[int]bool),
}
}
type MockObject struct {
typeId LWM2MObjectType
definition ObjectDefinition
enabler ObjectEnabler
instances map[int]bool
}
func (o *MockObject) AddInstance(int) {}
func (o *MockObject) RemoveInstance(int) {}
func (o *MockObject) GetInstances() []int {
return nil
}
func (o *MockObject) GetEnabler() ObjectEnabler {
return o.enabler
}
func (o *MockObject) GetType() LWM2MObjectType {
return LWM2MObjectType(0)
}
func (o *MockObject) GetDefinition() ObjectDefinition {
return nil
}
func (o *MockObject) SetEnabler(ObjectEnabler) {}
func NewMockClient() LWM2MClient {
return &MockClient{
enabledObjects: make(map[LWM2MObjectType]Object),
}
}
type MockClient struct {
enabledObjects map[LWM2MObjectType]Object
registry Registry
}
func (c *MockClient) AddObjectInstance(LWM2MObjectType, int) error {
return nil
}
func (c *MockClient) Register(string) (string, error) {
return "", nil
}
func (c *MockClient) UseRegistry(r Registry) {
c.registry = r
}
func (c *MockClient) EnableObject(t LWM2MObjectType, e ObjectEnabler) error {
_, ok := c.enabledObjects[t]
if !ok {
c.enabledObjects[t] = NewMockObject(t, e, c.GetRegistry())
return nil
} else {
return errors.New("Object already enabled")
}
}
func (c *MockClient) GetRegistry() Registry {
return c.registry
}
func (c *MockClient) GetEnabledObjects() map[LWM2MObjectType]Object {
return c.enabledObjects
}
func (c *MockClient) GetObject(n LWM2MObjectType) Object {
return c.enabledObjects[n]
}
func (c *MockClient) AddObjectInstances(LWM2MObjectType, ...int) {}
func (c *MockClient) AddResource() {}
func (c *MockClient) AddObject() {}
func (c *MockClient) Deregister() {}
func (c *MockClient) Update() {}
func (c *MockClient) SetEnabler(LWM2MObjectType, ObjectEnabler) {}
func (c *MockClient) Start() {}
func (c *MockClient) OnStartup(FnOnStartup) {}
func (c *MockClient) OnRead(FnOnRead) {}
func (c *MockClient) OnWrite(FnOnWrite) {}
func (c *MockClient) OnExecute(FnOnExecute) {}
func (c *MockClient) OnRegistered(FnOnRegistered) {}
func (c *MockClient) OnDeregistered(FnOnDeregistered) {}
func (c *MockClient) OnError(FnOnError) {}