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