|
| 1 | +/* |
| 2 | +Copyright SecureKey Technologies Inc. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package orgs |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + "strconv" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/hyperledger/fabric-sdk-go/api" |
| 15 | + fabrictxn "github.com/hyperledger/fabric-sdk-go/fabric-txn" |
| 16 | +) |
| 17 | + |
| 18 | +// TestOrgsEndToEnd creates a channel with two organisations, installs chaincode |
| 19 | +// on each of them, and finally invokes a transaction on an org2 peer and queries |
| 20 | +// the result from an org1 peer |
| 21 | +func TestOrgsEndToEnd(t *testing.T) { |
| 22 | + // Bootstrap network |
| 23 | + initializeFabricClient(t) |
| 24 | + loadOrgUsers(t) |
| 25 | + loadOrgPeers(t) |
| 26 | + loadOrderer(t) |
| 27 | + createTestChannel(t) |
| 28 | + joinTestChannel(t) |
| 29 | + installAndInstantiate(t) |
| 30 | + |
| 31 | + fmt.Printf("peer0 is %+v, peer1 is %+v\n", orgTestPeer0, orgTestPeer1) |
| 32 | + |
| 33 | + // Query initial value on org1 peer |
| 34 | + orgTestClient.SetUserContext(org1User) |
| 35 | + orgTestChannel.SetPrimaryPeer(orgTestPeer0) |
| 36 | + result, err := fabrictxn.QueryChaincode(orgTestClient, orgTestChannel, |
| 37 | + "exampleCC", generateQueryArgs()) |
| 38 | + failTestIfError(err, t) |
| 39 | + initialValue, err := strconv.Atoi(result) |
| 40 | + failTestIfError(err, t) |
| 41 | + |
| 42 | + // Change value on org2 peer |
| 43 | + orgTestClient.SetUserContext(org2User) |
| 44 | + orgTestChannel.SetPrimaryPeer(orgTestPeer1) |
| 45 | + err = fabrictxn.InvokeChaincode(orgTestClient, orgTestChannel, []api.Peer{orgTestPeer1}, |
| 46 | + peer0EventHub, "exampleCC", generateInvokeArgs(), nil) |
| 47 | + failTestIfError(err, t) |
| 48 | + |
| 49 | + // Assert changed value on org1 peer |
| 50 | + orgTestClient.SetUserContext(org1User) |
| 51 | + orgTestChannel.SetPrimaryPeer(orgTestPeer0) |
| 52 | + result, err = fabrictxn.QueryChaincode(orgTestClient, orgTestChannel, |
| 53 | + "exampleCC", generateQueryArgs()) |
| 54 | + failTestIfError(err, t) |
| 55 | + finalValue, err := strconv.Atoi(result) |
| 56 | + failTestIfError(err, t) |
| 57 | + |
| 58 | + if initialValue+1 != finalValue { |
| 59 | + t.Fatalf("Org1 invoke result was not propagated to org2. Expected %d, got: %d", |
| 60 | + (initialValue + 1), finalValue) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func generateQueryArgs() []string { |
| 65 | + var args []string |
| 66 | + args = append(args, "invoke") |
| 67 | + args = append(args, "query") |
| 68 | + args = append(args, "b") |
| 69 | + |
| 70 | + return args |
| 71 | +} |
| 72 | + |
| 73 | +func generateInvokeArgs() []string { |
| 74 | + var args []string |
| 75 | + args = append(args, "invoke") |
| 76 | + args = append(args, "move") |
| 77 | + args = append(args, "a") |
| 78 | + args = append(args, "b") |
| 79 | + args = append(args, "1") |
| 80 | + |
| 81 | + return args |
| 82 | +} |
0 commit comments