Skip to content

Commit acc18bb

Browse files
committed
add test cases and format string
Signed-off-by: SammyOina <[email protected]>
1 parent 3cd4521 commit acc18bb

File tree

2 files changed

+74
-61
lines changed

2 files changed

+74
-61
lines changed

pkg/bootstrap/bootstrap_test.go

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,61 +21,59 @@ func TestBootstrap(t *testing.T) {
2121
http.Error(w, "Invalid authorization header", http.StatusUnauthorized)
2222
return
2323
}
24+
resp := `
25+
{
26+
"thing_id": "e22c383a-d2ab-47c1-89cd-903955da993d",
27+
"thing_key": "fc987711-1828-461b-aa4b-16d5b2c642fe",
28+
"channels": [
29+
%s
30+
],
31+
"content": "{\"agent\":{\"edgex\":{\"url\":\"http://localhost:48090/api/v1/\"},\"heartbeat\":{\"interval\":\"30s\"},\"log\":{\"level\":\"debug\"},\"mqtt\":{\"mtls\":false,\"qos\":0,\"retain\":false,\"skip_tls_ver\":true,\"url\":\"tcp://mainflux-domain.com:1883\"},\"server\":{\"nats_url\":\"localhost:4222\",\"port\":\"9000\"},\"terminal\":{\"session_timeout\":\"30s\"}},\"export\":{\"exp\":{\"cache_db\":\"0\",\"cache_pass\":\"\",\"cache_url\":\"localhost:6379\",\"log_level\":\"debug\",\"nats\":\"nats://localhost:4222\",\"port\":\"8172\"},\"mqtt\":{\"ca_path\":\"ca.crt\",\"cert_path\":\"thing.crt\",\"channel\":\"\",\"host\":\"tcp://mainflux-domain.com:1883\",\"mtls\":false,\"password\":\"\",\"priv_key_path\":\"thing.key\",\"qos\":0,\"retain\":false,\"skip_tls_ver\":false,\"username\":\"\"},\"routes\":[{\"mqtt_topic\":\"\",\"nats_topic\":\"channels\",\"subtopic\":\"\",\"type\":\"mfx\",\"workers\":10},{\"mqtt_topic\":\"\",\"nats_topic\":\"export\",\"subtopic\":\"\",\"type\":\"default\",\"workers\":10}]}}"
32+
}
33+
`
2434
if r.Header.Get("Authorization") == "Thing invalidChannels" {
2535
// Simulate a malformed response.
26-
resp := `
36+
channels := `
2737
{
28-
"thing_id": "e22c383a-d2ab-47c1-89cd-903955da993d",
29-
"thing_key": "fc987711-1828-461b-aa4b-16d5b2c642fe",
30-
"channels": [
31-
{
32-
"id": "fa5f9ba8-a1fc-4380-9edb-d0c23eaa24ec",
33-
"name": "control-channel",
34-
"metadata": {
35-
"type": "control"
36-
}
37-
}
38-
],
39-
"content": "{\"agent\":{\"edgex\":{\"url\":\"http://localhost:48090/api/v1/\"},\"heartbeat\":{\"interval\":\"30s\"},\"log\":{\"level\":\"debug\"},\"mqtt\":{\"mtls\":false,\"qos\":0,\"retain\":false,\"skip_tls_ver\":true,\"url\":\"tcp://mainflux-domain.com:1883\"},\"server\":{\"nats_url\":\"localhost:4222\",\"port\":\"9000\"},\"terminal\":{\"session_timeout\":\"30s\"}},\"export\":{\"exp\":{\"cache_db\":\"0\",\"cache_pass\":\"\",\"cache_url\":\"localhost:6379\",\"log_level\":\"debug\",\"nats\":\"nats://localhost:4222\",\"port\":\"8172\"},\"mqtt\":{\"ca_path\":\"ca.crt\",\"cert_path\":\"thing.crt\",\"channel\":\"\",\"host\":\"tcp://mainflux-domain.com:1883\",\"mtls\":false,\"password\":\"\",\"priv_key_path\":\"thing.key\",\"qos\":0,\"retain\":false,\"skip_tls_ver\":false,\"username\":\"\"},\"routes\":[{\"mqtt_topic\":\"\",\"nats_topic\":\"channels\",\"subtopic\":\"\",\"type\":\"mfx\",\"workers\":10},{\"mqtt_topic\":\"\",\"nats_topic\":\"export\",\"subtopic\":\"\",\"type\":\"default\",\"workers\":10}]}}"
40-
}
38+
"id": "fa5f9ba8-a1fc-4380-9edb-d0c23eaa24ec",
39+
"name": "control-channel",
40+
"metadata": {
41+
"type": "control"
42+
}
43+
}
4144
`
45+
resp = fmt.Sprintf(resp, channels)
4246
w.WriteHeader(http.StatusOK)
4347
if _, err := io.WriteString(w, resp); err != nil {
4448
t.Errorf(err.Error())
4549
}
4650
return
4751
}
4852
// Simulate a successful response.
49-
resp := `
53+
channels := `
5054
{
51-
"thing_id": "e22c383a-d2ab-47c1-89cd-903955da993d",
52-
"thing_key": "fc987711-1828-461b-aa4b-16d5b2c642fe",
53-
"channels": [
54-
{
55-
"id": "fa5f9ba8-a1fc-4380-9edb-d0c23eaa24ec",
56-
"name": "control-channel",
57-
"metadata": {
58-
"type": "control"
59-
}
60-
},
61-
{
62-
"id": "24e5473e-3cbe-43d9-8a8b-a725ff918c0e",
63-
"name": "data-channel",
64-
"metadata": {
65-
"type": "data"
66-
}
67-
},
68-
{
69-
"id": "1eac45c2-0f72-4089-b255-ebd2e5732bbb",
70-
"name": "export-channel",
71-
"metadata": {
72-
"type": "export"
73-
}
74-
}
75-
],
76-
"content": "{\"agent\":{\"edgex\":{\"url\":\"http://localhost:48090/api/v1/\"},\"heartbeat\":{\"interval\":\"30s\"},\"log\":{\"level\":\"debug\"},\"mqtt\":{\"mtls\":false,\"qos\":0,\"retain\":false,\"skip_tls_ver\":true,\"url\":\"tcp://mainflux-domain.com:1883\"},\"server\":{\"nats_url\":\"localhost:4222\",\"port\":\"9000\"},\"terminal\":{\"session_timeout\":\"30s\"}},\"export\":{\"exp\":{\"cache_db\":\"0\",\"cache_pass\":\"\",\"cache_url\":\"localhost:6379\",\"log_level\":\"debug\",\"nats\":\"nats://localhost:4222\",\"port\":\"8172\"},\"mqtt\":{\"ca_path\":\"ca.crt\",\"cert_path\":\"thing.crt\",\"channel\":\"\",\"host\":\"tcp://mainflux-domain.com:1883\",\"mtls\":false,\"password\":\"\",\"priv_key_path\":\"thing.key\",\"qos\":0,\"retain\":false,\"skip_tls_ver\":false,\"username\":\"\"},\"routes\":[{\"mqtt_topic\":\"\",\"nats_topic\":\"channels\",\"subtopic\":\"\",\"type\":\"mfx\",\"workers\":10},{\"mqtt_topic\":\"\",\"nats_topic\":\"export\",\"subtopic\":\"\",\"type\":\"default\",\"workers\":10}]}}"
55+
"id": "fa5f9ba8-a1fc-4380-9edb-d0c23eaa24ec",
56+
"name": "control-channel",
57+
"metadata": {
58+
"type": "control"
59+
}
60+
},
61+
{
62+
"id": "24e5473e-3cbe-43d9-8a8b-a725ff918c0e",
63+
"name": "data-channel",
64+
"metadata": {
65+
"type": "data"
66+
}
67+
},
68+
{
69+
"id": "1eac45c2-0f72-4089-b255-ebd2e5732bbb",
70+
"name": "export-channel",
71+
"metadata": {
72+
"type": "export"
73+
}
7774
}
7875
`
76+
resp = fmt.Sprintf(resp, channels)
7977
w.WriteHeader(http.StatusOK)
8078
if _, err := io.WriteString(w, resp); err != nil {
8179
t.Errorf(err.Error())

pkg/terminal/terminal_test.go

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package terminal
22

33
import (
4+
"errors"
45
"testing"
56
"time"
67

@@ -14,31 +15,44 @@ const (
1415
)
1516

1617
// MockPublish is a mock function for the publish function used in NewSession.
17-
func MockPublish(channel, payload string) error {
18+
func mockPublish(channel, payload string) error {
1819
return nil
1920
}
2021

21-
func TestSessionWrite(t *testing.T) {
22-
publish := MockPublish
22+
func mockPublishFail(channel, payload string) error {
23+
return errors.New("")
24+
}
2325

24-
session, err := NewSession(uuid, timeout, publish, logger.NewMock())
25-
if err != nil {
26-
t.Fatalf("Expected no error, but got: %v", err)
27-
}
26+
func TestWrite(t *testing.T) {
27+
t.Run("successful publish", func(t *testing.T) {
28+
session, err := NewSession(uuid, timeout, mockPublish, logger.NewMock())
29+
if err != nil {
30+
t.Fatalf("Expected no error, but got: %v", err)
31+
}
2832

29-
// Simulate writing data to the session
30-
data := []byte("test data")
31-
n, err := session.Write(data)
32-
if err != nil {
33-
t.Fatalf("Expected no error, but got: %v", err)
34-
}
35-
assert.Equal(t, len(data), n)
36-
}
33+
// Simulate writing data to the session
34+
data := []byte("test data")
35+
n, err := session.Write(data)
36+
if err != nil {
37+
t.Fatalf("Expected no error, but got: %v", err)
38+
}
39+
assert.Equal(t, len(data), n)
40+
})
41+
t.Run("failed publish", func(t *testing.T) {
42+
session, err := NewSession(uuid, timeout, mockPublishFail, logger.NewMock())
43+
if err != nil {
44+
t.Fatalf("Expected no error, but got: %v", err)
45+
}
3746

38-
func TestSessionSend(t *testing.T) {
39-
publish := MockPublish
47+
// Simulate writing data to the session
48+
data := []byte("test data")
49+
_, err = session.Write(data)
50+
assert.NotNil(t, err)
51+
})
52+
}
4053

41-
session, err := NewSession(uuid, timeout, publish, logger.NewMock())
54+
func TestSend(t *testing.T) {
55+
session, err := NewSession(uuid, timeout, mockPublish, logger.NewMock())
4256
if err != nil {
4357
t.Fatalf("Expected no error, but got: %v", err)
4458
}
@@ -49,10 +63,11 @@ func TestSessionSend(t *testing.T) {
4963
if err = session.Send(data); err != nil {
5064
t.Fatalf("Expected no error, but got: %v", err)
5165
}
66+
5267
}
5368

54-
func TestSessionIsDone(t *testing.T) {
55-
publish := MockPublish
69+
func TestIsDone(t *testing.T) {
70+
publish := mockPublish
5671

5772
session, err := NewSession(uuid, timeout, publish, logger.NewMock())
5873
if err != nil {

0 commit comments

Comments
 (0)