public void BuyItem_Fails_If_Buyer_Is_Seller()
        {
            var seller = PartyA;

            this.mockPersistentState.Setup(s => s.GetAddress(nameof(ItemListing.Seller))).Returns(seller);

            this.mockContractState.Setup(s => s.Message.Sender).Returns(ParentContract);
            var itemListing = new ItemListing(this.mockContractState.Object, "Test", 1, seller, ParentContract, PartyA, PartyB);

            // Make the sender the seller.
            this.mockContractState.Setup(s => s.Message.Sender).Returns(seller);

            Assert.Throws <SmartContractAssertException>(() => itemListing.BuyItem());
        }
        public void BuyItem_Fails_If_Buyer_Balance_Insufficient()
        {
            var seller    = PartyA;
            var buyer     = PartyB;
            var itemPrice = 1UL;

            // Setup the expected state values.
            this.mockPersistentState.Setup(s => s.GetUInt64(nameof(ItemListing.ItemPrice))).Returns(itemPrice);
            this.mockPersistentState.Setup(s => s.GetAddress(nameof(ItemListing.Seller))).Returns(seller);
            this.mockPersistentState.Setup(s => s.GetAddress(nameof(ItemListing.ParentContract))).Returns(ParentContract);

            this.mockContractState.Setup(s => s.Message.Sender).Returns(ParentContract);
            var itemListing = new ItemListing(this.mockContractState.Object, "Test", itemPrice, seller, ParentContract, PartyA, PartyB);

            this.mockInternalExecutor
            .Setup(c => c.Call(
                       It.IsAny <ISmartContractState>(),
                       It.IsAny <Address>(),
                       It.IsAny <ulong>(),
                       nameof(Bazaar.HasBalance),
                       It.IsAny <object[]>(),
                       It.IsAny <ulong>()))
            .Returns(TransferResult.Transferred(false));

            this.mockContractState.Setup(s => s.Message.Sender).Returns(buyer);

            Assert.Throws <SmartContractAssertException>(() => itemListing.BuyItem());

            this.mockInternalExecutor
            .Verify(
                c => c.Call(
                    It.IsAny <ISmartContractState>(),
                    ParentContract,
                    0,
                    nameof(Bazaar.HasBalance),
                    It.Is <object[]>(o => (Address)o[0] == buyer && (ulong)o[1] == itemPrice),
                    0),
                Times.Once);
        }
        public void BuyItem_Succeeds()
        {
            var seller    = PartyA;
            var buyer     = PartyB;
            var itemPrice = 1UL;

            // Setup the expected state values.
            this.mockPersistentState.Setup(s => s.GetUInt64(nameof(ItemListing.ItemPrice))).Returns(itemPrice);
            this.mockPersistentState.Setup(s => s.GetAddress(nameof(ItemListing.Seller))).Returns(seller);
            this.mockPersistentState.Setup(s => s.GetAddress(nameof(ItemListing.ParentContract))).Returns(ParentContract);

            this.mockContractState.Setup(s => s.Message.Sender).Returns(ParentContract);
            var itemListing = new ItemListing(this.mockContractState.Object, "Test", itemPrice, seller, ParentContract, PartyA, PartyB);

            // HasBalance returns true.
            this.mockInternalExecutor
            .Setup(c => c.Call(
                       It.IsAny <ISmartContractState>(),
                       It.IsAny <Address>(),
                       It.IsAny <ulong>(),
                       nameof(Bazaar.HasBalance),
                       It.IsAny <object[]>(),
                       It.IsAny <ulong>()))
            .Returns(TransferResult.Transferred(true));

            // UpdateBalance is successful.
            this.mockInternalExecutor
            .Setup(c => c.Call(
                       It.IsAny <ISmartContractState>(),
                       It.IsAny <Address>(),
                       It.IsAny <ulong>(),
                       nameof(Bazaar.UpdateBalance),
                       It.IsAny <object[]>(),
                       It.IsAny <ulong>()))
            .Returns(TransferResult.Empty());

            this.mockContractState.Setup(s => s.Message.Sender).Returns(buyer);

            itemListing.BuyItem();

            this.mockInternalExecutor
            .Verify(
                c => c.Call(
                    It.IsAny <ISmartContractState>(),
                    ParentContract,
                    0,
                    nameof(Bazaar.HasBalance),
                    It.Is <object[]>(o => (Address)o[0] == buyer && (ulong)o[1] == itemPrice),
                    0),
                Times.Once);

            this.mockInternalExecutor
            .Verify(
                c => c.Call(
                    It.IsAny <ISmartContractState>(),
                    ParentContract,
                    0,
                    nameof(Bazaar.UpdateBalance),
                    It.Is <object[]>(o => (Address)o[0] == seller && (Address)o[1] == buyer && (ulong)o[2] == itemPrice),
                    0),
                Times.Once);

            this.mockPersistentState.Verify(s => s.SetUInt32(nameof(Bazaar.State), (uint)ItemListing.StateType.ItemSold));
        }