Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/baan tests #3

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/PULL_REQUEST_TEMPLATE/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [ ]  New features
- [ ]  Bug fixes
- [ ]  Breaking changes
- [ ] Refactor
## Describe what you have done
-
### New Features
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/selection/selection.handler.go -destination ./mocks/selection/selection.handler.go
mockgen -source ./internal/selection/selection.service.go -destination ./mocks/selection/selection.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

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/go-playground/validator/v10 v10.20.0
github.com/golang/mock v1.6.0
github.com/google/uuid v1.6.0
github.com/isd-sgcu/rpkm67-go-proto v0.0.4
github.com/isd-sgcu/rpkm67-go-proto v0.0.7
github.com/joho/godotenv v1.5.1
github.com/stretchr/testify v1.9.0
github.com/swaggo/files v1.0.1
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/isd-sgcu/rpkm67-go-proto v0.0.4 h1:ZO1m0EfTtjQ2IcRy6mUHj+mAb4loM3ckeisKefKqKmY=
github.com/isd-sgcu/rpkm67-go-proto v0.0.4/go.mod h1:Z5SYz5kEe4W+MdqPouF0zEOiaqvg+s9I1S5d0q6e+Jw=
github.com/isd-sgcu/rpkm67-go-proto v0.0.5 h1:8zTvNQ0NPokoVRK/NQJYxWjcBFMZNK0avotPs9RSPh0=
github.com/isd-sgcu/rpkm67-go-proto v0.0.5/go.mod h1:Z5SYz5kEe4W+MdqPouF0zEOiaqvg+s9I1S5d0q6e+Jw=
github.com/isd-sgcu/rpkm67-go-proto v0.0.6 h1:alECc0pLyJmbJ2cLBukNDplka+ucWutR6yLXqAn0lJM=
github.com/isd-sgcu/rpkm67-go-proto v0.0.6/go.mod h1:Z5SYz5kEe4W+MdqPouF0zEOiaqvg+s9I1S5d0q6e+Jw=
github.com/isd-sgcu/rpkm67-go-proto v0.0.7 h1:BrHjp7hDFmXTMuMKuA4mK3bjtHTvzozO0vYC1hV1a+w=
github.com/isd-sgcu/rpkm67-go-proto v0.0.7/go.mod h1:Z5SYz5kEe4W+MdqPouF0zEOiaqvg+s9I1S5d0q6e+Jw=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
Expand Down
14 changes: 7 additions & 7 deletions internal/auth/auth.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
type Service interface {
Validate()
RefreshToken()
SignUp(req *dto.SignUpRequest) (*dto.Credential, *apperror.AppError)
SignUp(req *dto.SignUpRequest) (*dto.SignupResponse, *apperror.AppError)
SignIn(req *dto.SignInRequest) (*dto.Credential, *apperror.AppError)
SignOut(req *dto.TokenPayloadAuth) (*dto.SignOutResponse, *apperror.AppError)
ForgotPassword(req *dto.ForgotPasswordRequest) (*dto.ForgotPasswordResponse, *apperror.AppError)
Expand All @@ -39,7 +39,7 @@ func (s *serviceImpl) Validate() {
func (s *serviceImpl) RefreshToken() {
}

func (s *serviceImpl) SignUp(req *dto.SignUpRequest) (*dto.Credential, *apperror.AppError) {
func (s *serviceImpl) SignUp(req *dto.SignUpRequest) (*dto.SignupResponse, *apperror.AppError) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

Expand All @@ -48,7 +48,6 @@ func (s *serviceImpl) SignUp(req *dto.SignUpRequest) (*dto.Credential, *apperror
Password: req.Password,
Firstname: req.Firstname,
Lastname: req.Lastname,
Role: "student",
})
if err != nil {
st, ok := status.FromError(err)
Expand All @@ -65,10 +64,11 @@ func (s *serviceImpl) SignUp(req *dto.SignUpRequest) (*dto.Credential, *apperror
}
}

return &dto.Credential{
AccessToken: res.Credential.AccessToken,
RefreshToken: res.Credential.RefreshToken,
ExpiresIn: int(res.Credential.ExpiresIn),
return &dto.SignupResponse{
Id: res.Id,
Email: res.Email,
Firstname: res.Firstname,
Lastname: res.Lastname,
}, nil
}

Expand Down
24 changes: 10 additions & 14 deletions internal/baan/test/baan.handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,40 +50,36 @@ func (t *BaanHandlerTest) SetupTest() {
}

func (t *BaanHandlerTest) TestFindAllBaanSuccess() {
baanSvc := baanMock.NewMockService(t.controller)
validator := validatorMock.NewMockDtoValidator(t.controller)
context := routerMock.NewMockContext(t.controller)
handler := baan.NewHandler(baanSvc, validator, t.logger)

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) TestFindOneBaanSuccess() {
baanSvc := baanMock.NewMockService(t.controller)
validator := validatorMock.NewMockDtoValidator(t.controller)
context := routerMock.NewMockContext(t.controller)
handler := baan.NewHandler(baanSvc, validator, t.logger)

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)
}

Expand Down
24 changes: 10 additions & 14 deletions internal/baan/test/baan.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/isd-sgcu/rpkm67-gateway/internal/dto"
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"
)
Expand Down Expand Up @@ -53,43 +52,40 @@ func (t *BaanServiceTest) SetupTest() {
}

