github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/examples/chaincode/go/chaincode_example02/chaincode_example02_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  )
    24  
    25  func checkInit(t *testing.T, stub *shim.MockStub, args [][]byte) {
    26  	res := stub.MockInit("1", args)
    27  	if res.Status != shim.OK {
    28  		fmt.Println("Init failed", string(res.Message))
    29  		t.FailNow()
    30  	}
    31  }
    32  
    33  func checkState(t *testing.T, stub *shim.MockStub, name string, value string) {
    34  	bytes := stub.State[name]
    35  	if bytes == nil {
    36  		fmt.Println("State", name, "failed to get value")
    37  		t.FailNow()
    38  	}
    39  	if string(bytes) != value {
    40  		fmt.Println("State value", name, "was not", value, "as expected")
    41  		t.FailNow()
    42  	}
    43  }
    44  
    45  func checkQuery(t *testing.T, stub *shim.MockStub, name string, value string) {
    46  	res := stub.MockInvoke("1", [][]byte{[]byte("query"), []byte(name)})
    47  	if res.Status != shim.OK {
    48  		fmt.Println("Query", name, "failed", string(res.Message))
    49  		t.FailNow()
    50  	}
    51  	if res.Payload == nil {
    52  		fmt.Println("Query", name, "failed to get value")
    53  		t.FailNow()
    54  	}
    55  	if string(res.Payload) != value {
    56  		fmt.Println("Query value", name, "was not", value, "as expected")
    57  		t.FailNow()
    58  	}
    59  }
    60  
    61  func checkInvoke(t *testing.T, stub *shim.MockStub, args [][]byte) {
    62  	res := stub.MockInvoke("1", args)
    63  	if res.Status != shim.OK {
    64  		fmt.Println("Invoke", args, "failed", string(res.Message))
    65  		t.FailNow()
    66  	}
    67  }
    68  
    69  func TestExample02_Init(t *testing.T) {
    70  	scc := new(SimpleChaincode)
    71  	stub := shim.NewMockStub("ex02", scc)
    72  
    73  	// Init A=123 B=234
    74  	checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("123"), []byte("B"), []byte("234")})
    75  
    76  	checkState(t, stub, "A", "123")
    77  	checkState(t, stub, "B", "234")
    78  }
    79  
    80  func TestExample02_Query(t *testing.T) {
    81  	scc := new(SimpleChaincode)
    82  	stub := shim.NewMockStub("ex02", scc)
    83  
    84  	// Init A=345 B=456
    85  	checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("345"), []byte("B"), []byte("456")})
    86  
    87  	// Query A
    88  	checkQuery(t, stub, "A", "345")
    89  
    90  	// Query B
    91  	checkQuery(t, stub, "B", "456")
    92  }
    93  
    94  func TestExample02_Invoke(t *testing.T) {
    95  	scc := new(SimpleChaincode)
    96  	stub := shim.NewMockStub("ex02", scc)
    97  
    98  	// Init A=567 B=678
    99  	checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("567"), []byte("B"), []byte("678")})
   100  
   101  	// Invoke A->B for 123
   102  	checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("A"), []byte("B"), []byte("123")})
   103  	checkQuery(t, stub, "A", "444")
   104  	checkQuery(t, stub, "B", "801")
   105  
   106  	// Invoke B->A for 234
   107  	checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("B"), []byte("A"), []byte("234")})
   108  	checkQuery(t, stub, "A", "678")
   109  	checkQuery(t, stub, "B", "567")
   110  	checkQuery(t, stub, "A", "678")
   111  	checkQuery(t, stub, "B", "567")
   112  }