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