github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/examples/chaincode/go/chaincode_example04/chaincode_example04_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  package main
    17  
    18  import (
    19  	"fmt"
    20  	"testing"
    21  
    22  	"github.com/hyperledger/fabric/core/chaincode/shim"
    23  	ex02 "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"
    24  )
    25  
    26  // this is the response to any successful Invoke() on chaincode_example04
    27  var eventResponse = "{\"Name\":\"Event\",\"Amount\":\"1\"}"
    28  
    29  func checkInit(t *testing.T, stub *shim.MockStub, args [][]byte) {
    30  	res := stub.MockInit("1", args)
    31  	if res.Status != shim.OK {
    32  		fmt.Println("Init failed", string(res.Message))
    33  		t.FailNow()
    34  	}
    35  }
    36  
    37  func checkState(t *testing.T, stub *shim.MockStub, name string, value string) {
    38  	bytes := stub.State[name]
    39  	if bytes == nil {
    40  		fmt.Println("State", name, "failed to get value")
    41  		t.FailNow()
    42  	}
    43  	if string(bytes) != value {
    44  		fmt.Println("State value", name, "was not", value, "as expected")
    45  		t.FailNow()
    46  	}
    47  }
    48  
    49  func checkQuery(t *testing.T, stub *shim.MockStub, name string, value string) {
    50  	res := stub.MockInvoke("1", [][]byte{[]byte("query"), []byte(name)})
    51  	if res.Status != shim.OK {
    52  		fmt.Println("Query", name, "failed", string(res.Message))
    53  		t.FailNow()
    54  	}
    55  	if res.Payload == nil {
    56  		fmt.Println("Query", name, "failed to get value")
    57  		t.FailNow()
    58  	}
    59  	if string(res.Payload) != value {
    60  		fmt.Println("Query value", name, "was not", value, "as expected")
    61  		t.FailNow()
    62  	}
    63  }
    64  
    65  func checkInvoke(t *testing.T, stub *shim.MockStub, args [][]byte) {
    66  	res := stub.MockInvoke("1", args)
    67  	if res.Status != shim.OK {
    68  		fmt.Println("Invoke", args, "failed", string(res.Message))
    69  		t.FailNow()
    70  	}
    71  }
    72  
    73  func TestExample04_Init(t *testing.T) {
    74  	scc := new(SimpleChaincode)
    75  	stub := shim.NewMockStub("ex04", scc)
    76  
    77  	// Init A=123 B=234
    78  	checkInit(t, stub, [][]byte{[]byte("init"), []byte("Event"), []byte("123")})
    79  
    80  	checkState(t, stub, "Event", "123")
    81  }
    82  
    83  func TestExample04_Query(t *testing.T) {
    84  	scc := new(SimpleChaincode)
    85  	stub := shim.NewMockStub("ex04", scc)
    86  
    87  	// Init A=345 B=456
    88  	checkInit(t, stub, [][]byte{[]byte("init"), []byte("Event"), []byte("1")})
    89  
    90  	// Query A
    91  	checkQuery(t, stub, "Event", eventResponse)
    92  }
    93  
    94  func TestExample04_Invoke(t *testing.T) {
    95  	scc := new(SimpleChaincode)
    96  	stub := shim.NewMockStub("ex04", scc)
    97  
    98  	chaincodeToInvoke := "ex02"
    99  
   100  	ccEx2 := new(ex02.SimpleChaincode)
   101  	stubEx2 := shim.NewMockStub(chaincodeToInvoke, ccEx2)
   102  	checkInit(t, stubEx2, [][]byte{[]byte("init"), []byte("a"), []byte("111"), []byte("b"), []byte("222")})
   103  	stub.MockPeerChaincode(chaincodeToInvoke, stubEx2)
   104  
   105  	// Init A=567 B=678
   106  	checkInit(t, stub, [][]byte{[]byte("init"), []byte("Event"), []byte("1")})
   107  
   108  	// Invoke A->B for 10 via Example04's chaincode
   109  	checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte(chaincodeToInvoke), []byte("Event"), []byte("1")})
   110  	checkQuery(t, stub, "Event", eventResponse)
   111  	checkQuery(t, stubEx2, "a", "101")
   112  	checkQuery(t, stubEx2, "b", "232")
   113  
   114  	// Invoke A->B for 10 via Example04's chaincode
   115  	checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte(chaincodeToInvoke), []byte("Event"), []byte("1")})
   116  	checkQuery(t, stub, "Event", eventResponse)
   117  	checkQuery(t, stubEx2, "a", "91")
   118  	checkQuery(t, stubEx2, "b", "242")
   119  }