-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from microsoft/feature/serialization-helpers
feature/serialization helpers
- Loading branch information
Showing
8 changed files
with
326 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package abstractions | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/microsoft/kiota-abstractions-go/internal" | ||
"github.com/microsoft/kiota-abstractions-go/serialization" | ||
assert "github.com/stretchr/testify/assert" | ||
) | ||
|
||
var jsonContentType = "application/json" | ||
|
||
func TestItDefendsSerializationEmptyContentType(t *testing.T) { | ||
result, err := serialization.Serialize("", nil) | ||
assert.Nil(t, result) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestItDefendsSerializationNilValue(t *testing.T) { | ||
result, err := serialization.Serialize(jsonContentType, nil) | ||
assert.Nil(t, result) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestItDefendsCollectionSerializationEmptyContentType(t *testing.T) { | ||
result, err := serialization.SerializeCollection("", nil) | ||
assert.Nil(t, result) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestItDefendsCollectionSerializationNilValue(t *testing.T) { | ||
result, err := serialization.SerializeCollection(jsonContentType, nil) | ||
assert.Nil(t, result) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestItSerializesObject(t *testing.T) { | ||
serializedValue := "{\"id\":\"123\"}" | ||
metaFactory := func() serialization.SerializationWriterFactory { | ||
return &internal.MockSerializerFactory{ | ||
SerializedValue: serializedValue, | ||
} | ||
} | ||
RegisterDefaultSerializer(metaFactory) | ||
person := internal.NewPerson() | ||
id := "123" | ||
person.SetId(&id) | ||
result, err := serialization.Serialize(jsonContentType, person) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, result) | ||
assert.Equal(t, serializedValue, string(result)) | ||
serialization.DefaultSerializationWriterFactoryInstance.ContentTypeAssociatedFactories = make(map[string]serialization.SerializationWriterFactory) | ||
} | ||
|
||
func TestItSerializesACollectionOfObjects(t *testing.T) { | ||
serializedValue := "[{\"id\":\"123\"}]" | ||
metaFactory := func() serialization.SerializationWriterFactory { | ||
return &internal.MockSerializerFactory{ | ||
SerializedValue: serializedValue, | ||
} | ||
} | ||
RegisterDefaultSerializer(metaFactory) | ||
person := internal.NewPerson() | ||
id := "123" | ||
person.SetId(&id) | ||
result, err := serialization.SerializeCollection(jsonContentType, []serialization.Parsable{person}) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, result) | ||
assert.Equal(t, serializedValue, string(result)) | ||
serialization.DefaultSerializationWriterFactoryInstance.ContentTypeAssociatedFactories = make(map[string]serialization.SerializationWriterFactory) | ||
} | ||
|
||
func TestItDefendsDeserializationEmptyContentType(t *testing.T) { | ||
result, err := serialization.Deserialize("", nil, nil) | ||
assert.Nil(t, result) | ||
assert.NotNil(t, err) | ||
} | ||
func TestItDefendsDeserializationNilContent(t *testing.T) { | ||
result, err := serialization.Deserialize(jsonContentType, nil, nil) | ||
assert.Nil(t, result) | ||
assert.NotNil(t, err) | ||
} | ||
func TestItDefendsDeserializationNilFactory(t *testing.T) { | ||
result, err := serialization.Deserialize(jsonContentType, make([]byte, 0), nil) | ||
assert.Nil(t, result) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestItDefendsCollectionDeserializationEmptyContentType(t *testing.T) { | ||
result, err := serialization.DeserializeCollection("", nil, nil) | ||
assert.Nil(t, result) | ||
assert.NotNil(t, err) | ||
} | ||
func TestItDefendsCollectionDeserializationNilContent(t *testing.T) { | ||
result, err := serialization.DeserializeCollection(jsonContentType, nil, nil) | ||
assert.Nil(t, result) | ||
assert.NotNil(t, err) | ||
} | ||
func TestItDefendsCollectionDeserializationNilFactory(t *testing.T) { | ||
result, err := serialization.DeserializeCollection(jsonContentType, make([]byte, 0), nil) | ||
assert.Nil(t, result) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestItDeserializesAnObject(t *testing.T) { | ||
person := internal.NewPerson() | ||
id := "123" | ||
person.SetId(&id) | ||
metaFactory := func() serialization.ParseNodeFactory { | ||
return &internal.MockParseNodeFactory{ | ||
SerializedValue: person, | ||
} | ||
} | ||
RegisterDefaultDeserializer(metaFactory) | ||
|
||
result, err := serialization.Deserialize(jsonContentType, []byte("{\"id\": \"123\"}"), internal.CreatePersonFromDiscriminatorValue) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, result) | ||
resultAsPerson, ok := result.(*internal.Person) | ||
assert.True(t, ok) | ||
assert.Equal(t, id, *resultAsPerson.GetId()) | ||
serialization.DefaultParseNodeFactoryInstance.ContentTypeAssociatedFactories = make(map[string]serialization.ParseNodeFactory) | ||
} | ||
|
||
func TestItDeserializesAnObjectCollection(t *testing.T) { | ||
person := internal.NewPerson() | ||
id := "123" | ||
person.SetId(&id) | ||
metaFactory := func() serialization.ParseNodeFactory { | ||
return &internal.MockParseNodeFactory{ | ||
SerializedValue: []serialization.Parsable{person}, | ||
} | ||
} | ||
RegisterDefaultDeserializer(metaFactory) | ||
|
||
result, err := serialization.DeserializeCollection(jsonContentType, []byte("[{\"id\": \"123\"}]"), internal.CreatePersonFromDiscriminatorValue) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, result) | ||
resultAsPerson, ok := result[0].(*internal.Person) | ||
assert.True(t, ok) | ||
assert.Equal(t, id, *resultAsPerson.GetId()) | ||
serialization.DefaultParseNodeFactoryInstance.ContentTypeAssociatedFactories = make(map[string]serialization.ParseNodeFactory) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package serialization | ||
|
||
var jsonContentType = "application/json" | ||
|
||
// SerializeToJson serializes the given model to JSON | ||
func SerializeToJson(model Parsable) ([]byte, error) { | ||
return Serialize(jsonContentType, model) | ||
} | ||
|
||
// SerializeCollectionToJson serializes the given models to JSON | ||
func SerializeCollectionToJson(models []Parsable) ([]byte, error) { | ||
return SerializeCollection(jsonContentType, models) | ||
} | ||
|
||
// DeserializeFromJson deserializes the given JSON to a model | ||
func DeserializeFromJson(content []byte, parsableFactory ParsableFactory) (Parsable, error) { | ||
return Deserialize(jsonContentType, content, parsableFactory) | ||
} | ||
|
||
// DeserializeCollectionFromJson deserializes the given JSON to a collection of models | ||
func DeserializeCollectionFromJson(content []byte, parsableFactory ParsableFactory) ([]Parsable, error) { | ||
return DeserializeCollection(jsonContentType, content, parsableFactory) | ||
} |
Oops, something went wrong.