From 15660b2394447a9750dcc61795034d771790760a Mon Sep 17 00:00:00 2001 From: monsieurleberre Date: Mon, 12 Nov 2018 19:54:41 +0300 Subject: [PATCH] fix the serialisation of the hash, add test (#164) --- .../Interfaces/IBlockTip.cs | 3 ++ .../BlockTipModelTests.cs | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/Stratis.FederatedPeg.Tests/BlockTipModelTests.cs diff --git a/src/Stratis.FederatedPeg.Features.FederationGateway/Interfaces/IBlockTip.cs b/src/Stratis.FederatedPeg.Features.FederationGateway/Interfaces/IBlockTip.cs index b9787ad8b9d..222a4b2dd44 100644 --- a/src/Stratis.FederatedPeg.Features.FederationGateway/Interfaces/IBlockTip.cs +++ b/src/Stratis.FederatedPeg.Features.FederationGateway/Interfaces/IBlockTip.cs @@ -1,10 +1,13 @@ using NBitcoin; +using Newtonsoft.Json; using Stratis.Bitcoin.Utilities; +using Stratis.Bitcoin.Utilities.JsonConverters; namespace Stratis.FederatedPeg.Features.FederationGateway.Interfaces { public interface IBlockTip { + [JsonConverter(typeof(UInt256JsonConverter))] uint256 Hash { get; } int Height { get; } diff --git a/src/Stratis.FederatedPeg.Tests/BlockTipModelTests.cs b/src/Stratis.FederatedPeg.Tests/BlockTipModelTests.cs new file mode 100644 index 00000000000..7d7c2dc1197 --- /dev/null +++ b/src/Stratis.FederatedPeg.Tests/BlockTipModelTests.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Text; +using FluentAssertions; +using Newtonsoft.Json; +using Stratis.FederatedPeg.Features.FederationGateway.Interfaces; +using Stratis.FederatedPeg.Features.FederationGateway.Models; +using Stratis.FederatedPeg.Tests.Utils; +using Xunit; + +namespace Stratis.FederatedPeg.Tests +{ + public class BlockTipModelTests + { + public static IBlockTip PrepareBlockTip() + { + var blockHash = TestingValues.GetUint256(); + var blockHeight = TestingValues.GetPositiveInt(); + + var blockTip = new BlockTipModel(blockHash, blockHeight); + return blockTip; + } + + [Fact] + public void ShouldSerialiseAsJson() + { + var blockTip = PrepareBlockTip(); + var asJson = blockTip.ToString(); + + var reconverted = JsonConvert.DeserializeObject(asJson); + + reconverted.Hash.Should().Be(blockTip.Hash); + reconverted.Height.Should().Be(blockTip.Height); + } + } +}