Skip to content

Commit

Permalink
Server duration parser and test refactor (#729)
Browse files Browse the repository at this point in the history
* - Fixed #728
- server duration parser and test refactor

* Added more check in duration parser
  • Loading branch information
rg911 committed Dec 1, 2020
1 parent 1888cd0 commit 853f426
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 23 deletions.
36 changes: 29 additions & 7 deletions src/core/utils/DtoMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,31 @@ export class DtoMapping {
*/
public static parseServerDuration(serverValue: string): Duration {
const preprocessedValue = serverValue.replace(`'`, '').trim();
const regex = `([0-9]+)([hdms]+)[:\\s]?$`;
const patten = `([0-9]+)([hdms]+)[:\\s]?`;
const regex = new RegExp(patten, 'g');

// if the input doesn't match the patten, don't bother to do loop
if (!preprocessedValue.match(patten)) {
throw new Error('Duration value format is not recognized.');
}

let duration = Duration.ofSeconds(0);
const matcher = preprocessedValue.match(regex);
if (matcher && matcher.length === 3) {
const num = parseInt(matcher[1]);
const type = matcher[2];
let match;
const types: string[] = [];
let matchGroups = '';
while ((match = regex.exec(preprocessedValue))) {
if (!match) {
throw new Error('Duration value format is not recognized.');
}
const num = parseInt(match[1]);
const type = match[2];

// Added check make sure no duplicated types
if (types.indexOf(type) >= 0 || !match || match.length !== 3) {
throw new Error('Duration value format is not recognized.');
}
types.push(type);

switch (type) {
case 'ms':
duration = duration.plusMillis(num);
Expand All @@ -120,9 +139,12 @@ export class DtoMapping {
default:
throw new Error('Duration value format is not recognized.');
}
return duration;
matchGroups += match[0];
}
if (!types.length || matchGroups !== preprocessedValue) {
throw new Error('Duration value format is not recognized.');
}
throw new Error(`Duration value format is not recognized.`);
return duration;
}

/**
Expand Down
44 changes: 28 additions & 16 deletions test/core/utils/DtoMapping.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,42 @@ describe('DtoMapping', () => {
});

it('parseServerDuration', () => {
const epochS = '12345s';
expect(DtoMapping.parseServerDuration(epochS).seconds()).to.be.equal(12345);
const epochM = '12345m';
expect(DtoMapping.parseServerDuration(epochM).toMinutes()).to.be.equal(12345);
const epochH = '12345h';
expect(DtoMapping.parseServerDuration(epochH).toHours()).to.be.equal(12345);
const epochMS = '12345ms';
expect(DtoMapping.parseServerDuration(epochMS).toMillis()).to.be.equal(12345);
const epochD = '12345d';
expect(DtoMapping.parseServerDuration(epochD).toDays()).to.be.equal(12345);
expect(DtoMapping.parseServerDuration('15s').toString()).to.be.equal('PT15S');
expect(DtoMapping.parseServerDuration('10m:15s').toString()).to.be.equal('PT10M15S');
expect(DtoMapping.parseServerDuration('10m').toString()).to.be.equal('PT10M');
expect(DtoMapping.parseServerDuration('5h3m1s').toString()).to.be.equal('PT5H3M1S');
expect(DtoMapping.parseServerDuration('10d:5m1s').toString()).to.be.equal('PT240H5M1S');
expect(DtoMapping.parseServerDuration('10d 5m1s').toString()).to.be.equal('PT240H5M1S');
expect(DtoMapping.parseServerDuration('10d:5m100ms').toString()).to.be.equal('PT240H5M0.1S');
expect(DtoMapping.parseServerDuration('10d:5m1ms').toString()).to.be.equal('PT240H5M0.001S');
expect(DtoMapping.parseServerDuration(`1'200ms`).toString()).to.be.equal('PT1.2S');
expect(DtoMapping.parseServerDuration(`1d 2h`).toString()).to.be.equal('PT26H');
});

it('parseServerDuration - exception', () => {
expect(() => {
const epochS = '12345g';
DtoMapping.parseServerDuration(epochS).seconds();
DtoMapping.parseServerDuration('5sss');
}).to.throw();
expect(() => {
const epochS = 'adfs';
DtoMapping.parseServerDuration(epochS).seconds();
DtoMapping.parseServerDuration('10h:10h');
}).to.throw();
expect(() => {
const epochS = '123s45';
DtoMapping.parseServerDuration(epochS).seconds();
DtoMapping.parseServerDuration('abc');
}).to.throw();
expect(() => {
DtoMapping.parseServerDuration('abc 2s 1234');
}).to.throw();
expect(() => {
DtoMapping.parseServerDuration('5s10x');
}).to.throw();
expect(() => {
DtoMapping.parseServerDuration('10d 5m1s 1m');
}).to.throw();
expect(() => {
DtoMapping.parseServerDuration('5m 10d');
}).to.throw();
expect(() => {
DtoMapping.parseServerDuration('abc 10d');
}).to.throw();
});

Expand Down

0 comments on commit 853f426

Please sign in to comment.