This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
chaincode_test.go
177 lines (141 loc) · 6.06 KB
/
chaincode_test.go
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package cpaper_extended_test
import (
"testing"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/s7techlab/cckit/examples/cpaper_extended"
"github.com/s7techlab/cckit/examples/cpaper_extended/schema"
"github.com/s7techlab/cckit/examples/cpaper_extended/testdata"
idtestdata "github.com/s7techlab/cckit/identity/testdata"
testcc "github.com/s7techlab/cckit/testing"
expectcc "github.com/s7techlab/cckit/testing/expect"
)
const (
IssuerName = "SomeIssuer"
BuyerName = "SomeBuyer"
)
func TestCommercialPaper(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Commercial Paper Suite")
}
var _ = Describe(`CommercialPaper`, func() {
paperChaincode := testcc.NewMockStub(
// chaincode name
`commercial_paper`,
// chaincode implementation, supports Chaincode interface with Init and Invoke methods
cpaper_extended.NewCC(),
)
BeforeSuite(func() {
// Init chaincode with admin identity
adminIdentity := testdata.Certificates[0].MustIdentity(idtestdata.DefaultMSP)
expectcc.ResponseOk(
paperChaincode.From(adminIdentity).Init())
})
Describe("Commercial Paper lifecycle", func() {
It("Allow issuer to issue new commercial paper", func(done Done) {
//input payload for chaincode method
issueTransactionData := &schema.IssueCommercialPaper{
Issuer: IssuerName,
PaperNumber: "0001",
IssueDate: timestamppb.Now(),
MaturityDate: testcc.MustProtoTimestamp(time.Now().AddDate(0, 2, 0)),
FaceValue: 100000,
ExternalId: "EXT0001",
}
// we expect tha `issue` method invocation with particular input payload returns response with 200 code
// &schema.IssueCommercialPaper wil automatically converts to bytes via proto.Marshall function
expectcc.ResponseOk(
paperChaincode.Invoke(`issue`, issueTransactionData))
// Validate event has been emitted with the transaction data
expectcc.EventStringerEqual(<-paperChaincode.ChaincodeEventsChannel,
`IssueCommercialPaper`, issueTransactionData)
// Clear events channel after a test case that emits an event
paperChaincode.ClearEvents()
close(done)
}, 0.1)
It("Allow issuer to get commercial paper by composite primary key", func() {
queryResponse := paperChaincode.Query("get", &schema.CommercialPaperId{
Issuer: IssuerName,
PaperNumber: "0001",
})
// we expect that returned []byte payload can be unmarshalled to *schema.CommercialPaper entity
paper := expectcc.PayloadIs(queryResponse, &schema.CommercialPaper{}).(*schema.CommercialPaper)
Expect(paper.Issuer).To(Equal(IssuerName))
Expect(paper.Owner).To(Equal(IssuerName))
Expect(paper.State).To(Equal(schema.CommercialPaper_STATE_ISSUED))
Expect(paper.PaperNumber).To(Equal("0001"))
Expect(paper.FaceValue).To(BeNumerically("==", 100000))
})
It("Allow issuer to get commercial paper by unique key", func() {
queryResponse := paperChaincode.Query("getByExternalId", "EXT0001")
paper := expectcc.PayloadIs(queryResponse, &schema.CommercialPaper{}).(*schema.CommercialPaper)
Expect(paper.Issuer).To(Equal(IssuerName))
Expect(paper.Owner).To(Equal(IssuerName))
Expect(paper.State).To(Equal(schema.CommercialPaper_STATE_ISSUED))
Expect(paper.PaperNumber).To(Equal("0001"))
Expect(paper.FaceValue).To(BeNumerically("==", 100000))
})
It("Allow issuer to get a list of commercial papers", func() {
queryResponse := paperChaincode.Query("list")
papers := expectcc.PayloadIs(queryResponse, &schema.CommercialPaperList{}).(*schema.CommercialPaperList)
Expect(len(papers.Items)).To(BeNumerically("==", 1))
Expect(papers.Items[0].Issuer).To(Equal(IssuerName))
Expect(papers.Items[0].Owner).To(Equal(IssuerName))
Expect(papers.Items[0].State).To(Equal(schema.CommercialPaper_STATE_ISSUED))
Expect(papers.Items[0].PaperNumber).To(Equal("0001"))
Expect(papers.Items[0].FaceValue).To(BeNumerically("==", 100000))
})
It("Allow buyer to buy commercial paper", func() {
buyTransactionData := &schema.BuyCommercialPaper{
Issuer: IssuerName,
PaperNumber: "0001",
CurrentOwner: IssuerName,
NewOwner: BuyerName,
Price: 95000,
PurchaseDate: timestamppb.Now(),
}
expectcc.ResponseOk(paperChaincode.Invoke(`buy`, buyTransactionData))
queryResponse := paperChaincode.Query("get", &schema.CommercialPaperId{
Issuer: IssuerName,
PaperNumber: "0001",
})
paper := expectcc.PayloadIs(queryResponse, &schema.CommercialPaper{}).(*schema.CommercialPaper)
Expect(paper.Owner).To(Equal(BuyerName))
Expect(paper.State).To(Equal(schema.CommercialPaper_STATE_TRADING))
expectcc.EventStringerEqual(<-paperChaincode.ChaincodeEventsChannel,
`BuyCommercialPaper`, buyTransactionData)
paperChaincode.ClearEvents()
})
It("Allow buyer to redeem commercial paper", func() {
redeemTransactionData := &schema.RedeemCommercialPaper{
Issuer: IssuerName,
PaperNumber: "0001",
RedeemingOwner: BuyerName,
RedeemDate: timestamppb.Now(),
}
expectcc.ResponseOk(paperChaincode.Invoke(`redeem`, redeemTransactionData))
queryResponse := paperChaincode.Query("get", &schema.CommercialPaperId{
Issuer: IssuerName,
PaperNumber: "0001",
})
paper := expectcc.PayloadIs(queryResponse, &schema.CommercialPaper{}).(*schema.CommercialPaper)
Expect(paper.Owner).To(Equal(IssuerName))
Expect(paper.State).To(Equal(schema.CommercialPaper_STATE_REDEEMED))
expectcc.EventStringerEqual(<-paperChaincode.ChaincodeEventsChannel,
`RedeemCommercialPaper`, redeemTransactionData)
paperChaincode.ClearEvents()
})
It("Allow issuer to delete commercial paper", func() {
expectcc.ResponseOk(paperChaincode.Invoke(`delete`, &schema.CommercialPaperId{
Issuer: IssuerName,
PaperNumber: "0001",
}))
// Validate there are 0 Commercial Papers in the world state
queryResponse := paperChaincode.Query("list")
papers := expectcc.PayloadIs(queryResponse, &schema.CommercialPaperList{}).(*schema.CommercialPaperList)
Expect(len(papers.Items)).To(BeNumerically("==", 0))
})
})
})