From bb5b2f23e9688a4b540f2b0ac6c662b05d770244 Mon Sep 17 00:00:00 2001 From: Danny Date: Wed, 4 Jan 2023 15:59:13 -0500 Subject: [PATCH] GC: Framework to use adapter's serialization writer. (#2032) ## Description The MS Graph's adapter contains a serialization writer within it. Create a framework to use this writer rather than creating the object within serialization workflows. The writer library was used separately when the package still had many bugs and the most current version was required. This does not change any of the writer instances within the package. Will change out the writer instances in follow-up PRs. ## Does this PR need a docs update or release note? - [x] :no_entry: No ## Type of change - [x] :sunflower: Feature ## Issue(s) *related to #2031 ## Test Plan - [x] :zap: Unit test --- src/internal/connector/graph/service.go | 18 ++++++ src/internal/connector/graph/service_test.go | 64 ++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 src/internal/connector/graph/service_test.go diff --git a/src/internal/connector/graph/service.go b/src/internal/connector/graph/service.go index f861666e1..7780e7941 100644 --- a/src/internal/connector/graph/service.go +++ b/src/internal/connector/graph/service.go @@ -3,7 +3,9 @@ package graph import ( "context" + absser "github.com/microsoft/kiota-abstractions-go/serialization" msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go" + "github.com/pkg/errors" "github.com/alcionai/corso/src/pkg/account" "github.com/alcionai/corso/src/pkg/path" @@ -43,6 +45,22 @@ func (s Service) Client() *msgraphsdk.GraphServiceClient { return s.client } +// Seraialize writes an M365 parsable object into a byte array using the built-in +// application/json writer within the adapter. +func (s Service) Serialize(object absser.Parsable) ([]byte, error) { + writer, err := s.adapter.GetSerializationWriterFactory().GetSerializationWriter("application/json") + if err != nil || writer == nil { + return nil, errors.Wrap(err, "creating json serialization writer") + } + + err = writer.WriteObjectValue("", object) + if err != nil { + return nil, errors.Wrap(err, "writeObjecValue serialization") + } + + return writer.GetSerializedContent() +} + type Servicer interface { // Client() returns msgraph Service client that can be used to process and execute // the majority of the queries to the M365 Backstore diff --git a/src/internal/connector/graph/service_test.go b/src/internal/connector/graph/service_test.go new file mode 100644 index 000000000..737419c88 --- /dev/null +++ b/src/internal/connector/graph/service_test.go @@ -0,0 +1,64 @@ +package graph_test + +import ( + "testing" + + "github.com/microsoftgraph/msgraph-sdk-go/models" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + + "github.com/alcionai/corso/src/internal/connector/graph" + "github.com/alcionai/corso/src/internal/tester" + "github.com/alcionai/corso/src/pkg/account" +) + +type GraphUnitSuite struct { + suite.Suite + credentials account.M365Config +} + +func TestGraphUnitSuite(t *testing.T) { + suite.Run(t, new(GraphUnitSuite)) +} + +func (suite *GraphUnitSuite) SetupSuite() { + t := suite.T() + a := tester.NewM365Account(t) + m365, err := a.M365Config() + require.NoError(t, err) + + suite.credentials = m365 +} + +func (suite *GraphUnitSuite) TestCreateAdapter() { + t := suite.T() + adpt, err := graph.CreateAdapter( + suite.credentials.AzureTenantID, + suite.credentials.AzureClientID, + suite.credentials.AzureClientSecret, + ) + + assert.NoError(t, err) + assert.NotNil(t, adpt) +} + +func (suite *GraphUnitSuite) TestSerializationEndPoint() { + t := suite.T() + adpt, err := graph.CreateAdapter( + suite.credentials.AzureTenantID, + suite.credentials.AzureClientID, + suite.credentials.AzureClientSecret, + ) + require.NoError(t, err) + + serv := graph.NewService(adpt) + email := models.NewMessage() + subject := "TestSerializationEndPoint" + email.SetSubject(&subject) + + byteArray, err := serv.Serialize(email) + assert.NoError(t, err) + assert.NotNil(t, byteArray) + t.Log(string(byteArray)) +}