chaincode_example04_test.go 3.38 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
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"
	ex02 "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"
)

// this is the response to any successful Invoke() on chaincode_example04
var eventResponse = "{\"Name\":\"Event\",\"Amount\":\"1\"}"

29
30
func checkInit(t *testing.T, stub *shim.MockStub, args [][]byte) {
	_, err := stub.MockInit("1", args)
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
	if err != nil {
		fmt.Println("Init failed", err)
		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) {
50
	bytes, err := stub.MockQuery([][]byte{[]byte("query"), []byte(name)})
51
52
53
54
55
56
57
58
59
60
61
62
63
64
	if err != nil {
		fmt.Println("Query", name, "failed", err)
		t.FailNow()
	}
	if bytes == nil {
		fmt.Println("Query", name, "failed to get value")
		t.FailNow()
	}
	if string(bytes) != value {
		fmt.Println("Query value", name, "was not", value, "as expected")
		t.FailNow()
	}
}

65
66
func checkInvoke(t *testing.T, stub *shim.MockStub, args [][]byte) {
	_, err := stub.MockInvoke("1", args)
67
68
69
70
71
72
73
	if err != nil {
		fmt.Println("Invoke", args, "failed", err)
		t.FailNow()
	}
}

func TestExample04_Init(t *testing.T) {
74
	scc := new(SimpleChaincode)
75
76
77
	stub := shim.NewMockStub("ex04", scc)

	// Init A=123 B=234
78
	checkInit(t, stub, [][]byte{[]byte("init"), []byte("Event"), []byte("123")})
79
80
81
82
83

	checkState(t, stub, "Event", "123")
}

func TestExample04_Query(t *testing.T) {
84
	scc := new(SimpleChaincode)
85
86
87
	stub := shim.NewMockStub("ex04", scc)

	// Init A=345 B=456
88
	checkInit(t, stub, [][]byte{[]byte("init"), []byte("Event"), []byte("1")})
89
90
91
92
93
94

	// Query A
	checkQuery(t, stub, "Event", eventResponse)
}

func TestExample04_Invoke(t *testing.T) {
95
	scc := new(SimpleChaincode)
96
97
98
99
	stub := shim.NewMockStub("ex04", scc)

	ccEx2 := new(ex02.SimpleChaincode)
	stubEx2 := shim.NewMockStub("ex02", ccEx2)
100
	checkInit(t, stubEx2, [][]byte{[]byte("init"), []byte("a"), []byte("111"), []byte("b"), []byte("222")})
101
102
103
	stub.MockPeerChaincode(scc.GetChaincodeToCall(), stubEx2)

	// Init A=567 B=678
104
	checkInit(t, stub, [][]byte{[]byte("init"), []byte("Event"), []byte("1")})
105
106

	// Invoke A->B for 10 via Example04's chaincode
107
	checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("Event"), []byte("1")})
108
109
110
111
112
	checkQuery(t, stub, "Event", eventResponse)
	checkQuery(t, stubEx2, "a", "101")
	checkQuery(t, stubEx2, "b", "232")

	// Invoke A->B for 10 via Example04's chaincode
113
	checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("Event"), []byte("1")})
114
115
116
117
	checkQuery(t, stub, "Event", eventResponse)
	checkQuery(t, stubEx2, "a", "91")
	checkQuery(t, stubEx2, "b", "242")
}