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

Add service interface with Mock #7

Merged
merged 2 commits into from
Jul 7, 2024
Merged
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
78 changes: 78 additions & 0 deletions service/mock.go
Original file line number Diff line number Diff line change
@@ -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)
}
89 changes: 89 additions & 0 deletions service/service.go
Original file line number Diff line number Diff line change
@@ -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")
}
Loading