diff --git a/be1-go/internal/handler/method/publish/hpublish/mocks/hub.go b/be1-go/internal/handler/method/publish/hpublish/mocks/hub.go new file mode 100644 index 0000000000..29ff9f4945 --- /dev/null +++ b/be1-go/internal/handler/method/publish/hpublish/mocks/hub.go @@ -0,0 +1,42 @@ +// Code generated by mockery v2.42.1. DO NOT EDIT. + +package mocks + +import mock "github.com/stretchr/testify/mock" + +// Hub is an autogenerated mock type for the Hub type +type Hub struct { + mock.Mock +} + +// NotifyResetRumorSender provides a mock function with given fields: +func (_m *Hub) NotifyResetRumorSender() error { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for NotifyResetRumorSender") + } + + var r0 error + if rf, ok := ret.Get(0).(func() error); ok { + r0 = rf() + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// NewHub creates a new instance of Hub. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewHub(t interface { + mock.TestingT + Cleanup(func()) +}) *Hub { + mock := &Hub{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/be1-go/internal/handler/method/publish/hpublish/mocks/message_handler.go b/be1-go/internal/handler/method/publish/hpublish/mocks/message_handler.go new file mode 100644 index 0000000000..67a6540bc7 --- /dev/null +++ b/be1-go/internal/handler/method/publish/hpublish/mocks/message_handler.go @@ -0,0 +1,46 @@ +// Code generated by mockery v2.42.1. DO NOT EDIT. + +package mocks + +import ( + mmessage "popstellar/internal/handler/message/mmessage" + + mock "github.com/stretchr/testify/mock" +) + +// MessageHandler is an autogenerated mock type for the MessageHandler type +type MessageHandler struct { + mock.Mock +} + +// Handle provides a mock function with given fields: channelPath, msg, fromRumor +func (_m *MessageHandler) Handle(channelPath string, msg mmessage.Message, fromRumor bool) error { + ret := _m.Called(channelPath, msg, fromRumor) + + if len(ret) == 0 { + panic("no return value specified for Handle") + } + + var r0 error + if rf, ok := ret.Get(0).(func(string, mmessage.Message, bool) error); ok { + r0 = rf(channelPath, msg, fromRumor) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// NewMessageHandler creates a new instance of MessageHandler. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMessageHandler(t interface { + mock.TestingT + Cleanup(func()) +}) *MessageHandler { + mock := &MessageHandler{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/be1-go/internal/handler/method/publish/hpublish/mocks/repository.go b/be1-go/internal/handler/method/publish/hpublish/mocks/repository.go new file mode 100644 index 0000000000..995d444c6f --- /dev/null +++ b/be1-go/internal/handler/method/publish/hpublish/mocks/repository.go @@ -0,0 +1,52 @@ +// Code generated by mockery v2.42.1. DO NOT EDIT. + +package mocks + +import mock "github.com/stretchr/testify/mock" + +// Repository is an autogenerated mock type for the Repository type +type Repository struct { + mock.Mock +} + +// AddMessageToMyRumor provides a mock function with given fields: messageID +func (_m *Repository) AddMessageToMyRumor(messageID string) (int, error) { + ret := _m.Called(messageID) + + if len(ret) == 0 { + panic("no return value specified for AddMessageToMyRumor") + } + + var r0 int + var r1 error + if rf, ok := ret.Get(0).(func(string) (int, error)); ok { + return rf(messageID) + } + if rf, ok := ret.Get(0).(func(string) int); ok { + r0 = rf(messageID) + } else { + r0 = ret.Get(0).(int) + } + + if rf, ok := ret.Get(1).(func(string) error); ok { + r1 = rf(messageID) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// NewRepository creates a new instance of Repository. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewRepository(t interface { + mock.TestingT + Cleanup(func()) +}) *Repository { + mock := &Repository{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/be1-go/internal/handler/method/publish/hpublish/publish_test.go b/be1-go/internal/handler/method/publish/hpublish/publish_test.go new file mode 100644 index 0000000000..e19be04c9e --- /dev/null +++ b/be1-go/internal/handler/method/publish/hpublish/publish_test.go @@ -0,0 +1,57 @@ +package hpublish + +import ( + "github.com/stretchr/testify/require" + "popstellar/internal/handler/method/publish/hpublish/mocks" + "popstellar/internal/logger" + mocks2 "popstellar/internal/network/socket/mocks" + "popstellar/internal/test/generator" + "testing" +) + +func Test_Handle(t *testing.T) { + log := logger.Logger.With().Str("test", "Handle").Logger() + + hub := mocks.NewHub(t) + messageHandler := mocks.NewMessageHandler(t) + db := mocks.NewRepository(t) + + publishHandler := New(hub, db, messageHandler, log) + + socket := mocks2.NewFakeSocket("0") + + queryID := 0 + channelPath := "/root" + msg := generator.NewNothingMsg(t, "sender", nil) + publishBuf := generator.NewPublishQuery(t, queryID, channelPath, msg) + + // succeed handling the publish + notify new rumor + + messageHandler.On("Handle", channelPath, msg, false).Return(nil).Once() + db.On("AddMessageToMyRumor", msg.MessageID).Return(thresholdMessagesByRumor, nil).Once() + hub.On("NotifyResetRumorSender").Return(nil).Once() + + id, err := publishHandler.Handle(socket, publishBuf) + require.Nil(t, id) + require.NoError(t, err) + + // succeed handling the publish but no notify rumor + + messageHandler.On("Handle", channelPath, msg, false).Return(nil).Once() + db.On("AddMessageToMyRumor", msg.MessageID).Return(thresholdMessagesByRumor-1, nil).Once() + + id, err = publishHandler.Handle(socket, publishBuf) + require.Nil(t, id) + require.NoError(t, err) + + // succeed handling the publish but no rumor because federation channel + + channelPath = "/root/laoID/federation" + publishBuf = generator.NewPublishQuery(t, queryID, channelPath, msg) + + messageHandler.On("Handle", channelPath, msg, false).Return(nil).Once() + + id, err = publishHandler.Handle(socket, publishBuf) + require.Nil(t, id) + require.NoError(t, err) +}