Files
Common-VS2019/SacsCoreUnitTests/Standard/Base/Utils/NewtonsoftJsonUtil_Tester.cs
T

117 lines
4.1 KiB
C#

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Sarand.Net.Standard.Base.Bus;
using Sarand.Net.Standard.Base.Helpers;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
namespace Sacs.UnitTests.Net.Standard.Base.Utils
{
[TestClass]
public class NewtonsoftJsonUtil_Tester
{
private class JsonData
{
public string Name { get; set; }
public int Count { get; set; }
public string Optional { get; set; }
public List<string> Values { get; set; } = new List<string> { "constructor value" };
}
// --- Serialization ---
[TestMethod]
public void SerializeObject_HandlesObjectsArraysNullsAndSettings()
{
JsonData data = new JsonData { Name = "alpha", Count = 3, Optional = null };
JsonSerializerSettings settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
string objectJson = NewtonsoftJsonUtil.SerializeObject(data, settings);
string arrayJson = NewtonsoftJsonUtil.SerializeObject(new[] { 1, 2, 3 });
StringAssert.Contains(objectJson, "\"Name\":\"alpha\"");
Assert.IsFalse(objectJson.Contains("Optional"));
Assert.AreEqual("[1,2,3]", arrayJson);
Assert.AreEqual("null", NewtonsoftJsonUtil.SerializeObject(null));
}
[TestMethod]
public void SysIdJsonConverter_RoundTripsValidAndInvalidValues()
{
string validJson = JsonConvert.SerializeObject(new SysId(42));
string invalidJson = JsonConvert.SerializeObject(new SysId());
SysId validId = JsonConvert.DeserializeObject<SysId>(validJson);
SysId invalidId = JsonConvert.DeserializeObject<SysId>(invalidJson);
Assert.AreEqual("42", validJson);
Assert.AreEqual("0", invalidJson);
Assert.AreEqual(42, validId.ToInt());
Assert.IsFalse(SysId.IsValid(invalidId));
Assert.IsFalse(SysId.IsValid(JsonConvert.DeserializeObject<SysId>("null")));
}
// --- Parsing ---
[TestMethod]
public void ParseJsonMethods_ParseTokensObjectsAndArrays()
{
JToken token = NewtonsoftJsonUtil.ParseJsonToken("123");
JObject jsonObject = NewtonsoftJsonUtil.ParseJsonObject("{\"name\":\"alpha\",\"nested\":{\"enabled\":true}}");
JArray jsonArray = NewtonsoftJsonUtil.ParseJsonArray("[1,2,3]");
Assert.AreEqual(123, token.Value<int>());
Assert.AreEqual("alpha", jsonObject["name"].Value<string>());
Assert.IsTrue(jsonObject["nested"]["enabled"].Value<bool>());
CollectionAssert.AreEqual(new[] { 1, 2, 3 }, jsonArray.ToObject<int[]>());
}
[TestMethod]
public void ParseJsonMethods_ReportInvalidInputWithoutThrowing()
{
Assert.IsNull(NewtonsoftJsonUtil.ParseJsonToken("{", out Exception tokenException));
Assert.IsNotNull(tokenException);
Assert.IsNull(NewtonsoftJsonUtil.ParseJsonObject("[]", out Exception objectException));
Assert.IsNotNull(objectException);
Assert.IsNull(NewtonsoftJsonUtil.ParseJsonArray("{}", out Exception arrayException));
Assert.IsNotNull(arrayException);
Assert.ThrowsExactly<JsonReaderException>(() => NewtonsoftJsonUtil.ParseJsonToken("{"));
}
[TestMethod]
public void DeserializeJsonObject_ProvidesDynamicPropertyAccess()
{
dynamic data = SacsJsonHelper.DeserializeJsonObject("{\"name\":\"alpha\",\"count\":3,\"items\":[\"a\",\"b\"]}");
Assert.AreEqual("alpha", (string)data.name);
Assert.AreEqual(3, (int)data.count);
Assert.AreEqual("b", (string)data.items[1]);
Assert.IsNull(SacsJsonHelper.DeserializeJsonObject("null"));
}
// --- Deep Copy ---
[TestMethod]
public void DeepCopy_CreatesIndependentNestedCollectionsAndReplacesConstructorValues()
{
JsonData source = new JsonData { Name = "source", Count = 2 };
source.Values.Clear();
source.Values.AddRange(new[] { "one", "two" });
JsonData copy = SerializableUtil.DeepCopy(source);
copy.Values.Add("copy only");
Assert.AreNotSame(source, copy);
Assert.AreNotSame(source.Values, copy.Values);
CollectionAssert.AreEqual(new[] { "one", "two" }, source.Values);
CollectionAssert.AreEqual(new[] { "one", "two", "copy only" }, copy.Values);
Assert.IsNull(SerializableUtil.DeepCopy<JsonData>(null));
Assert.AreEqual(String.Empty, SerializableUtil.ToJson<JsonData>(null));
}
// --- The End ---
}
}