public void TestGetPrivateData()
        {
            ChaincodeStub stub = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), null);

            byte[] value = new byte[] { 0x10, 0x20, 0x30 };
            handler.Setup(a => a.GetStateAsync("myc", "txId", "testcoll", "key", token)).ReturnsAsync(ByteString.CopyFrom(value));
            CollectionAssert.AreEqual(stub.GetPrivateData("testcoll", "key"), value);
            try
            {
                stub.GetPrivateData(null, "key");
                Assert.Fail("Null collection check fails");
            }
            catch (ArgumentException)
            {
                //ignored
            }

            try
            {
                stub.GetPrivateData("", "key");
                Assert.Fail("Empty collection check fails");
            }
            catch (ArgumentException)
            {
                //ignored
            }
        }
        public void TestDelPrivateState()
        {
            ChaincodeStub stub = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), null);

            stub.DelPrivateData("testcoll", "key");
            handler.Verify(a => a.DeleteStateAsync("myc", "txId", "testcoll", "key", token));
            try
            {
                stub.DelPrivateData(null, "key");
                Assert.Fail("Null collection check fails");
            }
            catch (ArgumentException)
            {
                //ignored
            }

            try
            {
                stub.DelPrivateData("", "key");
                Assert.Fail("Empty collection check fails");
            }
            catch (ArgumentException)
            {
                //ignored
            }
        }
        public void TestGetPrivateDataValidationParameter()
        {
            ChaincodeStub stub = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), null);

            byte[] value = new byte[] { 0x10, 0x20, 0x30 };
            Dictionary <string, ByteString> metaMap = new Dictionary <string, ByteString>();

            metaMap[ChaincodeStub.VALIDATION_PARAMETER] = ByteString.CopyFrom(value);

            handler.Setup(a => a.GetStateMetadataAsync("myc", "txId", "testcoll", "key", token)).ReturnsAsync(metaMap);
            CollectionAssert.AreEqual(stub.GetPrivateDataValidationParameter("testcoll", "key"), value);

            handler.Setup(a => a.GetStateMetadataAsync("myc", "txId", "testcoll", "key2", token)).ReturnsAsync(new Dictionary <string, ByteString>());
            Assert.IsNull(stub.GetPrivateDataValidationParameter("testcoll", "key2"));

            try
            {
                stub.GetPrivateDataValidationParameter(null, "key");
                Assert.Fail("Null collection check fails");
            }
            catch (ArgumentException)
            {
                //ignored
            }

            try
            {
                stub.GetPrivateDataValidationParameter("", "key");
                Assert.Fail("Empty collection check fails");
            }
            catch (ArgumentException)
            {
                //ignored
            }
        }
        public void TestSetStateValidationParameter()
        {
            ChaincodeStub stub = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), null);

            byte[] value = new byte[] { 0x10, 0x20, 0x30 };
            stub.SetStateValidationParameter("key", value);
            handler.Verify((a) => a.PutStateMetadataAsync("myc", "txId", "", "key", ChaincodeStub.VALIDATION_PARAMETER, ByteString.CopyFrom(value), token));
            try
            {
                stub.SetStateValidationParameter(null, value);
                Assert.Fail("Null key check fails");
            }
            catch (ArgumentException)
            {
                //ignored
            }

            try
            {
                stub.SetStateValidationParameter("", value);
                Assert.Fail("Empty key check fails");
            }
            catch (ArgumentException)
            {
                //ignored
            }
        }
        public void TestDelState()
        {
            ChaincodeStub stub = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), null);

            stub.DelState("key");
            handler.Verify((a) => a.DeleteStateAsync("myc", "txId", "", "key", token));
        }
        public void TestPutState()
        {
            ChaincodeStub stub = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), null);

            byte[] value = new byte[] { 0x10, 0x20, 0x30 };
            stub.PutState("key", value);
            handler.Verify((a) => a.PutStateAsync("myc", "txId", "", "key", ByteString.CopyFrom(value), token));
            try
            {
                stub.PutState(null, value);
                Assert.Fail("Null key check fails");
            }
            catch (ArgumentException)
            {
                //Ignore
            }

            try
            {
                stub.PutState("", value);
                Assert.Fail("Empty key check fails");
            }
            catch (ArgumentException)
            {
                //Ignore
            }
        }
        public void TestGetStateByPartialCompositeKeyWithPagination()
        {
            QueryMetadata queryMetadata = new QueryMetadata {
                Bookmark = "aaaa", PageSize = 1
            };

            ChaincodeStub stub = PrepareStubAndMockHandler(true, queryMetadata.ToByteString());

            CompositeKey key = new CompositeKey("KEY");
            IQueryResultsEnumerable <IKeyValue> queryResultsIterator = stub.GetStateByPartialCompositeKeyWithPagination(key, 1, "aaaa");

            queryResultsIterator.First(); //Otherwise GetStateByRangeAsync will not be executed, that Or GetMetadata()
            handler.Verify(a => a.GetStateByRangeAsync("myc", "txId", "", key.ToString(), key.ToString() + "\udbff\udfff", queryMetadata.ToByteString(), It.IsAny <CancellationToken>()));
            Assert.AreEqual(queryResultsIterator.GetMetadata().FetchedRecordsCount, 2);
            Assert.AreEqual(queryResultsIterator.GetMetadata().Bookmark, "bbbb");


            key = new CompositeKey("");
            queryResultsIterator = stub.GetStateByPartialCompositeKeyWithPagination(key, 1, "aaaa");
            queryResultsIterator.First(); //Otherwise GetStateByRangeAsync will not be executed, that Or GetMetadata()

            handler.Verify(a => a.GetStateByRangeAsync("myc", "txId", "", key.ToString(), key.ToString() + "\udbff\udfff", queryMetadata.ToByteString(), It.IsAny <CancellationToken>()));
            Assert.AreEqual(queryResultsIterator.GetMetadata().FetchedRecordsCount, 2);
            Assert.AreEqual(queryResultsIterator.GetMetadata().Bookmark, "bbbb");
        }
        public void TestGetHistoryForKeyWithException()
        {
            string        txId = "txId", key = "KEY", channelId = "myc";
            ChaincodeStub stub  = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), null);
            QueryResponse value = new QueryResponse {
                HasMore = false
            };

            value.Results.Add(new QueryResultBytes {
                ResultBytes = ByteString.CopyFromUtf8("exception")
            });

            handler.Setup(a => a.GetHistoryForKeyAsync(channelId, txId, key, token)).ReturnsAsync(value);
            try
            {
                stub.GetHistoryForKey(key).First();
            }
            catch (Exception e)
            {
                if (e is InvalidProtocolBufferException)
                {
                    throw;
                }
                if (e.InnerException != null)
                {
                    throw e.InnerException;
                }
                throw;
            }
        }
        public void TestGetStateByPartialCompositeKey_withAttributesAsString()
        {
            ChaincodeStub stub = PrepareStubAndMockHandler();
            CompositeKey  cKey = new CompositeKey("KEY", "attr1", "attr2");

            stub.GetStateByPartialCompositeKey(cKey.ToString()).First();
            handler.Verify(a => a.GetStateByRangeAsync("myc", "txId", "", cKey.ToString(), cKey.ToString() + "\udbff\udfff", null, token));
        }
        public void TestGetPrivateDataByPartialCompositeKey_withAttributesWithSplittedParams()
        {
            ChaincodeStub stub = PrepareStubAndMockHandler();
            CompositeKey  cKey = new CompositeKey("KEY", "attr1", "attr2", "attr3");

            stub.GetPrivateDataByPartialCompositeKey(TEST_COLLECTION, "KEY", "attr1", "attr2", "attr3").First(); //GetFirst Otherwise no deal
            handler.Verify(a => a.GetStateByRangeAsync("myc", "txId", TEST_COLLECTION, cKey.ToString(), cKey.ToString() + "\udbff\udfff", null, token));
        }
        public void TestGetState()
        {
            ChaincodeStub stub = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), null);

            byte[] value = new byte[] { 0x10, 0x20, 0x30 };
            handler.Setup((a) => a.GetStateAsync("myc", "txId", "", "key", token)).ReturnsAsync(ByteString.CopyFrom(value));
            CollectionAssert.AreEqual(stub.GetState("key"), value);
        }
        public void TestGetStringPrivateData()
        {
            ChaincodeStub stub  = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), null);
            string        value = "TEST";

            handler.Setup(a => a.GetStateAsync("myc", "txId", "testcoll", "key", token)).ReturnsAsync(ByteString.CopyFromUtf8(value));
            Assert.AreEqual(stub.GetPrivateDataUTF8("testcoll", "key"), value);
        }
        public void TestPutStringPrivateData()
        {
            ChaincodeStub stub  = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), null);
            string        value = "TEST";

            stub.PutPrivateData("testcoll", "key", value);
            handler.Verify(a => a.PutStateAsync("myc", "txId", "testcoll", "key", ByteString.CopyFromUtf8(value), token));
        }
        public void TestGetBindingEmptyProposal()
        {
            SignedProposal signedProposal = new SignedProposal {
                ProposalBytes = ByteString.Empty
            };
            ChaincodeStub stub = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), signedProposal);

            Assert.IsNull(stub.Binding);
        }
        public void TestGetSignedProposalWithEmptyProposal()
        {
            SignedProposal signedProposal = new SignedProposal {
                ProposalBytes = ByteString.Empty
            };
            ChaincodeStub stub = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), signedProposal);

            Assert.AreEqual(stub.SignedProposal, signedProposal);
        }
        public void TestCreateCompositeKey()
        {
            ChaincodeStub stub = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), null);
            CompositeKey  key  = stub.CreateCompositeKey("abc", "def", "ghi", "jkl", "mno");

            Assert.AreEqual(key.ObjectType, "abc");
            Assert.AreEqual(key.Attributes.Count, 4);
            Assert.AreEqual(key.ToString(), "\u0000abc\u0000def\u0000ghi\u0000jkl\u0000mno\u0000");
        }
        public void TestGetArgs()
        {
            List <ByteString> args = new List <ByteString>()
            {
                ByteString.CopyFromUtf8("arg0"), ByteString.CopyFromUtf8("arg1"), ByteString.CopyFromUtf8("arg2")
            };
            ChaincodeStub stub = new ChaincodeStub("myc", "txId", handler.Object, args, null);

            Assert.That.ContainsArray(stub.Args, args.Select(a => a.ToByteArray()));
        }
        public void TestGetFunction()
        {
            List <ByteString> args = new List <ByteString>()
            {
                ByteString.CopyFromUtf8("function"), ByteString.CopyFromUtf8("arg0"), ByteString.CopyFromUtf8("arg1")
            };
            ChaincodeStub stub = new ChaincodeStub("myc", "txId", handler.Object, args, null);

            Assert.AreEqual(stub.Function, "function");
        }
        public void TestSplitCompositeKey()
        {
            ChaincodeStub stub = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), null);
            CompositeKey  key  = stub.SplitCompositeKey("\u0000abc\u0000def\u0000ghi\u0000jkl\u0000mno\u0000");

            Assert.AreEqual(key.ObjectType, "abc");
            Assert.That.Contains(key.Attributes, new List <string> {
                "def", "ghi", "jkl", "mno"
            });
            Assert.AreEqual(key.ToString(), "\u0000abc\u0000def\u0000ghi\u0000jkl\u0000mno\u0000");
        }
        public void TestInvokeChaincode()
        {
            string        txId = "txId", chaincodeName = "CHAINCODE_ID", channel = "CHAINCODE_CHANNEL";
            ChaincodeStub stub             = new ChaincodeStub(channel, txId, handler.Object, new List <ByteString>(), null);
            Response      expectedResponse = new Response(Status.SUCCESS, "MESSAGE", "PAYLOAD".ToBytes());

            handler.Setup(a => a.InvokeChaincodeAsync(channel, txId, chaincodeName, new List <byte[]>(), token)).ReturnsAsync(expectedResponse);
            Assert.AreEqual(stub.InvokeChaincode(chaincodeName, new List <byte[]>()), expectedResponse);
            handler.Setup(a => a.InvokeChaincodeAsync(It.Is <string>(b => b == channel), It.Is <string>(b => b == txId), It.Is <string>(b => b == chaincodeName + "/" + channel), It.IsAny <List <byte[]> >(), token)).ReturnsAsync(expectedResponse);
            Assert.AreEqual(stub.InvokeChaincode(chaincodeName, new List <byte[]>(), channel), expectedResponse);
        }
        public void TestGetStateByRange()
        {
            ChaincodeStub stub     = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), null);
            string        startKey = "START";
            string        endKey   = "END";

            KV[]          keyValues = PrepareKeyValuePairs(2);
            QueryResponse value     = PrepareQueryResponseForRange(keyValues, false);

            handler.Setup((a) => a.GetStateByRangeAsync("myc", "txId", "", startKey, endKey, null, token)).ReturnsAsync(value);
            Assert.That.Contains(stub.GetStateByRange(startKey, endKey), keyValues.Select(a => new KeyValue(a)));
        }
        public void TestGetStateByPartialCompositeKey_withCompositeKey()
        {
            ChaincodeStub stub = PrepareStubAndMockHandler();

            CompositeKey key = new CompositeKey("KEY");

            stub.GetStateByPartialCompositeKey(key).First();
            handler.Verify(a => a.GetStateByRangeAsync("myc", "txId", "", key.ToString(), key.ToString() + "\udbff\udfff", null, token));
            key = new CompositeKey("");
            stub.GetStateByPartialCompositeKey(key).First();
            handler.Verify(a => a.GetStateByRangeAsync("myc", "txId", "", key.ToString(), key.ToString() + "\udbff\udfff", null, token));
        }
        public void TestGetParameters()
        {
            List <ByteString> args = new List <ByteString>()
            {
                ByteString.CopyFromUtf8("function"), ByteString.CopyFromUtf8("arg0"), ByteString.CopyFromUtf8("arg1")
            };
            ChaincodeStub stub = new ChaincodeStub("myc", "txId", handler.Object, args, null);

            Assert.That.Contains(stub.Parameters, new List <string> {
                "arg0", "arg1"
            });
        }
        public void TestGetStateValidationParameter()
        {
            ChaincodeStub stub = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), null);

            byte[] value = new byte[] { 0x10, 0x20, 0x30 };
            Dictionary <string, ByteString> metaMap = new Dictionary <string, ByteString>();

            metaMap[ChaincodeStub.VALIDATION_PARAMETER] = ByteString.CopyFrom(value);
            handler.Setup((a) => a.GetStateMetadataAsync("myc", "txId", "", "key", token)).ReturnsAsync(metaMap);
            CollectionAssert.AreEqual(stub.GetStateValidationParameter("key"), value);
            handler.Setup((a) => a.GetStateMetadataAsync("myc", "txId", "", "key2", token)).ReturnsAsync(new Dictionary <string, ByteString>());
            Assert.IsNull(stub.GetStateValidationParameter("key2"));
        }
        public void TestGetPrivateDataByPartialCompositeKey()
        {
            ChaincodeStub stub = PrepareStubAndMockHandler();

            CompositeKey key = new CompositeKey("KEY");

            stub.GetPrivateDataByPartialCompositeKey(TEST_COLLECTION, "KEY").First();
            handler.Verify(a => a.GetStateByRangeAsync("myc", "txId", TEST_COLLECTION, key.ToString(), key.ToString() + "\udbff\udfff", null, token));
            key = new CompositeKey("");
            stub.GetPrivateDataByPartialCompositeKey(TEST_COLLECTION, (string)null).First();
            stub.GetPrivateDataByPartialCompositeKey(TEST_COLLECTION, "").First();
            handler.Verify(a => a.GetStateByRangeAsync("myc", "txId", TEST_COLLECTION, key.ToString(), key.ToString() + "\udbff\udfff", null, token), Times.AtLeast(2));
        }
        public void TestGetSignedProposal()
        {
            SignedProposal signedProposal = new SignedProposal {
                ProposalBytes = new Proposal {
                    Header = new Header {
                        ChannelHeader = new ChannelHeader {
                            Type = (int)HeaderType.EndorserTransaction, Timestamp = new Timestamp()
                        }.ToByteString()
                    }.ToByteString()
                }.ToByteString()
            };
            ChaincodeStub stub = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), signedProposal);

            Assert.AreEqual(stub.SignedProposal, signedProposal);
        }
        public void TestGetTxTimestamp()
        {
            DateTimeOffset?instant        = DateTimeOffset.Now;
            Timestamp      timestamp      = Timestamp.FromDateTimeOffset(instant.Value);
            SignedProposal signedProposal = new SignedProposal {
                ProposalBytes = new Proposal {
                    Header = new Header {
                        ChannelHeader = new ChannelHeader {
                            Type = (int)HeaderType.EndorserTransaction, Timestamp = timestamp
                        }.ToByteString()
                    }.ToByteString()
                }.ToByteString()
            };
            ChaincodeStub stub = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), signedProposal);

            Assert.AreEqual(stub.TxTimestamp, instant);
        }
        public void TestSetGetEvent()
        {
            ChaincodeStub stub = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), null);

            byte[] payload   = new byte[] { 0x10, 0x20, 0x20 };
            string eventName = "event_name";

            stub.SetEvent(eventName, payload);
            ChaincodeEvent evnt = stub.Event;

            Assert.AreEqual(evnt.EventName, eventName);
            Assert.AreEqual(evnt.Payload, ByteString.CopyFrom(payload));
            stub.SetEvent(eventName, null);
            evnt = stub.Event;
            Assert.IsNotNull(evnt);
            Assert.AreEqual(evnt.EventName, eventName);
            Assert.AreEqual(evnt.Payload, ByteString.CopyFrom(new byte[0]));
        }
        public void TestGetPrivateDataByRange()
        {
            ChaincodeStub stub     = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), null);
            string        startKey = "START";
            string        endKey   = "END";

            KV[] keyValues = new KV[] { new KV {
                                            Key = "A", Value = ByteString.CopyFromUtf8("Value of A")
                                        }, new KV {
                                            Key = "B", Value = ByteString.CopyFromUtf8("Value of B")
                                        } };
            QueryResponse value = new QueryResponse {
                HasMore = false
            };

            value.Results.Add(new QueryResultBytes {
                ResultBytes = keyValues[0].ToByteString()
            });
            value.Results.Add(new QueryResultBytes {
                ResultBytes = keyValues[1].ToByteString()
            });
            handler.Setup(a => a.GetStateByRangeAsync("myc", "txId", "testcoll", startKey, endKey, null, token)).ReturnsAsync(value);
            Assert.That.Contains(stub.GetPrivateDataByRange("testcoll", startKey, endKey), keyValues.Select(a => new KeyValue(a)));

            try
            {
                stub.GetPrivateDataByRange(null, startKey, endKey);
                Assert.Fail("Null collection check fails");
            }
            catch (ArgumentException)
            {
                //ignored
            }

            try
            {
                stub.GetPrivateDataByRange("", startKey, endKey);
                Assert.Fail("Empty collection check fails");
            }
            catch (ArgumentException)
            {
                //ignored
            }
        }
        public void TestGetBinding()
        {
            byte[] expectedDigest = "5093dd4f4277e964da8f4afbde0a9674d17f2a6a5961f0670fc21ae9b67f2983".FromHexString();

            SignedProposal signedProposal = new SignedProposal {
                ProposalBytes = new Proposal {
                    Header = new Header {
                        ChannelHeader = new ChannelHeader {
                            Type = (int)HeaderType.EndorserTransaction, Timestamp = new Timestamp(), Epoch = 10
                        }.ToByteString(), SignatureHeader = new SignatureHeader {
                            Nonce = ByteString.CopyFromUtf8("nonce"), Creator = ByteString.CopyFromUtf8("creator")
                        }.ToByteString()
                    }.ToByteString(),
                }.ToByteString()
            };
            ChaincodeStub stub = new ChaincodeStub("myc", "txId", handler.Object, new List <ByteString>(), signedProposal);

            CollectionAssert.AreEqual(stub.Binding, expectedDigest);
        }