Skip to content

Commit

Permalink
Add mocks with mockery for mqtt
Browse files Browse the repository at this point in the history
Signed-off-by: JeffMboya <[email protected]>

Refactor mqtt handler tests to include eventStore in newHandler function

Signed-off-by: JeffMboya <[email protected]>

NOISSUE - Add property based testing to things service (absmach#2088)

Signed-off-by: Rodney Osodo <[email protected]>
Signed-off-by: rodneyosodo <[email protected]>
Signed-off-by: JeffMboya <[email protected]>

Fix URLs in api-tests.yml

Signed-off-by: JeffMboya <[email protected]>
  • Loading branch information
JeffMboya committed Apr 13, 2024
1 parent c10c453 commit 7e7f4c5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/api-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,4 @@ jobs:

- name: Stop containers
if: always()
run: make run down args="-v"
run: make run down args="-v"
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 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
Expand Down
26 changes: 15 additions & 11 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(t)

cases := []struct {
desc string
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -222,7 +224,7 @@ func TestAuthSubscribe(t *testing.T) {
}

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

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

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

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

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

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

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

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

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

cases := []struct {
Expand All @@ -432,22 +434,24 @@ 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)
}
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
}
62 changes: 52 additions & 10 deletions mqtt/mocks/redis.go

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

0 comments on commit 7e7f4c5

Please sign in to comment.