Skip to content

Commit

Permalink
MG-2170 - Generate mocks with mockery for HTTP service (absmach#2171)
Browse files Browse the repository at this point in the history
Signed-off-by: nyagamunene <stevenyaga2014@gmail.com>
  • Loading branch information
nyagamunene authored Apr 15, 2024
1 parent 2246d79 commit 4935ae8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 42 deletions.
12 changes: 7 additions & 5 deletions http/api/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
authmocks "github.com/absmach/magistrala/auth/mocks"
server "github.com/absmach/magistrala/http"
"github.com/absmach/magistrala/http/api"
"github.com/absmach/magistrala/http/mocks"
"github.com/absmach/magistrala/internal/apiutil"
mglog "github.com/absmach/magistrala/logger"
pubsub "github.com/absmach/magistrala/pkg/messaging/mocks"
mproxy "github.com/absmach/mproxy/pkg/http"
"github.com/absmach/mproxy/pkg/session"
"github.com/stretchr/testify/assert"
Expand All @@ -26,9 +26,9 @@ import (

const instanceID = "5de9b29a-feb9-11ed-be56-0242ac120002"

func newService(auth magistrala.AuthzServiceClient) session.Handler {
pub := mocks.NewPublisher()
return server.NewHandler(pub, mglog.NewMock(), auth)
func newService(auth magistrala.AuthzServiceClient) (session.Handler, *pubsub.PubSub) {
pub := new(pubsub.PubSub)
return server.NewHandler(pub, mglog.NewMock(), auth), pub
}

func newTargetHTTPServer() *httptest.Server {
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestPublish(t *testing.T) {
msg := `[{"n":"current","t":-1,"v":1.6}]`
msgJSON := `{"field1":"val1","field2":"val2"}`
msgCBOR := `81A3616E6763757272656E746174206176FB3FF999999999999A`
svc := newService(auth)
svc, pub := newService(auth)
target := newTargetHTTPServer()
defer target.Close()
ts, err := newProxyHTPPServer(svc, target)
Expand Down Expand Up @@ -171,6 +171,7 @@ func TestPublish(t *testing.T) {

for desc, tc := range cases {
t.Run(desc, func(t *testing.T) {
svcCall := pub.On("Publish", mock.Anything, tc.chanID, mock.Anything).Return(nil)
req := testRequest{
client: ts.Client(),
method: http.MethodPost,
Expand All @@ -183,6 +184,7 @@ func TestPublish(t *testing.T) {
res, err := req.make()
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", desc, err))
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", desc, tc.status, res.StatusCode))
svcCall.Unset()
})
}
}
5 changes: 0 additions & 5 deletions http/mocks/doc.go

This file was deleted.

25 changes: 0 additions & 25 deletions http/mocks/publisher.go

This file was deleted.

16 changes: 9 additions & 7 deletions pkg/sdk/go/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,31 @@ import (
authmocks "github.com/absmach/magistrala/auth/mocks"
adapter "github.com/absmach/magistrala/http"
"github.com/absmach/magistrala/http/api"
"github.com/absmach/magistrala/http/mocks"
"github.com/absmach/magistrala/internal/apiutil"
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/errors"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
pubsub "github.com/absmach/magistrala/pkg/messaging/mocks"
sdk "github.com/absmach/magistrala/pkg/sdk/go"
mproxy "github.com/absmach/mproxy/pkg/http"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func setupMessages() (*httptest.Server, *authmocks.AuthClient) {
func setupMessages() (*httptest.Server, *authmocks.AuthClient, *pubsub.PubSub) {
auth := new(authmocks.AuthClient)
pub := mocks.NewPublisher()
pub := new(pubsub.PubSub)
handler := adapter.NewHandler(pub, mglog.NewMock(), auth)

mux := api.MakeHandler("")
target := httptest.NewServer(mux)

mp, err := mproxy.NewProxy("", target.URL, handler, mglog.NewMock())
if err != nil {
return nil, nil
return nil, nil, nil
}

return httptest.NewServer(http.HandlerFunc(mp.Handler)), auth
return httptest.NewServer(http.HandlerFunc(mp.Handler)), auth, pub
}

func TestSendMessage(t *testing.T) {
Expand All @@ -46,7 +46,7 @@ func TestSendMessage(t *testing.T) {
invalidToken := "invalid_token"
msg := `[{"n":"current","t":-1,"v":1.6}]`

ts, auth := setupMessages()
ts, auth, pub := setupMessages()
defer ts.Close()
sdkConf := sdk.Config{
HTTPAdapterURL: ts.URL,
Expand Down Expand Up @@ -104,18 +104,20 @@ func TestSendMessage(t *testing.T) {
},
}
for desc, tc := range cases {
svcCall := pub.On("Publish", mock.Anything, tc.chanID, mock.Anything).Return(tc.err)
err := mgsdk.SendMessage(tc.chanID, tc.msg, tc.auth)
switch tc.err {
case nil:
assert.Nil(t, err, fmt.Sprintf("%s: got unexpected error: %s", desc, err))
default:
assert.Equal(t, tc.err.Error(), err.Error(), fmt.Sprintf("%s: expected error %s, got %s", desc, tc.err, err))
}
svcCall.Unset()
}
}

func TestSetContentType(t *testing.T) {
ts, _ := setupMessages()
ts, _, _ := setupMessages()
defer ts.Close()

sdkConf := sdk.Config{
Expand Down

0 comments on commit 4935ae8

Please sign in to comment.