Skip to content

Commit

Permalink
add test cases and format string
Browse files Browse the repository at this point in the history
Signed-off-by: SammyOina <[email protected]>
  • Loading branch information
SammyOina committed Oct 18, 2023
1 parent 3cd4521 commit acc18bb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 61 deletions.
80 changes: 39 additions & 41 deletions pkg/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,61 +21,59 @@ func TestBootstrap(t *testing.T) {
http.Error(w, "Invalid authorization header", http.StatusUnauthorized)
return
}
resp := `
{
"thing_id": "e22c383a-d2ab-47c1-89cd-903955da993d",
"thing_key": "fc987711-1828-461b-aa4b-16d5b2c642fe",
"channels": [
%s
],
"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}]}}"
}
`
if r.Header.Get("Authorization") == "Thing invalidChannels" {
// Simulate a malformed response.
resp := `
channels := `
{
"thing_id": "e22c383a-d2ab-47c1-89cd-903955da993d",
"thing_key": "fc987711-1828-461b-aa4b-16d5b2c642fe",
"channels": [
{
"id": "fa5f9ba8-a1fc-4380-9edb-d0c23eaa24ec",
"name": "control-channel",
"metadata": {
"type": "control"
}
}
],
"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}]}}"
}
"id": "fa5f9ba8-a1fc-4380-9edb-d0c23eaa24ec",
"name": "control-channel",
"metadata": {
"type": "control"
}
}
`
resp = fmt.Sprintf(resp, channels)
w.WriteHeader(http.StatusOK)
if _, err := io.WriteString(w, resp); err != nil {
t.Errorf(err.Error())
}
return
}
// Simulate a successful response.
resp := `
channels := `
{
"thing_id": "e22c383a-d2ab-47c1-89cd-903955da993d",
"thing_key": "fc987711-1828-461b-aa4b-16d5b2c642fe",
"channels": [
{
"id": "fa5f9ba8-a1fc-4380-9edb-d0c23eaa24ec",
"name": "control-channel",
"metadata": {
"type": "control"
}
},
{
"id": "24e5473e-3cbe-43d9-8a8b-a725ff918c0e",
"name": "data-channel",
"metadata": {
"type": "data"
}
},
{
"id": "1eac45c2-0f72-4089-b255-ebd2e5732bbb",
"name": "export-channel",
"metadata": {
"type": "export"
}
}
],
"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}]}}"
"id": "fa5f9ba8-a1fc-4380-9edb-d0c23eaa24ec",
"name": "control-channel",
"metadata": {
"type": "control"
}
},
{
"id": "24e5473e-3cbe-43d9-8a8b-a725ff918c0e",
"name": "data-channel",
"metadata": {
"type": "data"
}
},
{
"id": "1eac45c2-0f72-4089-b255-ebd2e5732bbb",
"name": "export-channel",
"metadata": {
"type": "export"
}
}
`
resp = fmt.Sprintf(resp, channels)
w.WriteHeader(http.StatusOK)
if _, err := io.WriteString(w, resp); err != nil {
t.Errorf(err.Error())
Expand Down
55 changes: 35 additions & 20 deletions pkg/terminal/terminal_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package terminal

import (
"errors"
"testing"
"time"

Expand All @@ -14,31 +15,44 @@ const (
)

// MockPublish is a mock function for the publish function used in NewSession.
func MockPublish(channel, payload string) error {
func mockPublish(channel, payload string) error {
return nil
}

func TestSessionWrite(t *testing.T) {
publish := MockPublish
func mockPublishFail(channel, payload string) error {
return errors.New("")
}

session, err := NewSession(uuid, timeout, publish, logger.NewMock())
if err != nil {
t.Fatalf("Expected no error, but got: %v", err)
}
func TestWrite(t *testing.T) {
t.Run("successful publish", func(t *testing.T) {
session, err := NewSession(uuid, timeout, mockPublish, logger.NewMock())
if err != nil {
t.Fatalf("Expected no error, but got: %v", err)
}

// Simulate writing data to the session
data := []byte("test data")
n, err := session.Write(data)
if err != nil {
t.Fatalf("Expected no error, but got: %v", err)
}
assert.Equal(t, len(data), n)
}
// Simulate writing data to the session
data := []byte("test data")
n, err := session.Write(data)
if err != nil {
t.Fatalf("Expected no error, but got: %v", err)
}
assert.Equal(t, len(data), n)
})
t.Run("failed publish", func(t *testing.T) {
session, err := NewSession(uuid, timeout, mockPublishFail, logger.NewMock())
if err != nil {
t.Fatalf("Expected no error, but got: %v", err)
}

func TestSessionSend(t *testing.T) {
publish := MockPublish
// Simulate writing data to the session
data := []byte("test data")
_, err = session.Write(data)
assert.NotNil(t, err)
})
}

session, err := NewSession(uuid, timeout, publish, logger.NewMock())
func TestSend(t *testing.T) {
session, err := NewSession(uuid, timeout, mockPublish, logger.NewMock())
if err != nil {
t.Fatalf("Expected no error, but got: %v", err)
}
Expand All @@ -49,10 +63,11 @@ func TestSessionSend(t *testing.T) {
if err = session.Send(data); err != nil {
t.Fatalf("Expected no error, but got: %v", err)
}

}

func TestSessionIsDone(t *testing.T) {
publish := MockPublish
func TestIsDone(t *testing.T) {
publish := mockPublish

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

0 comments on commit acc18bb

Please sign in to comment.