Skip to content

Commit

Permalink
[FAB-3435] Test case to validate transient data
Browse files Browse the repository at this point in the history
Change-Id: I51c07814d704ac454647d8897df13f26fc4d2434
Signed-off-by: biljana lukovic <biljana.lukovic@securekey.com>
  • Loading branch information
biljanaLukovic committed Apr 28, 2017
1 parent ec9d2ef commit f55b585
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 16 deletions.
4 changes: 2 additions & 2 deletions fabric-client/helpers/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ import (

// CreateAndSendTransactionProposal combines create and send transaction proposal methods into one method.
// See CreateTransactionProposal and SendTransactionProposal
func CreateAndSendTransactionProposal(chain fabricClient.Chain, chainCodeID string, chainID string, args []string, targets []fabricClient.Peer) ([]*fabricClient.TransactionProposalResponse, string, error) {
func CreateAndSendTransactionProposal(chain fabricClient.Chain, chainCodeID string, chainID string, args []string, targets []fabricClient.Peer, transientData map[string][]byte) ([]*fabricClient.TransactionProposalResponse, string, error) {

signedProposal, err := chain.CreateTransactionProposal(chainCodeID, chainID, args, true, nil)
signedProposal, err := chain.CreateTransactionProposal(chainCodeID, chainID, args, true, transientData)
if err != nil {
return nil, "", fmt.Errorf("SendTransactionProposal return error: %v", err)
}
Expand Down
14 changes: 12 additions & 2 deletions test/fixtures/src/github.com/example_cc/example_cc.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Error(err.Error())
}

if transientMap, err := stub.GetTransient(); err == nil {
if transientData, ok := transientMap["result"]; ok {
fmt.Printf("Transient data in 'init' : %s\n", transientData)
return shim.Success(transientData)
}
}
return shim.Success(nil)

}
Expand Down Expand Up @@ -111,7 +117,6 @@ func (t *SimpleChaincode) move(stub shim.ChaincodeStubInterface, args []string)
var Aval, Bval int // Asset holdings
var X int // Transaction value
var err error

if len(args) != 4 {
return shim.Error("Incorrect number of arguments. Expecting 4, function followed by 2 names and 1 value")
}
Expand Down Expand Up @@ -159,6 +164,12 @@ func (t *SimpleChaincode) move(stub shim.ChaincodeStubInterface, args []string)
return shim.Error(err.Error())
}

if transientMap, err := stub.GetTransient(); err == nil {
if transientData, ok := transientMap["result"]; ok {
fmt.Printf("Transient data in 'move' : %s\n", transientData)
return shim.Success(transientData)
}
}
return shim.Success(nil)
}

Expand All @@ -181,7 +192,6 @@ func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string

