Skip to content

Commit

Permalink
MG-2457 - Update groups tests (#2509)
Browse files Browse the repository at this point in the history
Signed-off-by: Arvindh <[email protected]>
Signed-off-by: Felix Gateru <[email protected]>
Co-authored-by: Arvindh <[email protected]>
  • Loading branch information
felixgateru and arvindh123 committed Nov 26, 2024
1 parent 0eb9af7 commit c1faa58
Show file tree
Hide file tree
Showing 25 changed files with 4,774 additions and 867 deletions.
1 change: 0 additions & 1 deletion groups/api/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type grpcClient struct {
// NewClient returns new gRPC client instance.
func NewClient(conn *grpc.ClientConn, timeout time.Duration) grpcGroupsV1.GroupsServiceClient {
return &grpcClient{

retrieveEntity: kitgrpc.NewClient(
conn,
svcName,
Expand Down
3 changes: 0 additions & 3 deletions groups/api/grpc/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ import (

func retrieveEntityEndpoint(svc groups.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {

req := request.(retrieveEntityReq)
group, err := svc.RetrieveById(ctx, req.Id)

if err != nil {
return retrieveEntityRes{}, err
}

return retrieveEntityRes{id: group.ID, domain: group.Domain, parentGroup: group.Parent, status: uint8(group.Status)}, nil

}
}
160 changes: 160 additions & 0 deletions groups/api/grpc/endpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package grpc_test

import (
"context"
"fmt"
"net"
"testing"
"time"

"github.com/absmach/magistrala/groups"
grpcapi "github.com/absmach/magistrala/groups/api/grpc"
prmocks "github.com/absmach/magistrala/groups/private/mocks"
grpcCommonV1 "github.com/absmach/magistrala/internal/grpc/common/v1"
grpcGroupsV1 "github.com/absmach/magistrala/internal/grpc/groups/v1"
"github.com/absmach/magistrala/internal/testsutil"
"github.com/absmach/magistrala/pkg/errors"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

const port = 7004

var (
validID = testsutil.GenerateUUID(&testing.T{})
valid = "valid"
validGroupResp = groups.Group{
ID: testsutil.GenerateUUID(&testing.T{}),
Name: valid,
Description: valid,
Domain: testsutil.GenerateUUID(&testing.T{}),
Parent: testsutil.GenerateUUID(&testing.T{}),
Metadata: groups.Metadata{
"name": "test",
},
Children: []*groups.Group{},
CreatedAt: time.Now().Add(-1 * time.Second),
UpdatedAt: time.Now(),
UpdatedBy: testsutil.GenerateUUID(&testing.T{}),
Status: groups.EnabledStatus,
}
)

func startGRPCServer(svc *prmocks.Service, port int) {
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
panic(fmt.Sprintf("failed to obtain port: %s", err))
}
server := grpc.NewServer()
grpcGroupsV1.RegisterGroupsServiceServer(server, grpcapi.NewServer(svc))
go func() {
if err := server.Serve(listener); err != nil {
panic(fmt.Sprintf("failed to serve: %s", err))
}
}()
}

func TestRetrieveEntityEndpoint(t *testing.T) {
svc := new(prmocks.Service)
startGRPCServer(svc, port)
grpAddr := fmt.Sprintf("localhost:%d", port)
conn, _ := grpc.NewClient(grpAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
client := grpcapi.NewClient(conn, time.Second)

cases := []struct {
desc string
req *grpcCommonV1.RetrieveEntityReq
svcRes groups.Group
svcErr error
res *grpcCommonV1.RetrieveEntityRes
err error
}{
{
desc: "retrieve group successfully",
req: &grpcCommonV1.RetrieveEntityReq{
Id: validID,
},
svcRes: validGroupResp,
svcErr: nil,
res: &grpcCommonV1.RetrieveEntityRes{
Entity: &grpcCommonV1.EntityBasic{
Id: validGroupResp.ID,
DomainId: validGroupResp.Domain,
ParentGroupId: validGroupResp.Parent,
Status: uint32(validGroupResp.Status),
},
},
err: nil,
},
{
desc: "retrieve group with authentication error",
req: &grpcCommonV1.RetrieveEntityReq{
Id: validID,
},
svcErr: svcerr.ErrAuthentication,
res: &grpcCommonV1.RetrieveEntityRes{},
err: svcerr.ErrAuthentication,
},
{
desc: "retrieve group with authorization error",
req: &grpcCommonV1.RetrieveEntityReq{
Id: validID,
},
svcErr: svcerr.ErrAuthorization,
res: &grpcCommonV1.RetrieveEntityRes{},
err: svcerr.ErrAuthorization,
},
{
desc: "retrieve group with not found error",
req: &grpcCommonV1.RetrieveEntityReq{
Id: validID,
},
svcErr: svcerr.ErrNotFound,
res: &grpcCommonV1.RetrieveEntityRes{},
err: svcerr.ErrNotFound,
},
{
desc: "retrieve group with malformed entity error",
req: &grpcCommonV1.RetrieveEntityReq{
Id: validID,
},
svcErr: errors.ErrMalformedEntity,
res: &grpcCommonV1.RetrieveEntityRes{},
err: errors.ErrMalformedEntity,
},
{
desc: "retrieve group with conflict error",
req: &grpcCommonV1.RetrieveEntityReq{
Id: validID,
},
svcErr: svcerr.ErrConflict,
res: &grpcCommonV1.RetrieveEntityRes{},
err: svcerr.ErrConflict,
},
{
desc: "retrieve group with unknown error",
req: &grpcCommonV1.RetrieveEntityReq{
Id: validID,
},
svcErr: errors.ErrUnidentified,
res: &grpcCommonV1.RetrieveEntityRes{},
err: errors.ErrUnidentified,
},
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
svcCall := svc.On("RetrieveById", mock.Anything, tc.req.Id).Return(tc.svcRes, tc.svcErr)
res, err := client.RetrieveEntity(context.Background(), tc.req)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s", tc.desc, tc.err, err))
assert.Equal(t, tc.res, res, fmt.Sprintf("%s: expected %s got %s", tc.desc, tc.res, res))
svcCall.Unset()
})
}
}
1 change: 1 addition & 0 deletions groups/api/http/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func decodeHierarchyPageMeta(r *http.Request) (mggroups.HierarchyPageMeta, error
Tree: tree,
}, nil
}

func decodePageMeta(r *http.Request) (mggroups.PageMeta, error) {
s, err := apiutil.ReadStringQuery(r, api.StatusKey, api.DefGroupStatus)
if err != nil {
Expand Down
Loading

0 comments on commit c1faa58

Please sign in to comment.