Skip to content

Commit

Permalink
new test and test update for http server
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Freeman committed Oct 6, 2024
1 parent 58ed061 commit f62b486
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
50 changes: 50 additions & 0 deletions pkg/eventingest/grpc_forwarder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package eventingest

import (
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"gofr.dev/pkg/gofr"
"google.golang.org/protobuf/types/known/emptypb"
)

func TestGRPCEventForwarder_ForwardEvent(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockClient := NewMockServiceClient(ctrl)
forwarder := &GRPCEventForwarder{
client: mockClient,
}

ctx := &gofr.Context{}
tenantID := uuid.New()
customerID := uuid.New()
eventData := []byte("test event data")

expectedRequest := &IngestEventRequest{
TenantID: tenantID,
CustomerID: customerID,
EventData: eventData,
}

// Test successful forward
mockClient.EXPECT().
IngestEvent(gomock.Any(), gomock.Eq(expectedRequest), gomock.Any()).
Return(&emptypb.Empty{}, nil).
Times(1)

err := forwarder.ForwardEvent(ctx, tenantID, customerID, eventData)
assert.NoError(t, err)

// Test error case
mockClient.EXPECT().
IngestEvent(gomock.Any(), gomock.Eq(expectedRequest), gomock.Any()).
Return(nil, assert.AnError).
Times(1)

err = forwarder.ForwardEvent(ctx, tenantID, customerID, eventData)
assert.Error(t, err)
}
12 changes: 6 additions & 6 deletions pkg/eventingest/http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestHandleEvent(t *testing.T) {
{
name: "Success",
setupMocks: func(ctrl *gomock.Controller) (customctx.Context, EventForwarder) {
mockCtx := customctx.NewMockInterface(ctrl)
mockCtx := customctx.NewMockContext(ctrl)
mockEF := NewMockEventForwarder(ctrl)

tenantID := uuid.New()
Expand All @@ -43,7 +43,7 @@ func TestHandleEvent(t *testing.T) {
{
name: "Missing tenant ID",
setupMocks: func(ctrl *gomock.Controller) (customctx.Context, EventForwarder) {
mockCtx := customctx.NewMockInterface(ctrl)
mockCtx := customctx.NewMockContext(ctrl)
mockEF := NewMockEventForwarder(ctrl)

mockCtx.EXPECT().GetUUIDClaim("tenant_id").Return(uuid.Nil, false)
Expand All @@ -56,7 +56,7 @@ func TestHandleEvent(t *testing.T) {
{
name: "Missing customer ID",
setupMocks: func(ctrl *gomock.Controller) (customctx.Context, EventForwarder) {
mockCtx := customctx.NewMockInterface(ctrl)
mockCtx := customctx.NewMockContext(ctrl)
mockEF := NewMockEventForwarder(ctrl)

mockCtx.EXPECT().GetUUIDClaim("tenant_id").Return(uuid.New(), true)
Expand All @@ -70,7 +70,7 @@ func TestHandleEvent(t *testing.T) {
{
name: "Invalid request body",
setupMocks: func(ctrl *gomock.Controller) (customctx.Context, EventForwarder) {
mockCtx := customctx.NewMockInterface(ctrl)
mockCtx := customctx.NewMockContext(ctrl)
mockEF := NewMockEventForwarder(ctrl)

mockCtx.EXPECT().GetUUIDClaim("tenant_id").Return(uuid.New(), true)
Expand All @@ -85,7 +85,7 @@ func TestHandleEvent(t *testing.T) {
{
name: "Forward event failure",
setupMocks: func(ctrl *gomock.Controller) (customctx.Context, EventForwarder) {
mockCtx := customctx.NewMockInterface(ctrl)
mockCtx := customctx.NewMockContext(ctrl)
mockEF := NewMockEventForwarder(ctrl)

tenantID := uuid.New()
Expand Down Expand Up @@ -114,7 +114,7 @@ func TestHandleEvent(t *testing.T) {
mockCtx, mockEF := tt.setupMocks(ctrl)

server := NewHTTPServer(&gofr.App{}, mockEF)
result, err := server.HandleEvent(&mockCtx)
result, err := server.HandleEvent(mockCtx)

assert.Equal(t, tt.expectedResult, result)
assert.Equal(t, tt.expectedError, err)
Expand Down

0 comments on commit f62b486

Please sign in to comment.