From 3b9c8e284c7fb475f45139d88b7f6574f668c6ca Mon Sep 17 00:00:00 2001 From: Danny Date: Tue, 23 Aug 2022 16:46:59 -0400 Subject: [PATCH] GC: Reduce the amount of folders upon restore (#637) * Issue #595: All collections with the same `fullPath` name are to be stored within the same folder. --- src/internal/connector/graph_connector.go | 14 +++++++------ .../connector/graph_connector_test.go | 21 +++++++++++++++---- src/internal/connector/support/status.go | 6 +++--- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/internal/connector/graph_connector.go b/src/internal/connector/graph_connector.go index c8d6e586f..358962592 100644 --- a/src/internal/connector/graph_connector.go +++ b/src/internal/connector/graph_connector.go @@ -250,16 +250,18 @@ func (gc *GraphConnector) RestoreMessages(ctx context.Context, dcs []data.Collec policy := control.Copy // TODO policy to be updated from external source after completion of refactoring for _, dc := range dcs { + directory := strings.Join(dc.FullPath(), "") user := dc.FullPath()[1] items := dc.Items() - pathCounter[strings.Join(dc.FullPath(), "")] = true - if policy == control.Copy { - folderID, errs = exchange.GetCopyRestoreFolder(&gc.graphService, user) - if errs != nil { - return errs + if _, ok := pathCounter[directory]; !ok { + pathCounter[directory] = true + if policy == control.Copy { + folderID, errs = exchange.GetCopyRestoreFolder(&gc.graphService, user) + if errs != nil { + return errs + } } } - var exit bool for !exit { select { diff --git a/src/internal/connector/graph_connector_test.go b/src/internal/connector/graph_connector_test.go index 6ccb33bfe..2e4398fb2 100644 --- a/src/internal/connector/graph_connector_test.go +++ b/src/internal/connector/graph_connector_test.go @@ -224,15 +224,28 @@ func (suite *GraphConnectorIntegrationSuite) TestEventsSerializationRegression() } // TestRestoreMessages uses mock data to ensure GraphConnector -// is able to restore a single messageable item to a Mailbox. +// is able to restore a several messageable item to a Mailbox. +// The result should be all successful items restored within the same folder. func (suite *GraphConnectorIntegrationSuite) TestRestoreMessages() { - user := tester.M365UserID(suite.T()) + t := suite.T() + connector := loadConnector(t) + user := tester.M365UserID(t) if len(user) == 0 { suite.T().Skip("Environment not configured: missing m365 test user") } - mdc := mockconnector.NewMockExchangeCollection([]string{"tenant", user, mailCategory, "Inbox"}, 1) - err := suite.connector.RestoreMessages(context.Background(), []data.Collection{mdc}) + + collection := make([]data.Collection, 0) + for i := 0; i < 3; i++ { + mdc := mockconnector.NewMockExchangeCollection([]string{"tenant", user, mailCategory, "Inbox"}, 1) + collection = append(collection, mdc) + } + + err := connector.RestoreMessages(context.Background(), collection) assert.NoError(suite.T(), err) + status := connector.AwaitStatus() + assert.NotNil(t, status) + assert.Equal(t, status.ObjectCount, status.Successful) + assert.Equal(t, status.FolderCount, 1) } // TestGraphConnector_SingleMailFolderCollectionQuery verifies single folder support diff --git a/src/internal/connector/support/status.go b/src/internal/connector/support/status.go index 928355ba1..8109a41b0 100644 --- a/src/internal/connector/support/status.go +++ b/src/internal/connector/support/status.go @@ -10,7 +10,7 @@ import ( type ConnectorOperationStatus struct { lastOperation Operation ObjectCount int - folderCount int + FolderCount int Successful int errorCount int incomplete bool @@ -42,7 +42,7 @@ func CreateStatus( status := ConnectorOperationStatus{ lastOperation: op, ObjectCount: objects, - folderCount: folders, + FolderCount: folders, Successful: success, errorCount: numErr, incomplete: hasErrors, @@ -60,7 +60,7 @@ func CreateStatus( func (cos *ConnectorOperationStatus) String() string { message := fmt.Sprintf("Action: %s performed on %d of %d objects within %d directories.", cos.lastOperation.String(), - cos.Successful, cos.ObjectCount, cos.folderCount) + cos.Successful, cos.ObjectCount, cos.FolderCount) if cos.incomplete { message += " " + cos.incompleteReason }