func (t *BaanServiceTest) TestFindAllBaanSuccess() {
client := baanMock.BaanClientMock{}
svc := baan.NewService(&client, t.logger)

protoResp := &baanProto.FindAllBaanResponse{
Baans: t.BaansProto,
}

findAllBaansDto := baan.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)
t.Nil(err)
t.Equal(expected, actual)
}

func (t *BaanServiceTest) TestFindOneBaanSuccess() {
client := baanMock.BaanClientMock{}
svc := baan.NewService(&client, t.logger)

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)
t.Nil(err)
t.Equal(expected, actual)
}

func (t *BaanServiceTest) TearDownTest() {
Expand Down
32 changes: 32 additions & 0 deletions internal/dto/selection.dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dto

type Selection struct {
Id string `json:"id"`
GroupId string `json:"group_id"`
BaanIds []string `json:"baan_ids"`
}

type CreateSelectionRequest struct {
GroupId string `json:"group_id" validate:"required"`
BaanIds []string `json:"baan_ids" validate:"required"`
}

type CreateSelectionResponse struct {
Selection *Selection `json:"selection"`
}

type FindByGroupIdSelectionRequest struct {
GroupId string `json:"group_id" validate:"required"`
}

type FindByGroupIdSelectionResponse struct {
Selection *Selection `json:"selection"`
}

type UpdateSelectionRequest struct {
Selection *Selection `json:"selection" validate:"required"`
}

type UpdateSelectionResponse struct {
Success bool `json:"success"`
}
118 changes: 118 additions & 0 deletions internal/selection/selection.handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package selection

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 {
CreateSelection(c router.Context)
FindByGroupIdSelection(c router.Context)
UpdateSelection(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
log *zap.Logger
}

func (h *handlerImpl) CreateSelection(c router.Context) {
body := &dto.CreateSelectionRequest{}
if err := c.Bind(body); err != nil {
h.log.Named("CreateSelection").Error("Bind: failed to bind request body", zap.Error(err))
c.BadRequestError(err.Error())
return
}

if errorList := h.validate.Validate(body); errorList != nil {
h.log.Named("CreateSelection").Error("Validate: ", zap.Strings("errorList", errorList))
c.BadRequestError(strings.Join(errorList, ", "))
return
}

req := &dto.CreateSelectionRequest{
GroupId: body.GroupId,
BaanIds: body.BaanIds,
}

res, appErr := h.svc.CreateSelection(req)
if appErr != nil {
h.log.Named("CreateSelection").Error("CreateSelection: ", zap.Error(appErr))
c.ResponseError(appErr)
return
}

c.JSON(http.StatusCreated, &dto.CreateSelectionResponse{Selection: res.Selection})
}

func (h *handlerImpl) FindByGroupIdSelection(c router.Context) {
groupdId := c.Param("id")
if groupdId == "" {
h.log.Named("FindByGroupIdSelection").Error("Param: id not found")
c.BadRequestError("url parameter 'id' not found")
return
}

req := &dto.FindByGroupIdSelectionRequest{
GroupId: groupdId,
}

if errorList := h.validate.Validate(req); errorList != nil {
h.log.Named("FindByGroupIdSelection").Error("Validate: ", zap.Strings("errorList", errorList))
c.BadRequestError(strings.Join(errorList, ", "))
return
}

res, appErr := h.svc.FindByGroupIdSelection(req)
if appErr != nil {
h.log.Named("FindByGroupIdSelection").Error("FindByGroupIdSelection: ", zap.Error(appErr))
c.ResponseError(appErr)
return
}

c.JSON(http.StatusOK, &dto.FindByGroupIdSelectionResponse{Selection: res.Selection})
}

func (h *handlerImpl) UpdateSelection(c router.Context) {
body := &dto.UpdateSelectionRequest{}
if err := c.Bind(body); err != nil {
h.log.Named("UpdateSelection").Error("Bind: ", zap.Error(err))
c.BadRequestError(err.Error())
return
}

if errorList := h.validate.Validate(body); errorList != nil {
h.log.Named("UpdateSelection").Error("Validate: ", zap.Strings("errorList", errorList))
c.BadRequestError(strings.Join(errorList, ", "))
return
}

req := &dto.UpdateSelectionRequest{
Selection: body.Selection,
}

res, appErr := h.svc.UpdateSelection(req)
if appErr != nil {
h.log.Named("UpdateSelection").Error("UpdateSelection: ", zap.Error(appErr))
c.ResponseError(appErr)
return
}

c.JSON(http.StatusOK, &dto.UpdateSelectionResponse{
Success: res.Success,
})
}
Loading
Loading