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)
h.UserHandler = NewUserHandler(userBackend)
h.DashboardHandler = NewDashboardHandler(b.UserResourceMappingService, b.LabelService, b.UserService)
h.DashboardHandler.DashboardService = authorizer.NewDashboardService(b.DashboardService)
h.DashboardHandler.DashboardOperationLogService = b.DashboardOperationLogService
dashboardBackend := NewDashboardBackend(b)
dashboardBackend.DashboardService = authorizer.NewDashboardService(b.DashboardService)
h.DashboardHandler = NewDashboardHandler(dashboardBackend)
h.MacroHandler = NewMacroHandler()
h.MacroHandler.MacroService = authorizer.NewMacroService(b.MacroService)

View File

@ -14,6 +14,30 @@ import (
"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
type DashboardHandler struct {
*httprouter.Router
@ -43,14 +67,16 @@ const (
)
// 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{
Router: NewRouter(),
Logger: zap.NewNop(),
Logger: b.Logger,
UserResourceMappingService: mappingService,
LabelService: labelService,
UserService: userService,
DashboardService: b.DashboardService,
DashboardOperationLogService: b.DashboardOperationLogService,
UserResourceMappingService: b.UserResourceMappingService,
LabelService: b.LabelService,
UserService: b.UserService,
}
h.HandlerFunc("POST", dashboardsPath, h.handlePostDashboard)

View File

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

View File

@ -24,6 +24,39 @@ type DashboardService struct {
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) {
return s.FindDashboardByIDF(ctx, id)
}