Reformat/cleanup
All checks were successful
NuGet Package Publish / nuget (push) Successful in 1m10s
All checks were successful
NuGet Package Publish / nuget (push) Successful in 1m10s
This commit is contained in:
@@ -1,77 +1,73 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.CBDDC.Core;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ZB.MOM.WW.CBDDC.Core.Tests
|
||||
namespace ZB.MOM.WW.CBDDC.Core.Tests;
|
||||
|
||||
public class OplogEntryTests
|
||||
{
|
||||
public class OplogEntryTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Verifies that hash computation is deterministic even when payload content differs.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ComputeHash_ShouldBeDeterministic_RegardlessOfPayload()
|
||||
{
|
||||
// Arrange
|
||||
var collection = "test-collection";
|
||||
var key = "test-key";
|
||||
var op = OperationType.Put;
|
||||
var timestamp = new HlcTimestamp(100, 0, "node-1");
|
||||
var prevHash = "prev-hash";
|
||||
/// <summary>
|
||||
/// Verifies that hash computation is deterministic even when payload content differs.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ComputeHash_ShouldBeDeterministic_RegardlessOfPayload()
|
||||
{
|
||||
// Arrange
|
||||
var collection = "test-collection";
|
||||
var key = "test-key";
|
||||
var op = OperationType.Put;
|
||||
var timestamp = new HlcTimestamp(100, 0, "node-1");
|
||||
var prevHash = "prev-hash";
|
||||
|
||||
var payload1 = JsonDocument.Parse("{\"prop\": 1}").RootElement;
|
||||
var payload2 = JsonDocument.Parse("{\"prop\": 2, \"extra\": \"whitespace\"}").RootElement;
|
||||
var payload1 = JsonDocument.Parse("{\"prop\": 1}").RootElement;
|
||||
var payload2 = JsonDocument.Parse("{\"prop\": 2, \"extra\": \"whitespace\"}").RootElement;
|
||||
|
||||
// Act
|
||||
var entry1 = new OplogEntry(collection, key, op, payload1, timestamp, prevHash);
|
||||
var entry2 = new OplogEntry(collection, key, op, payload2, timestamp, prevHash);
|
||||
|
||||
// Assert
|
||||
entry2.Hash.ShouldBe(entry1.Hash);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that hash computation uses invariant culture formatting for timestamp values.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ComputeHash_ShouldUseInvariantCulture_ForTimestamp()
|
||||
{
|
||||
// Arrange
|
||||
var originalCulture = CultureInfo.CurrentCulture;
|
||||
try
|
||||
{
|
||||
var culture = CultureInfo.GetCultureInfo("de-DE");
|
||||
CultureInfo.CurrentCulture = culture;
|
||||
|
||||
var timestamp = new HlcTimestamp(123456789, 1, "node");
|
||||
var entry = new OplogEntry("col", "key", OperationType.Put, null, timestamp, "prev");
|
||||
|
||||
// Act
|
||||
var entry1 = new OplogEntry(collection, key, op, payload1, timestamp, prevHash);
|
||||
var entry2 = new OplogEntry(collection, key, op, payload2, timestamp, prevHash);
|
||||
string hash = entry.ComputeHash();
|
||||
|
||||
// Assert
|
||||
entry2.Hash.ShouldBe(entry1.Hash);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that hash computation uses invariant culture formatting for timestamp values.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ComputeHash_ShouldUseInvariantCulture_ForTimestamp()
|
||||
{
|
||||
// Arrange
|
||||
var originalCulture = CultureInfo.CurrentCulture;
|
||||
try
|
||||
{
|
||||
var culture = CultureInfo.GetCultureInfo("de-DE");
|
||||
CultureInfo.CurrentCulture = culture;
|
||||
|
||||
var timestamp = new HlcTimestamp(123456789, 1, "node");
|
||||
var entry = new OplogEntry("col", "key", OperationType.Put, null, timestamp, "prev");
|
||||
|
||||
// Act
|
||||
var hash = entry.ComputeHash();
|
||||
|
||||
// Assert
|
||||
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
|
||||
var expectedEntry = new OplogEntry("col", "key", OperationType.Put, null, timestamp, "prev");
|
||||
hash.ShouldBe(expectedEntry.Hash);
|
||||
}
|
||||
finally
|
||||
{
|
||||
CultureInfo.CurrentCulture = originalCulture;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that an entry is valid when its stored hash matches computed content.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void IsValid_ShouldReturnTrue_WhenHashMatches()
|
||||
{
|
||||
var timestamp = new HlcTimestamp(100, 0, "node-1");
|
||||
var entry = new OplogEntry("col", "key", OperationType.Put, null, timestamp, "prev");
|
||||
|
||||
entry.IsValid().ShouldBeTrue();
|
||||
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
|
||||
var expectedEntry = new OplogEntry("col", "key", OperationType.Put, null, timestamp, "prev");
|
||||
hash.ShouldBe(expectedEntry.Hash);
|
||||
}
|
||||
finally
|
||||
{
|
||||
CultureInfo.CurrentCulture = originalCulture;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that an entry is valid when its stored hash matches computed content.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void IsValid_ShouldReturnTrue_WhenHashMatches()
|
||||
{
|
||||
var timestamp = new HlcTimestamp(100, 0, "node-1");
|
||||
var entry = new OplogEntry("col", "key", OperationType.Put, null, timestamp, "prev");
|
||||
|
||||
entry.IsValid().ShouldBeTrue();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user