feat(http): construct dashboard handler by DashboardBackend

pull/11700/head
zhulongcheng 2019-01-16 11:54:47 +08:00 committed by Leo Di Donato
parent ef308ab9bd
commit ac43477d68
4 changed files with 109 additions and 53 deletions

View File

@ -92,9 +92,9 @@ func NewAPIHandler(b *APIBackend) *APIHandler {
userBackend.UserService = authorizer.NewUserService(b.UserService) userBackend.UserService = authorizer.NewUserService(b.UserService)
h.UserHandler = NewUserHandler(userBackend) h.UserHandler = NewUserHandler(userBackend)
h.DashboardHandler = NewDashboardHandler(b.UserResourceMappingService, b.LabelService, b.UserService) dashboardBackend := NewDashboardBackend(b)
h.DashboardHandler.DashboardService = authorizer.NewDashboardService(b.DashboardService) dashboardBackend.DashboardService = authorizer.NewDashboardService(b.DashboardService)
h.DashboardHandler.DashboardOperationLogService = b.DashboardOperationLogService h.DashboardHandler = NewDashboardHandler(dashboardBackend)
h.MacroHandler = NewMacroHandler() h.MacroHandler = NewMacroHandler()
h.MacroHandler.MacroService = authorizer.NewMacroService(b.MacroService) h.MacroHandler.MacroService = authorizer.NewMacroService(b.MacroService)

View File

@ -14,6 +14,30 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
// DashboardBackend is all services and associated parameters required to construct
// the DashboardHandler.
type DashboardBackend struct {
Logger *zap.Logger
DashboardService platform.DashboardService
DashboardOperationLogService platform.DashboardOperationLogService
UserResourceMappingService platform.UserResourceMappingService
LabelService platform.LabelService
UserService platform.UserService
}
func NewDashboardBackend(b *APIBackend) *DashboardBackend {
return &DashboardBackend{
Logger: b.Logger.With(zap.String("handler", "dashboard")),
DashboardService: b.DashboardService,
DashboardOperationLogService: b.DashboardOperationLogService,
UserResourceMappingService: b.UserResourceMappingService,
LabelService: b.LabelService,
UserService: b.UserService,
}
}
// DashboardHandler is the handler for the dashboard service // DashboardHandler is the handler for the dashboard service
type DashboardHandler struct { type DashboardHandler struct {
*httprouter.Router *httprouter.Router
@ -43,14 +67,16 @@ const (
) )
// NewDashboardHandler returns a new instance of DashboardHandler. // NewDashboardHandler returns a new instance of DashboardHandler.
func NewDashboardHandler(mappingService platform.UserResourceMappingService, labelService platform.LabelService, userService platform.UserService) *DashboardHandler { func NewDashboardHandler(b *DashboardBackend) *DashboardHandler {
h := &DashboardHandler{ h := &DashboardHandler{
Router: NewRouter(), Router: NewRouter(),
Logger: zap.NewNop(), Logger: b.Logger,
UserResourceMappingService: mappingService, DashboardService: b.DashboardService,
LabelService: labelService, DashboardOperationLogService: b.DashboardOperationLogService,
UserService: userService, UserResourceMappingService: b.UserResourceMappingService,
LabelService: b.LabelService,
UserService: b.UserService,
} }
h.HandlerFunc("POST", dashboardsPath, h.handlePostDashboard) h.HandlerFunc("POST", dashboardsPath, h.handlePostDashboard)

View File

@ -5,6 +5,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"go.uber.org/zap"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -18,6 +19,19 @@ import (
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
) )
// NewMockDashboardBackend returns a DashboardBackend with mock services.
func NewMockDashboardBackend() *DashboardBackend {
return &DashboardBackend{
Logger: zap.NewNop().With(zap.String("handler", "dashboard")),
DashboardService: mock.NewDashboardService(),
DashboardOperationLogService: mock.NewDashboardOperationLogService(),
UserResourceMappingService: mock.NewUserResourceMappingService(),
LabelService: mock.NewLabelService(),
UserService: mock.NewUserService(),
}
}
func TestService_handleGetDashboards(t *testing.T) { func TestService_handleGetDashboards(t *testing.T) {
type fields struct { type fields struct {
DashboardService platform.DashboardService DashboardService platform.DashboardService
@ -309,11 +323,10 @@ func TestService_handleGetDashboards(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
mappingService := mock.NewUserResourceMappingService() dashboardBackend := NewMockDashboardBackend()
labelService := tt.fields.LabelService dashboardBackend.LabelService = tt.fields.LabelService
userService := mock.NewUserService() dashboardBackend.DashboardService = tt.fields.DashboardService
h := NewDashboardHandler(mappingService, labelService, userService) h := NewDashboardHandler(dashboardBackend)
h.DashboardService = tt.fields.DashboardService
r := httptest.NewRequest("GET", "http://any.url", nil) r := httptest.NewRequest("GET", "http://any.url", nil)
@ -461,11 +474,9 @@ func TestService_handleGetDashboard(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
mappingService := mock.NewUserResourceMappingService() dashboardBackend := NewMockDashboardBackend()
labelService := mock.NewLabelService() dashboardBackend.DashboardService = tt.fields.DashboardService
userService := mock.NewUserService() h := NewDashboardHandler(dashboardBackend)
h := NewDashboardHandler(mappingService, labelService, userService)
h.DashboardService = tt.fields.DashboardService
r := httptest.NewRequest("GET", "http://any.url", nil) r := httptest.NewRequest("GET", "http://any.url", nil)
@ -594,11 +605,9 @@ func TestService_handlePostDashboard(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
mappingService := mock.NewUserResourceMappingService() dashboardBackend := NewMockDashboardBackend()
labelService := mock.NewLabelService() dashboardBackend.DashboardService = tt.fields.DashboardService
userService := mock.NewUserService() h := NewDashboardHandler(dashboardBackend)
h := NewDashboardHandler(mappingService, labelService, userService)
h.DashboardService = tt.fields.DashboardService
b, err := json.Marshal(tt.args.dashboard) b, err := json.Marshal(tt.args.dashboard)
if err != nil { if err != nil {
@ -689,11 +698,9 @@ func TestService_handleDeleteDashboard(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
mappingService := mock.NewUserResourceMappingService() dashboardBackend := NewMockDashboardBackend()
labelService := mock.NewLabelService() dashboardBackend.DashboardService = tt.fields.DashboardService
userService := mock.NewUserService() h := NewDashboardHandler(dashboardBackend)
h := NewDashboardHandler(mappingService, labelService, userService)
h.DashboardService = tt.fields.DashboardService
r := httptest.NewRequest("GET", "http://any.url", nil) r := httptest.NewRequest("GET", "http://any.url", nil)
@ -868,11 +875,9 @@ func TestService_handlePatchDashboard(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
mappingService := mock.NewUserResourceMappingService() dashboardBackend := NewMockDashboardBackend()
labelService := mock.NewLabelService() dashboardBackend.DashboardService = tt.fields.DashboardService
userService := mock.NewUserService() h := NewDashboardHandler(dashboardBackend)
h := NewDashboardHandler(mappingService, labelService, userService)
h.DashboardService = tt.fields.DashboardService
upd := platform.DashboardUpdate{} upd := platform.DashboardUpdate{}
if tt.args.name != "" { if tt.args.name != "" {
@ -977,11 +982,9 @@ func TestService_handlePostDashboardCell(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
mappingService := mock.NewUserResourceMappingService() dashboardBackend := NewMockDashboardBackend()
labelService := mock.NewLabelService() dashboardBackend.DashboardService = tt.fields.DashboardService
userService := mock.NewUserService() h := NewDashboardHandler(dashboardBackend)
h := NewDashboardHandler(mappingService, labelService, userService)
h.DashboardService = tt.fields.DashboardService
b, err := json.Marshal(tt.args.cell) b, err := json.Marshal(tt.args.cell)
if err != nil { if err != nil {
@ -1062,11 +1065,9 @@ func TestService_handleDeleteDashboardCell(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
mappingService := mock.NewUserResourceMappingService() dashboardBackend := NewMockDashboardBackend()
labelService := mock.NewLabelService() dashboardBackend.DashboardService = tt.fields.DashboardService
userService := mock.NewUserService() h := NewDashboardHandler(dashboardBackend)
h := NewDashboardHandler(mappingService, labelService, userService)
h.DashboardService = tt.fields.DashboardService
r := httptest.NewRequest("GET", "http://any.url", nil) r := httptest.NewRequest("GET", "http://any.url", nil)
@ -1174,11 +1175,9 @@ func TestService_handlePatchDashboardCell(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
mappingService := mock.NewUserResourceMappingService() dashboardBackend := NewMockDashboardBackend()
labelService := mock.NewLabelService() dashboardBackend.DashboardService = tt.fields.DashboardService
userService := mock.NewUserService() h := NewDashboardHandler(dashboardBackend)
h := NewDashboardHandler(mappingService, labelService, userService)
h.DashboardService = tt.fields.DashboardService
upd := platform.CellUpdate{} upd := platform.CellUpdate{}
if tt.args.x != 0 { if tt.args.x != 0 {
@ -1271,11 +1270,9 @@ func initDashboardService(f platformtesting.DashboardFields, t *testing.T) (plat
} }
} }
mappingService := mock.NewUserResourceMappingService() dashboardBackend := NewMockDashboardBackend()
labelService := mock.NewLabelService() dashboardBackend.DashboardService = svc
userService := mock.NewUserService() h := NewDashboardHandler(dashboardBackend)
h := NewDashboardHandler(mappingService, labelService, userService)
h.DashboardService = svc
server := httptest.NewServer(h) server := httptest.NewServer(h)
client := DashboardService{ client := DashboardService{
Addr: server.URL, Addr: server.URL,

View File

@ -24,6 +24,39 @@ type DashboardService struct {
ReplaceDashboardCellsF func(ctx context.Context, id platform.ID, cs []*platform.Cell) error ReplaceDashboardCellsF func(ctx context.Context, id platform.ID, cs []*platform.Cell) error
} }
// NewDashboardService returns a mock of DashboardService where its methods will return zero values.
func NewDashboardService() *DashboardService {
return &DashboardService{
CreateDashboardF: func(context.Context, *platform.Dashboard) error { return nil },
FindDashboardByIDF: func(context.Context, platform.ID) (*platform.Dashboard, error) { return nil, nil },
FindDashboardsF: func(context.Context, platform.DashboardFilter, platform.FindOptions) ([]*platform.Dashboard, int, error) {
return nil, 0, nil
},
UpdateDashboardF: func(context.Context, platform.ID, platform.DashboardUpdate) (*platform.Dashboard, error) {
return nil, nil
},
DeleteDashboardF: func(context.Context, platform.ID) error { return nil },
AddDashboardCellF: func(ctx context.Context, id platform.ID, c *platform.Cell, opts platform.AddDashboardCellOptions) error {
return nil
},
RemoveDashboardCellF: func(ctx context.Context, dashboardID platform.ID, cellID platform.ID) error { return nil },
GetDashboardCellViewF: func(ctx context.Context, dashboardID platform.ID, cellID platform.ID) (*platform.View, error) {
return nil, nil
},
UpdateDashboardCellViewF: func(ctx context.Context, dashboardID platform.ID, cellID platform.ID, upd platform.ViewUpdate) (*platform.View, error) {
return nil, nil
},
UpdateDashboardCellF: func(ctx context.Context, dashbaordID platform.ID, cellID platform.ID, upd platform.CellUpdate) (*platform.Cell, error) {
return nil, nil
},
CopyDashboardCellF: func(ctx context.Context, dashbaordID platform.ID, cellID platform.ID) (*platform.Cell, error) {
return nil, nil
},
ReplaceDashboardCellsF: func(ctx context.Context, id platform.ID, cs []*platform.Cell) error { return nil },
}
}
func (s *DashboardService) FindDashboardByID(ctx context.Context, id platform.ID) (*platform.Dashboard, error) { func (s *DashboardService) FindDashboardByID(ctx context.Context, id platform.ID) (*platform.Dashboard, error) {
return s.FindDashboardByIDF(ctx, id) return s.FindDashboardByIDF(ctx, id)
} }