fabric-samples/chaincode-docker-devmode/chaincode/chaincode_example02/chaincode_example02_test.go
Nick Gaski ba777f3bcb [FAB-4371] - Chaincode Dev Mode
Adds a docker environment for running peer in dev mode
pre-baked crypto and orderer/channel artifacts
README still needs an updated curl command to bundle
these scripts.
simplified some of the paths
add sacc from write your first chaincode
remove binary

Change-Id: Ic1537987c819f0350ae6dbf740fd00e1571045e5
Signed-off-by: Nick Gaski <ngaski@us.ibm.com>
Signed-off-by: Christopher Ferris <chrisfer@us.ibm.com>
2017-06-23 13:00:49 -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")
}