diff --git a/src/app/dto/common.dto.go b/src/app/dto/common.dto.go index 611c90e..ba64d2d 100644 --- a/src/app/dto/common.dto.go +++ b/src/app/dto/common.dto.go @@ -67,3 +67,9 @@ type ResponseGatewayTimeoutErr struct { Message string `json:"message" example:"Connection timeout"` Data interface{} `json:"data"` } + +type ResponseSuccess struct { + StatusCode int `json:"status_code" example:"200"` + Message string `json:"message" example:"success"` + Data interface{} `json:"data"` +} diff --git a/src/app/dto/pet.dto.go b/src/app/dto/pet.dto.go index cf2fd94..0d7c5b1 100644 --- a/src/app/dto/pet.dto.go +++ b/src/app/dto/pet.dto.go @@ -4,8 +4,33 @@ import ( "github.com/isd-sgcu/johnjud-gateway/src/constant/pet" ) -type PetDto struct { - Id string `json:"id"` +type ImageResponse struct { + Id string `json:"id"` + Url string `json:"url"` +} + +type PetResponse struct { + Id string `json:"id"` + Type string `json:"type"` + Species string `json:"species"` + Name string `json:"name"` + Birthdate string `json:"birthdate"` + Gender pet.Gender `json:"gender"` + Habit string `json:"habit"` + Caption string `json:"caption"` + Status pet.Status `json:"status"` + IsSterile *bool `json:"is_sterile"` + IsVaccinated *bool `json:"is_vaccinated"` + IsVisible *bool `json:"is_visible"` + IsClubPet *bool `json:"is_club_pet"` + Background string `json:"background"` + Address string `json:"address"` + Contact string `json:"contact"` + AdoptBy string `json:"adopt_by"` + Images []ImageResponse `json:"images"` +} + +type CreatePetRequest struct { Type string `json:"type" validate:"required"` Species string `json:"species" validate:"required"` Name string `json:"name" validate:"required"` @@ -22,20 +47,39 @@ type PetDto struct { Address string `json:"address"` Contact string `json:"contact"` AdoptBy string `json:"adopt_by"` -} - -type CreatePetRequest struct { - Pet *PetDto `json:"pet" validate:"required"` + Images []string `json:"images"` } type ChangeViewPetRequest struct { Visible bool `json:"visible" validate:"required"` } -type UpdatePetRequest struct { - Pet *PetDto `json:"pet" validate:"required"` +type ChangeViewPetResponse struct { + Success bool `json:"success" validate:"required"` } +type UpdatePetRequest struct { + Type string `json:"type"` + Species string `json:"species"` + Name string `json:"name"` + Birthdate string `json:"birthdate"` + Gender pet.Gender `json:"gender"` + Habit string `json:"habit"` + Caption string `json:"caption"` + Status pet.Status `json:"status"` + IsSterile *bool `json:"is_sterile"` + IsVaccinated *bool `json:"is_vaccinated"` + IsVisible *bool `json:"is_visible"` + IsClubPet *bool `json:"is_club_pet"` + Background string `json:"background"` + Address string `json:"address"` + Contact string `json:"contact"` + AdoptBy string `json:"adopt_by"` + Images []string `json:"images"` +} type DeleteRequest struct { Id string `json:"id" validate:"required"` } +type DeleteResponse struct { + Success bool `json:"success" validate:"required"` +} diff --git a/src/app/handler/pet/pet.handler.go b/src/app/handler/pet/pet.handler.go index 66e179d..6af1f2b 100644 --- a/src/app/handler/pet/pet.handler.go +++ b/src/app/handler/pet/pet.handler.go @@ -9,7 +9,7 @@ import ( imageSrv "github.com/isd-sgcu/johnjud-gateway/src/app/handler/image" "github.com/isd-sgcu/johnjud-gateway/src/app/router" "github.com/isd-sgcu/johnjud-gateway/src/app/validator" - pet_proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" + petconst "github.com/isd-sgcu/johnjud-gateway/src/constant/pet" ) type Handler struct { @@ -19,12 +19,12 @@ type Handler struct { } type Service interface { - FindAll() ([]*pet_proto.Pet, *dto.ResponseErr) - FindOne(string) (*pet_proto.Pet, *dto.ResponseErr) - Create(*dto.CreatePetRequest) (*pet_proto.Pet, *dto.ResponseErr) - Update(string, *dto.UpdatePetRequest) (*pet_proto.Pet, *dto.ResponseErr) - ChangeView(string, *dto.ChangeViewPetRequest) (bool, *dto.ResponseErr) - Delete(string) (bool, *dto.ResponseErr) + FindAll() ([]*dto.PetResponse, *dto.ResponseErr) + FindOne(string) (*dto.PetResponse, *dto.ResponseErr) + Create(*dto.CreatePetRequest) (*dto.PetResponse, *dto.ResponseErr) + Update(string, *dto.UpdatePetRequest) (*dto.PetResponse, *dto.ResponseErr) + ChangeView(string, *dto.ChangeViewPetRequest) (*dto.ChangeViewPetResponse, *dto.ResponseErr) + Delete(string) (*dto.DeleteResponse, *dto.ResponseErr) } func NewHandler(service Service, imageService imageSrv.Service, validate validator.IDtoValidator) *Handler { @@ -48,7 +48,11 @@ func (h *Handler) FindAll(c router.IContext) { return } - c.JSON(http.StatusOK, response) + c.JSON(http.StatusOK, dto.ResponseSuccess{ + StatusCode: http.StatusOK, + Message: petconst.FindAllPetSuccessMessage, + Data: response, + }) return } @@ -67,7 +71,7 @@ func (h *Handler) FindAll(c router.IContext) { func (h *Handler) FindOne(c router.IContext) { id, err := c.Param("id") if err != nil { - c.JSON(http.StatusInternalServerError, &dto.ResponseErr{ + c.JSON(http.StatusInternalServerError, dto.ResponseErr{ StatusCode: http.StatusInternalServerError, Message: "Invalid ID", Data: nil, @@ -81,7 +85,11 @@ func (h *Handler) FindOne(c router.IContext) { return } - c.JSON(http.StatusOK, response) + c.JSON(http.StatusOK, dto.ResponseSuccess{ + StatusCode: http.StatusOK, + Message: petconst.FindOnePetSuccessMessage, + Data: response, + }) return } @@ -98,9 +106,7 @@ func (h *Handler) FindOne(c router.IContext) { // @Failure 503 {object} dto.ResponseServiceDownErr "Service is down" // @Router /v1/pets/create [post] func (h *Handler) Create(c router.IContext) { - request := &dto.CreatePetRequest{ - Pet: &dto.PetDto{}, - } + request := &dto.CreatePetRequest{} err := c.Bind(request) if err != nil { c.JSON(http.StatusBadRequest, dto.ResponseErr{ @@ -130,7 +136,11 @@ func (h *Handler) Create(c router.IContext) { return } - c.JSON(http.StatusCreated, response) + c.JSON(http.StatusCreated, dto.ResponseSuccess{ + StatusCode: http.StatusCreated, + Message: petconst.CreatePetSuccessMessage, + Data: response, + }) return } @@ -158,9 +168,7 @@ func (h *Handler) Update(c router.IContext) { return } - request := &dto.UpdatePetRequest{ - Pet: &dto.PetDto{}, - } + request := &dto.UpdatePetRequest{} err = c.Bind(request) if err != nil { @@ -191,7 +199,11 @@ func (h *Handler) Update(c router.IContext) { return } - c.JSON(http.StatusOK, pet) + c.JSON(http.StatusOK, dto.ResponseSuccess{ + StatusCode: http.StatusOK, + Message: petconst.UpdatePetSuccessMessage, + Data: pet, + }) return } @@ -211,7 +223,7 @@ func (h *Handler) Update(c router.IContext) { func (h *Handler) ChangeView(c router.IContext) { id, err := c.Param("id") if err != nil { - c.JSON(http.StatusBadRequest, &dto.ResponseErr{ + c.JSON(http.StatusBadRequest, dto.ResponseErr{ StatusCode: http.StatusBadRequest, Message: err.Error(), Data: nil, @@ -250,7 +262,11 @@ func (h *Handler) ChangeView(c router.IContext) { return } - c.JSON(http.StatusOK, res) + c.JSON(http.StatusOK, dto.ResponseSuccess{ + StatusCode: http.StatusOK, + Message: petconst.ChangeViewPetSuccessMessage, + Data: res, + }) return } @@ -269,7 +285,7 @@ func (h *Handler) ChangeView(c router.IContext) { func (h *Handler) Delete(c router.IContext) { id, err := c.Param("id") if err != nil { - c.JSON(http.StatusBadRequest, &dto.ResponseErr{ + c.JSON(http.StatusBadRequest, dto.ResponseErr{ StatusCode: http.StatusBadRequest, Message: err.Error(), Data: nil, @@ -283,6 +299,10 @@ func (h *Handler) Delete(c router.IContext) { return } - c.JSON(http.StatusOK, res) + c.JSON(http.StatusOK, dto.ResponseSuccess{ + StatusCode: http.StatusOK, + Message: petconst.DeletePetSuccessMessage, + Data: res, + }) return } diff --git a/src/app/handler/pet/pet.handler_test.go b/src/app/handler/pet/pet.handler_test.go index a971caa..9f613dd 100644 --- a/src/app/handler/pet/pet.handler_test.go +++ b/src/app/handler/pet/pet.handler_test.go @@ -14,6 +14,8 @@ import ( mock_pet "github.com/isd-sgcu/johnjud-gateway/src/mocks/service/pet" mock_validator "github.com/isd-sgcu/johnjud-gateway/src/mocks/validator" + utils "github.com/isd-sgcu/johnjud-gateway/src/app/utils/pet" + petconst "github.com/isd-sgcu/johnjud-gateway/src/constant/pet" pet_proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" image_proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/file/image/v1" "github.com/stretchr/testify/suite" @@ -23,7 +25,7 @@ type PetHandlerTest struct { suite.Suite Pet *pet_proto.Pet Pets []*pet_proto.Pet - PetDto *dto.PetDto + PetDto *dto.PetResponse CreatePetRequest *dto.CreatePetRequest ChangeViewPetRequest *dto.ChangeViewPetRequest UpdatePetRequest *dto.UpdatePetRequest @@ -31,6 +33,8 @@ type PetHandlerTest struct { NotFoundErr *dto.ResponseErr ServiceDownErr *dto.ResponseErr InternalErr *dto.ResponseErr + Images []*image_proto.Image + ImagesList [][]*image_proto.Image } func TestPetHandler(t *testing.T) { @@ -38,6 +42,9 @@ func TestPetHandler(t *testing.T) { } func (t *PetHandlerTest) SetupTest() { + imagesList := utils.MockImageList(3) + t.ImagesList = imagesList + t.Images = imagesList[0] var pets []*pet_proto.Pet for i := 0; i <= 3; i++ { pet := &pet_proto.Pet{ @@ -67,7 +74,7 @@ func (t *PetHandlerTest) SetupTest() { t.Pets = pets t.Pet = t.Pets[0] - t.PetDto = &dto.PetDto{ + t.PetDto = &dto.PetResponse{ Id: t.Pet.Id, Type: t.Pet.Type, Species: t.Pet.Species, @@ -87,13 +94,9 @@ func (t *PetHandlerTest) SetupTest() { AdoptBy: t.Pet.AdoptBy, } - t.CreatePetRequest = &dto.CreatePetRequest{ - Pet: &dto.PetDto{}, - } + t.CreatePetRequest = &dto.CreatePetRequest{} - t.UpdatePetRequest = &dto.UpdatePetRequest{ - Pet: &dto.PetDto{}, - } + t.UpdatePetRequest = &dto.UpdatePetRequest{} t.ChangeViewPetRequest = &dto.ChangeViewPetRequest{} @@ -122,7 +125,12 @@ func (t *PetHandlerTest) SetupTest() { } func (t *PetHandlerTest) TestFindAllSuccess() { - findAllResponse := t.Pets + findAllResponse := utils.RawToDtoList(t.Pets, t.ImagesList) + expectedResponse := dto.ResponseSuccess{ + StatusCode: http.StatusOK, + Message: petconst.FindAllPetSuccessMessage, + Data: findAllResponse, + } controller := gomock.NewController(t.T()) @@ -132,14 +140,19 @@ func (t *PetHandlerTest) TestFindAllSuccess() { context := mock_router.NewMockIContext(controller) petSvc.EXPECT().FindAll().Return(findAllResponse, nil) - context.EXPECT().JSON(http.StatusOK, findAllResponse) + context.EXPECT().JSON(http.StatusOK, expectedResponse) handler := NewHandler(petSvc, imageSvc, validator) handler.FindAll(context) } func (t *PetHandlerTest) TestFindOneSuccess() { - findOneResponse := t.Pet + findOneResponse := utils.ProtoToDto(t.Pet, t.Images) + expectedResponse := dto.ResponseSuccess{ + StatusCode: http.StatusOK, + Message: petconst.FindOnePetSuccessMessage, + Data: findOneResponse, + } controller := gomock.NewController(t.T()) @@ -150,7 +163,7 @@ func (t *PetHandlerTest) TestFindOneSuccess() { context.EXPECT().Param("id").Return(t.Pet.Id, nil) petSvc.EXPECT().FindOne(t.Pet.Id).Return(findOneResponse, nil) - context.EXPECT().JSON(http.StatusOK, findOneResponse) + context.EXPECT().JSON(http.StatusOK, expectedResponse) handler := NewHandler(petSvc, imageSvc, validator) handler.FindOne(context) @@ -193,7 +206,12 @@ func (t *PetHandlerTest) TestFindOneGrpcErr() { } func (t *PetHandlerTest) TestCreateSuccess() { - createErrorResponse := t.Pet + createResponse := utils.ProtoToDto(t.Pet, t.Images) + expectedResponse := dto.ResponseSuccess{ + StatusCode: http.StatusCreated, + Message: petconst.CreatePetSuccessMessage, + Data: createResponse, + } controller := gomock.NewController(t.T()) @@ -204,8 +222,8 @@ func (t *PetHandlerTest) TestCreateSuccess() { context.EXPECT().Bind(t.CreatePetRequest).Return(nil) validator.EXPECT().Validate(t.CreatePetRequest).Return(nil) - petSvc.EXPECT().Create(t.CreatePetRequest).Return(createErrorResponse, nil) - context.EXPECT().JSON(http.StatusCreated, createErrorResponse) + petSvc.EXPECT().Create(t.CreatePetRequest).Return(createResponse, nil) + context.EXPECT().JSON(http.StatusCreated, expectedResponse) handler := NewHandler(petSvc, imageSvc, validator) handler.Create(context) @@ -231,7 +249,12 @@ func (t *PetHandlerTest) TestCreateGrpcErr() { } func (t *PetHandlerTest) TestUpdateSuccess() { - updateResponse := t.Pet + updateResponse := utils.ProtoToDto(t.Pet, t.Images) + expectedResponse := dto.ResponseSuccess{ + StatusCode: http.StatusOK, + Message: petconst.UpdatePetSuccessMessage, + Data: updateResponse, + } controller := gomock.NewController(t.T()) @@ -244,7 +267,7 @@ func (t *PetHandlerTest) TestUpdateSuccess() { context.EXPECT().Bind(t.UpdatePetRequest).Return(nil) validator.EXPECT().Validate(t.UpdatePetRequest).Return(nil) petSvc.EXPECT().Update(t.Pet.Id, t.UpdatePetRequest).Return(updateResponse, nil) - context.EXPECT().JSON(http.StatusOK, updateResponse) + context.EXPECT().JSON(http.StatusOK, expectedResponse) handler := NewHandler(petSvc, imageSvc, validator) handler.Update(context) @@ -291,7 +314,14 @@ func (t *PetHandlerTest) TestUpdateGrpcErr() { } func (t *PetHandlerTest) TestDeleteSuccess() { - deleteResponse := true + deleteResponse := &dto.DeleteResponse{ + Success: true, + } + expectedResponse := dto.ResponseSuccess{ + StatusCode: http.StatusOK, + Message: petconst.DeletePetSuccessMessage, + Data: deleteResponse, + } controller := gomock.NewController(t.T()) @@ -302,13 +332,15 @@ func (t *PetHandlerTest) TestDeleteSuccess() { context.EXPECT().Param("id").Return(t.Pet.Id, nil) petSvc.EXPECT().Delete(t.Pet.Id).Return(deleteResponse, nil) - context.EXPECT().JSON(http.StatusOK, deleteResponse) + context.EXPECT().JSON(http.StatusOK, expectedResponse) handler := NewHandler(petSvc, imageSvc, validator) handler.Delete(context) } func (t *PetHandlerTest) TestDeleteNotFound() { - deleteResponse := false + deleteResponse := &dto.DeleteResponse{ + Success: false, + } controller := gomock.NewController(t.T()) @@ -326,7 +358,9 @@ func (t *PetHandlerTest) TestDeleteNotFound() { } func (t *PetHandlerTest) TestDeleteGrpcErr() { - deleteResponse := false + deleteResponse := &dto.DeleteResponse{ + Success: false, + } controller := gomock.NewController(t.T()) @@ -344,7 +378,14 @@ func (t *PetHandlerTest) TestDeleteGrpcErr() { } func (t *PetHandlerTest) TestChangeViewSuccess() { - changeViewResponse := true + changeViewResponse := &dto.ChangeViewPetResponse{ + Success: true, + } + expectedResponse := dto.ResponseSuccess{ + StatusCode: http.StatusOK, + Message: petconst.ChangeViewPetSuccessMessage, + Data: changeViewResponse, + } controller := gomock.NewController(t.T()) @@ -354,15 +395,19 @@ func (t *PetHandlerTest) TestChangeViewSuccess() { context := mock_router.NewMockIContext(controller) context.EXPECT().Param("id").Return(t.Pet.Id, nil) - petSvc.EXPECT().Delete(t.Pet.Id).Return(changeViewResponse, nil) - context.EXPECT().JSON(http.StatusOK, changeViewResponse) + context.EXPECT().Bind(t.ChangeViewPetRequest).Return(nil) + validator.EXPECT().Validate(t.ChangeViewPetRequest).Return(nil) + petSvc.EXPECT().ChangeView(t.Pet.Id, t.ChangeViewPetRequest).Return(changeViewResponse, nil) + context.EXPECT().JSON(http.StatusOK, expectedResponse) handler := NewHandler(petSvc, imageSvc, validator) - handler.Delete(context) + handler.ChangeView(context) } func (t *PetHandlerTest) TestChangeViewNotFound() { - changeViewResponse := false + changeViewResponse := &dto.ChangeViewPetResponse{ + Success: false, + } controller := gomock.NewController(t.T()) @@ -372,15 +417,19 @@ func (t *PetHandlerTest) TestChangeViewNotFound() { context := mock_router.NewMockIContext(controller) context.EXPECT().Param("id").Return(t.Pet.Id, nil) - petSvc.EXPECT().Delete(t.Pet.Id).Return(changeViewResponse, t.NotFoundErr) + context.EXPECT().Bind(t.ChangeViewPetRequest).Return(nil) + validator.EXPECT().Validate(t.ChangeViewPetRequest).Return(nil) + petSvc.EXPECT().ChangeView(t.Pet.Id, t.ChangeViewPetRequest).Return(changeViewResponse, t.NotFoundErr) context.EXPECT().JSON(http.StatusNotFound, t.NotFoundErr) handler := NewHandler(petSvc, imageSvc, validator) - handler.Delete(context) + handler.ChangeView(context) } func (t *PetHandlerTest) TestChangeViewGrpcErr() { - changeViewResponse := false + changeViewResponse := &dto.ChangeViewPetResponse{ + Success: false, + } controller := gomock.NewController(t.T()) @@ -390,9 +439,11 @@ func (t *PetHandlerTest) TestChangeViewGrpcErr() { context := mock_router.NewMockIContext(controller) context.EXPECT().Param("id").Return(t.Pet.Id, nil) - petSvc.EXPECT().Delete(t.Pet.Id).Return(changeViewResponse, t.ServiceDownErr) + context.EXPECT().Bind(t.ChangeViewPetRequest).Return(nil) + validator.EXPECT().Validate(t.ChangeViewPetRequest).Return(nil) + petSvc.EXPECT().ChangeView(t.Pet.Id, t.ChangeViewPetRequest).Return(changeViewResponse, t.ServiceDownErr) context.EXPECT().JSON(http.StatusServiceUnavailable, t.ServiceDownErr) handler := NewHandler(petSvc, imageSvc, validator) - handler.Delete(context) + handler.ChangeView(context) } diff --git a/src/app/service/pet/pet.service.go b/src/app/service/pet/pet.service.go index 7ada5ff..147b22c 100644 --- a/src/app/service/pet/pet.service.go +++ b/src/app/service/pet/pet.service.go @@ -7,29 +7,28 @@ import ( "github.com/isd-sgcu/johnjud-gateway/src/app/constant" "github.com/isd-sgcu/johnjud-gateway/src/app/dto" - "github.com/isd-sgcu/johnjud-gateway/src/constant/pet" - proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" - image_proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/file/image/v1" + utils "github.com/isd-sgcu/johnjud-gateway/src/app/utils/pet" + petproto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" "github.com/rs/zerolog/log" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) type Service struct { - petClient proto.PetServiceClient + petClient petproto.PetServiceClient } -func NewService(petClient proto.PetServiceClient) *Service { +func NewService(petClient petproto.PetServiceClient) *Service { return &Service{ petClient: petClient, } } -func (s *Service) FindAll() (result []*proto.Pet, err *dto.ResponseErr) { +func (s *Service) FindAll() (result []*dto.PetResponse, err *dto.ResponseErr) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - res, errRes := s.petClient.FindAll(ctx, &proto.FindAllPetRequest{}) + res, errRes := s.petClient.FindAll(ctx, &petproto.FindAllPetRequest{}) if errRes != nil { st, _ := status.FromError(errRes) log.Error(). @@ -51,15 +50,16 @@ func (s *Service) FindAll() (result []*proto.Pet, err *dto.ResponseErr) { Data: nil, } } - _ = &image_proto.Image{} - return res.Pets, nil + imagesList := utils.MockImageList(len(res.Pets)) + findAllResponse := utils.RawToDtoList(res.Pets, imagesList) + return findAllResponse, nil } -func (s *Service) FindOne(id string) (result *proto.Pet, err *dto.ResponseErr) { +func (s *Service) FindOne(id string) (result *dto.PetResponse, err *dto.ResponseErr) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - res, errRes := s.petClient.FindOne(ctx, &proto.FindOnePetRequest{Id: id}) + res, errRes := s.petClient.FindOne(ctx, &petproto.FindOnePetRequest{Id: id}) if errRes != nil { st, _ := status.FromError(errRes) log.Error(). @@ -89,16 +89,18 @@ func (s *Service) FindOne(id string) (result *proto.Pet, err *dto.ResponseErr) { } } } - return res.Pet, nil + images := utils.MockImageList(1)[0] + findOneResponse := utils.ProtoToDto(res.Pet, images) + return findOneResponse, nil } -func (s *Service) Create(in *dto.CreatePetRequest) (ressult *proto.Pet, err *dto.ResponseErr) { +func (s *Service) Create(in *dto.CreatePetRequest) (ressult *dto.PetResponse, err *dto.ResponseErr) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - request := DtoToRaw(in.Pet) + request := utils.CreateDtoToProto(in) - res, errRes := s.petClient.Create(ctx, &proto.CreatePetRequest{Pet: request}) + res, errRes := s.petClient.Create(ctx, request) if errRes != nil { st, _ := status.FromError(errRes) log.Error(). @@ -127,35 +129,16 @@ func (s *Service) Create(in *dto.CreatePetRequest) (ressult *proto.Pet, err *dto } } } - return res.Pet, nil + images := utils.MockImageList(1)[0] + createPetResponse := utils.ProtoToDto(res.Pet, images) + return createPetResponse, nil } -func (s *Service) Update(id string, in *dto.UpdatePetRequest) (result *proto.Pet, err *dto.ResponseErr) { +func (s *Service) Update(id string, in *dto.UpdatePetRequest) (result *dto.PetResponse, err *dto.ResponseErr) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - request := &proto.UpdatePetRequest{ - Pet: &proto.Pet{ - Id: id, - Type: in.Pet.Type, - Species: in.Pet.Species, - Name: in.Pet.Name, - Birthdate: in.Pet.Birthdate, - Gender: proto.Gender(in.Pet.Gender), - Habit: in.Pet.Habit, - Caption: in.Pet.Caption, - Images: []*image_proto.Image{}, - Status: proto.PetStatus(in.Pet.Status), - IsSterile: *in.Pet.IsSterile, - IsVaccinated: *in.Pet.IsSterile, - IsVisible: *in.Pet.IsVaccinated, - IsClubPet: *in.Pet.IsClubPet, - Background: in.Pet.Background, - Address: in.Pet.Address, - Contact: in.Pet.Contact, - AdoptBy: in.Pet.AdoptBy, - }, - } + request := utils.UpdateDtoToProto(id, in) res, errRes := s.petClient.Update(ctx, request) if errRes != nil { @@ -192,14 +175,16 @@ func (s *Service) Update(id string, in *dto.UpdatePetRequest) (result *proto.Pet } } } - return res.Pet, nil + images := utils.MockImageList(1)[0] + updatePetResponse := utils.ProtoToDto(res.Pet, images) + return updatePetResponse, nil } -func (s *Service) Delete(id string) (result bool, err *dto.ResponseErr) { +func (s *Service) Delete(id string) (result *dto.DeleteResponse, err *dto.ResponseErr) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - res, errRes := s.petClient.Delete(ctx, &proto.DeletePetRequest{ + res, errRes := s.petClient.Delete(ctx, &petproto.DeletePetRequest{ Id: id, }) if errRes != nil { @@ -211,32 +196,40 @@ func (s *Service) Delete(id string) (result bool, err *dto.ResponseErr) { Msg(st.Message()) switch st.Code() { case codes.NotFound: - return false, &dto.ResponseErr{ - StatusCode: http.StatusNotFound, - Message: constant.PetNotFoundMessage, - Data: nil, - } + return &dto.DeleteResponse{ + Success: false, + }, &dto.ResponseErr{ + StatusCode: http.StatusNotFound, + Message: constant.PetNotFoundMessage, + Data: nil, + } case codes.Unavailable: - return false, &dto.ResponseErr{ - StatusCode: http.StatusServiceUnavailable, - Message: constant.UnavailableServiceMessage, + return &dto.DeleteResponse{ + Success: false, + }, &dto.ResponseErr{ + StatusCode: http.StatusServiceUnavailable, + Message: constant.UnavailableServiceMessage, + Data: nil, + } + } + return &dto.DeleteResponse{ + Success: false, + }, &dto.ResponseErr{ + StatusCode: http.StatusInternalServerError, + Message: constant.InternalErrorMessage, Data: nil, } - } - return false, &dto.ResponseErr{ - StatusCode: http.StatusInternalServerError, - Message: constant.InternalErrorMessage, - Data: nil, - } } - return res.Success, nil + return &dto.DeleteResponse{ + Success: res.Success, + }, nil } -func (s *Service) ChangeView(id string, in *dto.ChangeViewPetRequest) (result bool, err *dto.ResponseErr) { +func (s *Service) ChangeView(id string, in *dto.ChangeViewPetRequest) (result *dto.ChangeViewPetResponse, err *dto.ResponseErr) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - res, errRes := s.petClient.ChangeView(ctx, &proto.ChangeViewPetRequest{ + res, errRes := s.petClient.ChangeView(ctx, &petproto.ChangeViewPetRequest{ Id: id, Visible: in.Visible, }) @@ -249,69 +242,32 @@ func (s *Service) ChangeView(id string, in *dto.ChangeViewPetRequest) (result bo Msg(st.Message()) switch st.Code() { case codes.NotFound: - return false, &dto.ResponseErr{ - StatusCode: http.StatusNotFound, - Message: constant.PetNotFoundMessage, - Data: nil, - } + return &dto.ChangeViewPetResponse{ + Success: false, + }, &dto.ResponseErr{ + StatusCode: http.StatusNotFound, + Message: constant.PetNotFoundMessage, + Data: nil, + } case codes.Unavailable: - return false, &dto.ResponseErr{ - StatusCode: http.StatusServiceUnavailable, - Message: constant.UnavailableServiceMessage, - Data: nil, - } + return &dto.ChangeViewPetResponse{ + Success: false, + }, &dto.ResponseErr{ + StatusCode: http.StatusServiceUnavailable, + Message: constant.UnavailableServiceMessage, + Data: nil, + } default: - return false, &dto.ResponseErr{ - StatusCode: http.StatusServiceUnavailable, - Message: constant.InternalErrorMessage, - Data: nil, - } + return &dto.ChangeViewPetResponse{ + Success: false, + }, &dto.ResponseErr{ + StatusCode: http.StatusServiceUnavailable, + Message: constant.InternalErrorMessage, + Data: nil, + } } } - return res.Success, nil -} - -func DtoToRaw(in *dto.PetDto) *proto.Pet { - return &proto.Pet{ - Id: in.Id, - Type: in.Type, - Species: in.Species, - Name: in.Name, - Birthdate: in.Birthdate, - Gender: proto.Gender(in.Gender), - Habit: in.Habit, - Caption: in.Caption, - Images: []*image_proto.Image{}, - Status: proto.PetStatus(in.Status), - IsSterile: *in.IsSterile, - IsVaccinated: *in.IsVaccinated, - IsVisible: *in.IsVisible, - IsClubPet: *in.IsClubPet, - Background: in.Background, - Address: in.Address, - Contact: in.Contact, - AdoptBy: in.AdoptBy, - } -} - -func RawToDto(in *proto.Pet) *dto.PetDto { - return &dto.PetDto{ - Id: in.Id, - Type: in.Type, - Species: in.Species, - Name: in.Name, - Birthdate: in.Birthdate, - Gender: pet.Gender(in.Gender), - Habit: in.Habit, - Caption: in.Caption, - Status: pet.Status(in.Status), - IsSterile: &in.IsSterile, - IsVaccinated: &in.IsVaccinated, - IsVisible: &in.IsVisible, - IsClubPet: &in.IsClubPet, - Background: in.Background, - Address: in.Address, - Contact: in.Contact, - AdoptBy: in.AdoptBy, - } + return &dto.ChangeViewPetResponse{ + Success: res.Success, + }, nil } diff --git a/src/app/service/pet/pet.service_test.go b/src/app/service/pet/pet.service_test.go index 7700da4..abab448 100644 --- a/src/app/service/pet/pet.service_test.go +++ b/src/app/service/pet/pet.service_test.go @@ -8,9 +8,11 @@ import ( "github.com/go-faker/faker/v4" "github.com/isd-sgcu/johnjud-gateway/src/app/constant" "github.com/isd-sgcu/johnjud-gateway/src/app/dto" - petMock "github.com/isd-sgcu/johnjud-gateway/src/mocks/client/pet" - petProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" - imageProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/file/image/v1" + utils "github.com/isd-sgcu/johnjud-gateway/src/app/utils/pet" + "github.com/isd-sgcu/johnjud-gateway/src/constant/pet" + petmock "github.com/isd-sgcu/johnjud-gateway/src/mocks/client/pet" + petproto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" + imgproto "github.com/isd-sgcu/johnjud-go-proto/johnjud/file/image/v1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" "google.golang.org/grpc/codes" @@ -19,14 +21,15 @@ import ( type PetServiceTest struct { suite.Suite - Pets []*petProto.Pet - Pet *petProto.Pet - PetReq *petProto.Pet - PetNotVisible *petProto.Pet - UpdatePetReq *petProto.UpdatePetRequest - ChangeViewPetReq *petProto.ChangeViewPetRequest - AdoptReq *petProto.AdoptPetRequest - PetDto *dto.PetDto + Pets []*petproto.Pet + Pet *petproto.Pet + PetNotVisible *petproto.Pet + UpdatePetReq *petproto.UpdatePetRequest + CreatePetReq *petproto.CreatePetRequest + ChangeViewPetReq *petproto.ChangeViewPetRequest + DeletePetReq *petproto.DeletePetRequest + AdoptReq *petproto.AdoptPetRequest + PetDto *dto.PetResponse CreatePetDto *dto.CreatePetRequest UpdatePetDto *dto.UpdatePetRequest NotFoundErr *dto.ResponseErr @@ -35,10 +38,8 @@ type PetServiceTest struct { InternalErr *dto.ResponseErr ChangeViewedPetDto *dto.ChangeViewPetRequest - Images []*imageProto.Image - ImageUrls []string - ImagesList [][]*imageProto.Image - ImageUrlsList [][]string + Images []*imgproto.Image + ImagesList [][]*imgproto.Image } func TestPetService(t *testing.T) { @@ -46,19 +47,23 @@ func TestPetService(t *testing.T) { } func (t *PetServiceTest) SetupTest() { - var pets []*petProto.Pet + imagesList := utils.MockImageList(3) + t.ImagesList = imagesList + t.Images = imagesList[0] + + var pets []*petproto.Pet for i := 0; i <= 3; i++ { - pet := &petProto.Pet{ + pet := &petproto.Pet{ Id: faker.UUIDDigit(), Type: faker.Word(), Species: faker.Word(), Name: faker.Name(), Birthdate: faker.Word(), - Gender: petProto.Gender(rand.Intn(1) + 1), + Gender: petproto.Gender(rand.Intn(1) + 1), Habit: faker.Paragraph(), Caption: faker.Paragraph(), - Images: []*imageProto.Image{}, - Status: petProto.PetStatus(rand.Intn(1) + 1), + Images: imagesList[i], + Status: petproto.PetStatus(rand.Intn(1) + 1), IsSterile: true, IsVaccinated: true, IsVisible: true, @@ -75,7 +80,8 @@ func (t *PetServiceTest) SetupTest() { t.Pets = pets t.Pet = t.Pets[0] - t.PetReq = &petProto.Pet{ + t.PetNotVisible = &petproto.Pet{ + Id: t.Pet.Id, Type: t.Pet.Type, Species: t.Pet.Species, Name: t.Pet.Name, @@ -87,7 +93,7 @@ func (t *PetServiceTest) SetupTest() { Status: t.Pet.Status, IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, - IsVisible: t.Pet.IsVisible, + IsVisible: false, IsClubPet: t.Pet.IsClubPet, Background: t.Pet.Background, Address: t.Pet.Address, @@ -95,51 +101,61 @@ func (t *PetServiceTest) SetupTest() { AdoptBy: t.Pet.AdoptBy, } - t.PetNotVisible = &petProto.Pet{ - Id: t.Pet.Id, + t.PetDto = utils.ProtoToDto(t.Pet, t.Pet.Images) + + t.CreatePetDto = &dto.CreatePetRequest{ Type: t.Pet.Type, Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, - Gender: t.Pet.Gender, + Gender: pet.Gender(t.Pet.Gender), Habit: t.Pet.Habit, Caption: t.Pet.Caption, - Images: t.Pet.Images, - Status: t.Pet.Status, - IsSterile: t.Pet.IsSterile, - IsVaccinated: t.Pet.IsVaccinated, - IsVisible: false, - IsClubPet: t.Pet.IsClubPet, + Images: []string{}, + Status: pet.Status(t.Pet.Status), + IsSterile: &t.Pet.IsSterile, + IsVaccinated: &t.Pet.IsVaccinated, + IsVisible: &t.Pet.IsVisible, + IsClubPet: &t.Pet.IsClubPet, Background: t.Pet.Background, Address: t.Pet.Address, Contact: t.Pet.Contact, AdoptBy: t.Pet.AdoptBy, } - t.PetDto = RawToDto(t.Pet) - - t.CreatePetDto = &dto.CreatePetRequest{ - Pet: RawToDto(t.PetReq), - } - t.UpdatePetDto = &dto.UpdatePetRequest{ - Pet: RawToDto(t.PetReq), + Type: t.Pet.Type, + Species: t.Pet.Species, + Name: t.Pet.Name, + Birthdate: t.Pet.Birthdate, + Gender: pet.Gender(t.Pet.Gender), + Habit: t.Pet.Habit, + Caption: t.Pet.Caption, + Images: []string{}, + Status: pet.Status(t.Pet.Status), + IsSterile: &t.Pet.IsSterile, + IsVaccinated: &t.Pet.IsVaccinated, + IsVisible: &t.Pet.IsVisible, + IsClubPet: &t.Pet.IsClubPet, + Background: t.Pet.Background, + Address: t.Pet.Address, + Contact: t.Pet.Contact, + AdoptBy: t.Pet.AdoptBy, } - t.UpdatePetReq = &petProto.UpdatePetRequest{ - Pet: t.Pet, - } + t.CreatePetReq = utils.CreateDtoToProto(t.CreatePetDto) + t.UpdatePetReq = utils.UpdateDtoToProto(t.Pet.Id, t.UpdatePetDto) - t.ChangeViewedPetDto = &dto.ChangeViewPetRequest{ + t.ChangeViewPetReq = &petproto.ChangeViewPetRequest{ + Id: t.Pet.Id, Visible: false, } - t.ChangeViewPetReq = &petProto.ChangeViewPetRequest{ - Id: t.Pet.Id, + t.ChangeViewedPetDto = &dto.ChangeViewPetRequest{ Visible: false, } - t.AdoptReq = &petProto.AdoptPetRequest{ + t.AdoptReq = &petproto.AdoptPetRequest{ PetId: t.Pet.Id, UserId: t.Pet.AdoptBy, } @@ -170,14 +186,14 @@ func (t *PetServiceTest) SetupTest() { } func (t *PetServiceTest) TestFindAllSuccess() { - protoReq := &petProto.FindAllPetRequest{} - protoResp := &petProto.FindAllPetResponse{ + protoReq := &petproto.FindAllPetRequest{} + protoResp := &petproto.FindAllPetResponse{ Pets: t.Pets, } - expected := t.Pets + expected := utils.RawToDtoList(t.Pets, t.ImagesList) - client := petMock.PetClientMock{} + client := petmock.PetClientMock{} client.On("FindAll", protoReq).Return(protoResp, nil) svc := NewService(&client) @@ -188,13 +204,13 @@ func (t *PetServiceTest) TestFindAllSuccess() { } func (t *PetServiceTest) TestFindAllUnavailableServiceError() { - protoReq := &petProto.FindAllPetRequest{} + protoReq := &petproto.FindAllPetRequest{} expected := t.UnavailableServiceErr clientErr := status.Error(codes.Unavailable, constant.UnavailableServiceMessage) - client := petMock.PetClientMock{} + client := petmock.PetClientMock{} client.On("FindAll", protoReq).Return(nil, clientErr) svc := NewService(&client) @@ -205,16 +221,16 @@ func (t *PetServiceTest) TestFindAllUnavailableServiceError() { } func (t *PetServiceTest) TestFindOneSuccess() { - protoReq := &petProto.FindOnePetRequest{ + protoReq := &petproto.FindOnePetRequest{ Id: t.Pet.Id, } - protoResp := &petProto.FindOnePetResponse{ + protoResp := &petproto.FindOnePetResponse{ Pet: t.Pet, } - expected := t.Pet + expected := utils.ProtoToDto(t.Pet, t.Pet.Images) - client := petMock.PetClientMock{} + client := petmock.PetClientMock{} client.On("FindOne", protoReq).Return(protoResp, nil) svc := NewService(&client) @@ -225,7 +241,7 @@ func (t *PetServiceTest) TestFindOneSuccess() { } func (t *PetServiceTest) TestFindOneNotFoundError() { - protoReq := &petProto.FindOnePetRequest{ + protoReq := &petproto.FindOnePetRequest{ Id: t.Pet.Id, } @@ -233,7 +249,7 @@ func (t *PetServiceTest) TestFindOneNotFoundError() { expected := t.NotFoundErr - client := petMock.PetClientMock{} + client := petmock.PetClientMock{} client.On("FindOne", protoReq).Return(nil, clientErr) svc := NewService(&client) @@ -244,7 +260,7 @@ func (t *PetServiceTest) TestFindOneNotFoundError() { } func (t *PetServiceTest) TestFindOneUnavailableServiceError() { - protoReq := &petProto.FindOnePetRequest{ + protoReq := &petproto.FindOnePetRequest{ Id: t.Pet.Id, } @@ -252,7 +268,7 @@ func (t *PetServiceTest) TestFindOneUnavailableServiceError() { expected := t.UnavailableServiceErr - client := petMock.PetClientMock{} + client := petmock.PetClientMock{} client.On("FindOne", protoReq).Return(nil, clientErr) svc := NewService(&client) @@ -263,16 +279,14 @@ func (t *PetServiceTest) TestFindOneUnavailableServiceError() { } func (t *PetServiceTest) TestCreateSuccess() { - protoReq := &petProto.CreatePetRequest{ - Pet: t.PetReq, - } - protoResp := &petProto.CreatePetResponse{ + protoReq := t.CreatePetReq + protoResp := &petproto.CreatePetResponse{ Pet: t.Pet, } - expected := t.Pet + expected := utils.ProtoToDto(t.Pet, t.Pet.Images) - client := &petMock.PetClientMock{} + client := &petmock.PetClientMock{} client.On("Create", protoReq).Return(protoResp, nil) svc := NewService(client) @@ -283,15 +297,13 @@ func (t *PetServiceTest) TestCreateSuccess() { } func (t *PetServiceTest) TestCreateInvalidArgumentError() { - protoReq := &petProto.CreatePetRequest{ - Pet: t.PetReq, - } + protoReq := t.CreatePetReq expected := t.InvalidArgumentErr clientErr := status.Error(codes.InvalidArgument, constant.InvalidArgument) - client := &petMock.PetClientMock{} + client := &petmock.PetClientMock{} client.On("Create", protoReq).Return(nil, clientErr) svc := NewService(client) @@ -302,15 +314,13 @@ func (t *PetServiceTest) TestCreateInvalidArgumentError() { } func (t *PetServiceTest) TestCreateInternalError() { - protoReq := &petProto.CreatePetRequest{ - Pet: t.PetReq, - } + protoReq := t.CreatePetReq expected := t.InternalErr clientErr := status.Error(codes.Internal, constant.InternalErrorMessage) - client := &petMock.PetClientMock{} + client := &petmock.PetClientMock{} client.On("Create", protoReq).Return(nil, clientErr) svc := NewService(client) @@ -321,15 +331,13 @@ func (t *PetServiceTest) TestCreateInternalError() { } func (t *PetServiceTest) TestCreateUnavailableServiceError() { - protoReq := &petProto.CreatePetRequest{ - Pet: t.PetReq, - } + protoReq := t.CreatePetReq clientErr := status.Error(codes.Unavailable, constant.UnavailableServiceMessage) expected := t.UnavailableServiceErr - client := &petMock.PetClientMock{} + client := &petmock.PetClientMock{} client.On("Create", protoReq).Return(nil, clientErr) svc := NewService(client) @@ -341,13 +349,13 @@ func (t *PetServiceTest) TestCreateUnavailableServiceError() { func (t *PetServiceTest) TestUpdateSuccess() { protoReq := t.UpdatePetReq - protoResp := &petProto.UpdatePetResponse{ + protoResp := &petproto.UpdatePetResponse{ Pet: t.Pet, } - expected := t.Pet + expected := utils.ProtoToDto(t.Pet, t.Pet.Images) - client := &petMock.PetClientMock{} + client := &petmock.PetClientMock{} client.On("Update", protoReq).Return(protoResp, nil) svc := NewService(client) @@ -363,7 +371,7 @@ func (t *PetServiceTest) TestUpdateNotFound() { expected := t.NotFoundErr - client := &petMock.PetClientMock{} + client := &petmock.PetClientMock{} client.On("Update", protoReq).Return(nil, clientErr) svc := NewService(client) @@ -379,7 +387,7 @@ func (t *PetServiceTest) TestUpdateUnavailableServiceError() { expected := t.UnavailableServiceErr - client := &petMock.PetClientMock{} + client := &petmock.PetClientMock{} client.On("Update", protoReq).Return(nil, clientErr) svc := NewService(client) @@ -390,16 +398,16 @@ func (t *PetServiceTest) TestUpdateUnavailableServiceError() { } func (t *PetServiceTest) TestDeleteSuccess() { - protoReq := &petProto.DeletePetRequest{ + protoReq := &petproto.DeletePetRequest{ Id: t.Pet.Id, } - protoResp := &petProto.DeletePetResponse{ + protoResp := &petproto.DeletePetResponse{ Success: true, } - expected := true + expected := &dto.DeleteResponse{Success: true} - client := &petMock.PetClientMock{} + client := &petmock.PetClientMock{} client.On("Delete", protoReq).Return(protoResp, nil) svc := NewService(client) @@ -410,66 +418,66 @@ func (t *PetServiceTest) TestDeleteSuccess() { } func (t *PetServiceTest) TestDeleteNotFound() { - protoReq := &petProto.DeletePetRequest{ + protoReq := &petproto.DeletePetRequest{ Id: t.Pet.Id, } - protoResp := &petProto.DeletePetResponse{ + protoResp := &petproto.DeletePetResponse{ Success: false, } clientErr := status.Error(codes.NotFound, constant.PetNotFoundMessage) expected := t.NotFoundErr - client := &petMock.PetClientMock{} + client := &petmock.PetClientMock{} client.On("Delete", protoReq).Return(protoResp, clientErr) svc := NewService(client) actual, err := svc.Delete(t.Pet.Id) - assert.False(t.T(), actual) + assert.Equal(t.T(), &dto.DeleteResponse{Success: false}, actual) assert.Equal(t.T(), expected, err) } func (t *PetServiceTest) TestDeleteServiceUnavailableError() { - protoReq := &petProto.DeletePetRequest{ + protoReq := &petproto.DeletePetRequest{ Id: t.Pet.Id, } - protoResp := &petProto.DeletePetResponse{ + protoResp := &petproto.DeletePetResponse{ Success: false, } clientErr := status.Error(codes.Unavailable, constant.UnavailableServiceMessage) expected := t.UnavailableServiceErr - client := &petMock.PetClientMock{} + client := &petmock.PetClientMock{} client.On("Delete", protoReq).Return(protoResp, clientErr) svc := NewService(client) actual, err := svc.Delete(t.Pet.Id) - assert.False(t.T(), actual) + assert.Equal(t.T(), &dto.DeleteResponse{Success: false}, actual) assert.Equal(t.T(), expected, err) } func (t *PetServiceTest) TestChangeViewSuccess() { protoReq := t.ChangeViewPetReq - protoResp := &petProto.ChangeViewPetResponse{ + protoResp := &petproto.ChangeViewPetResponse{ Success: true, } - client := &petMock.PetClientMock{} + client := &petmock.PetClientMock{} client.On("ChangeView", protoReq).Return(protoResp, nil) svc := NewService(client) actual, err := svc.ChangeView(t.Pet.Id, t.ChangeViewedPetDto) assert.Nil(t.T(), err) - assert.True(t.T(), actual) + assert.Equal(t.T(), actual, &dto.ChangeViewPetResponse{Success: true}) } func (t *PetServiceTest) TestChangeViewNotFoundError() { protoReq := t.ChangeViewPetReq - protoResp := &petProto.ChangeViewPetResponse{ + protoResp := &petproto.ChangeViewPetResponse{ Success: false, } @@ -477,19 +485,19 @@ func (t *PetServiceTest) TestChangeViewNotFoundError() { expected := t.NotFoundErr - client := &petMock.PetClientMock{} + client := &petmock.PetClientMock{} client.On("ChangeView", protoReq).Return(protoResp, clientErr) svc := NewService(client) actual, err := svc.ChangeView(t.Pet.Id, t.ChangeViewedPetDto) - assert.False(t.T(), actual) + assert.Equal(t.T(), &dto.ChangeViewPetResponse{Success: false}, actual) assert.Equal(t.T(), expected, err) } func (t *PetServiceTest) TestChangeViewUnavailableServiceError() { protoReq := t.ChangeViewPetReq - protoResp := &petProto.ChangeViewPetResponse{ + protoResp := &petproto.ChangeViewPetResponse{ Success: false, } @@ -497,12 +505,12 @@ func (t *PetServiceTest) TestChangeViewUnavailableServiceError() { expected := t.UnavailableServiceErr - client := &petMock.PetClientMock{} + client := &petmock.PetClientMock{} client.On("ChangeView", protoReq).Return(protoResp, clientErr) svc := NewService(client) actual, err := svc.ChangeView(t.Pet.Id, t.ChangeViewedPetDto) - assert.False(t.T(), actual) + assert.Equal(t.T(), &dto.ChangeViewPetResponse{Success: false}, actual) assert.Equal(t.T(), expected, err) } diff --git a/src/app/utils/pet/pet.utils.go b/src/app/utils/pet/pet.utils.go new file mode 100644 index 0000000..0ed39e3 --- /dev/null +++ b/src/app/utils/pet/pet.utils.go @@ -0,0 +1,162 @@ +package pet + +import ( + "fmt" + + "github.com/isd-sgcu/johnjud-gateway/src/app/dto" + "github.com/isd-sgcu/johnjud-gateway/src/constant/pet" + petproto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" + imgproto "github.com/isd-sgcu/johnjud-go-proto/johnjud/file/image/v1" +) + +func MockImageList(n int) [][]*imgproto.Image { + var imagesList [][]*imgproto.Image + for i := 0; i <= n; i++ { + var images []*imgproto.Image + for j := 0; j <= 3; j++ { + images = append(images, &imgproto.Image{ + Id: fmt.Sprintf("%v%v", i, j), + PetId: fmt.Sprintf("%v%v", i, j), + ImageUrl: fmt.Sprintf("%v%v", i, j), + ObjectKey: fmt.Sprintf("%v%v", i, j), + }) + } + imagesList = append(imagesList, images) + } + + return imagesList +} + +func ProtoToDto(in *petproto.Pet, images []*imgproto.Image) *dto.PetResponse { + pet := &dto.PetResponse{ + Id: in.Id, + Type: in.Type, + Species: in.Species, + Name: in.Name, + Birthdate: in.Birthdate, + Gender: pet.Gender(in.Gender), + Habit: in.Habit, + Caption: in.Caption, + Status: pet.Status(in.Status), + IsSterile: &in.IsSterile, + IsVaccinated: &in.IsVaccinated, + IsVisible: &in.IsVisible, + IsClubPet: &in.IsClubPet, + Background: in.Background, + Address: in.Address, + Contact: in.Contact, + AdoptBy: in.AdoptBy, + Images: extractImages(images), + } + return pet +} + +func CreateDtoToProto(in *dto.CreatePetRequest) *petproto.CreatePetRequest { + return &petproto.CreatePetRequest{ + Pet: &petproto.Pet{ + Type: in.Type, + Species: in.Species, + Name: in.Name, + Birthdate: in.Birthdate, + Gender: petproto.Gender(in.Gender), + Habit: in.Habit, + Caption: in.Caption, + Images: []*imgproto.Image{}, + Status: petproto.PetStatus(in.Status), + IsSterile: *in.IsSterile, + IsVaccinated: *in.IsVaccinated, + IsVisible: *in.IsVisible, + IsClubPet: *in.IsClubPet, + Background: in.Background, + Address: in.Address, + Contact: in.Contact, + AdoptBy: in.AdoptBy, + }, + } +} + +func UpdateDtoToProto(id string, in *dto.UpdatePetRequest) *petproto.UpdatePetRequest { + req := &petproto.UpdatePetRequest{ + Pet: &petproto.Pet{ + Id: id, + Type: in.Type, + Species: in.Species, + Name: in.Name, + Birthdate: in.Birthdate, + Gender: petproto.Gender(in.Gender), + Habit: in.Habit, + Caption: in.Caption, + Images: []*imgproto.Image{}, + Status: petproto.PetStatus(in.Status), + Background: in.Background, + Address: in.Address, + Contact: in.Contact, + AdoptBy: in.AdoptBy, + }, + } + + if in.IsClubPet == nil { + req.Pet.IsClubPet = false + } else { + req.Pet.IsClubPet = *in.IsClubPet + } + + if in.IsSterile == nil { + req.Pet.IsSterile = false + } else { + req.Pet.IsSterile = *in.IsSterile + } + + if in.IsVaccinated == nil { + req.Pet.IsVaccinated = false + } else { + req.Pet.IsVaccinated = *in.IsVaccinated + } + + if in.IsVisible == nil { + req.Pet.IsVisible = false + } else { + req.Pet.IsVisible = *in.IsVisible + } + + return req +} + +func RawToDtoList(in []*petproto.Pet, imagesList [][]*imgproto.Image) []*dto.PetResponse { + var resp []*dto.PetResponse + for i, p := range in { + pet := &dto.PetResponse{ + Id: p.Id, + Type: p.Type, + Species: p.Species, + Name: p.Name, + Birthdate: p.Birthdate, + Gender: pet.Gender(p.Gender), + Habit: p.Habit, + Caption: p.Caption, + Status: pet.Status(p.Status), + IsSterile: &p.IsSterile, + IsVaccinated: &p.IsVaccinated, + IsVisible: &p.IsVisible, + IsClubPet: &p.IsClubPet, + Background: p.Background, + Address: p.Address, + Contact: p.Contact, + AdoptBy: p.AdoptBy, + Images: extractImages(imagesList[i]), + } + resp = append(resp, pet) + } + return resp +} + +func extractImages(images []*imgproto.Image) []dto.ImageResponse { + var result []dto.ImageResponse + for _, img := range images { + result = append(result, dto.ImageResponse{ + Id: img.Id, + Url: img.ImageUrl, + }) + } + return result +} diff --git a/src/constant/pet/pet.constant.go b/src/constant/pet/pet.constant.go index d8914fb..fe606fe 100644 --- a/src/constant/pet/pet.constant.go +++ b/src/constant/pet/pet.constant.go @@ -57,3 +57,10 @@ func (st *Status) UnmarshalJSON(data []byte) error { return errors.New("invalid gender") } } + +const FindAllPetSuccessMessage = "find all pets success" +const FindOnePetSuccessMessage = "find one pet success" +const CreatePetSuccessMessage = "create pet success" +const UpdatePetSuccessMessage = "update pet success" +const ChangeViewPetSuccessMessage = "change view pet success" +const DeletePetSuccessMessage = "delete pet success" diff --git a/src/mocks/service/pet/pet.mock.go b/src/mocks/service/pet/pet.mock.go index 617c149..1673704 100644 --- a/src/mocks/service/pet/pet.mock.go +++ b/src/mocks/service/pet/pet.mock.go @@ -1,5 +1,10 @@ // Code generated by MockGen. DO NOT EDIT. // Source: ./src/pkg/service/pet/pet.service.go +// +// Generated by this command: +// +// mockgen -source ./src/pkg/service/pet/pet.service.go -destination ./src/mocks/service/pet/pet.mock.go +// // Package mock_pet is a generated GoMock package. package mock_pet @@ -8,7 +13,6 @@ import ( reflect "reflect" dto "github.com/isd-sgcu/johnjud-gateway/src/app/dto" - v1 "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" gomock "github.com/golang/mock/gomock" ) @@ -51,10 +55,10 @@ func (mr *MockServiceMockRecorder) Adopt(arg0 any) *gomock.Call { } // ChangeView mocks base method. -func (m *MockService) ChangeView(arg0 string, arg1 *dto.ChangeViewPetRequest) (bool, *dto.ResponseErr) { +func (m *MockService) ChangeView(arg0 string, arg1 *dto.ChangeViewPetRequest) (*dto.ChangeViewPetResponse, *dto.ResponseErr) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ChangeView", arg0, arg1) - ret0, _ := ret[0].(bool) + ret0, _ := ret[0].(*dto.ChangeViewPetResponse) ret1, _ := ret[1].(*dto.ResponseErr) return ret0, ret1 } @@ -66,10 +70,10 @@ func (mr *MockServiceMockRecorder) ChangeView(arg0, arg1 any) *gomock.Call { } // Create mocks base method. -func (m *MockService) Create(arg0 *dto.CreatePetRequest) (*v1.Pet, *dto.ResponseErr) { +func (m *MockService) Create(arg0 *dto.CreatePetRequest) (*dto.PetResponse, *dto.ResponseErr) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Create", arg0) - ret0, _ := ret[0].(*v1.Pet) + ret0, _ := ret[0].(*dto.PetResponse) ret1, _ := ret[1].(*dto.ResponseErr) return ret0, ret1 } @@ -81,10 +85,10 @@ func (mr *MockServiceMockRecorder) Create(arg0 any) *gomock.Call { } // Delete mocks base method. -func (m *MockService) Delete(arg0 string) (bool, *dto.ResponseErr) { +func (m *MockService) Delete(arg0 string) (*dto.DeleteResponse, *dto.ResponseErr) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Delete", arg0) - ret0, _ := ret[0].(bool) + ret0, _ := ret[0].(*dto.DeleteResponse) ret1, _ := ret[1].(*dto.ResponseErr) return ret0, ret1 } @@ -96,10 +100,10 @@ func (mr *MockServiceMockRecorder) Delete(arg0 any) *gomock.Call { } // FindAll mocks base method. -func (m *MockService) FindAll() ([]*v1.Pet, *dto.ResponseErr) { +func (m *MockService) FindAll() ([]*dto.PetResponse, *dto.ResponseErr) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "FindAll") - ret0, _ := ret[0].([]*v1.Pet) + ret0, _ := ret[0].([]*dto.PetResponse) ret1, _ := ret[1].(*dto.ResponseErr) return ret0, ret1 } @@ -111,10 +115,10 @@ func (mr *MockServiceMockRecorder) FindAll() *gomock.Call { } // FindOne mocks base method. -func (m *MockService) FindOne(arg0 string) (*v1.Pet, *dto.ResponseErr) { +func (m *MockService) FindOne(arg0 string) (*dto.PetResponse, *dto.ResponseErr) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "FindOne", arg0) - ret0, _ := ret[0].(*v1.Pet) + ret0, _ := ret[0].(*dto.PetResponse) ret1, _ := ret[1].(*dto.ResponseErr) return ret0, ret1 } @@ -126,10 +130,10 @@ func (mr *MockServiceMockRecorder) FindOne(arg0 any) *gomock.Call { } // Update mocks base method. -func (m *MockService) Update(arg0 string, arg1 *dto.UpdatePetRequest) (*v1.Pet, *dto.ResponseErr) { +func (m *MockService) Update(arg0 string, arg1 *dto.UpdatePetRequest) (*dto.PetResponse, *dto.ResponseErr) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Update", arg0, arg1) - ret0, _ := ret[0].(*v1.Pet) + ret0, _ := ret[0].(*dto.PetResponse) ret1, _ := ret[1].(*dto.ResponseErr) return ret0, ret1 } diff --git a/src/pkg/service/pet/pet.service.go b/src/pkg/service/pet/pet.service.go index adc60a8..e538fb1 100644 --- a/src/pkg/service/pet/pet.service.go +++ b/src/pkg/service/pet/pet.service.go @@ -2,16 +2,14 @@ package pet import ( "github.com/isd-sgcu/johnjud-gateway/src/app/dto" - - proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" ) type Service interface { - FindAll() ([]*proto.Pet, *dto.ResponseErr) - FindOne(string) (*proto.Pet, *dto.ResponseErr) - Create(*dto.CreatePetRequest) (*proto.Pet, *dto.ResponseErr) - Update(string, *dto.UpdatePetRequest) (*proto.Pet, *dto.ResponseErr) - Delete(string) (bool, *dto.ResponseErr) - ChangeView(string, *dto.ChangeViewPetRequest) (bool, *dto.ResponseErr) + FindAll() ([]*dto.PetResponse, *dto.ResponseErr) + FindOne(string) (*dto.PetResponse, *dto.ResponseErr) + Create(*dto.CreatePetRequest) (*dto.PetResponse, *dto.ResponseErr) + Update(string, *dto.UpdatePetRequest) (*dto.PetResponse, *dto.ResponseErr) + Delete(string) (*dto.DeleteResponse, *dto.ResponseErr) + ChangeView(string, *dto.ChangeViewPetRequest) (*dto.ChangeViewPetResponse, *dto.ResponseErr) Adopt(*dto.AdoptDto) (bool, *dto.ResponseErr) }