tests: add and adapt unit tests

This commit is contained in:
Michael Barz
2024-07-24 21:32:32 +02:00
parent 08cb228500
commit 4c63ddc439
8 changed files with 182 additions and 7 deletions
+5
View File
@@ -10,3 +10,8 @@ packages:
ConnectorService:
ContentConnectorService:
FileConnectorService:
github.com/owncloud/ocis/v2/services/collaboration/pkg/locks:
config:
dir: "mocks"
interfaces:
LockParser:
@@ -1,4 +1,4 @@
// Code generated by mockery v2.40.2. DO NOT EDIT.
// Code generated by mockery v2.43.2. DO NOT EDIT.
package mocks
@@ -1,4 +1,4 @@
// Code generated by mockery v2.40.2. DO NOT EDIT.
// Code generated by mockery v2.43.2. DO NOT EDIT.
package mocks
@@ -1,4 +1,4 @@
// Code generated by mockery v2.40.2. DO NOT EDIT.
// Code generated by mockery v2.43.2. DO NOT EDIT.
package mocks
@@ -0,0 +1,78 @@
// Code generated by mockery v2.43.2. DO NOT EDIT.
package mocks
import mock "github.com/stretchr/testify/mock"
// LockParser is an autogenerated mock type for the LockParser type
type LockParser struct {
mock.Mock
}
type LockParser_Expecter struct {
mock *mock.Mock
}
func (_m *LockParser) EXPECT() *LockParser_Expecter {
return &LockParser_Expecter{mock: &_m.Mock}
}
// ParseLock provides a mock function with given fields: id
func (_m *LockParser) ParseLock(id string) string {
ret := _m.Called(id)
if len(ret) == 0 {
panic("no return value specified for ParseLock")
}
var r0 string
if rf, ok := ret.Get(0).(func(string) string); ok {
r0 = rf(id)
} else {
r0 = ret.Get(0).(string)
}
return r0
}
// LockParser_ParseLock_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ParseLock'
type LockParser_ParseLock_Call struct {
*mock.Call
}
// ParseLock is a helper method to define mock.On call
// - id string
func (_e *LockParser_Expecter) ParseLock(id interface{}) *LockParser_ParseLock_Call {
return &LockParser_ParseLock_Call{Call: _e.mock.On("ParseLock", id)}
}
func (_c *LockParser_ParseLock_Call) Run(run func(id string)) *LockParser_ParseLock_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(string))
})
return _c
}
func (_c *LockParser_ParseLock_Call) Return(_a0 string) *LockParser_ParseLock_Call {
_c.Call.Return(_a0)
return _c
}
func (_c *LockParser_ParseLock_Call) RunAndReturn(run func(string) string) *LockParser_ParseLock_Call {
_c.Call.Return(run)
return _c
}
// NewLockParser creates a new instance of LockParser. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
// The first argument is typically a *testing.T value.
func NewLockParser(t interface {
mock.TestingT
Cleanup(func())
}) *LockParser {
mock := &LockParser{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
}
@@ -27,9 +27,8 @@ const (
// All operations are expected to follow the definitions found in
// https://learn.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/rest/endpoints
type HttpAdapter struct {
con ConnectorService
config *config.Config
locks locks.LockParser
con ConnectorService
locks locks.LockParser
}
// NewHttpAdapter will create a new HTTP adapter. A new connector using the
@@ -20,6 +20,7 @@ var _ = Describe("HttpAdapter", func() {
fc *mocks.FileConnectorService
cc *mocks.ContentConnectorService
con *mocks.ConnectorService
locks *mocks.LockParser
httpAdapter *connector.HttpAdapter
)
@@ -31,7 +32,12 @@ var _ = Describe("HttpAdapter", func() {
con.On("GetContentConnector").Return(cc)
con.On("GetFileConnector").Return(fc)
httpAdapter = connector.NewHttpAdapterWithConnector(con)
locks = &mocks.LockParser{}
locks.EXPECT().ParseLock(mock.Anything).RunAndReturn(func(id string) string {
return id
})
httpAdapter = connector.NewHttpAdapterWithConnector(con, locks)
})
Describe("GetLock", func() {
@@ -0,0 +1,87 @@
package locks
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLegacyLockParser(t *testing.T) {
tests := []struct {
name string
lock string
cleanLock string
}{
{
name: "JsonStringWithLKey",
lock: createJsonString(map[string]interface{}{"L": "12345678", "F": 4, "E": 2, "C": "", "P": "3453345345346", "M": "12345678"}),
cleanLock: "12345678",
},
{
name: "JsonStringWithSKey",
lock: createJsonString(map[string]interface{}{"S": "12345678", "F": 4, "E": 2, "C": "", "P": "3453345345346", "M": "12345678"}),
cleanLock: "12345678",
},
{
name: "PlainString",
lock: "12345678",
cleanLock: "12345678",
},
{
name: "JsonStringUnknownFormat",
lock: createJsonString(map[string]interface{}{"A": "12345678", "F": 4, "E": 2, "C": "", "P": "3453345345346", "X": "12345678"}),
cleanLock: `{"A":"12345678","C":"","E":2,"F":4,"P":"3453345345346","X":"12345678"}`,
},
{
name: "InvalidJsonString",
lock: `"A":"12345678","C":"","E":2,"F":4,"P":"3453345345346","X":"12345678"}`,
cleanLock: `"A":"12345678","C":"","E":2,"F":4,"P":"3453345345346","X":"12345678"}`,
},
{
name: "EmptyString",
lock: "",
cleanLock: "",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
lockParser := &LegacyLockParser{}
lock := lockParser.ParseLock(test.lock)
assert.Equal(t, test.cleanLock, lock)
})
}
}
func TestNoopLockParser(t *testing.T) {
tests := []struct {
name string
lock string
}{
{
name: "PlainString",
lock: "123",
},
{
name: "EmptyString",
lock: "",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
lockParser := &NoopLockParser{}
lock := lockParser.ParseLock(test.lock)
assert.Equal(t, test.lock, lock)
})
}
}
func createJsonString(input map[string]interface{}) string {
rawData, err := json.Marshal(&input)
if err != nil {
return ""
}
return string(rawData)
}