2020-07-03 12:11:05 +10:00
|
|
|
using System.Collections.Generic;
|
2022-06-21 08:09:38 +02:00
|
|
|
using Moq;
|
|
|
|
|
using NUnit.Framework;
|
2021-10-19 23:11:54 +11:00
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Core.PropertyEditors;
|
|
|
|
|
using Umbraco.Cms.Infrastructure.PublishedCache.DataSource;
|
2020-07-03 12:11:05 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.PublishedCache;
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class ContentSerializationTests
|
2020-07-03 12:11:05 +10:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void GivenACacheModel_WhenItsSerializedAndDeserializedWithAnySerializer_TheResultsAreTheSame()
|
2020-07-03 12:11:05 +10:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
var jsonSerializer = new JsonContentNestedDataSerializer();
|
|
|
|
|
var msgPackSerializer = new MsgPackContentNestedDataSerializer(Mock.Of<IPropertyCacheCompression>());
|
2020-07-03 12:11:05 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var now = DateTime.Now;
|
|
|
|
|
var cacheModel = new ContentCacheDataModel
|
|
|
|
|
{
|
|
|
|
|
PropertyData = new Dictionary<string, PropertyData[]>
|
2020-07-03 12:11:05 +10:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
["propertyOne"] =
|
|
|
|
|
new[] { new PropertyData { Culture = "en-US", Segment = "test", Value = "hello world" } },
|
|
|
|
|
["propertyTwo"] = new[]
|
2020-07-03 12:11:05 +10:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
new PropertyData { Culture = "en-US", Segment = "test", Value = "Lorem ipsum" },
|
2020-07-03 12:11:05 +10:00
|
|
|
},
|
2022-06-21 08:09:38 +02:00
|
|
|
},
|
|
|
|
|
CultureData = new Dictionary<string, CultureVariation>
|
|
|
|
|
{
|
|
|
|
|
["en-US"] = new() { Date = now, IsDraft = false, Name = "Home", UrlSegment = "home" },
|
|
|
|
|
},
|
|
|
|
|
UrlSegment = "home",
|
|
|
|
|
};
|
2021-01-28 13:50:18 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var content = Mock.Of<IReadOnlyContentBase>(x => x.ContentTypeId == 1);
|
2020-07-03 12:11:05 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var json = jsonSerializer.Serialize(content, cacheModel, false).StringData;
|
|
|
|
|
var msgPack = msgPackSerializer.Serialize(content, cacheModel, false).ByteData;
|
2020-07-03 12:11:05 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Console.WriteLine(json);
|
|
|
|
|
Console.WriteLine(msgPackSerializer.ToJson(msgPack));
|
2020-07-03 12:11:05 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var jsonContent = jsonSerializer.Deserialize(content, json, null, false);
|
|
|
|
|
var msgPackContent = msgPackSerializer.Deserialize(content, null, msgPack, false);
|
2020-07-03 12:11:05 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
CollectionAssert.AreEqual(jsonContent.CultureData.Keys, msgPackContent.CultureData.Keys);
|
|
|
|
|
CollectionAssert.AreEqual(jsonContent.PropertyData.Keys, msgPackContent.PropertyData.Keys);
|
|
|
|
|
CollectionAssert.AreEqual(jsonContent.CultureData.Values, msgPackContent.CultureData.Values, new CultureVariationComparer());
|
|
|
|
|
CollectionAssert.AreEqual(jsonContent.PropertyData.Values, msgPackContent.PropertyData.Values, new PropertyDataComparer());
|
|
|
|
|
Assert.AreEqual(jsonContent.UrlSegment, msgPackContent.UrlSegment);
|
|
|
|
|
}
|
2020-07-03 12:11:05 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public class CultureVariationComparer : Comparer<CultureVariation>
|
|
|
|
|
{
|
|
|
|
|
public override int Compare(CultureVariation x, CultureVariation y)
|
2020-07-03 12:11:05 +10:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
if (x == null && y == null)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (x == null && y != null)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (x != null && y == null)
|
2020-07-03 12:11:05 +10:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
return 1;
|
2020-07-03 12:11:05 +10:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
|
|
|
|
|
return x.Date.CompareTo(y.Date) | x.IsDraft.CompareTo(y.IsDraft) | x.Name.CompareTo(y.Name) |
|
|
|
|
|
x.UrlSegment.CompareTo(y.UrlSegment);
|
2020-07-03 12:11:05 +10:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2020-07-03 12:11:05 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public class PropertyDataComparer : Comparer<PropertyData>
|
|
|
|
|
{
|
|
|
|
|
public override int Compare(PropertyData x, PropertyData y)
|
2020-07-03 12:11:05 +10:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
if (x == null && y == null)
|
2020-07-03 12:11:05 +10:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
2020-07-03 12:11:05 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
if (x == null && y != null)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2020-07-03 12:11:05 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
if (x != null && y == null)
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
2020-07-03 12:11:05 +10:00
|
|
|
}
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var xVal = x.Value?.ToString() ?? string.Empty;
|
|
|
|
|
var yVal = y.Value?.ToString() ?? string.Empty;
|
|
|
|
|
|
|
|
|
|
return x.Culture.CompareTo(y.Culture) | x.Segment.CompareTo(y.Segment) | xVal.CompareTo(yVal);
|
|
|
|
|
}
|
2020-07-03 12:11:05 +10:00
|
|
|
}
|
|
|
|
|
}
|