Skip to content

Commit

Permalink
Test/identifiers test (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexveljkovic authored and kipliklotrika committed Nov 26, 2018
1 parent 6df6a53 commit a24ac42
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 3 deletions.
5 changes: 4 additions & 1 deletion modules/EventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,10 @@ class EventEmitter {
message: error.message,
});
remoteControl.importFailed(error);
notifyError(error);

if (error.type !== 'ImporterError') {
notifyError(error);
}
return;
}

Expand Down
17 changes: 17 additions & 0 deletions modules/GS1Importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Utilities = require('./Utilities');
const models = require('../models');
const ImportUtilities = require('./ImportUtilities');
const { denormalizeGraph, normalizeGraph } = require('./Database/graph-converter');
const { ImporterError } = require('./errors');

class GS1Importer {
/**
Expand Down Expand Up @@ -676,6 +677,22 @@ class GS1Importer {
});

try {
const sortedEvents = eventVertices.sort((a, b) => {
if (a._key < b._key) {
return -1;
}
if (a._key > b._key) {
return 1;
}
return 0;
});

for (let i = 1; i < sortedEvents.length; i += 1) {
if (sortedEvents[i]._key === sortedEvents[i - 1]._key) {
throw new ImporterError('Double event identifiers');
}
}

for (const vertex of allVertices) {
if (vertex.identifiers !== null) {
for (const identifier in vertex.identifiers) {
Expand Down
16 changes: 16 additions & 0 deletions modules/errors/importer-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Represents error that occurred during dataset import.
*/
class ImporterError extends Error {
constructor(message) {
super(message);

// Ensure the name of this error is the same as the class name
this.name = this.constructor.name;

// This clips the constructor invocation from the stack trace.
Error.captureStackTrace(this, this.constructor);
}
}

module.exports = ImporterError;
2 changes: 2 additions & 0 deletions modules/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
module.exports.NetworkRequestIgnoredError = require('./network-request-ignored-error');

module.exports.TransactionFailedError = require('./transaction-failed-error');

module.exports.ImporterError = require('./importer-error');
3 changes: 1 addition & 2 deletions modules/importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,7 @@ class Importer {
} catch (error) {
this.log.error(`Import error: ${error}.\n${error.stack}`);
this.remoteControl.importError(`Import error: ${error}.`);
this.notifyError(error);
const errorObject = { message: error.toString(), status: 400 };
const errorObject = { type: error.name, message: error.toString(), status: 400 };
return {
response: null,
error: errorObject,
Expand Down
12 changes: 12 additions & 0 deletions test/modules/gs1-importer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,18 @@ describe('GS1 Importer tests', () => {
it('and throw an error releted to missing SenderContactInformation', async () => expect(gs1.parseGS1(await Utilities.fileContents(xmlWithoutSenderContactinfo))).to.be.rejectedWith(Error, "Cannot read property 'EmailAddress' of undefined"));
});

describe('Double event identifiers should fail', () => {
const xmlDoubleIds = path.join(__dirname, 'test_xml/DoubleEventId.xml');

it('Should fail to import double event identifiers', async () => expect(gs1.parseGS1(await Utilities.fileContents(xmlDoubleIds))).to.rejectedWith(Error, 'Double event identifiers'));
});

describe('Multiple same identifiers for different vertices should import correctly', () => {
const xmlDoubleIds = path.join(__dirname, 'test_xml/MultipleIdentifiers.xml');

it('Should import without error', async () => expect(gs1.parseGS1(await Utilities.fileContents(xmlDoubleIds))).to.be.fulfilled);
});

afterEach('Drop DB', async () => {
if (systemDb) {
const listOfDatabases = await systemDb.listDatabases();
Expand Down
139 changes: 139 additions & 0 deletions test/modules/test_xml/DoubleEventId.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?xml version="1.0" encoding="UTF-8"?>
<epcis:EPCISDocument xmlns:epcis="urn:epcglobal:epcis:xsd:1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sbdh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader" schemaVersion="0" creationDate="2001-12-17T09:30:47Z" xsi:schemaLocation="urn:epcglobal:epcis:xsd:1 http://www.gs1si.org/BMS/epcis/1_2/EPCglobal-epcis-1_2.xsd">
<EPCISHeader>
<sbdh:StandardBusinessDocumentHeader>
<sbdh:HeaderVersion>1.0</sbdh:HeaderVersion>
<sbdh:Sender>
<sbdh:Identifier Authority="OriginTrail">urn:ot:object:actor:id:Company_1</sbdh:Identifier>
<sbdh:ContactInformation>
<sbdh:Contact>Abraham Smith</sbdh:Contact>
<sbdh:EmailAddress>[email protected]</sbdh:EmailAddress>
</sbdh:ContactInformation>
</sbdh:Sender>
<sbdh:Receiver>
<sbdh:Identifier Authority="OriginTrail">urn:ot:object:actor:id:OT</sbdh:Identifier>
<sbdh:ContactInformation>
<sbdh:Contact>Betty Johnson</sbdh:Contact>
<sbdh:EmailAddress>[email protected]</sbdh:EmailAddress>
</sbdh:ContactInformation>
</sbdh:Receiver>
<sbdh:DocumentIdentification>
<sbdh:Standard>GS1</sbdh:Standard>
<sbdh:TypeVersion>V1.3</sbdh:TypeVersion>
<sbdh:InstanceIdentifier>100001</sbdh:InstanceIdentifier>
<sbdh:Type>Shipment</sbdh:Type>
<sbdh:CreationDateAndTime>2018-01-01T00:31:52Z</sbdh:CreationDateAndTime>
</sbdh:DocumentIdentification>
<sbdh:BusinessScope>
<sbdh:Scope>
<sbdh:Type>BusinessProcess</sbdh:Type>
<sbdh:InstanceIdentifier>Shipment/version2-251</sbdh:InstanceIdentifier>
<sbdh:Identifier>EDI-Shipment</sbdh:Identifier>
</sbdh:Scope>
</sbdh:BusinessScope>
</sbdh:StandardBusinessDocumentHeader>
<extension>
<EPCISMasterData>
<VocabularyList>
<Vocabulary type="urn:ot:object:actor">
<VocabularyElementList>
<VocabularyElement id="urn:ot:object:actor:id:Company_1">
<attribute id="urn:ot:object:actor:name">Company_1</attribute>
<attribute id="urn:ot:object:actor:category">Company_1</attribute>
<attribute id="urn:ot:object:actor:wallet">0xe1e9c5379c5df627a8de3a951fa493028394a050</attribute>
</VocabularyElement>
</VocabularyElementList>
</Vocabulary>
<Vocabulary type="urn:ot:object:location">
<VocabularyElementList>
<VocabularyElement id="urn:epc:id:sgln:Company_1">
<attribute id="urn:ot:object:location:category">Building</attribute>
<attribute id="urn:ot:object:location:description">Labeling area</attribute>
<attribute id="urn:ot:object:location:actorId">urn:ot:object:actor:id:Company_1</attribute>
</VocabularyElement>
</VocabularyElementList>
</Vocabulary>
<Vocabulary type="urn:ot:object:product">
<VocabularyElementList>
<VocabularyElement id="urn:ot:object:product:id:P1">
<attribute id="urn:ot:object:product:name">P1</attribute>
<attribute id="urn:ot:object:product:category">CAT1</attribute>
</VocabularyElement>
</VocabularyElementList>
</Vocabulary>
<Vocabulary type="urn:ot:object:batch">
<VocabularyElementList>
<VocabularyElement id="urn:epc:id:sgtin:B1">
<attribute id="urn:ot:object:product:batch:productId">urn:ot:object:product:id:P1</attribute>
</VocabularyElement>
</VocabularyElementList>
</Vocabulary>
</VocabularyList>
</EPCISMasterData>
</extension>
</EPCISHeader>
<EPCISBody>
<EventList>
<ObjectEvent>
<eventTime>2018-01-20T00:00:01</eventTime> <!-- Mandatory-->
<eventTimeZoneOffset>-00:00</eventTimeZoneOffset> <!-- Mandatory-->
<epcList>
<epc>urn:epc:id:sgtin:B1</epc>
</epcList>
<action>OBSERVE</action> <!-- Mandatory-->
<bizStep>urn:epcglobal:cbv:bizstep:labeling</bizStep> <!-- Optional -->
<disposition>urn:epcglobal:cbv:disp:active</disposition> <!-- Optional -->
<readPoint> <!-- Optional -->
<id>urn:epc:id:sgln:Company_1</id>
</readPoint>
<bizLocation> <!-- Optional -->
<id>urn:epc:id:sgln:Company_1</id>
</bizLocation>
<extension>
<quantityList> <!-- Optional -->
<quantityElement>
<epcClass>urn:epc:id:sgtin:B1</epcClass>
<quantity>1</quantity>
<uom>PCS</uom>
</quantityElement>
</quantityList>
<extension>
<documentId>E1</documentId>
<OTEventClass>urn:ot:event:Observation</OTEventClass>
<OTEventType>Labeling</OTEventType>
</extension>
</extension>
</ObjectEvent>
<ObjectEvent>
<eventTime>2018-01-20T00:00:01</eventTime> <!-- Mandatory-->
<eventTimeZoneOffset>-00:00</eventTimeZoneOffset> <!-- Mandatory-->
<epcList>
<epc>urn:epc:id:sgtin:B1</epc>
</epcList>
<action>OBSERVE</action> <!-- Mandatory-->
<bizStep>urn:epcglobal:cbv:bizstep:labeling</bizStep> <!-- Optional -->
<disposition>urn:epcglobal:cbv:disp:active</disposition> <!-- Optional -->
<readPoint> <!-- Optional -->
<id>urn:epc:id:sgln:Company_1</id>
</readPoint>
<bizLocation> <!-- Optional -->
<id>urn:epc:id:sgln:Company_1</id>
</bizLocation>
<extension>
<quantityList> <!-- Optional -->
<quantityElement>
<epcClass>urn:epc:id:sgtin:B1</epcClass>
<quantity>1</quantity>
<uom>PCS</uom>
</quantityElement>
</quantityList>
<extension>
<documentId>E1</documentId>
<OTEventClass>urn:ot:event:Observation</OTEventClass>
<OTEventType>Labeling</OTEventType>
</extension>
</extension>
</ObjectEvent>
</EventList>
</EPCISBody>
</epcis:EPCISDocument>
111 changes: 111 additions & 0 deletions test/modules/test_xml/MultipleIdentifiers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?xml version="1.0" encoding="UTF-8"?>
<epcis:EPCISDocument xmlns:epcis="urn:epcglobal:epcis:xsd:1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sbdh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader" schemaVersion="0" creationDate="2001-12-17T09:30:47Z" xsi:schemaLocation="urn:epcglobal:epcis:xsd:1 http://www.gs1si.org/BMS/epcis/1_2/EPCglobal-epcis-1_2.xsd">
<EPCISHeader>
<sbdh:StandardBusinessDocumentHeader>
<sbdh:HeaderVersion>1.0</sbdh:HeaderVersion>
<sbdh:Sender>
<sbdh:Identifier Authority="OriginTrail">urn:ot:object:actor:id:Company_1</sbdh:Identifier>
<sbdh:ContactInformation>
<sbdh:Contact>Abraham Smith</sbdh:Contact>
<sbdh:EmailAddress>[email protected]</sbdh:EmailAddress>
</sbdh:ContactInformation>
</sbdh:Sender>
<sbdh:Receiver>
<sbdh:Identifier Authority="OriginTrail">urn:ot:object:actor:id:OT</sbdh:Identifier>
<sbdh:ContactInformation>
<sbdh:Contact>Betty Johnson</sbdh:Contact>
<sbdh:EmailAddress>[email protected]</sbdh:EmailAddress>
</sbdh:ContactInformation>
</sbdh:Receiver>
<sbdh:DocumentIdentification>
<sbdh:Standard>GS1</sbdh:Standard>
<sbdh:TypeVersion>V1.3</sbdh:TypeVersion>
<sbdh:InstanceIdentifier>100001</sbdh:InstanceIdentifier>
<sbdh:Type>Shipment</sbdh:Type>
<sbdh:CreationDateAndTime>2018-01-01T00:31:52Z</sbdh:CreationDateAndTime>
</sbdh:DocumentIdentification>
<sbdh:BusinessScope>
<sbdh:Scope>
<sbdh:Type>BusinessProcess</sbdh:Type>
<sbdh:InstanceIdentifier>Shipment/version2-251</sbdh:InstanceIdentifier>
<sbdh:Identifier>EDI-Shipment</sbdh:Identifier>
</sbdh:Scope>
</sbdh:BusinessScope>
</sbdh:StandardBusinessDocumentHeader>
<extension>
<EPCISMasterData>
<VocabularyList>
<Vocabulary type="urn:ot:object:actor">
<VocabularyElementList>
<VocabularyElement id="urn:ot:object:actor:id:Company_1">
<attribute id="urn:ot:object:actor:name">Company_1</attribute>
<attribute id="urn:ot:object:actor:category">Company_1</attribute>
<attribute id="urn:ot:object:actor:wallet">0xe1e9c5379c5df627a8de3a951fa493028394a050</attribute>
</VocabularyElement>
</VocabularyElementList>
</Vocabulary>
<Vocabulary type="urn:ot:object:location">
<VocabularyElementList>
<VocabularyElement id="urn:epc:id:sgln:Company_1">
<attribute id="urn:ot:object:location:category">Building</attribute>
<attribute id="urn:ot:object:location:description">Labeling area</attribute>
<attribute id="urn:ot:object:location:actorId">urn:ot:object:actor:id:Company_1</attribute>
</VocabularyElement>
</VocabularyElementList>
</Vocabulary>
<Vocabulary type="urn:ot:object:product">
<VocabularyElementList>
<VocabularyElement id="urn:ot:object:product:id:P1">
<attribute id="urn:ot:object:product:name">P1</attribute>
<attribute id="urn:ot:object:product:category">CAT1</attribute>
<attribute id="urn:ot:object:product:ean13" identifier="true">1234567890123</attribute>
</VocabularyElement>
</VocabularyElementList>
</Vocabulary>
<Vocabulary type="urn:ot:object:batch">
<VocabularyElementList>
<VocabularyElement id="urn:epc:id:sgtin:B1">
<attribute id="urn:ot:object:product:batch:productId">urn:ot:object:product:id:P1</attribute>
<attribute id="urn:ot:object:product:batch:ean13" identifier="true">1234567890123</attribute>
</VocabularyElement>
</VocabularyElementList>
</Vocabulary>
</VocabularyList>
</EPCISMasterData>
</extension>
</EPCISHeader>
<EPCISBody>
<EventList>
<ObjectEvent>
<eventTime>2018-01-20T00:00:01</eventTime> <!-- Mandatory-->
<eventTimeZoneOffset>-00:00</eventTimeZoneOffset> <!-- Mandatory-->
<epcList>
<epc>urn:epc:id:sgtin:B1</epc>
</epcList>
<action>OBSERVE</action> <!-- Mandatory-->
<bizStep>urn:epcglobal:cbv:bizstep:labeling</bizStep> <!-- Optional -->
<disposition>urn:epcglobal:cbv:disp:active</disposition> <!-- Optional -->
<readPoint> <!-- Optional -->
<id>urn:epc:id:sgln:Company_1</id>
</readPoint>
<bizLocation> <!-- Optional -->
<id>urn:epc:id:sgln:Company_1</id>
</bizLocation>
<extension>
<quantityList> <!-- Optional -->
<quantityElement>
<epcClass>urn:epc:id:sgtin:B1</epcClass>
<quantity>1</quantity>
<uom>PCS</uom>
</quantityElement>
</quantityList>
<extension>
<documentId>E1</documentId>
<OTEventClass>urn:ot:event:Observation</OTEventClass>
<OTEventType>Labeling</OTEventType>
</extension>
</extension>
</ObjectEvent>
</EventList>
</EPCISBody>
</epcis:EPCISDocument>

0 comments on commit a24ac42

Please sign in to comment.