This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
169 lines (133 loc) · 4.37 KB
/
api_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
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
package main
import (
"testing"
"time"
"log"
MQTT "github.com/eclipse/paho.mqtt.golang"
"strings"
"os"
)
const API_TIMEOUT =60*1e9
//func TestNoAPI(t *testing.T){
//
//
// time.Sleep(1)
//
//}
type TestResultmatrix struct {
listthingsPassed chan bool
thingstatusPassed chan bool
removethingPassed chan bool
addthingarchivePassed chan bool
}
func (matrix *TestResultmatrix) OnListThings(client MQTT.Client, msg MQTT.Message) {
payload := string(msg.Payload())
if strings.Contains(payload,"TRNG Generator") && strings.Contains(payload,"42"){
matrix.listthingsPassed<-true
log.Println("[TestAPI:OnListThings] PAYLOAD: ", payload)
log.Println("[TestAPI:OnListThings] LSTP/listthings API works. GOOD")
}else {
log.Panic("[TestAPI:OnListThings] PAYLOAD: ", payload)
log.Panic("[TestAPI:OnListThings] LSTP/listthings API broken")
os.Exit(1)
}
}
func (matrix *TestResultmatrix) OnUploadThing(client MQTT.Client, msg MQTT.Message) {
payload := string(msg.Payload())
if strings.Contains(payload,"running"){
matrix.addthingarchivePassed<-true
log.Println("[TestAPI:OnThingStatus] PAYLOAD: ", payload)
log.Println("[TestAPI:OnUploadThing] LSTP/addthingarchive API works. GOOD")
}else {
//log.Panic("[OnThingStatus] PAYLOAD: ", payload)
log.Panic("[TestAPI:OnUploadThing] LSTP/addthingarchive API broken")
os.Exit(1)
}
}
func (matrix *TestResultmatrix) OnRemoveThing(client MQTT.Client, msg MQTT.Message) {
payload := string(msg.Payload())
if strings.Contains(payload,"not available"){
matrix.removethingPassed<-true
log.Println("[TestAPI:OnRemoveThing] PAYLOAD: ", payload)
log.Println("[TestAPI:OnRemoveThing] LSTP/removething API works. GOOD")
}else {
log.Panic("[TestAPI:OnRemoveThing] PAYLOAD: ", payload)
log.Panic("[TestAPI:OnRemoveThing] LSTP/removething API broken")
os.Exit(1)
}
}
func TestAPI(t *testing.T){
cleanup()
prepare()
defer cleanup()
matrix := TestResultmatrix{
listthingsPassed: make (chan bool),
thingstatusPassed:make (chan bool),
removethingPassed:make (chan bool),
addthingarchivePassed:make (chan bool),
}
manager := newAgentManager()
go manager.startAgentManager()
defer manager.stopAgentManager()
opts := MQTT.NewClientOptions().AddBroker(manager.mConfig.Broker)
//
client := MQTT.NewClient(opts)
//
if token_connect := client.Connect(); token_connect.Wait() && token_connect.Error() != nil {
log.Fatal("[TestAPI] ",token_connect.Error())
os.Exit(1)
}
defer client.Disconnect(250)
log.Println("[TestAPI] Connected to : ",manager.mConfig.Broker)
time.Sleep(time.Second * (VALIDATE_TIMER*2))
// *********************
// listthings API test
client.Subscribe("LSTP/things",1,matrix.OnListThings)
time.Sleep(time.Second*3)
_ = client.Publish("LSTP/listthings", 1, false, "listhingspayload")
select {
case <- matrix.listthingsPassed:
client.Unsubscribe("LSTP/things")
log.Println("[TestAPI] (1) listthing API test passed. GOOD")
case <- time.After(API_TIMEOUT):
log.Println("[TestAPI] (1) listthing API timeout")
os.Exit(1)
}
client.Unsubscribe("LSTP/things")
// *********************
// addarchive API test
client.Subscribe("LSTP/thing/Temperature",1,matrix.OnUploadThing)
workdir,_ := os.Getwd()
thingarchive, _ := os.Open(workdir+ARCHIVES_DIR+"temperature.tar.gz")
buffer := make([]byte,472)
_,err := thingarchive.Read(buffer)
if err == nil {
log.Println("[TestAPI] temperature.tar.gz archive loaded")
}else{
log.Panic("[TestAPI] temperature.tar.gz archive not loaded")
os.Exit(1)
}
_ = client.Publish("LSTP/addthingarchive", 1, false, buffer)
select {
case <- matrix.addthingarchivePassed:
client.Unsubscribe("LSTP/thing/Temperature")
log.Println("[TestAPI] (2) addthingarchive API test passed. GOOD")
case <- time.After(API_TIMEOUT):
log.Println("[TestAPI] (2) addthingarchive API timeout")
os.Exit(1)
}
client.Unsubscribe("LSTP/thing/Temperature")
// *********************
// removething API test
client.Subscribe("LSTP/thing/Temperature",1,matrix.OnRemoveThing)
_ = client.Publish("LSTP/removething/Temperature", 1, false, "")
select {
case <- matrix.removethingPassed:
client.Unsubscribe("LSTP/thing/TRNG Generator")
log.Println("[TestAPI] (3) removething API test passed. GOOD")
case <- time.After(API_TIMEOUT):
log.Println("[TestAPI] (3) removething API timeout")
os.Exit(1)
}
client.Unsubscribe("LSTP/thing/TRNG Generator")
}