@@ -46,6 +46,10 @@ func TestInit(t *testing.T) {
46
46
func TestInvoke (t * testing.T ) {
47
47
e := new (EndorserOneValidSignature )
48
48
stub := shim .NewMockStub ("endorseronevalidsignature" , e )
49
+ successResponse := & pb.Response {Status : 200 , Payload : []byte ("payload" )}
50
+ failResponse := & pb.Response {Status : 500 , Message : "error" }
51
+ successRes , _ := putils .GetBytesResponse (successResponse )
52
+ failRes , _ := putils .GetBytesResponse (failResponse )
49
53
50
54
// Initialize ESCC supplying the identity of the signer
51
55
args := [][]byte {[]byte ("DEFAULT" ), []byte ("PEER" )}
@@ -72,27 +76,47 @@ func TestInvoke(t *testing.T) {
72
76
t .Fatalf ("escc invoke should have failed with invalid number of args: %v" , args )
73
77
}
74
78
79
+ // Failed path: Not enough parameters
80
+ args = [][]byte {[]byte ("test" ), []byte ("test" ), []byte ("test" ), []byte ("test" )}
81
+ if res := stub .MockInvoke ("1" , args ); res .Status == shim .OK {
82
+ t .Fatalf ("escc invoke should have failed with invalid number of args: %v" , args )
83
+ }
84
+
75
85
// Failed path: header is null
76
- args = [][]byte {[]byte ("test" ), nil , []byte ("test" ), []byte ("test" )}
86
+ args = [][]byte {[]byte ("test" ), nil , []byte ("test" ), successRes , []byte ("test" )}
77
87
if res := stub .MockInvoke ("1" , args ); res .Status == shim .OK {
78
88
fmt .Println ("Invoke" , args , "failed" , string (res .Message ))
79
89
t .Fatalf ("escc invoke should have failed with a null header. args: %v" , args )
80
90
}
81
91
82
92
// Failed path: payload is null
83
- args = [][]byte {[]byte ("test" ), []byte ("test" ), nil , []byte ("test" )}
93
+ args = [][]byte {[]byte ("test" ), []byte ("test" ), nil , successRes , []byte ("test" )}
84
94
if res := stub .MockInvoke ("1" , args ); res .Status == shim .OK {
85
95
fmt .Println ("Invoke" , args , "failed" , string (res .Message ))
86
96
t .Fatalf ("escc invoke should have failed with a null payload. args: %v" , args )
87
97
}
88
98
99
+ // Failed path: response is null
100
+ args = [][]byte {[]byte ("test" ), []byte ("test" ), []byte ("test" ), nil , []byte ("test" )}
101
+ if res := stub .MockInvoke ("1" , args ); res .Status == shim .OK {
102
+ fmt .Println ("Invoke" , args , "failed" , string (res .Message ))
103
+ t .Fatalf ("escc invoke should have failed with a null response. args: %v" , args )
104
+ }
105
+
89
106
// Failed path: action struct is null
90
- args = [][]byte {[]byte ("test" ), []byte ("test" ), []byte ("test" ), nil }
107
+ args = [][]byte {[]byte ("test" ), []byte ("test" ), []byte ("test" ), successRes , nil }
91
108
if res := stub .MockInvoke ("1" , args ); res .Status == shim .OK {
92
109
fmt .Println ("Invoke" , args , "failed" , string (res .Message ))
93
110
t .Fatalf ("escc invoke should have failed with a null action struct. args: %v" , args )
94
111
}
95
112
113
+ // Failed path: status code >=500
114
+ args = [][]byte {[]byte ("test" ), []byte ("test" ), []byte ("test" ), failRes , []byte ("test" )}
115
+ if res := stub .MockInvoke ("1" , args ); res .Status == shim .OK {
116
+ fmt .Println ("Invoke" , args , "failed" , string (res .Message ))
117
+ t .Fatalf ("escc invoke should have failed with a null response. args: %v" , args )
118
+ }
119
+
96
120
// Successful path - create a proposal
97
121
cs := & pb.ChaincodeSpec {
98
122
ChaincodeID : & pb.ChaincodeID {Name : "foo" },
@@ -127,15 +151,15 @@ func TestInvoke(t *testing.T) {
127
151
// success test 1: invocation with mandatory args only
128
152
simRes := []byte ("simulation_result" )
129
153
130
- args = [][]byte {[]byte ("" ), proposal .Header , proposal .Payload , simRes }
154
+ args = [][]byte {[]byte ("" ), proposal .Header , proposal .Payload , successRes , simRes }
131
155
res := stub .MockInvoke ("1" , args )
132
156
if res .Status != shim .OK {
133
157
t .Fail ()
134
158
t .Fatalf ("escc invoke failed with: %s" , res .Message )
135
159
return
136
160
}
137
161
138
- err = validateProposalResponse (res .Payload , proposal , nil , simRes , nil )
162
+ err = validateProposalResponse (res .Payload , proposal , nil , successResponse , simRes , nil )
139
163
if err != nil {
140
164
t .Fail ()
141
165
t .Fatalf ("%s" , err )
@@ -145,15 +169,15 @@ func TestInvoke(t *testing.T) {
145
169
// success test 2: invocation with mandatory args + events
146
170
events := []byte ("events" )
147
171
148
- args = [][]byte {[]byte ("" ), proposal .Header , proposal .Payload , simRes , events }
172
+ args = [][]byte {[]byte ("" ), proposal .Header , proposal .Payload , successRes , simRes , events }
149
173
res = stub .MockInvoke ("1" , args )
150
174
if res .Status != shim .OK {
151
175
t .Fail ()
152
176
t .Fatalf ("escc invoke failed with: %s" , res .Message )
153
177
return
154
178
}
155
179
156
- err = validateProposalResponse (res .Payload , proposal , nil , simRes , events )
180
+ err = validateProposalResponse (res .Payload , proposal , nil , successResponse , simRes , events )
157
181
if err != nil {
158
182
t .Fail ()
159
183
t .Fatalf ("%s" , err )
@@ -163,23 +187,23 @@ func TestInvoke(t *testing.T) {
163
187
// success test 3: invocation with mandatory args + events and visibility
164
188
visibility := []byte ("visibility" )
165
189
166
- args = [][]byte {[]byte ("" ), proposal .Header , proposal .Payload , simRes , events , visibility }
190
+ args = [][]byte {[]byte ("" ), proposal .Header , proposal .Payload , successRes , simRes , events , visibility }
167
191
res = stub .MockInvoke ("1" , args )
168
192
if res .Status != shim .OK {
169
193
t .Fail ()
170
194
t .Fatalf ("escc invoke failed with: %s" , res .Message )
171
195
return
172
196
}
173
197
174
- err = validateProposalResponse (res .Payload , proposal , visibility , simRes , events )
198
+ err = validateProposalResponse (res .Payload , proposal , visibility , successResponse , simRes , events )
175
199
if err != nil {
176
200
t .Fail ()
177
201
t .Fatalf ("%s" , err )
178
202
return
179
203
}
180
204
}
181
205
182
- func validateProposalResponse (prBytes []byte , proposal * pb.Proposal , visibility []byte , simRes []byte , events []byte ) error {
206
+ func validateProposalResponse (prBytes []byte , proposal * pb.Proposal , visibility []byte , response * pb. Response , simRes []byte , events []byte ) error {
183
207
if visibility == nil {
184
208
// TODO: set visibility to the default visibility mode once modes are defined
185
209
}
@@ -224,6 +248,17 @@ func validateProposalResponse(prBytes []byte, proposal *pb.Proposal, visibility
224
248
return fmt .Errorf ("could not unmarshal the chaincode action structure: err %s" , err )
225
249
}
226
250
251
+ // validate that the response match
252
+ if cact .Response .Status != response .Status {
253
+ return fmt .Errorf ("response status do not match" )
254
+ }
255
+ if cact .Response .Message != response .Message {
256
+ return fmt .Errorf ("response message do not match" )
257
+ }
258
+ if bytes .Compare (cact .Response .Payload , response .Payload ) != 0 {
259
+ return fmt .Errorf ("response payload do not match" )
260
+ }
261
+
227
262
// validate that the results match
228
263
if bytes .Compare (cact .Results , simRes ) != 0 {
229
264
return fmt .Errorf ("results do not match" )
0 commit comments