Skip to content

Commit

Permalink
Fixed test cases for error handling changes
Browse files Browse the repository at this point in the history
  • Loading branch information
winklerj committed Oct 23, 2023
1 parent c943431 commit ada0222
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 50 deletions.
76 changes: 33 additions & 43 deletions spec/attachment2xml.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,40 +130,35 @@ describe('should convert XML attachment 2 JSON', function () {
});

it('XML too large', async () => {
let error;
try {
await attachmentToJson.process.bind(self)({
attachments: {
'po.xml': {
url: mockSever,
size: '66214473',
},
await attachmentToJson.process.bind(self)({
attachments: {
'po.xml': {
url: mockSever,
size: '66214473',
},
metadata: {},
}, cfg);
} catch (e) {
error = e;
}
expect(error.message).to.include('File limit is: 52428800 byte, file given was: 66214473 byte.');
},
metadata: {},
}, cfg);
const result = self.emit.getCalls();
expect(result[0].args[0]).to.eql('error');
expect(result[0].args[1].message).to.include('File limit is: 52428800 byte, file given was: 66214473 byte.');
});

it('Lower Max File Size - XML too large', async () => {
let error;
cfg.maxFileSize = 10971520;
try {
await attachmentToJson.process.bind(self)({
attachments: {
'po.xml': {
url: mockSever,
size: '16214473',
},
await attachmentToJson.process.bind(self)({
attachments: {
'po.xml': {
url: mockSever,
size: '16214473',
},
metadata: {},
}, cfg);
} catch (e) {
error = e;
}
expect(error.message).to.include('File limit is: 10971520 byte, file given was: 16214473 byte.');
},
metadata: {},
}, cfg);

const result = self.emit.getCalls();
expect(result[0].args[0]).to.eql('error');
expect(result[0].args[1].message).to.include('File limit is: 10971520 byte, file given was: 16214473 byte.');
});

it('Higher Max File Size - Convert attachment to JSON', async () => {
Expand Down Expand Up @@ -206,22 +201,17 @@ describe('should convert XML attachment 2 JSON', function () {
});

it('Empty attachment', async () => {
let error;

try {
await attachmentToJson.process.bind(self)({
attachments: {
'po.xml': {
url: `${mockSever}/EmptyFile`,
},
await attachmentToJson.process.bind(self)({
attachments: {
'po.xml': {
url: `${mockSever}/EmptyFile`,
},
metadata: {},
}, cfg);
} catch (e) {
error = e;
}
// eslint-disable-next-line no-unused-expressions
expect(error.message).to.be.equal('Empty attachment received for file po.xml');
},
metadata: {},
}, cfg);
const result = self.emit.getCalls();
expect(result[0].args[0]).to.eql('error');
expect(result[0].args[1].message).to.be.equal('Empty attachment received for file po.xml');
});

it('Custom JSONata transformation', async () => {
Expand Down
21 changes: 17 additions & 4 deletions spec/json2xml.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const context = {
logger: {
debug: () => {},
info: () => {},
error: () => {},
child: () => context.logger,
},
};
Expand Down Expand Up @@ -124,7 +125,10 @@ describe('JSON to XML', () => {
headerStandalone: false,
};

await expect(json2xml.process.call(context, msg, cfg, {})).to.be.rejectedWith('XML data is 15728713 bytes, and is too large to upload as an attachment. Max attachment size is 10485760 bytes');
await json2xml.process.call(context, msg, cfg, {});
const result = context.emit.getCalls();
expect(result[0].args[0]).to.equal('error');
expect(result[0].args[1].message).to.equal('XML data is 15728713 bytes, and is too large to upload as an attachment. Max attachment size is 10485760 bytes');
});

it('Non object input', async () => {
Expand All @@ -140,7 +144,10 @@ describe('JSON to XML', () => {
};
const cfg = {};

await expect(json2xml.process.call(context, msg, cfg, {})).to.be.rejectedWith('Input must be an object with exactly one key.');
await json2xml.process.call(context, msg, cfg, {});
const result = context.emit.getCalls();
expect(result[0].args[0]).to.equal('error');
expect(result[0].args[1].message).to.equal('Input must be an object with exactly one key.');
});

it('Too many keys input', async () => {
Expand All @@ -155,7 +162,10 @@ describe('JSON to XML', () => {
};
const cfg = {};

await expect(json2xml.process.call(context, msg, cfg, {})).to.be.rejectedWith('Input must be an object with exactly one key.');
await json2xml.process.call(context, msg, cfg, {});
const result = context.emit.getCalls();
expect(result[0].args[0]).to.equal('error');
expect(result[0].args[1].message).to.equal('Input must be an object with exactly one key.');
});

it('Invalid xml tag name', async () => {
Expand All @@ -173,7 +183,10 @@ describe('JSON to XML', () => {
};
const cfg = {};

await expect(json2xml.process.call(context, msg, cfg, {})).to.be.rejectedWith('Invalid character in name');
await json2xml.process.call(context, msg, cfg, {});
const result = context.emit.getCalls();
expect(result[0].args[0]).to.equal('error');
expect(result[0].args[1].message).to.equal('Invalid character in name');
});

it('should convert JSON to XML 1', async () => {
Expand Down
8 changes: 5 additions & 3 deletions spec/json2xmlold.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('JSON 2 XML converter (Old)', () => {
logger: {
debug: () => {},
info: () => {},
error: () => {},
child: () => self.logger,
},
emit: sinon.spy(),
Expand Down Expand Up @@ -48,8 +49,9 @@ describe('JSON 2 XML converter (Old)', () => {
const messageText = 'Can\'t create XML element from prop that starts with digit.'
+ 'See XML naming rules https://www.w3schools.com/xml/xml_elements.asp';

await jsonToXml.process.call(self, message, {}).catch((error) => {
expect(error.message).to.equal(`Prop name is invalid for XML tag: 386. ${messageText}`);
});
await jsonToXml.process.call(self, message, {});
const result = self.emit.getCalls();
expect(result[0].args[0]).to.equal('error');
expect(result[0].args[1].message).to.equal(`Prop name is invalid for XML tag: 386. ${messageText}`);
});
});

0 comments on commit ada0222

Please sign in to comment.