Skip to content

Commit

Permalink
Removes unusable routes (#492)
Browse files Browse the repository at this point in the history
* deletes add/remove-rooms from rpc

* deletes add/remove-rooms case from management api

* deletes handler func and handler test for add/remove-rooms

* files generated

* deletes remove-add/rooms test
  • Loading branch information
mflilian authored Jul 14, 2022
1 parent f053f0a commit ec3fb79
Show file tree
Hide file tree
Showing 15 changed files with 331 additions and 936 deletions.
4 changes: 0 additions & 4 deletions cmd/managementapi/management_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,6 @@ func buildMuxWithMetricsMdlw(mdlw middleware.Middleware, mux *runtime.ServeMux)
handler = std.Handler("/schedulers", mdlw, mux)
case commom.MatchPath(path, fmt.Sprintf("^/schedulers/%s$", anyWordRegex)):
handler = std.Handler("/schedulers/:schedulerName", mdlw, mux)
case commom.MatchPath(path, fmt.Sprintf("^/schedulers/%s/add-rooms$", anyWordRegex)):
handler = std.Handler("/schedulers/:schedulerName/add-rooms", mdlw, mux)
case commom.MatchPath(path, fmt.Sprintf("^/schedulers/%s/remove-rooms$", anyWordRegex)):
handler = std.Handler("/schedulers/:schedulerName/remove-rooms", mdlw, mux)
case commom.MatchPath(path, fmt.Sprintf("^/schedulers/%s/versions$", anyWordRegex)):
handler = std.Handler("/schedulers/:schedulerName/versions", mdlw, mux)
case commom.MatchPath(path, "^/schedulers/info$"):
Expand Down
34 changes: 0 additions & 34 deletions internal/api/handlers/schedulers_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,40 +161,6 @@ func (h *SchedulersHandler) CreateScheduler(ctx context.Context, request *api.Cr
return &api.CreateSchedulerResponse{Scheduler: returnScheduler}, nil
}

func (h *SchedulersHandler) AddRooms(ctx context.Context, request *api.AddRoomsRequest) (*api.AddRoomsResponse, error) {
handlerLogger := h.logger.With(zap.String(logs.LogFieldSchedulerName, request.GetSchedulerName()))
handlerLogger.Info("handling add rooms request")
operation, err := h.schedulerManager.AddRooms(ctx, request.GetSchedulerName(), request.GetAmount())

if err != nil {
handlerLogger.Error(fmt.Sprintf("error adding rooms to scheduler, amount: %d", request.GetAmount()), zap.Error(err))
if errors.Is(err, portsErrors.ErrNotFound) {
return nil, status.Error(codes.NotFound, err.Error())
}
return nil, status.Error(codes.Unknown, err.Error())
}
handlerLogger.Info("finish handling add rooms request")

return &api.AddRoomsResponse{OperationId: operation.ID}, nil
}

func (h *SchedulersHandler) RemoveRooms(ctx context.Context, request *api.RemoveRoomsRequest) (*api.RemoveRoomsResponse, error) {
handlerLogger := h.logger.With(zap.String(logs.LogFieldSchedulerName, request.GetSchedulerName()))
handlerLogger.Info("handling remove rooms request")
operation, err := h.schedulerManager.RemoveRooms(ctx, request.GetSchedulerName(), int(request.GetAmount()))

if err != nil {
handlerLogger.Error("error removing rooms from scheduler", zap.Error(err))
if errors.Is(err, portsErrors.ErrNotFound) {
return nil, status.Error(codes.NotFound, err.Error())
}
return nil, status.Error(codes.Unknown, err.Error())
}

handlerLogger.Info("finish handling remove rooms request")
return &api.RemoveRoomsResponse{OperationId: operation.ID}, nil
}

func (h *SchedulersHandler) NewSchedulerVersion(ctx context.Context, request *api.NewSchedulerVersionRequest) (*api.NewSchedulerVersionResponse, error) {
handlerLogger := h.logger.With(zap.String(logs.LogFieldSchedulerName, request.GetName()), zap.String(logs.LogFieldGame, request.GetGame()))
handlerLogger.Info("handling new scheduler version request")
Expand Down
163 changes: 0 additions & 163 deletions internal/api/handlers/schedulers_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,169 +660,6 @@ func TestCreateScheduler(t *testing.T) {
})
}

