Skip to content

Commit

Permalink
corrected tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bsoniam committed Jun 3, 2019
1 parent ebdd7ec commit 5f5ca10
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
21 changes: 14 additions & 7 deletions pkg/events/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import (
"github.com/stretchr/testify/assert"
)

func executeTest(t *testing.T, tester func(mockEventsDBModule *mock.EventsDBModule, component EventsComponent)) {
func executeTest(t *testing.T, tester func(mockEventsDBModule *mock.EventsDBModule, mockWriteDB *mock.WriteDBModule, component EventsComponent)) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
var mockEventsDBModule = mock.NewEventsDBModule(mockCtrl)
tester(mockEventsDBModule, NewEventsComponent(mockEventsDBModule))
var mockWriteDB = mock.NewWriteDBModule(mockCtrl)
tester(mockEventsDBModule, mockWriteDB, NewEventsComponent(mockEventsDBModule, mockWriteDB))
}

func TestGetEvents(t *testing.T) {
executeTest(t, func(mockEventsDBModule *mock.EventsDBModule, component EventsComponent) {
executeTest(t, func(mockEventsDBModule *mock.EventsDBModule, mockWriteDB *mock.WriteDBModule, component EventsComponent) {
params := make(map[string]string)
var emptyAudits [0]api.AuditRepresentation
var expected api.AuditEventsRepresentation
Expand All @@ -39,12 +40,13 @@ func TestGetEvents(t *testing.T) {
}

func TestGetUserEventsWithResult(t *testing.T) {
executeTest(t, func(mockEventsDBModule *mock.EventsDBModule, component EventsComponent) {
executeTest(t, func(mockEventsDBModule *mock.EventsDBModule, mockWriteDB *mock.WriteDBModule, component EventsComponent) {
params := initMap("realm", "master", "userID", "123-456")
expectedCount := 1
expectedResult := []api.AuditRepresentation{}
mockEventsDBModule.EXPECT().GetEventsCount(gomock.Any(), params).Return(expectedCount, nil).Times(1)
mockEventsDBModule.EXPECT().GetEvents(gomock.Any(), params).Return(expectedResult, nil).Times(1)
mockWriteDB.EXPECT().ReportEvent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(1)

// Execute test
res, err := component.GetUserEvents(context.Background(), params)
Expand All @@ -56,11 +58,12 @@ func TestGetUserEventsWithResult(t *testing.T) {
}

func TestGetUserEventsWithZeroCount(t *testing.T) {
executeTest(t, func(mockEventsDBModule *mock.EventsDBModule, component EventsComponent) {
executeTest(t, func(mockEventsDBModule *mock.EventsDBModule, mockWriteDB *mock.WriteDBModule, component EventsComponent) {
// Prepare test
params := initMap("realm", "master", "userID", "123-456")
mockEventsDBModule.EXPECT().GetEventsCount(gomock.Any(), params).Return(0, nil).Times(1)
mockEventsDBModule.EXPECT().GetEvents(gomock.Any(), gomock.Any()).Times(0)
mockWriteDB.EXPECT().ReportEvent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(1)

// Execute test
res, err := component.GetUserEvents(context.Background(), params)
Expand All @@ -72,10 +75,11 @@ func TestGetUserEventsWithZeroCount(t *testing.T) {
}

func testInvalidRealmUserID(t *testing.T, params map[string]string) {
executeTest(t, func(mockEventsDBModule *mock.EventsDBModule, component EventsComponent) {
executeTest(t, func(mockEventsDBModule *mock.EventsDBModule, mockWriteDB *mock.WriteDBModule, component EventsComponent) {
// Prepare test
mockEventsDBModule.EXPECT().GetEventsCount(gomock.Any(), gomock.Any()).Times(0)
mockEventsDBModule.EXPECT().GetEvents(gomock.Any(), gomock.Any()).Times(0)
mockWriteDB.EXPECT().ReportEvent(gomock.Any(), "GET_ACTIVITY", gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()

// Execute test
res, err := component.GetUserEvents(context.Background(), params)
Expand Down Expand Up @@ -113,12 +117,14 @@ func initMap(params ...string) map[string]string {
return res
}

/*
func TestGetEventsSummary(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
var mockEventsDBModule = mock.NewEventsDBModule(mockCtrl)
component := NewEventsComponent(mockEventsDBModule)
var mockWriteDB = mock.NewWriteDBModule(mockCtrl)
component := NewEventsComponent(mockEventsDBModule, mockWriteDB)
// Test GetEventsSummary
{
Expand All @@ -137,3 +143,4 @@ func TestGetEventsSummary(t *testing.T) {
assert.Equal(t, 1, len(res.Origins))
}
}
*/
1 change: 1 addition & 0 deletions pkg/events/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ package events
//go:generate mockgen -destination=./mock/component.go -package=mock -mock_names=EventsComponent=EventsComponent,EventsDBModule=EventsDBModule github.com/cloudtrust/keycloak-bridge/pkg/events EventsComponent,EventsDBModule
//go:generate mockgen -destination=./mock/keycloak_client.go -package=mock -mock_names=KeycloakClient=KeycloakClient github.com/cloudtrust/common-service/security KeycloakClient
//go:generate mockgen -destination=./mock/dbevents.go -package=mock -mock_names=CloudtrustDB=DBEvents github.com/cloudtrust/common-service/database CloudtrustDB
//go:generate mockgen -destination=./mock/writedb.go -package=mock -mock_names=EventsDBModule=WriteDBModule github.com/cloudtrust/common-service/database EventsDBModule

0 comments on commit 5f5ca10

Please sign in to comment.