-
Notifications
You must be signed in to change notification settings - Fork 120
/
steps.js
231 lines (203 loc) · 8.2 KB
/
steps.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const Path = require('path');
const Chai = require('chai');
const expect = Chai.expect;
const Util = require('@accordproject/ergo-test/lib/util');
const Template = require('@accordproject/cicero-core').Template;
const Clause = require('@accordproject/cicero-core').Clause;
const Engine = require('@accordproject/cicero-engine').Engine;
const { Before, Given, When, Then, setDefaultTimeout } = require('cucumber');
// Defaults
const defaultState = {
'$class':'org.accordproject.cicero.contract.AccordContractState',
'stateId':'org.accordproject.cicero.contract.AccordContractState#1'
};
/**
* Initializes the contract
*
* @param {object} engine - the Cicero engine
* @param {object} clause - the clause instance
* @param {string} currentTime - the definition of 'now'
* @returns {object} Promise to the response
*/
async function init(engine,clause,currentTime) {
return engine.init(clause,currentTime);
}
/**
* Send a request to the contract
*
* @param {object} engine - the Cicero engine
* @param {object} clause - the clause instance
* @param {object} request - the request data in JSON
* @param {object} state - the state data in JSON
* @param {string} currentTime - the definition of 'now'
* @returns {object} Promise to the response
*/
async function trigger(engine,clause,request,state,currentTime) {
if (state === null) {
const initAnswer = await init(engine,clause,currentTime);
const initState = initAnswer.state;
return engine.trigger(clause,request,initState,currentTime);
} else {
return engine.trigger(clause,request,state,currentTime);
}
}
/**
* Load a clause from directory
*
* @param {string} templateDir the directory for the template
* @return {object} Promise to the new clause
*/
async function loadClause(templateDir) {
const template = await Template.fromDirectory(templateDir);
return new Clause(template);
}
Before(function () {
setDefaultTimeout(40 * 1000);
this.engine = new Engine();
this.currentTime = '1970-01-01T00:00:00Z';
this.state = null;
this.clause = null;
this.request = null;
this.answer = null;
});
Given('the template in {string}', async function(dir) {
const templateDir = Path.resolve(Util.resolveRootDir(this.parameters),dir);
const clause = await loadClause(templateDir);
this.request = clause.getTemplate().getMetadata().getRequest();
this.clause = clause;
});
Given('the current time is {string}', function(currentTime) {
this.currentTime = currentTime;
});
Given('that the contract says', async function (contractText) {
if (!this.clause) {
const templateDir = Path.resolve(Util.resolveRootDir(this.parameters),'.');
const clause = await loadClause(templateDir);
this.request = clause.getTemplate().getMetadata().getRequest();
this.clause = clause;
}
this.clause.parse(contractText,this.currentTime);
});
Given('that the contract data is', async function (contractData) {
const dataJson = JSON.parse(contractData);
if (!this.clause) {
const templateDir = Path.resolve(Util.resolveRootDir(this.parameters),'.');
const clause = await loadClause(templateDir);
this.request = clause.getTemplate().getMetadata().getRequest();
this.clause = clause;
}
this.clause.setData(dataJson);
});
Given('the default( sample) contract', async function () {
if (!this.clause) {
const templateDir = Path.resolve(Util.resolveRootDir(this.parameters),'.');
const clause = await loadClause(templateDir);
this.request = clause.getTemplate().getMetadata().getRequest();
this.clause = clause;
}
this.clause.parse(this.clause.getTemplate().getMetadata().getSample(),this.currentTime);
});
Given('the state', function (actualState) {
this.state = JSON.parse(actualState);
});
When('it is in the state', function (actualState) {
this.state = JSON.parse(actualState);
});
When('it receives the request', function (actualRequest) {
this.request = JSON.parse(actualRequest);
});
When('it receives the default request', function () {
this.request = this.clause.getTemplate().getMetadata().getRequest();
});
Then('the initial state( of the contract) should be', function (expectedState) {
const state = JSON.parse(expectedState);
return init(this.engine,this.clause,this.currentTime)
.then((actualAnswer) => {
this.answer = actualAnswer;
expect(actualAnswer).to.have.property('state');
expect(actualAnswer).to.not.have.property('error');
return Util.compareSuccess({ state },actualAnswer);
});
});
Then('the initial state( of the contract) should be the default state', function () {
const state = defaultState;
return init(this.engine,this.clause,this.currentTime)
.then((actualAnswer) => {
this.answer = actualAnswer;
expect(actualAnswer).to.have.property('state');
expect(actualAnswer).to.not.have.property('error');
return Util.compareSuccess({ state },actualAnswer);
});
});
Then('the contract data should be', async function (contractData) {
const expectedData = JSON.parse(contractData);
return Util.compareComponent(expectedData,this.clause.getData());
});
Then('it should respond with', function (expectedResponse) {
const response = JSON.parse(expectedResponse);
if (this.answer) {
expect(this.answer).to.have.property('response');
expect(this.answer).to.not.have.property('error');
return Util.compareSuccess({ response },this.answer);
} else {
return trigger(this.engine,this.clause,this.request,this.state,this.currentTime)
.then((actualAnswer) => {
this.answer = actualAnswer;
expect(actualAnswer).to.have.property('response');
expect(actualAnswer).to.not.have.property('error');
return Util.compareSuccess({ response },actualAnswer);
});
}
});
Then('the new state( of the contract) should be', function (expectedState) {
const state = JSON.parse(expectedState);
if (this.answer) {
expect(this.answer).to.have.property('state');
expect(this.answer).to.not.have.property('error');
return Util.compareSuccess({ state },this.answer);
} else {
return trigger(this.engine,this.clause,this.request,this.state,this.currentTime)
.then((actualAnswer) => {
this.answer = actualAnswer;
expect(actualAnswer).to.have.property('state');
expect(actualAnswer).to.not.have.property('error');
return Util.compareSuccess({ state },actualAnswer);
});
}
});
Then('the following obligations should have( also) been emitted', function (expectedEmit) {
const emit = JSON.parse(expectedEmit);
if (this.answer) {
expect(this.answer).to.have.property('emit');
expect(this.answer).to.not.have.property('error');
return Util.compareSuccess({ emit },this.answer);
} else {
return trigger(this.engine,this.clause,this.request,this.state,this.currentTime)
.then((actualAnswer) => {
this.answer = actualAnswer;
expect(actualAnswer).to.have.property('emit');
expect(actualAnswer).to.not.have.property('error');
return Util.compareSuccess({ emit },actualAnswer);
});
}
});
Then('it should reject the request with the error {string}', function (expectedError) {
return trigger(this.engine,this.clause,this.request,this.state,this.currentTime)
.catch((actualError) => {
expect(actualError.message).to.equal(expectedError);
});
});