func TestAddRooms(t *testing.T) {

t.Run("with success", func(t *testing.T) {
mockCtrl := gomock.NewController(t)

schedulerStorage := mockports.NewMockSchedulerStorage(mockCtrl)
operationManager := mock.NewMockOperationManager(mockCtrl)
roomStorage := mockports.NewMockRoomStorage(mockCtrl)
operationManager.EXPECT().CreateOperation(gomock.Any(), "scheduler-name-1", gomock.Any()).Return(&operation.Operation{ID: "id-1"}, nil)

schedulerCache := mockports.NewMockSchedulerCache(mockCtrl)
schedulerManager := scheduler_manager.NewSchedulerManager(schedulerStorage, schedulerCache, operationManager, roomStorage)

schedulerStorage.EXPECT().GetScheduler(gomock.Any(), "scheduler-name-1").Return(nil, nil)

mux := runtime.NewServeMux()
err := api.RegisterSchedulersServiceHandlerServer(context.Background(), mux, ProvideSchedulersHandler(schedulerManager))
require.NoError(t, err)

req, err := http.NewRequest(http.MethodPost, "/schedulers/scheduler-name-1/add-rooms", bytes.NewReader([]byte("{\"amount\": 10}")))
require.NoError(t, err)

rr := httptest.NewRecorder()
mux.ServeHTTP(rr, req)

require.Equal(t, http.StatusOK, rr.Code)
bodyString := rr.Body.String()
var body map[string]interface{}
err = json.Unmarshal([]byte(bodyString), &body)
require.NoError(t, err)

require.NotEmpty(t, body["operationId"])
})

t.Run("fails when scheduler does not exists", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
schedulerStorage := mockports.NewMockSchedulerStorage(mockCtrl)
roomStorage := mockports.NewMockRoomStorage(mockCtrl)
schedulerManager := scheduler_manager.NewSchedulerManager(schedulerStorage, nil, nil, roomStorage)

schedulerStorage.EXPECT().GetScheduler(gomock.Any(), "scheduler-name-1").Return(nil, errors.NewErrNotFound("err"))

mux := runtime.NewServeMux()
err := api.RegisterSchedulersServiceHandlerServer(context.Background(), mux, ProvideSchedulersHandler(schedulerManager))
require.NoError(t, err)

req, err := http.NewRequest(http.MethodPost, "/schedulers/scheduler-name-1/add-rooms", bytes.NewReader([]byte("{\"amount\": 10}")))
require.NoError(t, err)

rr := httptest.NewRecorder()
mux.ServeHTTP(rr, req)

require.Equal(t, http.StatusNotFound, rr.Code)
require.Contains(t, rr.Body.String(), "no scheduler found to add rooms on it: err")
})

t.Run("fails when operation enqueue fails", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
schedulerStorage := mockports.NewMockSchedulerStorage(mockCtrl)
operationManager := mock.NewMockOperationManager(mockCtrl)
roomStorage := mockports.NewMockRoomStorage(mockCtrl)
schedulerCache := mockports.NewMockSchedulerCache(mockCtrl)
schedulerManager := scheduler_manager.NewSchedulerManager(schedulerStorage, schedulerCache, operationManager, roomStorage)

schedulerStorage.EXPECT().GetScheduler(gomock.Any(), "scheduler-name-1").Return(nil, nil)
operationManager.EXPECT().CreateOperation(gomock.Any(), "scheduler-name-1", gomock.Any()).Return(nil, errors.NewErrUnexpected("storage offline"))

mux := runtime.NewServeMux()
err := api.RegisterSchedulersServiceHandlerServer(context.Background(), mux, ProvideSchedulersHandler(schedulerManager))
require.NoError(t, err)

req, err := http.NewRequest(http.MethodPost, "/schedulers/scheduler-name-1/add-rooms", bytes.NewReader([]byte("{\"amount\": 10}")))
require.NoError(t, err)

rr := httptest.NewRecorder()
mux.ServeHTTP(rr, req)

require.Equal(t, http.StatusInternalServerError, rr.Code)
require.Contains(t, rr.Body.String(), "not able to schedule the 'add rooms' operation: storage offline")
})
}

