|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package simple_test |
| 8 | + |
| 9 | +import ( |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/hyperledger-labs/fabric-smart-client/integration" |
| 13 | + "github.com/hyperledger-labs/fabric-smart-client/integration/fabricx/simple" |
| 14 | + "github.com/hyperledger-labs/fabric-smart-client/integration/fabricx/simple/views" |
| 15 | + nwocommon "github.com/hyperledger-labs/fabric-smart-client/integration/nwo/common" |
| 16 | + nwofabricx "github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fabricx" |
| 17 | + nwofsc "github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fsc" |
| 18 | + "github.com/hyperledger-labs/fabric-smart-client/platform/view/view" |
| 19 | + . "github.com/onsi/ginkgo/v2" |
| 20 | + . "github.com/onsi/gomega" |
| 21 | +) |
| 22 | + |
| 23 | +const testDataPath = "./out/testdata" |
| 24 | + |
| 25 | +func TestEndToEnd(t *testing.T) { |
| 26 | + RegisterFailHandler(Fail) |
| 27 | + RunSpecs(t, "simple suite") |
| 28 | +} |
| 29 | + |
| 30 | +type TestSuite struct { |
| 31 | + *integration.TestSuite |
| 32 | +} |
| 33 | + |
| 34 | +var _ = Describe("EndToEnd", func() { |
| 35 | + for _, c := range []nwofsc.P2PCommunicationType{nwofsc.WebSocket} { |
| 36 | + Describe("simple Life Cycle", Label(c), func() { |
| 37 | + s := NewTestSuite(c) |
| 38 | + BeforeEach(s.Setup) |
| 39 | + AfterEach(s.TearDown) |
| 40 | + It("succeeded", func() { |
| 41 | + RunTest(s.II) |
| 42 | + }) |
| 43 | + }) |
| 44 | + } |
| 45 | +}) |
| 46 | + |
| 47 | +var testObjects = []views.SomeObject{ |
| 48 | + { |
| 49 | + Owner: "Boss", |
| 50 | + Value: 100, |
| 51 | + }, |
| 52 | + { |
| 53 | + Owner: "Bob", |
| 54 | + Value: 200, |
| 55 | + }, |
| 56 | + { |
| 57 | + Owner: "RedHotChilli", |
| 58 | + Value: 900, |
| 59 | + }, |
| 60 | + { |
| 61 | + Owner: "DogsEatCarrots", |
| 62 | + Value: 600, |
| 63 | + }, |
| 64 | +} |
| 65 | + |
| 66 | +// This object has a negative value, it should return an error from the test. |
| 67 | +var negativeTestObjects = []views.SomeObject{ |
| 68 | + { |
| 69 | + Owner: "TigerEatsGrass", |
| 70 | + Value: -100, |
| 71 | + }, |
| 72 | +} |
| 73 | + |
| 74 | +func RunTest(n *integration.Infrastructure) { |
| 75 | + // create some objects |
| 76 | + for _, obj := range testObjects { |
| 77 | + res, err := n.Client(simple.CreatorNode).CallView("create", nwocommon.JSONMarshall(views.CreateParams{ |
| 78 | + Owner: obj.Owner, |
| 79 | + Value: obj.Value, |
| 80 | + Namespace: simple.Namespace, |
| 81 | + Approvers: []view.Identity{n.Identity(simple.ApproverNode)}, |
| 82 | + })) |
| 83 | + Expect(err).NotTo(HaveOccurred()) |
| 84 | + _ = res |
| 85 | + } |
| 86 | + |
| 87 | + // try to create one of our objects again, this should fail and return with an error |
| 88 | + someExistingObject := testObjects[0] |
| 89 | + _, err := n.Client(simple.CreatorNode).CallView("create", nwocommon.JSONMarshall(views.CreateParams{ |
| 90 | + Owner: someExistingObject.Owner, |
| 91 | + Value: someExistingObject.Value, |
| 92 | + Namespace: simple.Namespace, |
| 93 | + Approvers: []view.Identity{n.Identity(simple.ApproverNode)}, |
| 94 | + })) |
| 95 | + Expect(err).To(HaveOccurred()) |
| 96 | + |
| 97 | + // try to create an object with a negative value, this should fail and return with an error |
| 98 | + someAbsurdObject := negativeTestObjects[0] |
| 99 | + _, err = n.Client(simple.CreatorNode).CallView("create", nwocommon.JSONMarshall(views.CreateParams{ |
| 100 | + Owner: someAbsurdObject.Owner, |
| 101 | + Value: someAbsurdObject.Value, |
| 102 | + Namespace: simple.Namespace, |
| 103 | + Approvers: []view.Identity{n.Identity(simple.ApproverNode)}, |
| 104 | + })) |
| 105 | + Expect(err).To(HaveOccurred()) |
| 106 | + |
| 107 | + // lets see that we can query all our objects |
| 108 | + lookupIDs := make([]string, len(testObjects)) |
| 109 | + for i, obj := range testObjects { |
| 110 | + linearID, errx := obj.GetLinearID() |
| 111 | + Expect(errx).ToNot(HaveOccurred()) |
| 112 | + lookupIDs[i] = linearID |
| 113 | + } |
| 114 | + |
| 115 | + res, err := n.Client(simple.CreatorNode).CallView("query", nwocommon.JSONMarshall(views.QueryParams{ |
| 116 | + SomeIDs: lookupIDs, |
| 117 | + Namespace: simple.Namespace, |
| 118 | + })) |
| 119 | + Expect(err).ToNot(HaveOccurred()) |
| 120 | + |
| 121 | + raw, ok := res.([]byte) |
| 122 | + Expect(ok).To(BeTrue()) |
| 123 | + var objs []views.SomeObject |
| 124 | + nwocommon.JSONUnmarshal(raw, &objs) |
| 125 | + Expect(objs).To(ConsistOf(testObjects)) |
| 126 | + |
| 127 | + // do some lookup which do not exist |
| 128 | + _, err = n.Client(simple.CreatorNode).CallView("query", nwocommon.JSONMarshall(views.QueryParams{ |
| 129 | + SomeIDs: []string{"doo", "boo", "bong", "ding"}, |
| 130 | + Namespace: simple.Namespace, |
| 131 | + })) |
| 132 | + Expect(err).ToNot(HaveOccurred()) |
| 133 | +} |
| 134 | + |
| 135 | +func NewTestSuite(commType nwofsc.P2PCommunicationType) *TestSuite { |
| 136 | + return &TestSuite{integration.NewTestSuite(func() (*integration.Infrastructure, error) { |
| 137 | + ii, err := integration.New(integration.IOUPort.StartPortForNode(), testDataPath, simple.Topology(&simple.SDK{}, commType)...) |
| 138 | + if err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + |
| 142 | + ii.RegisterPlatformFactory(nwofabricx.NewPlatformFactory()) |
| 143 | + |
| 144 | + ii.DeleteOnStart = true |
| 145 | + ii.DeleteOnStop = false |
| 146 | + |
| 147 | + ii.Generate() |
| 148 | + |
| 149 | + return ii, nil |
| 150 | + })} |
| 151 | +} |
0 commit comments