From 8268cc6bc0ce3820248eee0bad3969ec309cde74 Mon Sep 17 00:00:00 2001 From: stuart Date: Thu, 13 Jun 2024 17:21:32 +0200 Subject: [PATCH] update the switch of the channels --- .../handler/message/hmessage/message.go | 40 ++---- .../handler/message/hmessage/message_test.go | 125 ++---------------- .../{data_handler.go => channel_handler.go} | 18 +-- be1-go/internal/hub/hub.go | 22 +-- 4 files changed, 42 insertions(+), 163 deletions(-) rename be1-go/internal/handler/message/hmessage/mocks/{data_handler.go => channel_handler.go} (50%) diff --git a/be1-go/internal/handler/message/hmessage/message.go b/be1-go/internal/handler/message/hmessage/message.go index f5a971c1bb..9fc33d98fa 100644 --- a/be1-go/internal/handler/message/hmessage/message.go +++ b/be1-go/internal/handler/message/hmessage/message.go @@ -3,7 +3,6 @@ package hmessage import ( "github.com/rs/zerolog" "popstellar/internal/errors" - "popstellar/internal/handler/channel" "popstellar/internal/handler/message/mmessage" ) @@ -16,27 +15,19 @@ type Repository interface { GetChannelType(channel string) (string, error) } -type DataHandler interface { +type ChannelHandler interface { Handle(channelPath string, msg mmessage.Message) error } -type DataHandlers struct { - Root DataHandler - Lao DataHandler - Election DataHandler - Chirp DataHandler - Reaction DataHandler - Coin DataHandler - Federation DataHandler -} +type ChannelHandlers map[string]ChannelHandler type Handler struct { db Repository - handlers DataHandlers + handlers ChannelHandlers log zerolog.Logger } -func New(db Repository, handlers DataHandlers, log zerolog.Logger) *Handler { +func New(db Repository, handlers ChannelHandlers, log zerolog.Logger) *Handler { return &Handler{ db: db, handlers: handlers, @@ -66,23 +57,10 @@ func (h *Handler) Handle(channelPath string, msg mmessage.Message, fromRumor boo return err } - switch channelType { - case channel.RootObject: - err = h.handlers.Root.Handle(channelPath, msg) - case channel.LAOObject: - err = h.handlers.Lao.Handle(channelPath, msg) - case channel.ElectionObject: - err = h.handlers.Election.Handle(channelPath, msg) - case channel.ChirpObject: - err = h.handlers.Chirp.Handle(channelPath, msg) - case channel.ReactionObject: - err = h.handlers.Reaction.Handle(channelPath, msg) - case channel.CoinObject: - err = h.handlers.Coin.Handle(channelPath, msg) - case channel.FederationObject: - err = h.handlers.Federation.Handle(channelPath, msg) - default: - err = errors.NewInvalidResourceError("unknown channelPath type for %s", channelPath) + handler, ok := h.handlers[channelType] + if !ok { + return errors.NewInvalidResourceError("unknown channelPath type for %s", channelPath) } - return err + + return handler.Handle(channelPath, msg) } diff --git a/be1-go/internal/handler/message/hmessage/message_test.go b/be1-go/internal/handler/message/hmessage/message_test.go index c9c10cf646..d96a87c633 100644 --- a/be1-go/internal/handler/message/hmessage/message_test.go +++ b/be1-go/internal/handler/message/hmessage/message_test.go @@ -14,21 +14,18 @@ import ( "time" ) +func Test_New(t *testing.T) { + +} + func Test_handleChannel(t *testing.T) { db := mocks.NewRepository(t) - dataHandler := mocks.NewDataHandler(t) - - subHandlers := DataHandlers{ - Root: dataHandler, - Lao: dataHandler, - Election: dataHandler, - Chirp: dataHandler, - Reaction: dataHandler, - Coin: dataHandler, - Federation: dataHandler, - } + channelHandler := mocks.NewChannelHandler(t) + + channelHandlers := make(ChannelHandlers) + channelHandlers[channel.Root] = channelHandler - msgHandler := New(db, subHandlers, zerolog.New(io.Discard)) + msgHandler := New(db, channelHandlers, zerolog.New(io.Discard)) _, publicBuf, private, _ := generator.GenerateKeyPair(t) sender := base64.URLEncoding.EncodeToString(publicBuf) @@ -189,12 +186,12 @@ func Test_handleChannel(t *testing.T) { // Test 12: success to handled message for channel root - channelPath = "rootMsg" - msg = generator.NewChirpAddMsg(t, sender, private, time.Now().Unix()) + channelPath = "/root" + msg = generator.NewLaoCreateMsg(t, sender, "laoID", "laoName", time.Now().Unix(), sender, private) db.On("HasMessage", msg.MessageID).Return(false, nil) - db.On("GetChannelType", channelPath).Return(channel.RootObject, nil) - dataHandler.On("Handle", channelPath, msg).Return(nil) + db.On("GetChannelType", channelPath).Return(channel.Root, nil).Once() + channelHandler.On("Handle", channelPath, msg).Return(nil) args = append(args, input{ name: "Test 12", @@ -203,102 +200,6 @@ func Test_handleChannel(t *testing.T) { isError: false, }) - // Test 13: success to handled message for channel lao - - channelPath = "laoMsg" - msg = generator.NewChirpAddMsg(t, sender, private, time.Now().Unix()) - - db.On("HasMessage", msg.MessageID).Return(false, nil) - db.On("GetChannelType", channelPath).Return(channel.LAOObject, nil) - dataHandler.On("Handle", channelPath, msg).Return(nil) - - args = append(args, input{ - name: "Test 13", - channelPath: channelPath, - message: msg, - isError: false, - }) - - // Test 14: success to handled message for channel election - - channelPath = "electionMsg" - msg = generator.NewChirpAddMsg(t, sender, private, time.Now().Unix()) - - db.On("HasMessage", msg.MessageID).Return(false, nil) - db.On("GetChannelType", channelPath).Return(channel.ElectionObject, nil) - dataHandler.On("Handle", channelPath, msg).Return(nil) - - args = append(args, input{ - name: "Test 14", - channelPath: channelPath, - message: msg, - isError: false, - }) - - // Test 15: success to handled message for channel chirp - - channelPath = "chirpMsg" - msg = generator.NewChirpAddMsg(t, sender, private, time.Now().Unix()) - - db.On("HasMessage", msg.MessageID).Return(false, nil) - db.On("GetChannelType", channelPath).Return(channel.ChirpObject, nil) - dataHandler.On("Handle", channelPath, msg).Return(nil) - - args = append(args, input{ - name: "Test 15", - channelPath: channelPath, - message: msg, - isError: false, - }) - - // Test 16: success to handled message for channel reaction - - channelPath = "reaction" - msg = generator.NewChirpAddMsg(t, sender, private, time.Now().Unix()) - - db.On("HasMessage", msg.MessageID).Return(false, nil) - db.On("GetChannelType", channelPath).Return(channel.ReactionObject, nil) - dataHandler.On("Handle", channelPath, msg).Return(nil) - - args = append(args, input{ - name: "Test 16", - channelPath: channelPath, - message: msg, - isError: false, - }) - - // Test 17: success to handled message for channel coin - - channelPath = "coinMsg" - msg = generator.NewChirpAddMsg(t, sender, private, time.Now().Unix()) - - db.On("HasMessage", msg.MessageID).Return(false, nil) - db.On("GetChannelType", channelPath).Return(channel.CoinObject, nil) - dataHandler.On("Handle", channelPath, msg).Return(nil) - - args = append(args, input{ - name: "Test 17", - channelPath: channelPath, - message: msg, - isError: false, - }) - - // Test 18: success to handled message for channel coin - - channelPath = "coinMsg" - msg = generator.NewChirpAddMsg(t, sender, private, time.Now().Unix()) - - db.On("HasMessage", msg.MessageID).Return(false, nil) - db.On("GetChannelType", channelPath).Return(channel.FederationObject, nil) - dataHandler.On("Handle", channelPath, msg).Return(nil) - - args = append(args, input{ - name: "Test 18", - channelPath: channelPath, - message: msg, - isError: false, - }) - for _, arg := range args { t.Run(arg.name, func(t *testing.T) { err := msgHandler.Handle(arg.channelPath, arg.message, false) diff --git a/be1-go/internal/handler/message/hmessage/mocks/data_handler.go b/be1-go/internal/handler/message/hmessage/mocks/channel_handler.go similarity index 50% rename from be1-go/internal/handler/message/hmessage/mocks/data_handler.go rename to be1-go/internal/handler/message/hmessage/mocks/channel_handler.go index 19409b6dfc..d937706f34 100644 --- a/be1-go/internal/handler/message/hmessage/mocks/data_handler.go +++ b/be1-go/internal/handler/message/hmessage/mocks/channel_handler.go @@ -3,18 +3,18 @@ package mocks import ( - message "popstellar/internal/handler/message/mmessage" + mmessage "popstellar/internal/handler/message/mmessage" mock "github.com/stretchr/testify/mock" ) -// DataHandler is an autogenerated mock type for the DataHandler type -type DataHandler struct { +// ChannelHandler is an autogenerated mock type for the ChannelHandler type +type ChannelHandler struct { mock.Mock } // Handle provides a mock function with given fields: channelPath, msg -func (_m *DataHandler) Handle(channelPath string, msg message.Message) error { +func (_m *ChannelHandler) Handle(channelPath string, msg mmessage.Message) error { ret := _m.Called(channelPath, msg) if len(ret) == 0 { @@ -22,7 +22,7 @@ func (_m *DataHandler) Handle(channelPath string, msg message.Message) error { } var r0 error - if rf, ok := ret.Get(0).(func(string, message.Message) error); ok { + if rf, ok := ret.Get(0).(func(string, mmessage.Message) error); ok { r0 = rf(channelPath, msg) } else { r0 = ret.Error(0) @@ -31,13 +31,13 @@ func (_m *DataHandler) Handle(channelPath string, msg message.Message) error { return r0 } -// NewDataHandler creates a new instance of DataHandler. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// NewChannelHandler creates a new instance of ChannelHandler. 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 NewDataHandler(t interface { +func NewChannelHandler(t interface { mock.TestingT Cleanup(func()) -}) *DataHandler { - mock := &DataHandler{} +}) *ChannelHandler { + mock := &ChannelHandler{} mock.Mock.Test(t) t.Cleanup(func() { mock.AssertExpectations(t) }) diff --git a/be1-go/internal/hub/hub.go b/be1-go/internal/hub/hub.go index 59df535384..f03e3ae31c 100644 --- a/be1-go/internal/hub/hub.go +++ b/be1-go/internal/hub/hub.go @@ -9,6 +9,7 @@ import ( "popstellar/internal/database/sqlite" poperrors "popstellar/internal/errors" "popstellar/internal/handler/answer/hanswer" + "popstellar/internal/handler/channel" "popstellar/internal/handler/channel/chirp/hchirp" "popstellar/internal/handler/channel/coin/hcoin" "popstellar/internal/handler/channel/election/helection" @@ -156,19 +157,18 @@ func New(dbPath string, ownerPubKey kyber.Point, clientAddress, serverAddress st return nil, err } - // Create the message data handlers - dataHandlers := hmessage.DataHandlers{ - Root: hroot.New(conf, &db, subs, peers, schemaValidator, log), - Lao: hlao.New(conf, subs, &db, schemaValidator, log), - Election: helection.New(conf, subs, &db, schemaValidator, log), - Chirp: hchirp.New(conf, subs, &db, schemaValidator, log), - Reaction: hreaction.New(subs, &db, schemaValidator, log), - Coin: hcoin.New(subs, &db, schemaValidator, log), - Federation: hfederation.New(hubParams, subs, &db, schemaValidator, log), - } + // Create the message channel handlers + channelHandlers := make(hmessage.ChannelHandlers) + channelHandlers[channel.RootObject] = hroot.New(conf, &db, subs, peers, schemaValidator, log) + channelHandlers[channel.LAOObject] = hlao.New(conf, subs, &db, schemaValidator, log) + channelHandlers[channel.ElectionObject] = helection.New(conf, subs, &db, schemaValidator, log) + channelHandlers[channel.ChirpObject] = hchirp.New(conf, subs, &db, schemaValidator, log) + channelHandlers[channel.ReactionObject] = hreaction.New(subs, &db, schemaValidator, log) + channelHandlers[channel.CoinObject] = hcoin.New(subs, &db, schemaValidator, log) + channelHandlers[channel.FederationObject] = hfederation.New(hubParams, subs, &db, schemaValidator, log) // Create the message handler - msgHandler := hmessage.New(&db, dataHandlers, log) + msgHandler := hmessage.New(&db, channelHandlers, log) // Create the greetserver handler greetserverHandler := hgreetserver.New(conf, peers, log)