func TestRemoveRooms(t *testing.T) {

t.Run("with success", func(t *testing.T) {
mockCtrl := gomock.NewController(t)

schedulerStorage := mockports.NewMockSchedulerStorage(mockCtrl)
operationManager := mock.NewMockOperationManager(mockCtrl)
roomStorage := mockports.NewMockRoomStorage(mockCtrl)
schedulerCache := mockports.NewMockSchedulerCache(mockCtrl)
schedulerManager := scheduler_manager.NewSchedulerManager(schedulerStorage, schedulerCache, operationManager, roomStorage)

schedulerStorage.EXPECT().GetScheduler(gomock.Any(), "scheduler-name-1").Return(nil, nil)
operationManager.EXPECT().CreateOperation(gomock.Any(), "scheduler-name-1", gomock.Any()).Return(&operation.Operation{ID: "id-1"}, nil)

mux := runtime.NewServeMux()
err := api.RegisterSchedulersServiceHandlerServer(context.Background(), mux, ProvideSchedulersHandler(schedulerManager))
require.NoError(t, err)

req, err := http.NewRequest(http.MethodPost, "/schedulers/scheduler-name-1/remove-rooms", bytes.NewReader([]byte("{\"amount\": 10}")))
require.NoError(t, err)

rr := httptest.NewRecorder()
mux.ServeHTTP(rr, req)

require.Equal(t, 200, rr.Code)
bodyString := rr.Body.String()
var body map[string]interface{}
err = json.Unmarshal([]byte(bodyString), &body)
require.NoError(t, err)

require.NotEmpty(t, body["operationId"])
})

t.Run("fails when scheduler does not exists", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
schedulerStorage := mockports.NewMockSchedulerStorage(mockCtrl)
roomStorage := mockports.NewMockRoomStorage(mockCtrl)
schedulerManager := scheduler_manager.NewSchedulerManager(schedulerStorage, nil, nil, roomStorage)

schedulerStorage.EXPECT().GetScheduler(gomock.Any(), "scheduler-name-1").Return(nil, errors.NewErrNotFound("err"))

mux := runtime.NewServeMux()
err := api.RegisterSchedulersServiceHandlerServer(context.Background(), mux, ProvideSchedulersHandler(schedulerManager))
require.NoError(t, err)

req, err := http.NewRequest(http.MethodPost, "/schedulers/scheduler-name-1/remove-rooms", bytes.NewReader([]byte("{\"amount\": 10}")))
require.NoError(t, err)

rr := httptest.NewRecorder()
mux.ServeHTTP(rr, req)

require.Equal(t, 404, rr.Code)
require.Contains(t, rr.Body.String(), "no scheduler found for removing rooms: err")
})

t.Run("fails when operation enqueue fails", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
schedulerStorage := mockports.NewMockSchedulerStorage(mockCtrl)
operationManager := mock.NewMockOperationManager(mockCtrl)
roomStorage := mockports.NewMockRoomStorage(mockCtrl)
schedulerCache := mockports.NewMockSchedulerCache(mockCtrl)
schedulerManager := scheduler_manager.NewSchedulerManager(schedulerStorage, schedulerCache, operationManager, roomStorage)

schedulerStorage.EXPECT().GetScheduler(gomock.Any(), "scheduler-name-1").Return(nil, nil)
operationManager.EXPECT().CreateOperation(gomock.Any(), "scheduler-name-1", gomock.Any()).Return(nil, errors.NewErrUnexpected("storage offline"))

mux := runtime.NewServeMux()
err := api.RegisterSchedulersServiceHandlerServer(context.Background(), mux, ProvideSchedulersHandler(schedulerManager))
require.NoError(t, err)

req, err := http.NewRequest(http.MethodPost, "/schedulers/scheduler-name-1/remove-rooms", bytes.NewReader([]byte("{\"amount\": 10}")))
require.NoError(t, err)

rr := httptest.NewRecorder()
mux.ServeHTTP(rr, req)

require.Equal(t, 500, rr.Code)
require.Contains(t, rr.Body.String(), "not able to schedule the 'remove rooms' operation: storage offline")
})
}

func TestNewSchedulerVersion(t *testing.T) {
dirPath, _ := os.Getwd()

Expand Down
2 changes: 1 addition & 1 deletion pkg/api/v1/messages.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/api/v1/operations.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions pkg/api/v1/operations.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions pkg/api/v1/operations_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/api/v1/rooms.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ec3fb79

Please sign in to comment.