github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/core/scc/qscc/query_test.go (about) 1 /* 2 Copyright IBM Corp. 2017 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 qscc 17 18 import ( 19 "fmt" 20 "os" 21 "testing" 22 23 "github.com/spf13/viper" 24 25 "github.com/hyperledger/fabric/core/chaincode/shim" 26 "github.com/hyperledger/fabric/core/peer" 27 ) 28 29 func TestInit(t *testing.T) { 30 viper.Set("peer.fileSystemPath", "/var/hyperledger/test1/") 31 defer os.RemoveAll("/var/hyperledger/test1/") 32 peer.MockInitialize() 33 peer.MockCreateChain("mytestchainid1") 34 35 e := new(LedgerQuerier) 36 stub := shim.NewMockStub("LedgerQuerier", e) 37 38 if res := stub.MockInit("1", nil); res.Status != shim.OK { 39 fmt.Println("Init failed", string(res.Message)) 40 t.FailNow() 41 } 42 } 43 44 func TestQueryGetChainInfo(t *testing.T) { 45 viper.Set("peer.fileSystemPath", "/var/hyperledger/test2/") 46 defer os.RemoveAll("/var/hyperledger/test2/") 47 peer.MockInitialize() 48 peer.MockCreateChain("mytestchainid2") 49 50 e := new(LedgerQuerier) 51 stub := shim.NewMockStub("LedgerQuerier", e) 52 53 args := [][]byte{[]byte(GetChainInfo), []byte("mytestchainid2")} 54 if res := stub.MockInvoke("1", args); res.Status != shim.OK { 55 t.Fatalf("qscc GetChainInfo failed with err: %s", res.Message) 56 } 57 } 58 59 func TestQueryGetTransactionByID(t *testing.T) { 60 viper.Set("peer.fileSystemPath", "/var/hyperledger/test3/") 61 defer os.RemoveAll("/var/hyperledger/test3/") 62 peer.MockInitialize() 63 peer.MockCreateChain("mytestchainid3") 64 65 e := new(LedgerQuerier) 66 stub := shim.NewMockStub("LedgerQuerier", e) 67 68 args := [][]byte{[]byte(GetTransactionByID), []byte("mytestchainid3"), []byte("1")} 69 if res := stub.MockInvoke("1", args); res.Status == shim.OK { 70 t.Fatalf("qscc getTransactionByID should have failed with invalid txid: 1") 71 } 72 } 73 74 func TestQueryWithWrongParameters(t *testing.T) { 75 viper.Set("peer.fileSystemPath", "/var/hyperledger/test4/") 76 defer os.RemoveAll("/var/hyperledger/test4/") 77 peer.MockInitialize() 78 peer.MockCreateChain("mytestchainid4") 79 80 e := new(LedgerQuerier) 81 stub := shim.NewMockStub("LedgerQuerier", e) 82 83 // Test with wrong number of parameters 84 args := [][]byte{[]byte(GetTransactionByID), []byte("mytestchainid4")} 85 if res := stub.MockInvoke("1", args); res.Status == shim.OK { 86 t.Fatalf("qscc getTransactionByID should have failed with invalid txid: 1") 87 } 88 } 89 90 func TestQueryGetBlockByNumber(t *testing.T) { 91 //t.Skip() 92 viper.Set("peer.fileSystemPath", "/var/hyperledger/test5/") 93 defer os.RemoveAll("/var/hyperledger/test5/") 94 peer.MockInitialize() 95 peer.MockCreateChain("mytestchainid5") 96 97 e := new(LedgerQuerier) 98 stub := shim.NewMockStub("LedgerQuerier", e) 99 100 args := [][]byte{[]byte(GetBlockByNumber), []byte("mytestchainid5"), []byte("0")} 101 if res := stub.MockInvoke("1", args); res.Status == shim.OK { 102 t.Fatalf("qscc GetBlockByNumber should have failed with invalid number: 0") 103 } 104 } 105 106 func TestQueryGetBlockByHash(t *testing.T) { 107 viper.Set("peer.fileSystemPath", "/var/hyperledger/test6/") 108 defer os.RemoveAll("/var/hyperledger/test6/") 109 peer.MockInitialize() 110 peer.MockCreateChain("mytestchainid6") 111 112 e := new(LedgerQuerier) 113 stub := shim.NewMockStub("LedgerQuerier", e) 114 115 args := [][]byte{[]byte(GetBlockByHash), []byte("mytestchainid6"), []byte("0")} 116 if res := stub.MockInvoke("1", args); res.Status == shim.OK { 117 t.Fatalf("qscc GetBlockByHash should have failed with invalid hash: 0") 118 } 119 } 120 121 func TestQueryGetBlockByTxID(t *testing.T) { 122 viper.Set("peer.fileSystemPath", "/var/hyperledger/test8/") 123 defer os.RemoveAll("/var/hyperledger/test8/") 124 peer.MockInitialize() 125 peer.MockCreateChain("mytestchainid8") 126 127 e := new(LedgerQuerier) 128 stub := shim.NewMockStub("LedgerQuerier", e) 129 130 txID := "" 131 132 args := [][]byte{[]byte(GetBlockByTxID), []byte("mytestchainid8"), []byte(txID)} 133 if res := stub.MockInvoke("1", args); res.Status == shim.OK { 134 t.Fatalf("qscc GetBlockByTxID should have failed with invalid txID: %s", txID) 135 } 136 }