Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:isd-sgcu/rpkm67-store into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
bookpanda committed Jun 29, 2024
2 parents 5b77a23 + 69c7e62 commit f3ab926
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions internal/object/test/object.repository_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package test

import (
"errors"
"net/http"
"testing"

"github.com/golang/mock/gomock"
"github.com/isd-sgcu/rpkm67-store/config"
"github.com/isd-sgcu/rpkm67-store/internal/object"
httpClient "github.com/isd-sgcu/rpkm67-store/mocks/client/http"
storeClient "github.com/isd-sgcu/rpkm67-store/mocks/client/store"
"github.com/minio/minio-go/v7"
"github.com/stretchr/testify/suite"
Expand All @@ -15,6 +18,7 @@ type ObjectRepositoryTest struct {
suite.Suite
conf *config.Store
controller *gomock.Controller
mockEndpoint string
}

func TestObjectRepository(t *testing.T) {
Expand All @@ -26,6 +30,7 @@ func (t *ObjectRepositoryTest) SetupTest() {
Endpoint: "mock-endpoint",
}
t.controller = gomock.NewController(t.T())
t.mockEndpoint = "https://mock-endpoint/bucket/object"
}

func (t *ObjectRepositoryTest) TestCreateObjectSuccess() {
Expand All @@ -43,3 +48,89 @@ func (t *ObjectRepositoryTest) TestCreateObjectSuccess() {
t.Equal("mock-key", key)
t.Equal(repo.GetURL("mock-bucket", "mock-key"), url)
}

func (t *ObjectRepositoryTest) TestUploadSuccess() {
storeClient := storeClient.NewMockClient(t.controller)
storeClient.EXPECT().PutObject(gomock.Any(), "bucket", "object", gomock.Any(), int64(0), gomock.Any()).Return(minio.UploadInfo{Key: "object"}, nil)

repo := object.NewRepository(t.conf, storeClient, nil)

url, key, err := repo.Upload([]byte{}, "bucket", "object")
t.Nil(err)
t.Equal("object", key)
t.Equal(repo.GetURL("bucket", "object"), url)
}

func (t *ObjectRepositoryTest) TestUploadError() {
storeClient := storeClient.NewMockClient(t.controller)
storeClient.EXPECT().PutObject(gomock.Any(), "bucket", "object", gomock.Any(), int64(0), gomock.Any()).Return(minio.UploadInfo{}, errors.New("error"))

repo := object.NewRepository(t.conf, storeClient, nil)

url, key, err := repo.Upload([]byte{}, "bucket", "object")
t.NotNil(err)
t.Empty(url)
t.Empty(key)
}

func (t *ObjectRepositoryTest) TestDeleteSuccess() {
storeClient := storeClient.NewMockClient(t.controller)
storeClient.EXPECT().RemoveObject(gomock.Any(), "bucket", "object", gomock.Any()).Return(nil)

repo := object.NewRepository(t.conf, storeClient, nil)

err:= repo.Delete("bucket", "object")
t.Nil(err)
}

func (t *ObjectRepositoryTest) TestDeleteError() {
storeClient := storeClient.NewMockClient(t.controller)
storeClient.EXPECT().RemoveObject(gomock.Any(), "bucket", "object", gomock.Any()).Return(errors.New("error"))

repo := object.NewRepository(t.conf, storeClient, nil)

err:= repo.Delete("bucket", "object")
t.NotNil(err)
}

func (t *ObjectRepositoryTest) TestGetSuccess() {
httpClient := httpClient.NewMockClient(t.controller)
httpClient.EXPECT().Get(t.mockEndpoint).Return(&http.Response{
StatusCode: http.StatusOK},nil)

repo := object.NewRepository(t.conf, nil, httpClient)

url,err:= repo.Get("bucket", "object")
t.Nil(err)
t.Equal(repo.GetURL("bucket","object"),url)
}

func (t *ObjectRepositoryTest) TestGetError() {
httpClient := httpClient.NewMockClient(t.controller)
httpClient.EXPECT().Get(t.mockEndpoint).Return(&http.Response{
StatusCode: http.StatusOK},errors.New("error"))

repo := object.NewRepository(t.conf, nil, httpClient)

url,err:= repo.Get("bucket", "object")
t.NotNil(err)
t.Empty(url)
}

func (t *ObjectRepositoryTest) TestGetStatusNotOK() {
httpClient := httpClient.NewMockClient(t.controller)
httpClient.EXPECT().Get(t.mockEndpoint).Return(&http.Response{
StatusCode: http.StatusNotFound},nil)

repo := object.NewRepository(t.conf, nil, httpClient)

url,err:= repo.Get("bucket", "object")
t.Nil(err)
t.Empty(url)
}

func (t *ObjectRepositoryTest) TestGetURL() {
repo := object.NewRepository(t.conf, nil, nil)
url := repo.GetURL("bucket","object")
t.Equal(t.mockEndpoint,url)
}

0 comments on commit f3ab926

Please sign in to comment.