From 03ef4286aae2470a2e618e5ea7947a1ff7836311 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 22 Jun 2024 22:08:53 +0700 Subject: [PATCH 01/10] feat: baan service --- internal/baan/baan.service.go | 83 +++++++++++++++++++++++++++++++++++ internal/dto/baan.dto.go | 55 +++++++++++++++++++++++ internal/utils/baan.utils.go | 32 ++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 internal/baan/baan.service.go create mode 100644 internal/dto/baan.dto.go create mode 100644 internal/utils/baan.utils.go diff --git a/internal/baan/baan.service.go b/internal/baan/baan.service.go new file mode 100644 index 0000000..e25d10b --- /dev/null +++ b/internal/baan/baan.service.go @@ -0,0 +1,83 @@ +package baan + +import ( + "context" + "time" + + "github.com/isd-sgcu/rpkm67-gateway/apperrors" + "github.com/isd-sgcu/rpkm67-gateway/internal/dto" + "github.com/isd-sgcu/rpkm67-gateway/internal/utils" + baanProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/baan/v1" + "go.uber.org/zap" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +type Service interface { + FindAllBaan(req *dto.FindAllBaanRequest) (*dto.FindAllBaanResponse, *apperrors.AppError) + FindOneBaan(req *dto.FindOneBaanRequest) (*dto.FindOneBaanResponse, *apperrors.AppError) +} + +type serviceImpl struct { + client baanProto.BaanServiceClient + log *zap.Logger +} + +func NewService(client baanProto.BaanServiceClient, log *zap.Logger) Service { + return &serviceImpl{ + client: client, + log: log, + } +} + +func (s *serviceImpl) FindAllBaan(req *dto.FindAllBaanRequest) (*dto.FindAllBaanResponse, *apperrors.AppError) { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + res, err := s.client.FindAllBaan(ctx, &baanProto.FindAllBaanRequest{}) + if err != nil { + st, ok := status.FromError(err) + if !ok { + return nil, apperrors.InternalServer + } + switch st.Code() { + case codes.NotFound: + return nil, apperrors.NotFoundError("Baans not found") + case codes.Internal: + return nil, apperrors.InternalServerError(err.Error()) + default: + return nil, apperrors.ServiceUnavailable + } + } + + return &dto.FindAllBaanResponse{ + Baans: utils.ProtoToDtoList(res.Baans), + }, nil +} + +func (s *serviceImpl) FindOneBaan(req *dto.FindOneBaanRequest) (*dto.FindOneBaanResponse, *apperrors.AppError) { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + res, err := s.client.FindOneBaan(ctx, &baanProto.FindOneBaanRequest{ + Id: req.Id, + }) + if err != nil { + st, ok := status.FromError(err) + if !ok { + return nil, apperrors.InternalServer + } + switch st.Code() { + case codes.NotFound: + return nil, apperrors.NotFoundError("Baan not found") + case codes.Internal: + return nil, apperrors.InternalServerError(err.Error()) + default: + return nil, apperrors.ServiceUnavailable + } + } + + return &dto.FindOneBaanResponse{ + Baan: *utils.ProtoToDto(res.Baan), + }, nil +} diff --git a/internal/dto/baan.dto.go b/internal/dto/baan.dto.go new file mode 100644 index 0000000..e673e2c --- /dev/null +++ b/internal/dto/baan.dto.go @@ -0,0 +1,55 @@ +package dto + +type BaanSize int32 + +const ( + UNKNOWN = 0 + S = 1 + M = 2 + L = 3 + XL = 4 + XXL = 5 +) + +func (bs BaanSize) String() string { + return [...]string{"UNKNOWN", "S", "M", "L", "XL", "XXL"}[bs] +} + +type Baan struct { + Id string `json:"id"` + NameTH string `json:"name_th"` + DescriptionTH string `json:"description_th"` + NameEN string `json:"name_en"` + DescriptionEN string `json:"description_en"` + Size BaanSize `json:"size"` + Facebook string `json:"facebook"` + FacebookUrl string `json:"facebook_url"` + Instagram string `json:"instagram"` + InstagramUrl string `json:"instagram_url"` + Line string `json:"line"` + LineUrl string `json:"line_url"` + ImageUrl string `json:"image_url"` +} + +type BaanInfo struct { + Id string `json:"id"` + NameTH string `json:"name_th"` + NameEN string `json:"name_en"` + ImageUrl string `json:"image_url"` + Size BaanSize `json:"size"` +} + +type FindAllBaanRequest struct { +} + +type FindAllBaanResponse struct { + Baans []*Baan `json:"baans"` +} + +type FindOneBaanRequest struct { + Id string `json:"id" validate:"required"` +} + +type FindOneBaanResponse struct { + Baan Baan `json:"baan"` +} diff --git a/internal/utils/baan.utils.go b/internal/utils/baan.utils.go new file mode 100644 index 0000000..ff1ae3f --- /dev/null +++ b/internal/utils/baan.utils.go @@ -0,0 +1,32 @@ +package utils + +import ( + "github.com/isd-sgcu/rpkm67-gateway/internal/dto" + baanProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/baan/v1" +) + +func ProtoToDto(in *baanProto.Baan) *dto.Baan { + return &dto.Baan{ + Id: in.Id, + NameTH: in.NameTH, + DescriptionTH: in.DescriptionTH, + NameEN: in.NameEN, + DescriptionEN: in.DescriptionEN, + Size: dto.BaanSize(in.Size), + Facebook: in.Facebook, + FacebookUrl: in.FacebookUrl, + Instagram: in.Instagram, + InstagramUrl: in.InstagramUrl, + Line: in.Line, + LineUrl: in.LineUrl, + ImageUrl: in.ImageUrl, + } +} + +func ProtoToDtoList(in []*baanProto.Baan) []*dto.Baan { + var out []*dto.Baan + for _, b := range in { + out = append(out, ProtoToDto(b)) + } + return out +} From c2c191f3c6896e299d91f873b3933a5c1f5e5076 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 22 Jun 2024 23:24:30 +0700 Subject: [PATCH 02/10] feat: mock-gen for baan --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index a334028..ae3beaa 100644 --- a/Makefile +++ b/Makefile @@ -6,10 +6,11 @@ server: watch: air - mock-gen: mockgen -source ./internal/auth/auth.handler.go -destination ./mocks/auth/auth.handler.go mockgen -source ./internal/auth/auth.service.go -destination ./mocks/auth/auth.service.go + mockgen -source ./internal/baan/baan.handler.go -destination ./mocks/baan/baan.handler.go + mockgen -source ./internal/baan/baan.service.go -destination ./mocks/baan/baan.service.go test: go vet ./... From d5fec8086d2b88b7f1ffb710fe590bfcb97403f9 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 22 Jun 2024 23:50:09 +0700 Subject: [PATCH 03/10] feat: baan mock --- mocks/auth/auth.handler.go | 16 ++++----- mocks/baan/baan.handler.go | 59 ++++++++++++++++++++++++++++++ mocks/baan/baan.service.go | 66 ++++++++++++++++++++++++++++++++++ mocks/client/baan/baan.mock.go | 33 +++++++++++++++++ 4 files changed, 166 insertions(+), 8 deletions(-) create mode 100644 mocks/baan/baan.handler.go create mode 100644 mocks/baan/baan.service.go create mode 100644 mocks/client/baan/baan.mock.go diff --git a/mocks/auth/auth.handler.go b/mocks/auth/auth.handler.go index df306e6..93daa37 100644 --- a/mocks/auth/auth.handler.go +++ b/mocks/auth/auth.handler.go @@ -7,8 +7,8 @@ package mock_auth import ( reflect "reflect" - gin "github.com/gin-gonic/gin" gomock "github.com/golang/mock/gomock" + router "github.com/isd-sgcu/rpkm67-gateway/internal/router" ) // MockHandler is a mock of Handler interface. @@ -35,7 +35,7 @@ func (m *MockHandler) EXPECT() *MockHandlerMockRecorder { } // ForgotPassword mocks base method. -func (m *MockHandler) ForgotPassword(c *gin.Context) { +func (m *MockHandler) ForgotPassword(c router.Context) { m.ctrl.T.Helper() m.ctrl.Call(m, "ForgotPassword", c) } @@ -47,7 +47,7 @@ func (mr *MockHandlerMockRecorder) ForgotPassword(c interface{}) *gomock.Call { } // RefreshToken mocks base method. -func (m *MockHandler) RefreshToken(c *gin.Context) { +func (m *MockHandler) RefreshToken(c router.Context) { m.ctrl.T.Helper() m.ctrl.Call(m, "RefreshToken", c) } @@ -59,7 +59,7 @@ func (mr *MockHandlerMockRecorder) RefreshToken(c interface{}) *gomock.Call { } // ResetPassword mocks base method. -func (m *MockHandler) ResetPassword(c *gin.Context) { +func (m *MockHandler) ResetPassword(c router.Context) { m.ctrl.T.Helper() m.ctrl.Call(m, "ResetPassword", c) } @@ -71,7 +71,7 @@ func (mr *MockHandlerMockRecorder) ResetPassword(c interface{}) *gomock.Call { } // SignIn mocks base method. -func (m *MockHandler) SignIn(c *gin.Context) { +func (m *MockHandler) SignIn(c router.Context) { m.ctrl.T.Helper() m.ctrl.Call(m, "SignIn", c) } @@ -83,7 +83,7 @@ func (mr *MockHandlerMockRecorder) SignIn(c interface{}) *gomock.Call { } // SignOut mocks base method. -func (m *MockHandler) SignOut(c *gin.Context) { +func (m *MockHandler) SignOut(c router.Context) { m.ctrl.T.Helper() m.ctrl.Call(m, "SignOut", c) } @@ -95,7 +95,7 @@ func (mr *MockHandlerMockRecorder) SignOut(c interface{}) *gomock.Call { } // SignUp mocks base method. -func (m *MockHandler) SignUp(c *gin.Context) { +func (m *MockHandler) SignUp(c router.Context) { m.ctrl.T.Helper() m.ctrl.Call(m, "SignUp", c) } @@ -107,7 +107,7 @@ func (mr *MockHandlerMockRecorder) SignUp(c interface{}) *gomock.Call { } // Validate mocks base method. -func (m *MockHandler) Validate(c *gin.Context) { +func (m *MockHandler) Validate(c router.Context) { m.ctrl.T.Helper() m.ctrl.Call(m, "Validate", c) } diff --git a/mocks/baan/baan.handler.go b/mocks/baan/baan.handler.go new file mode 100644 index 0000000..84da72d --- /dev/null +++ b/mocks/baan/baan.handler.go @@ -0,0 +1,59 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: ./internal/baan/baan.handler.go + +// Package mock_baan is a generated GoMock package. +package mock_baan + +import ( + reflect "reflect" + + gomock "github.com/golang/mock/gomock" + router "github.com/isd-sgcu/rpkm67-gateway/internal/router" +) + +// MockHandler is a mock of Handler interface. +type MockHandler struct { + ctrl *gomock.Controller + recorder *MockHandlerMockRecorder +} + +// MockHandlerMockRecorder is the mock recorder for MockHandler. +type MockHandlerMockRecorder struct { + mock *MockHandler +} + +// NewMockHandler creates a new mock instance. +func NewMockHandler(ctrl *gomock.Controller) *MockHandler { + mock := &MockHandler{ctrl: ctrl} + mock.recorder = &MockHandlerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockHandler) EXPECT() *MockHandlerMockRecorder { + return m.recorder +} + +// FindAllBaan mocks base method. +func (m *MockHandler) FindAllBaan(c router.Context) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "FindAllBaan", c) +} + +// FindAllBaan indicates an expected call of FindAllBaan. +func (mr *MockHandlerMockRecorder) FindAllBaan(c interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindAllBaan", reflect.TypeOf((*MockHandler)(nil).FindAllBaan), c) +} + +// FindOneBaan mocks base method. +func (m *MockHandler) FindOneBaan(c router.Context) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "FindOneBaan", c) +} + +// FindOneBaan indicates an expected call of FindOneBaan. +func (mr *MockHandlerMockRecorder) FindOneBaan(c interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOneBaan", reflect.TypeOf((*MockHandler)(nil).FindOneBaan), c) +} diff --git a/mocks/baan/baan.service.go b/mocks/baan/baan.service.go new file mode 100644 index 0000000..bfdc0af --- /dev/null +++ b/mocks/baan/baan.service.go @@ -0,0 +1,66 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: ./internal/baan/baan.service.go + +// Package mock_baan is a generated GoMock package. +package mock_baan + +import ( + reflect "reflect" + + gomock "github.com/golang/mock/gomock" + apperrors "github.com/isd-sgcu/rpkm67-gateway/apperrors" + dto "github.com/isd-sgcu/rpkm67-gateway/internal/dto" +) + +// MockService is a mock of Service interface. +type MockService struct { + ctrl *gomock.Controller + recorder *MockServiceMockRecorder +} + +// MockServiceMockRecorder is the mock recorder for MockService. +type MockServiceMockRecorder struct { + mock *MockService +} + +// NewMockService creates a new mock instance. +func NewMockService(ctrl *gomock.Controller) *MockService { + mock := &MockService{ctrl: ctrl} + mock.recorder = &MockServiceMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockService) EXPECT() *MockServiceMockRecorder { + return m.recorder +} + +// FindAllBaan mocks base method. +func (m *MockService) FindAllBaan(req *dto.FindAllBaanRequest) (*dto.FindAllBaanResponse, *apperrors.AppError) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "FindAllBaan", req) + ret0, _ := ret[0].(*dto.FindAllBaanResponse) + ret1, _ := ret[1].(*apperrors.AppError) + return ret0, ret1 +} + +// FindAllBaan indicates an expected call of FindAllBaan. +func (mr *MockServiceMockRecorder) FindAllBaan(req interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindAllBaan", reflect.TypeOf((*MockService)(nil).FindAllBaan), req) +} + +// FindOneBaan mocks base method. +func (m *MockService) FindOneBaan(req *dto.FindOneBaanRequest) (*dto.FindOneBaanResponse, *apperrors.AppError) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "FindOneBaan", req) + ret0, _ := ret[0].(*dto.FindOneBaanResponse) + ret1, _ := ret[1].(*apperrors.AppError) + return ret0, ret1 +} + +// FindOneBaan indicates an expected call of FindOneBaan. +func (mr *MockServiceMockRecorder) FindOneBaan(req interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOneBaan", reflect.TypeOf((*MockService)(nil).FindOneBaan), req) +} diff --git a/mocks/client/baan/baan.mock.go b/mocks/client/baan/baan.mock.go new file mode 100644 index 0000000..2fec9ec --- /dev/null +++ b/mocks/client/baan/baan.mock.go @@ -0,0 +1,33 @@ +package baan + +import ( + "context" + + baanProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/baan/v1" + "github.com/stretchr/testify/mock" + "google.golang.org/grpc" +) + +type BaanClientMock struct { + mock.Mock +} + +func (c *BaanClientMock) FindAllBaan(_ context.Context, req *baanProto.FindAllBaanRequest, _ ...grpc.CallOption) (res *baanProto.FindAllBaanResponse, err error) { + args := c.Called(req) + + if args.Get(0) != nil { + res = args.Get(0).(*baanProto.FindAllBaanResponse) + } + + return res, args.Error(1) +} + +func (c *BaanClientMock) FindOneBaan(_ context.Context, req *baanProto.FindOneBaanRequest, _ ...grpc.CallOption) (res *baanProto.FindOneBaanResponse, err error) { + args := c.Called(req) + + if args.Get(0) != nil { + res = args.Get(0).(*baanProto.FindOneBaanResponse) + } + + return res, args.Error(1) +} From 9ab944c214482a0d5ee37759c0b6c75fcf0a2306 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 22 Jun 2024 23:50:23 +0700 Subject: [PATCH 04/10] feat: baan test --- internal/baan/test/baan.handler_test.go | 55 +++++++++++++++ internal/baan/test/baan.service_test.go | 94 +++++++++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 internal/baan/test/baan.handler_test.go create mode 100644 internal/baan/test/baan.service_test.go diff --git a/internal/baan/test/baan.handler_test.go b/internal/baan/test/baan.handler_test.go new file mode 100644 index 0000000..4c170c5 --- /dev/null +++ b/internal/baan/test/baan.handler_test.go @@ -0,0 +1,55 @@ +package test + +import ( + "testing" + + "github.com/golang/mock/gomock" + "github.com/isd-sgcu/rpkm67-gateway/apperrors" + "github.com/isd-sgcu/rpkm67-gateway/internal/dto" + "github.com/isd-sgcu/rpkm67-gateway/internal/utils" + "github.com/stretchr/testify/suite" + "go.uber.org/zap" +) + +type BaanHandlerTest struct { + suite.Suite + controller *gomock.Controller + logger *zap.Logger + + Baans []*dto.Baan + Baan *dto.Baan + FindAllBaanReq *dto.FindAllBaanRequest + FindOneBaanReq *dto.FindOneBaanRequest + Err *apperrors.AppError +} + +func TestBaanHandler(t *testing.T) { + suite.Run(t, new(BaanHandlerTest)) +} + +func (t *BaanHandlerTest) SetupTest() { + t.controller = gomock.NewController(t.T()) + t.logger = zap.NewNop() + + baansProto := utils.MockBaansProto() + baanProto := baansProto[0] + + t.Baans = utils.ProtoToDtoList(baansProto) + t.Baan = utils.ProtoToDto(baanProto) + + t.FindAllBaanReq = &dto.FindAllBaanRequest{} + t.FindOneBaanReq = &dto.FindOneBaanRequest{ + Id: t.Baan.Id, + } +} + +func (t *BaanHandlerTest) TestFindAllBaan() { +} + +func (t *BaanHandlerTest) TestFindOneBaan() { + +} + +func (t *BaanHandlerTest) TearDownTest() { + t.controller.Finish() +} diff --git a/internal/baan/test/baan.service_test.go b/internal/baan/test/baan.service_test.go new file mode 100644 index 0000000..b1ce79f --- /dev/null +++ b/internal/baan/test/baan.service_test.go @@ -0,0 +1,94 @@ +package test + +import ( + "testing" + + "github.com/golang/mock/gomock" + "github.com/isd-sgcu/rpkm67-gateway/apperrors" + "github.com/isd-sgcu/rpkm67-gateway/internal/baan" + "github.com/isd-sgcu/rpkm67-gateway/internal/dto" + "github.com/isd-sgcu/rpkm67-gateway/internal/utils" + baanMock "github.com/isd-sgcu/rpkm67-gateway/mocks/client/baan" + baanProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/baan/v1" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" + "go.uber.org/zap" +) + +type BaanServiceTest struct { + suite.Suite + controller *gomock.Controller + logger *zap.Logger + BaansProto []*baanProto.Baan + BaanProto *baanProto.Baan + BaansDto []*dto.Baan + BaanDto *dto.Baan + FindAllBaanProtoReq *baanProto.FindAllBaanRequest + FindAllBaanDtoReq *dto.FindAllBaanRequest + FindOneBaanProtoReq *baanProto.FindOneBaanRequest + FindOneBaanDtoReq *dto.FindOneBaanRequest + Err apperrors.AppError +} + +func TestBaanService(t *testing.T) { + suite.Run(t, new(BaanServiceTest)) +} + +func (t *BaanServiceTest) SetupTest() { + t.controller = gomock.NewController(t.T()) + t.logger = zap.NewNop() + + t.BaansProto = utils.MockBaansProto() + t.BaanProto = t.BaansProto[0] + t.BaansDto = utils.ProtoToDtoList(t.BaansProto) + t.BaanDto = utils.ProtoToDto(t.BaanProto) + + t.FindAllBaanProtoReq = &baanProto.FindAllBaanRequest{} + t.FindOneBaanProtoReq = &baanProto.FindOneBaanRequest{ + Id: t.BaanProto.Id, + } + t.FindAllBaanDtoReq = &dto.FindAllBaanRequest{} + t.FindOneBaanDtoReq = &dto.FindOneBaanRequest{ + Id: t.BaanDto.Id, + } +} + +func (t *BaanServiceTest) TestFindAllBaanSuccess() { + protoResp := &baanProto.FindAllBaanResponse{ + Baans: t.BaansProto, + } + + findAllBaansDto := utils.ProtoToDtoList(protoResp.Baans) + + expected := &dto.FindAllBaanResponse{ + Baans: findAllBaansDto, + } + + client := baanMock.BaanClientMock{} + client.On("FindAllBaan", t.FindAllBaanProtoReq).Return(protoResp, nil) + + svc := baan.NewService(&client, t.logger) + actual, err := svc.FindAllBaan(t.FindAllBaanDtoReq) + + assert.Nil(t.T(), err) + assert.Equal(t.T(), expected, actual) +} + +func (t *BaanServiceTest) TestFindOneBaanSuccess() { + protoResp := &baanProto.FindOneBaanResponse{ + Baan: t.BaanProto, + } + + expected := &dto.FindOneBaanResponse{ + Baan: t.BaanDto, + } + + client := baanMock.BaanClientMock{} + client.On("FindOneBaan", t.FindOneBaanProtoReq).Return(protoResp, nil) + + svc := baan.NewService(&client, t.logger) + actual, err := svc.FindOneBaan(t.FindOneBaanDtoReq) + + assert.Nil(t.T(), err) + assert.Equal(t.T(), expected, actual) +} From 06706f16f0fa78b5df8c76319a25487ba139165b Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 22 Jun 2024 23:50:35 +0700 Subject: [PATCH 05/10] feat: baan core --- internal/baan/baan.handler.go | 61 +++++++++++++++++++++++++++++++++++ internal/baan/baan.service.go | 2 +- internal/dto/baan.dto.go | 2 +- internal/utils/baan.utils.go | 26 +++++++++++++++ 4 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 internal/baan/baan.handler.go diff --git a/internal/baan/baan.handler.go b/internal/baan/baan.handler.go new file mode 100644 index 0000000..253baf8 --- /dev/null +++ b/internal/baan/baan.handler.go @@ -0,0 +1,61 @@ +package baan + +import ( + "net/http" + "strings" + + "github.com/isd-sgcu/rpkm67-gateway/internal/dto" + "github.com/isd-sgcu/rpkm67-gateway/internal/router" + "github.com/isd-sgcu/rpkm67-gateway/internal/validator" + "go.uber.org/zap" +) + +type Handler interface { + FindAllBaan(c router.Context) + FindOneBaan(c router.Context) +} + +type handlerImpl struct { + svc Service + validate validator.DtoValidator + log *zap.Logger +} + +func (h *handlerImpl) FindAllBaan(c router.Context) { + req := &dto.FindAllBaanRequest{} + res, appErr := h.svc.FindAllBaan(req) + if appErr != nil { + c.ResponseError(appErr) + return + } + + c.JSON(http.StatusOK, &dto.FindAllBaanResponse{Baans: res.Baans}) +} + +func (h *handlerImpl) FindOneBaan(c router.Context) { + req := &dto.FindOneBaanRequest{ + Id: c.Param("id"), + } + + if errorList := h.validate.Validate(req); errorList != nil { + h.log.Named("baan hdr").Error("validation error", zap.Strings("errorList", errorList)) + c.BadRequestError(strings.Join(errorList, ", ")) + return + } + + res, appErr := h.svc.FindOneBaan(req) + if appErr != nil { + c.ResponseError(appErr) + return + } + + c.JSON(http.StatusOK, &dto.FindOneBaanResponse{Baan: res.Baan}) +} + +func NewHandler(svc Service, validate validator.DtoValidator, log *zap.Logger) Handler { + return &handlerImpl{ + svc: svc, + validate: validate, + log: log, + } +} diff --git a/internal/baan/baan.service.go b/internal/baan/baan.service.go index e25d10b..2fa4eeb 100644 --- a/internal/baan/baan.service.go +++ b/internal/baan/baan.service.go @@ -78,6 +78,6 @@ func (s *serviceImpl) FindOneBaan(req *dto.FindOneBaanRequest) (*dto.FindOneBaan } return &dto.FindOneBaanResponse{ - Baan: *utils.ProtoToDto(res.Baan), + Baan: utils.ProtoToDto(res.Baan), }, nil } diff --git a/internal/dto/baan.dto.go b/internal/dto/baan.dto.go index e673e2c..7fbd47b 100644 --- a/internal/dto/baan.dto.go +++ b/internal/dto/baan.dto.go @@ -51,5 +51,5 @@ type FindOneBaanRequest struct { } type FindOneBaanResponse struct { - Baan Baan `json:"baan"` + Baan *Baan `json:"baan"` } diff --git a/internal/utils/baan.utils.go b/internal/utils/baan.utils.go index ff1ae3f..08fd9a2 100644 --- a/internal/utils/baan.utils.go +++ b/internal/utils/baan.utils.go @@ -1,6 +1,9 @@ package utils import ( + "math/rand" + + "github.com/bxcodec/faker/v4" "github.com/isd-sgcu/rpkm67-gateway/internal/dto" baanProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/baan/v1" ) @@ -30,3 +33,26 @@ func ProtoToDtoList(in []*baanProto.Baan) []*dto.Baan { } return out } + +func MockBaansProto() []*baanProto.Baan { + var baans []*baanProto.Baan + for i := 0; i < 10; i++ { + baan := &baanProto.Baan{ + Id: faker.UUIDHyphenated(), + NameTH: faker.Name(), + DescriptionTH: faker.Sentence(), + NameEN: faker.Name(), + DescriptionEN: faker.Sentence(), + Size: baanProto.BaanSize(rand.Intn(6)), + Facebook: faker.URL(), + FacebookUrl: faker.URL(), + Instagram: faker.URL(), + InstagramUrl: faker.URL(), + Line: faker.URL(), + LineUrl: faker.URL(), + ImageUrl: faker.URL(), + } + baans = append(baans, baan) + } + return baans +} From 5d8ef10e3fddb6445bd464e865412b7dfb0d89fe Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 22 Jun 2024 23:50:40 +0700 Subject: [PATCH 06/10] chore --- go.mod | 3 +++ go.sum | 3 +++ 2 files changed, 6 insertions(+) diff --git a/go.mod b/go.mod index 99de3c2..95730d2 100644 --- a/go.mod +++ b/go.mod @@ -21,10 +21,13 @@ require ( google.golang.org/grpc v1.64.0 ) +require github.com/stretchr/objx v0.5.2 // indirect + require ( github.com/KyleBanks/depth v1.2.1 // indirect github.com/PuerkitoBio/purell v1.1.1 // indirect github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect + github.com/bxcodec/faker/v4 v4.0.0-beta.3 github.com/bytedance/sonic v1.11.6 // indirect github.com/bytedance/sonic/loader v0.1.1 // indirect github.com/cloudwego/base64x v0.1.4 // indirect diff --git a/go.sum b/go.sum index 4deb831..e62b602 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tN github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M= github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= +github.com/bxcodec/faker/v4 v4.0.0-beta.3 h1:gqYNBvN72QtzKkYohNDKQlm+pg+uwBDVMN28nWHS18k= +github.com/bxcodec/faker/v4 v4.0.0-beta.3/go.mod h1:m6+Ch1Lj3fqW/unZmvkXIdxWS5+XQWPWxcbbQW2X+Ho= github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= @@ -95,6 +97,7 @@ github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6po github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= From 35fdce1c0f3859ab9fd8e1ed8bbacc99d7987f5c Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sun, 23 Jun 2024 01:11:18 +0700 Subject: [PATCH 07/10] feat: mock context, validator --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index ae3beaa..cb178d3 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,8 @@ mock-gen: mockgen -source ./internal/auth/auth.service.go -destination ./mocks/auth/auth.service.go mockgen -source ./internal/baan/baan.handler.go -destination ./mocks/baan/baan.handler.go mockgen -source ./internal/baan/baan.service.go -destination ./mocks/baan/baan.service.go + mockgen -source ./internal/router/context.go -destination ./mocks/router/context.mock.go + mockgen -source ./internal/validator/validator.go -destination ./mocks/validator/validator.mock.go test: go vet ./... From ff9c70535ad374c43cfbbfeea9a2bf690856296c Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sun, 23 Jun 2024 01:11:54 +0700 Subject: [PATCH 08/10] fix: baan handler, service and test --- internal/baan/baan.handler.go | 23 +++++---- internal/baan/baan.service.go | 9 ++-- internal/baan/test/baan.handler_test.go | 51 ++++++++++++++++--- internal/baan/test/baan.service_test.go | 13 +++-- internal/baan/test/baan.service_test.utils.go | 31 +++++++++++ 5 files changed, 101 insertions(+), 26 deletions(-) create mode 100644 internal/baan/test/baan.service_test.utils.go diff --git a/internal/baan/baan.handler.go b/internal/baan/baan.handler.go index 253baf8..9c9fab0 100644 --- a/internal/baan/baan.handler.go +++ b/internal/baan/baan.handler.go @@ -15,6 +15,14 @@ type Handler interface { FindOneBaan(c router.Context) } +func NewHandler(svc Service, validate validator.DtoValidator, log *zap.Logger) Handler { + return &handlerImpl{ + svc: svc, + validate: validate, + log: log, + } +} + type handlerImpl struct { svc Service validate validator.DtoValidator @@ -33,8 +41,13 @@ func (h *handlerImpl) FindAllBaan(c router.Context) { } func (h *handlerImpl) FindOneBaan(c router.Context) { + baanId := c.Param("id") + if baanId == "" { + c.BadRequestError("url parameter 'id' not found") + } + req := &dto.FindOneBaanRequest{ - Id: c.Param("id"), + Id: baanId, } if errorList := h.validate.Validate(req); errorList != nil { @@ -51,11 +64,3 @@ func (h *handlerImpl) FindOneBaan(c router.Context) { c.JSON(http.StatusOK, &dto.FindOneBaanResponse{Baan: res.Baan}) } - -func NewHandler(svc Service, validate validator.DtoValidator, log *zap.Logger) Handler { - return &handlerImpl{ - svc: svc, - validate: validate, - log: log, - } -} diff --git a/internal/baan/baan.service.go b/internal/baan/baan.service.go index 2fa4eeb..63fdfb4 100644 --- a/internal/baan/baan.service.go +++ b/internal/baan/baan.service.go @@ -6,7 +6,6 @@ import ( "github.com/isd-sgcu/rpkm67-gateway/apperrors" "github.com/isd-sgcu/rpkm67-gateway/internal/dto" - "github.com/isd-sgcu/rpkm67-gateway/internal/utils" baanProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/baan/v1" "go.uber.org/zap" "google.golang.org/grpc/codes" @@ -41,8 +40,8 @@ func (s *serviceImpl) FindAllBaan(req *dto.FindAllBaanRequest) (*dto.FindAllBaan return nil, apperrors.InternalServer } switch st.Code() { - case codes.NotFound: - return nil, apperrors.NotFoundError("Baans not found") + case codes.InvalidArgument: + return nil, apperrors.BadRequestError("Invalid argument") case codes.Internal: return nil, apperrors.InternalServerError(err.Error()) default: @@ -51,7 +50,7 @@ func (s *serviceImpl) FindAllBaan(req *dto.FindAllBaanRequest) (*dto.FindAllBaan } return &dto.FindAllBaanResponse{ - Baans: utils.ProtoToDtoList(res.Baans), + Baans: ProtoToDtoList(res.Baans), }, nil } @@ -78,6 +77,6 @@ func (s *serviceImpl) FindOneBaan(req *dto.FindOneBaanRequest) (*dto.FindOneBaan } return &dto.FindOneBaanResponse{ - Baan: utils.ProtoToDto(res.Baan), + Baan: ProtoToDto(res.Baan), }, nil } diff --git a/internal/baan/test/baan.handler_test.go b/internal/baan/test/baan.handler_test.go index 4c170c5..3a21c6a 100644 --- a/internal/baan/test/baan.handler_test.go +++ b/internal/baan/test/baan.handler_test.go @@ -1,26 +1,30 @@ package test import ( + "net/http" "testing" "github.com/golang/mock/gomock" "github.com/isd-sgcu/rpkm67-gateway/apperrors" + "github.com/isd-sgcu/rpkm67-gateway/internal/baan" "github.com/isd-sgcu/rpkm67-gateway/internal/dto" - "github.com/isd-sgcu/rpkm67-gateway/internal/utils" + baanMock "github.com/isd-sgcu/rpkm67-gateway/mocks/baan" + routerMock "github.com/isd-sgcu/rpkm67-gateway/mocks/router" + validatorMock "github.com/isd-sgcu/rpkm67-gateway/mocks/validator" "github.com/stretchr/testify/suite" "go.uber.org/zap" ) type BaanHandlerTest struct { suite.Suite - controller *gomock.Controller - logger *zap.Logger - + controller *gomock.Controller + logger *zap.Logger Baans []*dto.Baan Baan *dto.Baan FindAllBaanReq *dto.FindAllBaanRequest FindOneBaanReq *dto.FindOneBaanRequest Err *apperrors.AppError + ParamMock string } func TestBaanHandler(t *testing.T) { @@ -31,23 +35,56 @@ func (t *BaanHandlerTest) SetupTest() { t.controller = gomock.NewController(t.T()) t.logger = zap.NewNop() - baansProto := utils.MockBaansProto() + baansProto := MockBaansProto() baanProto := baansProto[0] - t.Baans = utils.ProtoToDtoList(baansProto) - t.Baan = utils.ProtoToDto(baanProto) + t.Baans = baan.ProtoToDtoList(baansProto) + t.Baan = baan.ProtoToDto(baanProto) t.FindAllBaanReq = &dto.FindAllBaanRequest{} t.FindOneBaanReq = &dto.FindOneBaanRequest{ Id: t.Baan.Id, } + + t.ParamMock = t.Baan.Id } func (t *BaanHandlerTest) TestFindAllBaan() { + expectedResp := &dto.FindAllBaanResponse{ + Baans: t.Baans, + } + + controller := gomock.NewController(t.T()) + + baanSvc := baanMock.NewMockService(controller) + validator := validatorMock.NewMockDtoValidator(controller) + context := routerMock.NewMockContext(controller) + + baanSvc.EXPECT().FindAllBaan(t.FindAllBaanReq).Return(expectedResp, t.Err) + context.EXPECT().JSON(http.StatusOK, expectedResp) + + handler := baan.NewHandler(baanSvc, validator, t.logger) + handler.FindAllBaan(context) } func (t *BaanHandlerTest) TestFindOneBaan() { + expectedResp := &dto.FindOneBaanResponse{ + Baan: t.Baan, + } + + controller := gomock.NewController(t.T()) + + baanSvc := baanMock.NewMockService(controller) + validator := validatorMock.NewMockDtoValidator(controller) + context := routerMock.NewMockContext(controller) + + context.EXPECT().Param("id").Return(t.ParamMock) + validator.EXPECT().Validate(t.FindOneBaanReq).Return(nil) + baanSvc.EXPECT().FindOneBaan(t.FindOneBaanReq).Return(expectedResp, t.Err) + context.EXPECT().JSON(http.StatusOK, expectedResp) + handler := baan.NewHandler(baanSvc, validator, t.logger) + handler.FindOneBaan(context) } func (t *BaanHandlerTest) TearDownTest() { diff --git a/internal/baan/test/baan.service_test.go b/internal/baan/test/baan.service_test.go index b1ce79f..ef9eb94 100644 --- a/internal/baan/test/baan.service_test.go +++ b/internal/baan/test/baan.service_test.go @@ -7,7 +7,6 @@ import ( "github.com/isd-sgcu/rpkm67-gateway/apperrors" "github.com/isd-sgcu/rpkm67-gateway/internal/baan" "github.com/isd-sgcu/rpkm67-gateway/internal/dto" - "github.com/isd-sgcu/rpkm67-gateway/internal/utils" baanMock "github.com/isd-sgcu/rpkm67-gateway/mocks/client/baan" baanProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/baan/v1" "github.com/stretchr/testify/assert" @@ -38,10 +37,10 @@ func (t *BaanServiceTest) SetupTest() { t.controller = gomock.NewController(t.T()) t.logger = zap.NewNop() - t.BaansProto = utils.MockBaansProto() + t.BaansProto = MockBaansProto() t.BaanProto = t.BaansProto[0] - t.BaansDto = utils.ProtoToDtoList(t.BaansProto) - t.BaanDto = utils.ProtoToDto(t.BaanProto) + t.BaansDto = baan.ProtoToDtoList(t.BaansProto) + t.BaanDto = baan.ProtoToDto(t.BaanProto) t.FindAllBaanProtoReq = &baanProto.FindAllBaanRequest{} t.FindOneBaanProtoReq = &baanProto.FindOneBaanRequest{ @@ -58,7 +57,7 @@ func (t *BaanServiceTest) TestFindAllBaanSuccess() { Baans: t.BaansProto, } - findAllBaansDto := utils.ProtoToDtoList(protoResp.Baans) + findAllBaansDto := baan.ProtoToDtoList(protoResp.Baans) expected := &dto.FindAllBaanResponse{ Baans: findAllBaansDto, @@ -92,3 +91,7 @@ func (t *BaanServiceTest) TestFindOneBaanSuccess() { assert.Nil(t.T(), err) assert.Equal(t.T(), expected, actual) } + +func (t *BaanServiceTest) TearDownTest() { + t.controller.Finish() +} diff --git a/internal/baan/test/baan.service_test.utils.go b/internal/baan/test/baan.service_test.utils.go new file mode 100644 index 0000000..cd6ad16 --- /dev/null +++ b/internal/baan/test/baan.service_test.utils.go @@ -0,0 +1,31 @@ +package test + +import ( + "math/rand" + + "github.com/bxcodec/faker/v4" + baanProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/baan/v1" +) + +func MockBaansProto() []*baanProto.Baan { + var baans []*baanProto.Baan + for i := 0; i < 10; i++ { + baan := &baanProto.Baan{ + Id: faker.UUIDHyphenated(), + NameTH: faker.Name(), + DescriptionTH: faker.Sentence(), + NameEN: faker.Name(), + DescriptionEN: faker.Sentence(), + Size: baanProto.BaanSize(rand.Intn(6)), + Facebook: faker.URL(), + FacebookUrl: faker.URL(), + Instagram: faker.URL(), + InstagramUrl: faker.URL(), + Line: faker.URL(), + LineUrl: faker.URL(), + ImageUrl: faker.URL(), + } + baans = append(baans, baan) + } + return baans +} From 45605d332dfb06b074cd8320af58c95d5535e029 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sun, 23 Jun 2024 01:12:09 +0700 Subject: [PATCH 09/10] chore --- internal/{utils => baan}/baan.utils.go | 28 +--- mocks/router/context.mock.go | 170 +++++++++++++++++++++++++ mocks/validator/validator.mock.go | 48 +++++++ 3 files changed, 219 insertions(+), 27 deletions(-) rename internal/{utils => baan}/baan.utils.go (52%) create mode 100644 mocks/router/context.mock.go create mode 100644 mocks/validator/validator.mock.go diff --git a/internal/utils/baan.utils.go b/internal/baan/baan.utils.go similarity index 52% rename from internal/utils/baan.utils.go rename to internal/baan/baan.utils.go index 08fd9a2..afa1f4d 100644 --- a/internal/utils/baan.utils.go +++ b/internal/baan/baan.utils.go @@ -1,9 +1,6 @@ -package utils +package baan import ( - "math/rand" - - "github.com/bxcodec/faker/v4" "github.com/isd-sgcu/rpkm67-gateway/internal/dto" baanProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/baan/v1" ) @@ -33,26 +30,3 @@ func ProtoToDtoList(in []*baanProto.Baan) []*dto.Baan { } return out } - -func MockBaansProto() []*baanProto.Baan { - var baans []*baanProto.Baan - for i := 0; i < 10; i++ { - baan := &baanProto.Baan{ - Id: faker.UUIDHyphenated(), - NameTH: faker.Name(), - DescriptionTH: faker.Sentence(), - NameEN: faker.Name(), - DescriptionEN: faker.Sentence(), - Size: baanProto.BaanSize(rand.Intn(6)), - Facebook: faker.URL(), - FacebookUrl: faker.URL(), - Instagram: faker.URL(), - InstagramUrl: faker.URL(), - Line: faker.URL(), - LineUrl: faker.URL(), - ImageUrl: faker.URL(), - } - baans = append(baans, baan) - } - return baans -} diff --git a/mocks/router/context.mock.go b/mocks/router/context.mock.go new file mode 100644 index 0000000..358da37 --- /dev/null +++ b/mocks/router/context.mock.go @@ -0,0 +1,170 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: ./internal/router/context.go + +// Package mock_router is a generated GoMock package. +package mock_router + +import ( + reflect "reflect" + + gomock "github.com/golang/mock/gomock" + uuid "github.com/google/uuid" + apperrors "github.com/isd-sgcu/rpkm67-gateway/apperrors" + dto "github.com/isd-sgcu/rpkm67-gateway/internal/dto" +) + +// MockContext is a mock of Context interface. +type MockContext struct { + ctrl *gomock.Controller + recorder *MockContextMockRecorder +} + +// MockContextMockRecorder is the mock recorder for MockContext. +type MockContextMockRecorder struct { + mock *MockContext +} + +// NewMockContext creates a new mock instance. +func NewMockContext(ctrl *gomock.Controller) *MockContext { + mock := &MockContext{ctrl: ctrl} + mock.recorder = &MockContextMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockContext) EXPECT() *MockContextMockRecorder { + return m.recorder +} + +// BadRequestError mocks base method. +func (m *MockContext) BadRequestError(err string) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "BadRequestError", err) +} + +// BadRequestError indicates an expected call of BadRequestError. +func (mr *MockContextMockRecorder) BadRequestError(err interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BadRequestError", reflect.TypeOf((*MockContext)(nil).BadRequestError), err) +} + +// Bind mocks base method. +func (m *MockContext) Bind(obj interface{}) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Bind", obj) + ret0, _ := ret[0].(error) + return ret0 +} + +// Bind indicates an expected call of Bind. +func (mr *MockContextMockRecorder) Bind(obj interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Bind", reflect.TypeOf((*MockContext)(nil).Bind), obj) +} + +// FormFile mocks base method. +func (m *MockContext) FormFile(key string, allowedContentType map[string]struct{}, maxFileSize int64) (*dto.DecomposedFile, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "FormFile", key, allowedContentType, maxFileSize) + ret0, _ := ret[0].(*dto.DecomposedFile) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// FormFile indicates an expected call of FormFile. +func (mr *MockContextMockRecorder) FormFile(key, allowedContentType, maxFileSize interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FormFile", reflect.TypeOf((*MockContext)(nil).FormFile), key, allowedContentType, maxFileSize) +} + +// InternalServerError mocks base method. +func (m *MockContext) InternalServerError(err string) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "InternalServerError", err) +} + +// InternalServerError indicates an expected call of InternalServerError. +func (mr *MockContextMockRecorder) InternalServerError(err interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InternalServerError", reflect.TypeOf((*MockContext)(nil).InternalServerError), err) +} + +// JSON mocks base method. +func (m *MockContext) JSON(statusCode int, obj interface{}) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "JSON", statusCode, obj) +} + +// JSON indicates an expected call of JSON. +func (mr *MockContextMockRecorder) JSON(statusCode, obj interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "JSON", reflect.TypeOf((*MockContext)(nil).JSON), statusCode, obj) +} + +// NewUUID mocks base method. +func (m *MockContext) NewUUID() uuid.UUID { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "NewUUID") + ret0, _ := ret[0].(uuid.UUID) + return ret0 +} + +// NewUUID indicates an expected call of NewUUID. +func (mr *MockContextMockRecorder) NewUUID() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewUUID", reflect.TypeOf((*MockContext)(nil).NewUUID)) +} + +// Param mocks base method. +func (m *MockContext) Param(key string) string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Param", key) + ret0, _ := ret[0].(string) + return ret0 +} + +// Param indicates an expected call of Param. +func (mr *MockContextMockRecorder) Param(key interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Param", reflect.TypeOf((*MockContext)(nil).Param), key) +} + +// PostForm mocks base method. +func (m *MockContext) PostForm(key string) string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "PostForm", key) + ret0, _ := ret[0].(string) + return ret0 +} + +// PostForm indicates an expected call of PostForm. +func (mr *MockContextMockRecorder) PostForm(key interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PostForm", reflect.TypeOf((*MockContext)(nil).PostForm), key) +} + +// Query mocks base method. +func (m *MockContext) Query(key string) string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Query", key) + ret0, _ := ret[0].(string) + return ret0 +} + +// Query indicates an expected call of Query. +func (mr *MockContextMockRecorder) Query(key interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Query", reflect.TypeOf((*MockContext)(nil).Query), key) +} + +// ResponseError mocks base method. +func (m *MockContext) ResponseError(err *apperrors.AppError) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "ResponseError", err) +} + +// ResponseError indicates an expected call of ResponseError. +func (mr *MockContextMockRecorder) ResponseError(err interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ResponseError", reflect.TypeOf((*MockContext)(nil).ResponseError), err) +} diff --git a/mocks/validator/validator.mock.go b/mocks/validator/validator.mock.go new file mode 100644 index 0000000..6fc8a06 --- /dev/null +++ b/mocks/validator/validator.mock.go @@ -0,0 +1,48 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: ./internal/validator/validator.go + +// Package mock_validator is a generated GoMock package. +package mock_validator + +import ( + reflect "reflect" + + gomock "github.com/golang/mock/gomock" +) + +// MockDtoValidator is a mock of DtoValidator interface. +type MockDtoValidator struct { + ctrl *gomock.Controller + recorder *MockDtoValidatorMockRecorder +} + +// MockDtoValidatorMockRecorder is the mock recorder for MockDtoValidator. +type MockDtoValidatorMockRecorder struct { + mock *MockDtoValidator +} + +// NewMockDtoValidator creates a new mock instance. +func NewMockDtoValidator(ctrl *gomock.Controller) *MockDtoValidator { + mock := &MockDtoValidator{ctrl: ctrl} + mock.recorder = &MockDtoValidatorMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockDtoValidator) EXPECT() *MockDtoValidatorMockRecorder { + return m.recorder +} + +// Validate mocks base method. +func (m *MockDtoValidator) Validate(arg0 interface{}) []string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Validate", arg0) + ret0, _ := ret[0].([]string) + return ret0 +} + +// Validate indicates an expected call of Validate. +func (mr *MockDtoValidatorMockRecorder) Validate(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Validate", reflect.TypeOf((*MockDtoValidator)(nil).Validate), arg0) +} From 42ef04405fc1da8362d87eafc4e71b60001792f2 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sun, 23 Jun 2024 01:24:39 +0700 Subject: [PATCH 10/10] chore chore --- internal/baan/test/baan.handler_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/baan/test/baan.handler_test.go b/internal/baan/test/baan.handler_test.go index 3a21c6a..a33e763 100644 --- a/internal/baan/test/baan.handler_test.go +++ b/internal/baan/test/baan.handler_test.go @@ -49,7 +49,7 @@ func (t *BaanHandlerTest) SetupTest() { t.ParamMock = t.Baan.Id } -func (t *BaanHandlerTest) TestFindAllBaan() { +func (t *BaanHandlerTest) TestFindAllBaanSuccess() { expectedResp := &dto.FindAllBaanResponse{ Baans: t.Baans, } @@ -67,7 +67,7 @@ func (t *BaanHandlerTest) TestFindAllBaan() { handler.FindAllBaan(context) } -func (t *BaanHandlerTest) TestFindOneBaan() { +func (t *BaanHandlerTest) TestFindOneBaanSuccess() { expectedResp := &dto.FindOneBaanResponse{ Baan: t.Baan, }