Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ArangoDBNetStandard.Serialization;
using ArangoDBNetStandard.TransactionApi.Models;
using ArangoDBNetStandardTest.Serialization.Models;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Text;
Expand All @@ -11,21 +12,20 @@ namespace ArangoDBNetStandardTest.Serialization
{
public class JsonNetApiClientSerializationTest
{

[Fact]
public void Serialize_ShouldSucceed()
{
var model = new TestModel()
{
NullPropertyToIgnore = null,
PropertyToCamelCase = "myvalue",
EnumToConvertToString = TestModel.Number.Two
NullProperty = null,
PropertyToCheckIfCamelCase = "myvalue",
EnumToConvert = TestModel.Number.Two
};

var serialization = new JsonNetApiClientSerialization();

// Perform serialize with camel case option
byte[] jsonBytesWithCamelCase = serialization.Serialize(model,
byte[] jsonBytesWithCamelCase = serialization.Serialize(model,
new ApiClientSerializationOptions(true, true, true));
string jsonStringWithCamelCase = Encoding.UTF8.GetString(jsonBytesWithCamelCase);

Expand All @@ -35,36 +35,62 @@ public void Serialize_ShouldSucceed()
string jsonStringWithoutCamelCase = Encoding.UTF8.GetString(jsonBytesWithoutCamelCase);

// standard property with and without camelCase
Assert.Contains("propertyToCamelCase", jsonStringWithCamelCase);
Assert.Contains("PropertyToCamelCase", jsonStringWithoutCamelCase);
Assert.Contains("propertyToCheckIfCamelCase", jsonStringWithCamelCase);
Assert.Contains("PropertyToCheckIfCamelCase", jsonStringWithoutCamelCase);

// Null property should be ignored in both cases
// (ignore case is important to make sure we don't miss the string
// if it is using different casing than we checked for)
Assert.DoesNotContain(
"nullPropertyToIgnore",
"nullProperty",
jsonStringWithCamelCase,
System.StringComparison.OrdinalIgnoreCase);

Assert.DoesNotContain("nullPropertyToIgnore",
Assert.DoesNotContain("nullProperty",
jsonStringWithoutCamelCase,
System.StringComparison.OrdinalIgnoreCase);

// We expect enum conversion to string, as well as camelCase
Assert.Contains("enumToConvertToString", jsonStringWithCamelCase);
Assert.Contains("enumToConvert", jsonStringWithCamelCase);

// We expect enum conversion to string, but not camelCase
Assert.Contains("EnumToConvertToString", jsonStringWithoutCamelCase);
Assert.Contains("EnumToConvert", jsonStringWithoutCamelCase);
}

[Fact]
public void Serialize_ShouldSucceed_WhenUsingDefaultOptions()
{
var model = new TestModel()
{
NullPropertyToIgnore = null,
PropertyToCamelCase = "myvalue",
EnumToConvertToString = TestModel.Number.Two
NullProperty = null,
PropertyToCheckIfCamelCase = "myvalue",
EnumToConvert = TestModel.Number.Two,
PropertyWithDifferentJsonName = "aaaaaaa",
MyStringDict = new Dictionary<string, object>()
{
["DictKeyNotCamelCasedAndNotIgnored"] = null,
["moreParams"] = new TestModel()
{
PropertyToCheckIfCamelCase = "fneozirnzgiodedzf",
MyStringDict = new Dictionary<string, object>()
{
["string1"] = "value1"
},
PropertyWithDifferentJsonName = "wow",
AnotherNullProperty = null,
EnumToConvert = TestModel.Number.One,
PropertyWithClassType = new InnerTestModel()
{
InnerTestModel_NullProperty = null,
InnerTestModel_PropertyToCheckIfCamelCase = "fjnzfiuzefnzeb"
}
}
},
PropertyWithClassType = new InnerTestModel()
{
InnerTestModel_NullProperty = null,
InnerTestModel_PropertyToCheckIfCamelCase = "djenfiuzrfiubzeiu"
}
};
var serialization = new JsonNetApiClientSerialization();

Expand All @@ -73,12 +99,31 @@ public void Serialize_ShouldSucceed_WhenUsingDefaultOptions()
byte[] jsonBytes = serialization.Serialize(model, null);
string jsonString = Encoding.UTF8.GetString(jsonBytes);

Assert.Contains("PropertyToCamelCase", jsonString);
Assert.Equal(
"{\"PropertyToCheckIfCamelCase\":\"myvalue\",\"EnumToConvert\":2,\"nameFromJsonProperty\":\"aaaaaaa\",\"MyStringDict\":{\"DictKeyNotCamelCasedAndNotIgnored\":null,\"moreParams\":{\"PropertyToCheckIfCamelCase\":\"fneozirnzgiodedzf\",\"EnumToConvert\":1,\"nameFromJsonProperty\":\"wow\",\"MyStringDict\":{\"string1\":\"value1\"},\"PropertyWithClassType\":{\"InnerTestModel_PropertyToCheckIfCamelCase\":\"fjnzfiuzefnzeb\"}}},\"PropertyWithClassType\":{\"InnerTestModel_PropertyToCheckIfCamelCase\":\"djenfiuzrfiubzeiu\"}}",
jsonString);

// Explicited asserts

Assert.Contains("PropertyToCheckIfCamelCase", jsonString);
Assert.DoesNotContain(
"NullPropetyToIgnore",
"NullProperty",
jsonString,
System.StringComparison.OrdinalIgnoreCase);
Assert.Contains("EnumToConvertToString", jsonString);
Assert.Contains("EnumToConvert", jsonString);
Assert.DoesNotContain("Two", jsonString);
Assert.Contains("nameFromJsonProperty", jsonString);
Assert.Contains("MyStringDict", jsonString);
Assert.Contains("DictKeyNotCamelCasedAndNotIgnored", jsonString);
Assert.Contains("moreParams", jsonString);
Assert.Contains("string1", jsonString);
Assert.DoesNotContain("PropertyWithDifferentJsonName", jsonString);
Assert.DoesNotContain(
"AnotherNullProperty",
jsonString,
System.StringComparison.OrdinalIgnoreCase);
Assert.Contains("PropertyWithClassType", jsonString);
Assert.Contains("InnerTestModel_PropertyToCheckIfCamelCase", jsonString);
}

[Fact]
Expand Down Expand Up @@ -126,7 +171,6 @@ public void Serialize_ShouldNotCamelCaseParams_WhenSerializingPostTransactionBod
Assert.DoesNotContain("dontCamelCaseKey", jsonString);
}


[Fact]
public void Serialize_ShouldNotIgnoreNull_WhenSerializingPostCursorBody()
{
Expand All @@ -146,7 +190,6 @@ public void Serialize_ShouldNotIgnoreNull_WhenSerializingPostCursorBody()
Assert.Contains("DontCamelCaseKey", jsonString);
}


[Fact]
public void Serialize_ShouldNotIgnoreNull_WhenSerializingPostTransactionBody()
{
Expand All @@ -173,16 +216,16 @@ public void DeserializeFromStream_ShouldSucceed()
// Deserializing should match both "camelCase" and "CamelCase"

byte[] jsonBytes = Encoding.UTF8.GetBytes(
"{\"propertyToCamelCase\":\"myvalue\",\"NullPropertyToIgnore\":\"something\"}");
"{\"propertyToCheckIfCamelCase\":\"myvalue\",\"NullProperty\":\"something\"}");

var stream = new MemoryStream(jsonBytes);

var serialization = new JsonNetApiClientSerialization();

TestModel model = serialization.DeserializeFromStream<TestModel>(stream);

Assert.Equal("myvalue", model.PropertyToCamelCase);
Assert.Equal("something", model.NullPropertyToIgnore);
Assert.Equal("myvalue", model.PropertyToCheckIfCamelCase);
Assert.Equal("something", model.NullProperty);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ArangoDBNetStandardTest.Serialization.Models
{
public class InnerTestModel
{
public string InnerTestModel_NullProperty { get; set; }

public string InnerTestModel_PropertyToCheckIfCamelCase { get; set; }
}
}
20 changes: 16 additions & 4 deletions arangodb-net-standard.Test/Serialization/Models/TestModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace ArangoDBNetStandardTest.Serialization.Models
using Newtonsoft.Json;
using System.Collections.Generic;

namespace ArangoDBNetStandardTest.Serialization.Models
{
public class TestModel
{
Expand All @@ -8,10 +11,19 @@ public enum Number
Two = 2
}

public string NullPropertyToIgnore { get; set; }
public string NullProperty { get; set; }

public string AnotherNullProperty { get; set; }

public string PropertyToCheckIfCamelCase { get; set; }

public Number EnumToConvert { get; set; }

[JsonProperty(PropertyName = "nameFromJsonProperty")]
public string PropertyWithDifferentJsonName { get; set; }

public string PropertyToCamelCase { get; set; }
public Dictionary<string, object> MyStringDict { get; set; }

public Number EnumToConvertToString { get; set; }
public InnerTestModel PropertyWithClassType { get; set; }
}
}