Skip to content

Commit ef14742

Browse files
committed
update test
1 parent 5459754 commit ef14742

File tree

2 files changed

+147
-55
lines changed

2 files changed

+147
-55
lines changed

index.js

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -220,51 +220,6 @@ class ServerlessStepFunctions {
220220
});
221221
}
222222

223-
yamlParse() {
224-
const servicePath = this.serverless.config.servicePath;
225-
226-
if (!servicePath) {
227-
return BbPromise.resolve();
228-
}
229-
230-
let serverlessYmlPath = path.join(servicePath, 'serverless.yml');
231-
if (!this.serverless.utils.fileExistsSync(serverlessYmlPath)) {
232-
serverlessYmlPath = path
233-
.join(this.serverless.config.servicePath, 'serverless.yaml');
234-
}
235-
236-
return this.serverless.yamlParser
237-
.parse(serverlessYmlPath)
238-
.then((serverlessFileParam) => {
239-
this.stepFunctions = serverlessFileParam.stepFunctions;
240-
return BbPromise.resolve();
241-
});
242-
}
243-
244-
compile() {
245-
if (!this.stepFunctions) {
246-
return BbPromise.resolve();
247-
}
248-
249-
if (typeof this.stepFunctions[this.options.state] === 'undefined') {
250-
const errorMessage = [
251-
`Step function "${this.options.state}" is not exists`,
252-
].join('');
253-
throw new this.serverless.classes.Error(errorMessage);
254-
}
255-
256-
_.forEach(this.stepFunctions[this.options.state].States, (value, key) => {
257-
if (value.Resource && !value.Resource.match(/arn:aws:lambda/)) {
258-
this.stepFunctions[this.options.state].States[key].Resource
259-
= this.functionArns[value.Resource];
260-
}
261-
});
262-
263-
this.awsStateLanguage[this.options.state] =
264-
JSON.stringify(this.stepFunctions[this.options.state]);
265-
return BbPromise.resolve();
266-
}
267-
268223
deleteStateMachine() {
269224
return this.provider.request('StepFunctions',
270225
'deleteStateMachine',
@@ -339,5 +294,50 @@ class ServerlessStepFunctions {
339294
return BbPromise.resolve();
340295
});
341296
}
297+
298+
yamlParse() {
299+
const servicePath = this.serverless.config.servicePath;
300+
301+
if (!servicePath) {
302+
return BbPromise.resolve();
303+
}
304+
305+
let serverlessYmlPath = path.join(servicePath, 'serverless.yml');
306+
if (!this.serverless.utils.fileExistsSync(serverlessYmlPath)) {
307+
serverlessYmlPath = path
308+
.join(this.serverless.config.servicePath, 'serverless.yaml');
309+
}
310+
311+
return this.serverless.yamlParser
312+
.parse(serverlessYmlPath)
313+
.then((serverlessFileParam) => {
314+
this.stepFunctions = serverlessFileParam.stepFunctions;
315+
return BbPromise.resolve();
316+
});
317+
}
318+
319+
compile() {
320+
if (!this.stepFunctions) {
321+
return BbPromise.resolve();
322+
}
323+
324+
if (typeof this.stepFunctions[this.options.state] === 'undefined') {
325+
const errorMessage = [
326+
`Step function "${this.options.state}" is not exists`,
327+
].join('');
328+
throw new this.serverless.classes.Error(errorMessage);
329+
}
330+
331+
_.forEach(this.stepFunctions[this.options.state].States, (value, key) => {
332+
if (value.Resource && !value.Resource.match(/arn:aws:lambda/)) {
333+
this.stepFunctions[this.options.state].States[key].Resource
334+
= this.functionArns[value.Resource];
335+
}
336+
});
337+
338+
this.awsStateLanguage[this.options.state] =
339+
JSON.stringify(this.stepFunctions[this.options.state]);
340+
return BbPromise.resolve();
341+
}
342342
}
343343
module.exports = ServerlessStepFunctions;

index.test.js

