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
@@ -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)
}