From 7e7f4c57392a40c8c5b91daac38eaad193d1f622 Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Wed, 10 Apr 2024 15:15:43 +0300 Subject: [PATCH] Add mocks with mockery for mqtt Signed-off-by: JeffMboya Refactor mqtt handler tests to include eventStore in newHandler function Signed-off-by: JeffMboya NOISSUE - Add property based testing to things service (#2088) Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: rodneyosodo Signed-off-by: JeffMboya Fix URLs in api-tests.yml Signed-off-by: JeffMboya --- .github/workflows/api-tests.yml | 2 +- mqtt/events/streams.go | 1 + mqtt/handler_test.go | 26 ++++++++------ mqtt/mocks/redis.go | 62 +++++++++++++++++++++++++++------ 4 files changed, 69 insertions(+), 22 deletions(-) diff --git a/.github/workflows/api-tests.yml b/.github/workflows/api-tests.yml index b3426e14830..9dffe1b6d28 100644 --- a/.github/workflows/api-tests.yml +++ b/.github/workflows/api-tests.yml @@ -183,4 +183,4 @@ jobs: - name: Stop containers if: always() - run: make run down args="-v" + run: make run down args="-v" \ No newline at end of file diff --git a/mqtt/events/streams.go b/mqtt/events/streams.go index ee03a243ab0..bd5325bd8c8 100644 --- a/mqtt/events/streams.go +++ b/mqtt/events/streams.go @@ -12,6 +12,7 @@ import ( const streamID = "magistrala.mqtt" +//go:generate mockery --name EventStore --output=../mocks --filename redis.go --quiet --note "Copyright (c) Abstract Machines" type EventStore interface { Connect(ctx context.Context, clientID string) error Disconnect(ctx context.Context, clientID string) error diff --git a/mqtt/handler_test.go b/mqtt/handler_test.go index 0dd52808e2e..7ecce2b9649 100644 --- a/mqtt/handler_test.go +++ b/mqtt/handler_test.go @@ -64,7 +64,7 @@ var ( ) func TestAuthConnect(t *testing.T) { - handler, _ := newHandler() + handler, _, eventStore := newHandler(t) cases := []struct { desc string @@ -107,17 +107,19 @@ func TestAuthConnect(t *testing.T) { } for _, tc := range cases { + repoCall := eventStore.On("Connect", mock.Anything, mock.Anything).Return(nil) ctx := context.TODO() if tc.session != nil { ctx = session.NewContext(ctx, tc.session) } err := handler.AuthConnect(ctx) assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err)) + repoCall.Unset() } } func TestAuthPublish(t *testing.T) { - handler, auth := newHandler() + handler, auth, _ := newHandler(t) cases := []struct { desc string @@ -169,7 +171,7 @@ func TestAuthPublish(t *testing.T) { } func TestAuthSubscribe(t *testing.T) { - handler, auth := newHandler() + handler, auth, _ := newHandler(t) cases := []struct { desc string @@ -222,7 +224,7 @@ func TestAuthSubscribe(t *testing.T) { } func TestConnect(t *testing.T) { - handler, _ := newHandler() + handler, _, _ := newHandler(t) logBuffer.Reset() cases := []struct { @@ -256,7 +258,7 @@ func TestConnect(t *testing.T) { } func TestPublish(t *testing.T) { - handler, _ := newHandler() + handler, _, _ := newHandler(t) logBuffer.Reset() malformedSubtopics := topic + "/" + subtopic + "%" @@ -335,7 +337,7 @@ func TestPublish(t *testing.T) { } func TestSubscribe(t *testing.T) { - handler, _ := newHandler() + handler, _, _ := newHandler(t) logBuffer.Reset() cases := []struct { @@ -371,7 +373,7 @@ func TestSubscribe(t *testing.T) { } func TestUnsubscribe(t *testing.T) { - handler, _ := newHandler() + handler, _, _ := newHandler(t) logBuffer.Reset() cases := []struct { @@ -407,7 +409,7 @@ func TestUnsubscribe(t *testing.T) { } func TestDisconnect(t *testing.T) { - handler, _ := newHandler() + handler, _, eventStore := newHandler(t) logBuffer.Reset() cases := []struct { @@ -432,6 +434,7 @@ func TestDisconnect(t *testing.T) { } for _, tc := range cases { + repoCall := eventStore.On("Disconnect", mock.Anything, mock.Anything).Return(nil) ctx := context.TODO() if tc.session != nil { ctx = session.NewContext(ctx, tc.session) @@ -439,15 +442,16 @@ func TestDisconnect(t *testing.T) { err := handler.Disconnect(ctx) assert.Contains(t, logBuffer.String(), tc.logMsg) assert.Equal(t, tc.err, err) + repoCall.Unset() } } -func newHandler() (session.Handler, *authmocks.AuthClient) { +func newHandler(t *testing.T) (session.Handler, *authmocks.AuthClient, *mocks.EventStore) { logger, err := mglog.New(&logBuffer, "debug") if err != nil { log.Fatalf("failed to create logger: %s", err) } auth := new(authmocks.AuthClient) - eventStore := mocks.NewEventStore() - return mqtt.NewHandler(mocks.NewPublisher(), eventStore, logger, auth), auth + eventStore := mocks.NewEventStore(t) + return mqtt.NewHandler(mocks.NewPublisher(), eventStore, logger, auth), auth, eventStore } diff --git a/mqtt/mocks/redis.go b/mqtt/mocks/redis.go index be63be8a44c..d7ba830bad5 100644 --- a/mqtt/mocks/redis.go +++ b/mqtt/mocks/redis.go @@ -1,24 +1,66 @@ +// Code generated by mockery v2.42.1. DO NOT EDIT. + // Copyright (c) Abstract Machines -// SPDX-License-Identifier: Apache-2.0 package mocks import ( - "context" + context "context" - "github.com/absmach/magistrala/mqtt/events" + mock "github.com/stretchr/testify/mock" ) -type MockEventStore struct{} +// EventStore is an autogenerated mock type for the EventStore type +type EventStore struct { + mock.Mock +} + +// Connect provides a mock function with given fields: ctx, clientID +func (_m *EventStore) Connect(ctx context.Context, clientID string) error { + ret := _m.Called(ctx, clientID) + + if len(ret) == 0 { + panic("no return value specified for Connect") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, string) error); ok { + r0 = rf(ctx, clientID) + } else { + r0 = ret.Error(0) + } -func NewEventStore() events.EventStore { - return MockEventStore{} + return r0 } -func (es MockEventStore) Connect(ctx context.Context, clientID string) error { - return nil +// Disconnect provides a mock function with given fields: ctx, clientID +func (_m *EventStore) Disconnect(ctx context.Context, clientID string) error { + ret := _m.Called(ctx, clientID) + + if len(ret) == 0 { + panic("no return value specified for Disconnect") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, string) error); ok { + r0 = rf(ctx, clientID) + } else { + r0 = ret.Error(0) + } + + return r0 } -func (es MockEventStore) Disconnect(ctx context.Context, clientID string) error { - return nil +// NewEventStore creates a new instance of EventStore. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewEventStore(t interface { + mock.TestingT + Cleanup(func()) +}) *EventStore { + mock := &EventStore{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock }