From acc18bb310fe3a7458fb004fb3dbcc435c858174 Mon Sep 17 00:00:00 2001 From: SammyOina Date: Wed, 18 Oct 2023 16:07:38 +0300 Subject: [PATCH] add test cases and format string Signed-off-by: SammyOina --- pkg/bootstrap/bootstrap_test.go | 80 ++++++++++++++++----------------- pkg/terminal/terminal_test.go | 55 ++++++++++++++--------- 2 files changed, 74 insertions(+), 61 deletions(-) diff --git a/pkg/bootstrap/bootstrap_test.go b/pkg/bootstrap/bootstrap_test.go index 7341c96..b94c037 100644 --- a/pkg/bootstrap/bootstrap_test.go +++ b/pkg/bootstrap/bootstrap_test.go @@ -21,24 +21,28 @@ 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()) @@ -46,36 +50,30 @@ func TestBootstrap(t *testing.T) { 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()) diff --git a/pkg/terminal/terminal_test.go b/pkg/terminal/terminal_test.go index 6c06e51..0b7bc63 100644 --- a/pkg/terminal/terminal_test.go +++ b/pkg/terminal/terminal_test.go @@ -1,6 +1,7 @@ package terminal import ( + "errors" "testing" "time" @@ -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) } @@ -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 {