-
Notifications
You must be signed in to change notification settings - Fork 4
/
base_device_test.go
128 lines (96 loc) · 2.72 KB
/
base_device_test.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
package iot
import (
"testing"
)
const deviceId = "611d13360ad1ed028658e089_device_cli"
const devicePwd = "123456789"
const server = "tls://iot-mqtts.cn-north-4.myhuaweicloud.com:8883"
const qos = 1
func TestBaseIotDevice_Init(t *testing.T) {
device := createBaseIotDevice()
result := device.Init()
if !result {
t.Errorf("device init failed")
}
}
func TestBaseIotDevice_IsConnected(t *testing.T) {
device := createBaseIotDevice()
device.Init()
if !device.IsConnected() {
t.Errorf("device connecte to server failed")
}
}
func TestBaseIotDevice_DisConnect(t *testing.T) {
device := createBaseIotDevice()
device.Init()
device.DisConnect()
if device.IsConnected() {
t.Errorf("device disconnect to server failed")
}
}
func TestBaseIotDevice_AddMessageHandler(t *testing.T) {
device := createBaseIotDevice()
device.AddMessageHandler(func(message Message) bool {
return true
})
if len(device.messageHandlers) == 0 {
t.Errorf("add message handler failed")
}
}
func TestBaseIotDevice_AddCommandHandler(t *testing.T) {
device := createBaseIotDevice()
device.AddCommandHandler(func(command Command) bool {
return true
})
if len(device.commandHandler) == 0 {
t.Errorf("add command handlers failed")
}
}
func TestBaseIotDevice_AddPropertiesSetHandler(t *testing.T) {
device := createBaseIotDevice()
device.AddPropertiesSetHandler(func(message DevicePropertyDownRequest) bool {
return true
})
if len(device.propertiesSetHandlers) == 0 {
t.Errorf("add properties handler failed")
}
}
func TestBaseIotDevice_SetPropertyQueryHandler(t *testing.T) {
device := createBaseIotDevice()
device.SetPropertyQueryHandler(func(query DevicePropertyQueryRequest) DevicePropertyEntry {
return DevicePropertyEntry{}
})
if device.propertyQueryHandler == nil {
t.Errorf("set property query handler failed")
}
}
func TestBaseIotDevice_SetSwFwVersionReporter(t *testing.T) {
device := createBaseIotDevice()
device.SetSwFwVersionReporter(func() (string, string) {
return "1.0", "2.0"
})
if device.swFwVersionReporter == nil {
t.Errorf("set sw fw version reporter failed")
}
}
func TestBaseIotDevice_SetDeviceUpgradeHandler(t *testing.T) {
device := createBaseIotDevice()
device.SetDeviceUpgradeHandler(func(upgradeType byte, info UpgradeInfo) UpgradeProgress {
return UpgradeProgress{}
})
if device.deviceUpgradeHandler == nil {
t.Errorf("set device upgrade handler failed")
}
}
func createBaseIotDevice() baseIotDevice {
device := baseIotDevice{}
device.Id = deviceId
device.Password = devicePwd
device.Servers = server
device.messageHandlers = []MessageHandler{}
device.commandHandler = []CommandHandler{}
device.fileUrls = map[string]string{}
device.qos = qos
device.batchSubDeviceSize = 10
return device
}