Lines changed: 102 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe('ServerlessStepFunctions', () => {
2929
name: 'first',
3030
},
3131
state: 'stateMachine',
32+
data: 'inputData',
3233
};
3334
serverless.init();
3435
serverless.setProvider('aws', new AwsProvider(serverless));
@@ -187,7 +188,7 @@ describe('ServerlessStepFunctions', () => {
187188
let getRoleStub;
188189
beforeEach(() => {
189190
getRoleStub = sinon.stub(serverlessStepFunctions.provider, 'request')
190-
.returns(BbPromise.resolve({Role:{Arn:'roleArn'}}));
191+
.returns(BbPromise.resolve({ Role: { Arn: 'roleArn' } }));
191192
});
192193

193194
it('should getIamRole with correct params', () => serverlessStepFunctions.getIamRole()
@@ -210,12 +211,13 @@ describe('ServerlessStepFunctions', () => {
210211
it('should createRole when statusCode is 404', () => {
211212
serverlessStepFunctions.provider.request.restore();
212213
const getRoleErrorStub = sinon.stub(serverlessStepFunctions.provider, 'request')
213-
.returns(BbPromise.reject({statusCode: 404}));
214+
.returns(BbPromise.reject({ statusCode: 404 }));
214215
const createIamRoleStub = sinon
215216
.stub(serverlessStepFunctions, 'createIamRole').returns(BbPromise.resolve());
216217

217-
serverlessStepFunctions.getIamRole().catch((error) => {
218-
expect(createIamRole.calledOnce).to.be.equal(true);
218+
serverlessStepFunctions.getIamRole().catch(() => {
219+
expect(createIamRoleStub.calledOnce).to.be.equal(true);
220+
expect(getRoleErrorStub.calledOnce).to.be.equal(true);
219221
serverlessStepFunctions.provider.request.restore();
220222
serverlessStepFunctions.createIamRole.restore();
221223
});
@@ -224,9 +226,10 @@ describe('ServerlessStepFunctions', () => {
224226
it('should throw error when statusCode is not 404', () => {
225227
serverlessStepFunctions.provider.request.restore();
226228
const getRoleErrorStub = sinon.stub(serverlessStepFunctions.provider, 'request')
227-
.returns(BbPromise.reject({statusCode: 502}));
229+
.returns(BbPromise.reject({ statusCode: 502 }));
228230

229231
serverlessStepFunctions.getIamRole().catch((error) => {
232+
expect(getRoleErrorStub.calledOnce).to.be.equal(true);
230233
expect(error.name).to.be.equal('ServerlessError');
231234
serverlessStepFunctions.provider.request.restore();
232235
});
@@ -237,7 +240,7 @@ describe('ServerlessStepFunctions', () => {
237240
let getCallerIdentityStub;
238241
beforeEach(() => {
239242
getCallerIdentityStub = sinon.stub(serverlessStepFunctions.provider, 'request')
240-
.returns(BbPromise.resolve({Account:1234}));
243+
.returns(BbPromise.resolve({ Account: 1234 }));
241244
});
242245

243246
it('should getFunctionArns with correct params', () => serverlessStepFunctions.getFunctionArns()
@@ -261,8 +264,8 @@ describe('ServerlessStepFunctions', () => {
261264
let createIamRoleStub;
262265
beforeEach(() => {
263266
createIamRoleStub = sinon.stub(serverlessStepFunctions.provider, 'request');
264-
createIamRoleStub.onFirstCall().returns(BbPromise.resolve({Role:{Arn:'roleArn'}}));
265-
createIamRoleStub.onSecondCall().returns(BbPromise.resolve({Policy:{Arn:'policyArn'}}));
267+
createIamRoleStub.onFirstCall().returns(BbPromise.resolve({ Role: { Arn: 'roleArn' } }));
268+
createIamRoleStub.onSecondCall().returns(BbPromise.resolve({ Policy: { Arn: 'policyArn' } }));
266269
createIamRoleStub.onThirdCall().returns(BbPromise.resolve());
267270
});
268271

@@ -284,10 +287,11 @@ describe('ServerlessStepFunctions', () => {
284287
let getStateMachineStub;
285288
beforeEach(() => {
286289
getStateMachineStub = sinon.stub(serverlessStepFunctions.provider, 'request')
287-
.returns(BbPromise.resolve({Account:1234}));
290+
.returns(BbPromise.resolve({ Account: 1234 }));
288291
});
289292

290-
it('should getStateMachineStub with correct params', () => serverlessStepFunctions.getStateMachineArn()
293+
it('should getStateMachineStub with correct params'
294+
, () => serverlessStepFunctions.getStateMachineArn()
291295
.then(() => {
292296
expect(getStateMachineStub.calledOnce).to.be.equal(true);
293297
expect(getStateMachineStub.calledWithExactly(
@@ -303,5 +307,93 @@ describe('ServerlessStepFunctions', () => {
303307
})
304308
);
305309
});
310+
311+
describe('#deleteStateMachine()', () => {
312+
let deleteStateMachineStub;
313+
beforeEach(() => {
314+
deleteStateMachineStub = sinon.stub(serverlessStepFunctions.provider, 'request')
315+
.returns(BbPromise.resolve({ Account: 1234 }));
316+
});
317+
318+
it('should deleteStateMachine with correct params'
319+
, () => serverlessStepFunctions.deleteStateMachine()
320+
.then(() => {
321+
expect(deleteStateMachineStub.calledOnce).to.be.equal(true);
322+
expect(deleteStateMachineStub.calledWithExactly(
323+
'StepFunctions',
324+
'deleteStateMachine',
325+
{
326+
stateMachineArn: serverlessStepFunctions.stateMachineArn,
327+
},
328+
serverlessStepFunctions.options.stage,
329+
serverlessStepFunctions.options.region
330+
)).to.be.equal(true);
331+
serverlessStepFunctions.provider.request.restore();
332+
})
333+
);
334+
});
335+
336+
describe('#createStateMachine()', () => {
337+
// todo
338+
});
339+
340+
describe('#startExecution()', () => {
341+
let startExecutionStub;
342+
beforeEach(() => {
343+
startExecutionStub = sinon.stub(serverlessStepFunctions.provider, 'request')
344+
.returns(BbPromise.resolve({ executionArn: 'executionArn' }));
345+
});
346+
347+
it('should startExecution with correct params', () => serverlessStepFunctions.startExecution()
348+
.then(() => {
349+
expect(startExecutionStub.calledOnce).to.be.equal(true);
350+
expect(startExecutionStub.calledWithExactly(
351+
'StepFunctions',
352+
'startExecution',
353+
{
354+
stateMachineArn: serverlessStepFunctions.stateMachineArn,
355+
input: serverlessStepFunctions.options.data,
356+
},
357+
serverlessStepFunctions.options.stage,
358+
serverlessStepFunctions.options.region
359+
)).to.be.equal(true);
360+
expect(serverlessStepFunctions.executionArn).to.be.equal('executionArn');
361+
serverlessStepFunctions.provider.request.restore();
362+
})
363+
);
364+
});
365+
366+
describe('#describeExecution()', () => {
367+
let describeExecutionStub;
368+
beforeEach(() => {
369+
describeExecutionStub = sinon.stub(serverlessStepFunctions.provider, 'request')
370+
.returns(BbPromise.resolve({ status: 'SUCCESS' }));
371+
});
372+
373+
it('should describeExecution with correct params'
374+
, () => serverlessStepFunctions.describeExecution()
375+
.then(() => {
376+
expect(describeExecutionStub.calledOnce).to.be.equal(true);
377+
expect(describeExecutionStub.calledWithExactly(
378+
'StepFunctions',
379+
'describeExecution',
380+
{
381+
executionArn: serverlessStepFunctions.executionArn,
382+
},
383+
serverlessStepFunctions.options.stage,
384+
serverlessStepFunctions.options.region
385+
)).to.be.equal(true);
386+
serverlessStepFunctions.provider.request.restore();
387+
})
388+
);
389+
});
390+
391+
describe('#yamlParse()', () => {
392+
// todo
393+
});
394+
395+
describe('#compile()', () => {
396+
// todo
397+
});
306398
});
307399

0 commit comments

Comments
 (0)