Skip to content

Commit

Permalink
MG-2161 - Generate mocks with mockery for mqtt (absmach#2163)
Browse files Browse the repository at this point in the history
Signed-off-by: JeffMboya <[email protected]>
  • Loading branch information
JeffMboya authored Apr 15, 2024
1 parent 4935ae8 commit f6477ed
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 38 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/check-generated-files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ jobs:
- "invitations/invitations.go"
- "users/emailer.go"
- "users/hasher.go"
- "mqtt/events/streams.go"
- name: Set up protoc
if: steps.changes.outputs.proto == 'true'
Expand Down Expand Up @@ -132,6 +133,7 @@ jobs:
mv ./invitations/mocks/repository.go ./invitations/mocks/repository.go.tmp
mv ./users/mocks/emailer.go ./users/mocks/emailer.go.tmp
mv ./users/mocks/hasher.go ./users/mocks/hasher.go.tmp
mv ./mqtt/mocks/events.go ./mqtt/mocks/events.go.tmp
make mocks
Expand Down Expand Up @@ -170,3 +172,4 @@ jobs:
check_mock_changes ./invitations/mocks/repository.go "Invitations Repository ./invitations/mocks/repository.go"
check_mock_changes ./users/mocks/emailer.go "Users Emailer ./users/mocks/emailer.go"
check_mock_changes ./users/mocks/hasher.go "Users Hasher ./users/mocks/hasher.go"
check_mock_changes ./mqtt/mocks/events.go "MQTT Events Store ./mqtt/mocks/events.go"
1 change: 1 addition & 0 deletions mqtt/events/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

const streamID = "magistrala.mqtt"

//go:generate mockery --name EventStore --output=../mocks --filename events.go --quiet --note "Copyright (c) Abstract Machines"
type EventStore interface {
Connect(ctx context.Context, clientID string) error
Disconnect(ctx context.Context, clientID string) error
Expand Down
35 changes: 21 additions & 14 deletions mqtt/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var (
)

func TestAuthConnect(t *testing.T) {
handler, _ := newHandler()
handler, _, eventStore := newHandler()

cases := []struct {
desc string
Expand Down Expand Up @@ -105,19 +105,22 @@ func TestAuthConnect(t *testing.T) {
session: &sessionClient,
},
}

for _, tc := range cases {
ctx := context.TODO()
password := ""
if tc.session != nil {
ctx = session.NewContext(ctx, tc.session)
password = string(tc.session.Password)
}
svcCall := eventStore.On("Connect", mock.Anything, password).Return(tc.err)
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))
svcCall.Unset()
}
}

func TestAuthPublish(t *testing.T) {
handler, auth := newHandler()
handler, auth, _ := newHandler()

cases := []struct {
desc string
Expand Down Expand Up @@ -157,7 +160,7 @@ func TestAuthPublish(t *testing.T) {
}

for _, tc := range cases {
repocall := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true, Id: testsutil.GenerateUUID(t)}, nil)
repocall := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true, Id: testsutil.GenerateUUID(t)}, tc.err)
ctx := context.TODO()
if tc.session != nil {
ctx = session.NewContext(ctx, tc.session)
Expand All @@ -169,7 +172,7 @@ func TestAuthPublish(t *testing.T) {
}

func TestAuthSubscribe(t *testing.T) {
handler, auth := newHandler()
handler, auth, _ := newHandler()

cases := []struct {
desc string
Expand Down Expand Up @@ -210,7 +213,7 @@ func TestAuthSubscribe(t *testing.T) {
}

for _, tc := range cases {
repocall := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true, Id: testsutil.GenerateUUID(t)}, nil)
repocall := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true, Id: testsutil.GenerateUUID(t)}, tc.err)
ctx := context.TODO()
if tc.session != nil {
ctx = session.NewContext(ctx, tc.session)
Expand All @@ -222,7 +225,7 @@ func TestAuthSubscribe(t *testing.T) {
}

func TestConnect(t *testing.T) {
handler, _ := newHandler()
handler, _, _ := newHandler()
logBuffer.Reset()

cases := []struct {
Expand Down Expand Up @@ -256,7 +259,7 @@ func TestConnect(t *testing.T) {
}

func TestPublish(t *testing.T) {
handler, _ := newHandler()
handler, _, _ := newHandler()
logBuffer.Reset()

malformedSubtopics := topic + "/" + subtopic + "%"
Expand Down Expand Up @@ -335,7 +338,7 @@ func TestPublish(t *testing.T) {
}

func TestSubscribe(t *testing.T) {
handler, _ := newHandler()
handler, _, _ := newHandler()
logBuffer.Reset()

cases := []struct {
Expand Down Expand Up @@ -371,7 +374,7 @@ func TestSubscribe(t *testing.T) {
}

func TestUnsubscribe(t *testing.T) {
handler, _ := newHandler()
handler, _, _ := newHandler()
logBuffer.Reset()

cases := []struct {
Expand Down Expand Up @@ -407,7 +410,7 @@ func TestUnsubscribe(t *testing.T) {
}

func TestDisconnect(t *testing.T) {
handler, _ := newHandler()
handler, _, eventStore := newHandler()
logBuffer.Reset()

cases := []struct {
Expand All @@ -433,21 +436,25 @@ func TestDisconnect(t *testing.T) {

for _, tc := range cases {
ctx := context.TODO()
password := ""
if tc.session != nil {
ctx = session.NewContext(ctx, tc.session)
password = string(tc.session.Password)
}
svcCall := eventStore.On("Disconnect", mock.Anything, password).Return(tc.err)
err := handler.Disconnect(ctx)
assert.Contains(t, logBuffer.String(), tc.logMsg)
assert.Equal(t, tc.err, err)
svcCall.Unset()
}
}

func newHandler() (session.Handler, *authmocks.AuthClient) {
func newHandler() (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 := new(mocks.EventStore)
return mqtt.NewHandler(mocks.NewPublisher(), eventStore, logger, auth), auth, eventStore
}
66 changes: 66 additions & 0 deletions mqtt/mocks/events.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 0 additions & 24 deletions mqtt/mocks/redis.go

This file was deleted.

0 comments on commit f6477ed

Please sign in to comment.