github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/examples/chaincode/go/invokereturnsvalue/invokereturnsvalue_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, retval []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  	if retval != nil {
    32  		if res.Payload == nil {
    33  			fmt.Printf("Init returned nil, expected %s", string(retval))
    34  			t.FailNow()
    35  		}
    36  		if string(res.Payload) != string(retval) {
    37  			fmt.Printf("Init returned %s, expected %s", string(res.Payload), string(retval))
    38  			t.FailNow()
    39  		}
    40  	}
    41  }
    42  
    43  func checkState(t *testing.T, stub *shim.MockStub, name string, value 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) != value {
    50  		fmt.Println("State value", name, "was not", value, "as expected")
    51  		t.FailNow()
    52  	}
    53  }
    54  
    55  func checkInvoke(t *testing.T, stub *shim.MockStub, args [][]byte, retval []byte) {
    56  	res := stub.MockInvoke("1", args)
    57  	if res.Status != shim.OK {
    58  		fmt.Println("Invoke", args, "failed", string(res.Message))
    59  		t.FailNow()
    60  	}
    61  
    62  	if retval != nil {
    63  		if res.Payload == nil {
    64  			fmt.Printf("Invoke returned nil, expected %s", string(retval))
    65  			t.FailNow()
    66  		}
    67  		if string(res.Payload) != string(retval) {
    68  			fmt.Printf("Invoke returned %s, expected %s", string(res.Payload), string(retval))
    69  			t.FailNow()
    70  		}
    71  	}
    72  }
    73  
    74  func Test_Init(t *testing.T) {
    75  	scc := new(SimpleChaincode)
    76  	stub := shim.NewMockStub("ex02", scc)
    77  
    78  	// Init A=123 B=234
    79  	checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("123"), []byte("B"), []byte("234")}, []byte("OK"))
    80  
    81  	checkState(t, stub, "A", "123")
    82  	checkState(t, stub, "B", "234")
    83  }
    84  
    85  func Test_Invoke(t *testing.T) {
    86  	scc := new(SimpleChaincode)
    87  	stub := shim.NewMockStub("ex02", scc)
    88  
    89  	// Init A=567 B=678
    90  	checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("567"), []byte("B"), []byte("678")}, []byte("OK"))
    91  
    92  	// Invoke A->B for 123
    93  	checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("A"), []byte("B"), []byte("123")}, []byte("{444,801}"))
    94  
    95  	// Invoke B->A for 234
    96  	checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("B"), []byte("A"), []byte("234")}, []byte("{567,678}"))
    97  }