diff --git a/src/go.mod b/src/go.mod index 50d5047cf..593b11737 100644 --- a/src/go.mod +++ b/src/go.mod @@ -77,6 +77,7 @@ require ( github.com/rs/xid v1.4.0 // indirect github.com/sirupsen/logrus v1.8.1 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/stretchr/objx v0.2.0 // indirect github.com/yosida95/uritemplate/v3 v3.0.2 // indirect github.com/zeebo/blake3 v0.2.3 // indirect go.opentelemetry.io/otel v1.7.0 // indirect diff --git a/src/go.sum b/src/go.sum index 2c6b78df7..f4415ca54 100644 --- a/src/go.sum +++ b/src/go.sum @@ -336,6 +336,8 @@ github.com/spf13/viper v1.12.0 h1:CZ7eSOd3kZoaYDLbXnmzgQI5RlciuXBMA+18HwHRfZQ= github.com/spf13/viper v1.12.0/go.mod h1:b6COn30jlNxbm/V2IqWiNWkJ+vZNiMNksliPCiuKtSI= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48= +github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= diff --git a/src/internal/connector/mockconnector/mock_data_collection.go b/src/internal/connector/mockconnector/mock_data_collection.go new file mode 100644 index 000000000..3243f2f57 --- /dev/null +++ b/src/internal/connector/mockconnector/mock_data_collection.go @@ -0,0 +1,63 @@ +package mockconnector + +import ( + "bytes" + "io" + + "github.com/google/uuid" + + "github.com/alcionai/corso/internal/connector" +) + +// MockExchangeDataCollection represents a mock exchange mailbox +type MockExchangeDataCollection struct { + fullPath []string + messageCount int + messagesRead int +} + +var ( + _ connector.DataCollection = &MockExchangeDataCollection{} + _ connector.DataStream = &MockExchangeData{} +) + +// NewMockExchangeDataCollection creates an data collection that will return the specified number of +// mock messages when iterated +func NewMockExchangeDataCollection(pathRepresentation []string, numMessagesToReturn int) *MockExchangeDataCollection { + collection := &MockExchangeDataCollection{ + fullPath: pathRepresentation, + messageCount: numMessagesToReturn, + messagesRead: 0, + } + return collection +} + +func (medc *MockExchangeDataCollection) FullPath() []string { + return append([]string{}, medc.fullPath...) +} + +// NextItem returns either the next item in the collection or an error if one occurred. +// If not more items are available in the collection, returns (nil, nil). +func (medc *MockExchangeDataCollection) NextItem() (connector.DataStream, error) { + if medc.messagesRead < medc.messageCount { + medc.messagesRead++ + // We can plug in whatever data we want here (can be an io.Reader to a test data file if needed) + m := []byte("test message") + return &MockExchangeData{uuid.NewString(), bytes.NewReader(m)}, nil + } + return nil, io.EOF +} + +// ExchangeData represents a single item retrieved from exchange +type MockExchangeData struct { + id string + reader io.Reader +} + +func (med *MockExchangeData) UUID() string { + return med.id +} + +func (med *MockExchangeData) ToReader() io.Reader { + return med.reader +} diff --git a/src/internal/connector/mockconnector/mock_data_collection_test.go b/src/internal/connector/mockconnector/mock_data_collection_test.go new file mode 100644 index 000000000..f62db00c4 --- /dev/null +++ b/src/internal/connector/mockconnector/mock_data_collection_test.go @@ -0,0 +1,33 @@ +package mockconnector_test + +import ( + "io" + "io/ioutil" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" + + "github.com/alcionai/corso/internal/connector/mockconnector" +) + +type MockExchangeDataCollectionSuite struct { + suite.Suite +} + +func TestMockExchangeDataCollectionSuite(t *testing.T) { + suite.Run(t, new(MockExchangeDataCollectionSuite)) +} + +func (suite *MockExchangeDataCollectionSuite) TestMockExchangeDataCollection() { + mdc := mockconnector.NewMockExchangeDataCollection([]string{"foo", "bar"}, 2) + + messagesRead := 0 + + for item, err := mdc.NextItem(); err != io.EOF; item, err = mdc.NextItem() { + _, err := ioutil.ReadAll(item.ToReader()) + assert.NoError(suite.T(), err) + messagesRead++ + } + assert.Equal(suite.T(), 2, messagesRead) +}