From 7e7eca621e77c295d696828a951c969c60035223 Mon Sep 17 00:00:00 2001 From: Igor Shishkin Date: Sun, 7 Jul 2024 11:19:26 +0300 Subject: [PATCH] Add service interface with Mock Signed-off-by: Igor Shishkin --- service/mock.go | 78 ++++++++++++++++++++++++++++++++++++++++ service/service.go | 89 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 service/mock.go create mode 100644 service/service.go diff --git a/service/mock.go b/service/mock.go new file mode 100644 index 0000000..b23c966 --- /dev/null +++ b/service/mock.go @@ -0,0 +1,78 @@ +package service + +import ( + "context" + "io" + + "github.com/stretchr/testify/mock" +) + +var _ ManageService = (*Mock)(nil) + +type Mock struct { + mock.Mock +} + +func NewMock() *Mock { + return &Mock{} +} + +func (m *Mock) CreateContainer(_ context.Context, name string) error { + args := m.Called(name) + return args.Error(0) +} + +func (m *Mock) ListContainers(context.Context) ([]string, error) { + args := m.Called() + return args.Get(0).([]string), args.Error(1) +} + +func (m *Mock) DeleteContainer(_ context.Context, name string) error { + args := m.Called(name) + return args.Error(0) +} + +func (m *Mock) CreateVersion(_ context.Context, container string) (id string, err error) { + args := m.Called(container) + return args.Get(0).(string), args.Error(1) +} + +func (m *Mock) ListVersions(_ context.Context, container string) ([]string, error) { + args := m.Called(container) + return args.Get(0).([]string), args.Error(1) +} + +func (m *Mock) PublishVersion(_ context.Context, container, id string) error { + args := m.Called(container, id) + return args.Error(0) +} + +func (m *Mock) DeleteVersion(_ context.Context, container, id string) error { + args := m.Called(container, id) + return args.Error(0) +} + +func (m *Mock) AddObject(_ context.Context, container, versionID, key string, objReader io.Reader) error { + data, err := io.ReadAll(objReader) + if err != nil { + return err + } + + args := m.Called(container, versionID, key, data) + return args.Error(0) +} + +func (m *Mock) ListObjects(_ context.Context, container, versionID string) ([]string, error) { + args := m.Called(container, versionID) + return args.Get(0).([]string), args.Error(1) +} + +func (m *Mock) GetObjectURL(ctx context.Context, container, versionID, key string) (string, error) { + args := m.Called(container, versionID, key) + return args.String(0), args.Error(1) +} + +func (m *Mock) DeleteObject(_ context.Context, container, versionID, key string) error { + args := m.Called(container) + return args.Error(0) +} diff --git a/service/service.go b/service/service.go new file mode 100644 index 0000000..d30ddec --- /dev/null +++ b/service/service.go @@ -0,0 +1,89 @@ +package service + +import ( + "context" + "io" + + "github.com/teran/archived/repositories/blob" + "github.com/teran/archived/repositories/metadata" +) + +type ManageService interface { + AccessService + + CreateContainer(ctx context.Context, name string) error + DeleteContainer(ctx context.Context, name string) error + + CreateVersion(ctx context.Context, container string) (id string, err error) + PublishVersion(ctx context.Context, container, id string) error + DeleteVersion(ctx context.Context, container, id string) error + + AddObject(ctx context.Context, container, versionID, key string, objReader io.Reader) error + DeleteObject(ctx context.Context, container, versionID, key string) error +} + +type AccessService interface { + ListContainers(ctx context.Context) ([]string, error) + + ListVersions(ctx context.Context, container string) ([]string, error) + + ListObjects(ctx context.Context, container, versionID string) ([]string, error) + GetObjectURL(ctx context.Context, container, versionID, key string) (string, error) +} + +type service struct { + mdRepo metadata.Repository + blobRepo blob.Repository +} + +func NewManageService(mdRepo metadata.Repository, blobRepo blob.Repository) ManageService { + return &service{} +} + +func NewAccessService(mdRepo metadata.Repository, blobRepo blob.Repository) AccessService { + return &service{} +} + +func (s *service) CreateContainer(ctx context.Context, name string) error { + return s.mdRepo.CreateContainer(ctx, name) +} + +func (s *service) ListContainers(ctx context.Context) ([]string, error) { + return s.mdRepo.ListContainers(ctx) +} + +func (s *service) DeleteContainer(ctx context.Context, name string) error { + return s.mdRepo.DeleteContainer(ctx, name) +} + +func (s *service) CreateVersion(ctx context.Context, container string) (id string, err error) { + panic("not implemented") +} + +func (s *service) ListVersions(ctx context.Context, container string) ([]string, error) { + panic("not implemented") +} + +func (s *service) PublishVersion(ctx context.Context, container, id string) error { + panic("not implemented") +} + +func (s *service) DeleteVersion(ctx context.Context, container, id string) error { + panic("not implemented") +} + +func (s *service) AddObject(ctx context.Context, container, versionID, key string, objReader io.Reader) error { + panic("not implemented") +} + +func (s *service) ListObjects(ctx context.Context, container, versionID string) ([]string, error) { + panic("not implemented") +} + +func (s *service) GetObjectURL(ctx context.Context, container, versionID, key string) (string, error) { + panic("not implemented") +} + +func (s *service) DeleteObject(ctx context.Context, container, versionID, key string) error { + panic("not implemented") +}