github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/examples/chaincode/go/chaincode_example05/chaincode_example05_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  var chaincodeName = "ex02"
    27  
    28  // chaincode_example05 looks like it wanted to return a JSON response to Query()
    29  // it doesn't actually do this though, it just returns the sum value
    30  func jsonResponse(name string, value string) string {
    31  	return fmt.Sprintf("jsonResponse = \"{\"Name\":\"%v\",\"Value\":\"%v\"}", name, value)
    32  }
    33  
    34  func checkInit(t *testing.T, stub *shim.MockStub, args [][]byte) {
    35  	res := stub.MockInit("1", args)
    36  	if res.Status != shim.OK {
    37  		fmt.Println("Init failed", string(res.Message))
    38  		t.FailNow()
    39  	}
    40  }
    41  
    42  func checkState(t *testing.T, stub *shim.MockStub, name string, expect string) {
    43  	bytes := stub.State[name]
    44  	if bytes == nil {
    45  		fmt.Println("State", name, "failed to get value")
    46  		t.FailNow()
    47  	}
    48  	if string(bytes) != expect {
    49  		fmt.Println("State value", name, "was not", expect, "as expected")
    50  		t.FailNow()
    51  	}
    52  }
    53  
    54  func checkQuery(t *testing.T, stub *shim.MockStub, args [][]byte, expect string) {
    55  	res := stub.MockInvoke("1", args)
    56  	if res.Status != shim.OK {
    57  		fmt.Println("Query", args, "failed", string(res.Message))
    58  		t.FailNow()
    59  	}
    60  	if res.Payload == nil {
    61  		fmt.Println("Query", args, "failed to get result")
    62  		t.FailNow()
    63  	}
    64  	if string(res.Payload) != expect {
    65  		fmt.Println("Query result ", string(res.Payload), "was not", expect, "as expected")
    66  		t.FailNow()
    67  	}
    68  }
    69  
    70  func checkInvoke(t *testing.T, stub *shim.MockStub, args [][]byte) {
    71  	res := stub.MockInvoke("1", args)
    72  	if res.Status != shim.OK {
    73  		fmt.Println("Invoke", args, "failed", string(res.Message))
    74  		t.FailNow()
    75  	}
    76  }
    77  
    78  func TestExample05_Init(t *testing.T) {
    79  	scc := new(SimpleChaincode)
    80  	stub := shim.NewMockStub("ex05", scc)
    81  
    82  	// Init A=123 B=234
    83  	checkInit(t, stub, [][]byte{[]byte("init"), []byte("sumStoreName"), []byte("432")})
    84  
    85  	checkState(t, stub, "sumStoreName", "432")
    86  }
    87  
    88  func TestExample05_Query(t *testing.T) {
    89  	scc := new(SimpleChaincode)
    90  	stub := shim.NewMockStub("ex05", scc)
    91  
    92  	ccEx2 := new(ex02.SimpleChaincode)
    93  	stubEx2 := shim.NewMockStub(chaincodeName, ccEx2)
    94  	checkInit(t, stubEx2, [][]byte{[]byte("init"), []byte("a"), []byte("111"), []byte("b"), []byte("222")})
    95  	stub.MockPeerChaincode(chaincodeName, stubEx2)
    96  
    97  	checkInit(t, stub, [][]byte{[]byte("init"), []byte("sumStoreName"), []byte("0")})
    98  
    99  	// a + b = 111 + 222 = 333
   100  	checkQuery(t, stub, [][]byte{[]byte("query"), []byte(chaincodeName), []byte("sumStoreName"), []byte("")}, "333") // example05 doesn't return JSON?
   101  }
   102  
   103  func TestExample05_Invoke(t *testing.T) {
   104  	scc := new(SimpleChaincode)
   105  	stub := shim.NewMockStub("ex05", scc)
   106  
   107  	ccEx2 := new(ex02.SimpleChaincode)
   108  	stubEx2 := shim.NewMockStub(chaincodeName, ccEx2)
   109  	checkInit(t, stubEx2, [][]byte{[]byte("init"), []byte("a"), []byte("222"), []byte("b"), []byte("333")})
   110  	stub.MockPeerChaincode(chaincodeName, stubEx2)
   111  
   112  	checkInit(t, stub, [][]byte{[]byte("init"), []byte("sumStoreName"), []byte("0")})
   113  
   114  	// a + b = 222 + 333 = 555
   115  	checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte(chaincodeName), []byte("sumStoreName"), []byte("")})
   116  	checkQuery(t, stub, [][]byte{[]byte("query"), []byte(chaincodeName), []byte("sumStoreName"), []byte("")}, "555") // example05 doesn't return JSON?
   117  	checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("a")}, "222")
   118  	checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("b")}, "333")
   119  
   120  	// update A-=10 and B+=10
   121  	checkInvoke(t, stubEx2, [][]byte{[]byte("invoke"), []byte("a"), []byte("b"), []byte("10")})
   122  
   123  	// a + b = 212 + 343 = 555
   124  	checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte(chaincodeName), []byte("sumStoreName"), []byte("")})
   125  	checkQuery(t, stub, [][]byte{[]byte("query"), []byte(chaincodeName), []byte("sumStoreName"), []byte("")}, "555") // example05 doesn't return JSON?
   126  	checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("a")}, "212")
   127  	checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("b")}, "343")
   128  }