fabric-samples/chaincode/chaincode_example02/chaincode_example02_test.go
Jim Zhang ca8fad3151 [FAB-5260] Update samples to v1.0.0
basic-network:
 - moved "crypto-config" to top-level and removed
   "network" folder
 - added generate.sh to re-gen crypto materials
 - docker-compose.yaml to ref v1.0.0 images
 - add CLI to docker-compose
 - start.sh to only start CA, orderer, peer and couch
 - startFabric.sh to run start.sh, then launch CLI
   for create channel, install, instantiate, invoke
fabcar:
 - moved chaincode to central chaincode folder
 - moved "creds" to top-level and removed "network"
   folder
 - changed to use the basic-network as the target
   and removed docker-compose.yaml and crypto-config
 - updated package.json to require v1.0.0 modules

- added missing license headers
- restructured to use a central chaincode subdirectory

Change-Id: Ic784d1cf55ea51da5155624f3c38275883de1dca
Signed-off-by: Christopher Ferris <chrisfer@us.ibm.com>
Signed-off-by: Nick Gaski <ngaski@us.ibm.com>
Signed-off-by: Jim Zhang <jzhang@us.ibm.com>
2017-07-12 09:07:39 -04:00

112 lines
3.1 KiB
Go

/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"testing"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
func checkInit(t *testing.T, stub *shim.MockStub, args [][]byte) {
res := stub.MockInit("1", args)
if res.Status != shim.OK {
fmt.Println("Init failed", string(res.Message))
t.FailNow()
}
}
func checkState(t *testing.T, stub *shim.MockStub, name string, value string) {
bytes := stub.State[name]
if bytes == nil {
fmt.Println("State", name, "failed to get value")
t.FailNow()
}
if string(bytes) != value {
fmt.Println("State value", name, "was not", value, "as expected")
t.FailNow()
}
}
func checkQuery(t *testing.T, stub *shim.MockStub, name string, value string) {
res := stub.MockInvoke("1", [][]byte{[]byte("query"), []byte(name)})
if res.Status != shim.OK {
fmt.Println("Query", name, "failed", string(res.Message))
t.FailNow()
}
if res.Payload == nil {
fmt.Println("Query", name, "failed to get value")
t.FailNow()
}
if string(res.Payload) != value {
fmt.Println("Query value", name, "was not", value, "as expected")
t.FailNow()
}
}
func checkInvoke(t *testing.T, stub *shim.MockStub, args [][]byte) {
res := stub.MockInvoke("1", args)
if res.Status != shim.OK {
fmt.Println("Invoke", args, "failed", string(res.Message))
t.FailNow()
}
}
func TestExample02_Init(t *testing.T) {
scc := new(SimpleChaincode)
stub := shim.NewMockStub("ex02", scc)
// Init A=123 B=234
checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("123"), []byte("B"), []byte("234")})
checkState(t, stub, "A", "123")
checkState(t, stub, "B", "234")
}
func TestExample02_Query(t *testing.T) {
scc := new(SimpleChaincode)
stub := shim.NewMockStub("ex02", scc)
// Init A=345 B=456
checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("345"), []byte("B"), []byte("456")})
// Query A
checkQuery(t, stub, "A", "345")
// Query B
checkQuery(t, stub, "B", "456")
}
func TestExample02_Invoke(t *testing.T) {
scc := new(SimpleChaincode)
stub := shim.NewMockStub("ex02", scc)
// Init A=567 B=678
checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("567"), []byte("B"), []byte("678")})
// Invoke A->B for 123
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("A"), []byte("B"), []byte("123")})
checkQuery(t, stub, "A", "444")
checkQuery(t, stub, "B", "801")
// Invoke B->A for 234
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("B"), []byte("A"), []byte("234")})
checkQuery(t, stub, "A", "678")
checkQuery(t, stub, "B", "567")
checkQuery(t, stub, "A", "678")
checkQuery(t, stub, "B", "567")
}