// Query callback representing the query of a chaincode
func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {

var A string // Entities
var err error

Expand Down
15 changes: 7 additions & 8 deletions test/integration/base_test_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,10 @@ func (setup *BaseSetupImpl) InstallAndInstantiateExampleCC() error {

// Query ...
func (setup *BaseSetupImpl) Query(chainID string, chainCodeID string, args []string) (string, error) {

transactionProposalResponses, _, err := fcUtil.CreateAndSendTransactionProposal(setup.Chain, chainCodeID, chainID, args, []fabricClient.Peer{setup.Chain.GetPrimaryPeer()})
transactionProposalResponses, _, err := fcUtil.CreateAndSendTransactionProposal(setup.Chain, chainCodeID, chainID, args, []fabricClient.Peer{setup.Chain.GetPrimaryPeer()}, nil)
if err != nil {
return "", fmt.Errorf("CreateAndSendTransactionProposal return error: %v", err)
}

return string(transactionProposalResponses[0].GetResponsePayload()), nil
}

Expand All @@ -175,7 +173,6 @@ func (setup *BaseSetupImpl) QueryAsset() (string, error) {
args = append(args, "invoke")
args = append(args, "query")
args = append(args, "b")

return setup.Query(setup.ChainID, setup.ChainCodeID, args)
}

Expand All @@ -189,19 +186,21 @@ func (setup *BaseSetupImpl) MoveFunds() (string, error) {
args = append(args, "b")
args = append(args, "1")

transactionProposalResponse, txID, err := fcUtil.CreateAndSendTransactionProposal(setup.Chain, setup.ChainCodeID, setup.ChainID, args, []fabricClient.Peer{setup.Chain.GetPrimaryPeer()})
transientDataMap := make(map[string][]byte)
transientDataMap["result"] = []byte("Transient data in move funds...")

transactionProposalResponse, txID, err := fcUtil.CreateAndSendTransactionProposal(setup.Chain, setup.ChainCodeID, setup.ChainID, args, []fabricClient.Peer{setup.Chain.GetPrimaryPeer()}, transientDataMap)
if err != nil {
return "", fmt.Errorf("CreateAndSendTransactionProposal return error: %v", err)
}

// Register for commit event
done, fail := fcUtil.RegisterTxEvent(txID, setup.EventHub)

_, err = fcUtil.CreateAndSendTransaction(setup.Chain, transactionProposalResponse)
txResponse, err := fcUtil.CreateAndSendTransaction(setup.Chain, transactionProposalResponse)
if err != nil {
return "", fmt.Errorf("CreateAndSendTransaction return error: %v", err)
}

fmt.Println(txResponse)
select {
case <-done:
case <-fail:
Expand Down
1 change: 0 additions & 1 deletion test/integration/end_to_end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func TestChainCodeInvoke(t *testing.T) {
valueAfterInvokeInt, _ := strconv.Atoi(valueAfterInvoke)
if valueInt != valueAfterInvokeInt {
t.Fatalf("SendTransaction didn't change the QueryValue")

}

}
5 changes: 2 additions & 3 deletions test/integration/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,18 @@ func TestEvents(t *testing.T) {
}

func testFailedTx(t *testing.T, testSetup BaseSetupImpl) {

// Arguments for events CC
var args []string
args = append(args, "invoke")
args = append(args, "invoke")
args = append(args, "SEVERE")

tpResponses1, tx1, err := fcUtil.CreateAndSendTransactionProposal(testSetup.Chain, testSetup.ChainCodeID, testSetup.ChainID, args, []fabricClient.Peer{testSetup.Chain.GetPrimaryPeer()})
tpResponses1, tx1, err := fcUtil.CreateAndSendTransactionProposal(testSetup.Chain, testSetup.ChainCodeID, testSetup.ChainID, args, []fabricClient.Peer{testSetup.Chain.GetPrimaryPeer()}, nil)
if err != nil {
t.Fatalf("CreateAndSendTransactionProposal return error: %v \n", err)
}

tpResponses2, tx2, err := fcUtil.CreateAndSendTransactionProposal(testSetup.Chain, testSetup.ChainCodeID, testSetup.ChainID, args, []fabricClient.Peer{testSetup.Chain.GetPrimaryPeer()})
tpResponses2, tx2, err := fcUtil.CreateAndSendTransactionProposal(testSetup.Chain, testSetup.ChainCodeID, testSetup.ChainID, args, []fabricClient.Peer{testSetup.Chain.GetPrimaryPeer()}, nil)
if err != nil {
t.Fatalf("CreateAndSendTransactionProposal return error: %v \n", err)
}
Expand Down
64 changes: 64 additions & 0 deletions test/integration/transient_data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package integration

import (
"testing"

fabricClient "github.com/hyperledger/fabric-sdk-go/fabric-client"
fcUtil "github.com/hyperledger/fabric-sdk-go/fabric-client/helpers"
)

// TestTransient ...
func TestTransient(t *testing.T) {

testSetup := BaseSetupImpl{
ConfigFile: "../fixtures/config/config_test.yaml",
ChainID: "testchannel",
ChannelConfig: "../fixtures/channel/testchannel.tx",
ConnectEventHub: true,
}

if err := testSetup.Initialize(); err != nil {
t.Fatalf(err.Error())
}

if err := testSetup.InstallAndInstantiateExampleCC(); err != nil {
t.Fatalf("InstallAndInstantiateExampleCC return error: %v", err)
}

var args []string
args = append(args, "invoke")
args = append(args, "move")
args = append(args, "a")
args = append(args, "b")
args = append(args, "0")
transientData := "Transient data test..."

transientDataMap := make(map[string][]byte)
transientDataMap["result"] = []byte(transientData)

transactionProposalResponse, _, err := fcUtil.CreateAndSendTransactionProposal(testSetup.Chain, testSetup.ChainCodeID, testSetup.ChainID, args, []fabricClient.Peer{testSetup.Chain.GetPrimaryPeer()}, transientDataMap)
if err != nil {
t.Fatalf("CreateAndSendTransactionProposal return error: %v", err)
}
strResponse := string(transactionProposalResponse[0].GetResponsePayload())
//validate transient data exists in proposal
if len(strResponse) == 0 {
t.Fatalf("Transient data does not exist: expected %s", transientData)
}
//verify transient data content
if strResponse != transientData {
t.Fatalf("Expected '%s' in transient data field. Received '%s' ", transientData, strResponse)
}
//transient data null
transientDataMap["result"] = []byte{}
transactionProposalResponse, _, err = fcUtil.CreateAndSendTransactionProposal(testSetup.Chain, testSetup.ChainCodeID, testSetup.ChainID, args, []fabricClient.Peer{testSetup.Chain.GetPrimaryPeer()}, transientDataMap)
if err != nil {
t.Fatalf("CreateAndSendTransactionProposal with empty transient data return an error: %v", err)
}
//validate that transient data does not exist in proposal
strResponse = string(transactionProposalResponse[0].GetResponsePayload())
if len(strResponse) != 0 {
t.Fatalf("Transient data validation has failed. An empty transient data was expected but %s was returned", strResponse)
}

}

0 comments on commit f55b585

Please sign in to comment.