diff --git a/packages/vehicle-lifecycle-network/.eslintignore b/packages/vehicle-lifecycle-network/.eslintignore index ccb9c0a..9083c5c 100644 --- a/packages/vehicle-lifecycle-network/.eslintignore +++ b/packages/vehicle-lifecycle-network/.eslintignore @@ -1,6 +1,5 @@ coverage dist go -lib node_modules out \ No newline at end of file diff --git a/packages/vehicle-lifecycle-network/.eslintrc.yml b/packages/vehicle-lifecycle-network/.eslintrc.yml index 59931e1..3438670 100755 --- a/packages/vehicle-lifecycle-network/.eslintrc.yml +++ b/packages/vehicle-lifecycle-network/.eslintrc.yml @@ -4,13 +4,9 @@ env: mocha: true extends: 'eslint:recommended' parserOptions: + ecmaVersion: 8 sourceType: - script -globals: - getAssetRegistry: true - getFactory: true - getParticipantRegistry: true - getCurrentParticipant: true rules: indent: - error @@ -25,13 +21,13 @@ rules: - error - always no-unused-vars: - - 0 + - error - args: none + no-var: error no-console: off curly: error eqeqeq: error no-throw-literal: error - strict: error dot-notation: error no-tabs: error no-trailing-spaces: error diff --git a/packages/vehicle-lifecycle-network/lib/manufacturer.js b/packages/vehicle-lifecycle-network/lib/manufacturer.js index d0a80ed..be55024 100644 --- a/packages/vehicle-lifecycle-network/lib/manufacturer.js +++ b/packages/vehicle-lifecycle-network/lib/manufacturer.js @@ -12,37 +12,33 @@ * limitations under the License. */ +/* global getFactory getAssetRegistry emit */ /** * Place an order for a vehicle * @param {org.acme.vehicle.lifecycle.manufacturer.PlaceOrder} placeOrder - the PlaceOrder transaction * @transaction */ -function placeOrder(placeOrder) { +async function placeOrder(placeOrder) { // eslint-disable-line no-unused-vars console.log('placeOrder'); - var factory = getFactory(); - var NS_M = 'org.acme.vehicle.lifecycle.manufacturer'; - var NS = 'org.acme.vehicle.lifecycle'; - var NS_D = 'org.vda'; + const factory = getFactory(); + const NS_M = 'org.acme.vehicle.lifecycle.manufacturer'; + const NS = 'org.acme.vehicle.lifecycle'; - var order = factory.newResource(NS_M, 'Order', placeOrder.orderId); + const order = factory.newResource(NS_M, 'Order', placeOrder.orderId); order.vehicleDetails = placeOrder.vehicleDetails; order.orderStatus = 'PLACED'; order.manufacturer = placeOrder.manufacturer; order.orderer = factory.newRelationship(NS, 'PrivateOwner', placeOrder.orderer.getIdentifier()); // save the order - return getAssetRegistry(order.getFullyQualifiedType()) - .then(function (registry) { - return registry.add(order); - }) - .then(function(){ - var placeOrderEvent = factory.newEvent(NS_M, 'PlaceOrderEvent'); - placeOrderEvent.orderId = order.orderId; - placeOrderEvent.vehicleDetails = order.vehicleDetails; - emit(placeOrderEvent); - }); + const registry = await getAssetRegistry(order.getFullyQualifiedType()); + await registry.add(order); + const placeOrderEvent = factory.newEvent(NS_M, 'PlaceOrderEvent'); + placeOrderEvent.orderId = order.orderId; + placeOrderEvent.vehicleDetails = order.vehicleDetails; + emit(placeOrderEvent); } /** @@ -50,71 +46,61 @@ function placeOrder(placeOrder) { * @param {org.acme.vehicle.lifecycle.manufacturer.UpdateOrderStatus} updateOrderStatus - the UpdateOrderStatus transaction * @transaction */ -function updateOrderStatus(updateOrderStatus) { +async function updateOrderStatus(updateOrderStatus) { // eslint-disable-line no-unused-vars console.log('updateOrderStatus'); - var factory = getFactory(); - var NS_M = 'org.acme.vehicle.lifecycle.manufacturer'; - var NS = 'org.acme.vehicle.lifecycle'; - var NS_D = 'org.vda'; + const factory = getFactory(); + const NS_M = 'org.acme.vehicle.lifecycle.manufacturer'; + const NS = 'org.acme.vehicle.lifecycle'; + const NS_D = 'org.vda'; // save the new status of the order updateOrderStatus.order.orderStatus = updateOrderStatus.orderStatus; - // get vehicle registry - return getAssetRegistry(NS_D + '.Vehicle') - .then(function(registry) { - if (updateOrderStatus.orderStatus === 'VIN_ASSIGNED') { - var vehicle = factory.newResource(NS_D, 'Vehicle', updateOrderStatus.vin ); - vehicle.vehicleDetails = updateOrderStatus.order.vehicleDetails; - vehicle.vehicleDetails.vin = updateOrderStatus.vin; - vehicle.vehicleStatus = 'OFF_THE_ROAD'; - return registry.add(vehicle); - } else if(updateOrderStatus.orderStatus === 'OWNER_ASSIGNED') { - if (!updateOrderStatus.order.orderer.vehicles) { - updateOrderStatus.order.orderer.vehicles = []; - } + // get vehicle registry + const registry = await getAssetRegistry(NS_D + '.Vehicle'); + if (updateOrderStatus.orderStatus === 'VIN_ASSIGNED') { + const vehicle = factory.newResource(NS_D, 'Vehicle', updateOrderStatus.vin); + vehicle.vehicleDetails = updateOrderStatus.order.vehicleDetails; + vehicle.vehicleDetails.vin = updateOrderStatus.vin; + vehicle.vehicleStatus = 'OFF_THE_ROAD'; + return registry.add(vehicle); + } else if (updateOrderStatus.orderStatus === 'OWNER_ASSIGNED') { + if (!updateOrderStatus.order.orderer.vehicles) { + updateOrderStatus.order.orderer.vehicles = []; + } - return registry.get(updateOrderStatus.vin) - .then(function(vehicle) { - vehicle.vehicleStatus = 'ACTIVE'; - vehicle.owner = factory.newRelationship('org.acme.vehicle.lifecycle', 'PrivateOwner', updateOrderStatus.order.orderer.email); - vehicle.numberPlate = updateOrderStatus.numberPlate || ''; - vehicle.vehicleDetails.numberPlate = updateOrderStatus.numberPlate || ''; - vehicle.vehicleDetails.v5c = updateOrderStatus.v5c || ''; - if (!vehicle.logEntries) { - vehicle.logEntries = []; - } - var logEntry = factory.newConcept(NS_D, 'VehicleTransferLogEntry'); - logEntry.vehicle = factory.newRelationship(NS_D, 'Vehicle', updateOrderStatus.vin); - logEntry.buyer = factory.newRelationship(NS, 'PrivateOwner', updateOrderStatus.order.orderer.email); - logEntry.timestamp = updateOrderStatus.timestamp; - vehicle.logEntries.push(logEntry); - return registry.update(vehicle); - }); - } - }) - .then(function() { - // get order registry - return getAssetRegistry(updateOrderStatus.order.getFullyQualifiedType()); - }) - .then(function(registry) { - // update order status - updateOrderStatus.order.vehicleDetails.vin = updateOrderStatus.vin || ''; - - if (!updateOrderStatus.order.statusUpdates) { - updateOrderStatus.order.statusUpdates = []; - } + const vehicle = await registry.get(updateOrderStatus.vin); + vehicle.vehicleStatus = 'ACTIVE'; + vehicle.owner = factory.newRelationship('org.acme.vehicle.lifecycle', 'PrivateOwner', updateOrderStatus.order.orderer.email); + vehicle.numberPlate = updateOrderStatus.numberPlate || ''; + vehicle.vehicleDetails.numberPlate = updateOrderStatus.numberPlate || ''; + vehicle.vehicleDetails.v5c = updateOrderStatus.v5c || ''; + if (!vehicle.logEntries) { + vehicle.logEntries = []; + } + const logEntry = factory.newConcept(NS_D, 'VehicleTransferLogEntry'); + logEntry.vehicle = factory.newRelationship(NS_D, 'Vehicle', updateOrderStatus.vin); + logEntry.buyer = factory.newRelationship(NS, 'PrivateOwner', updateOrderStatus.order.orderer.email); + logEntry.timestamp = updateOrderStatus.timestamp; + vehicle.logEntries.push(logEntry); + await registry.update(vehicle); + } - updateOrderStatus.order.statusUpdates.push(updateOrderStatus); + // get order registry + const orderRegistry = await getAssetRegistry(updateOrderStatus.order.getFullyQualifiedType()); + // update order status + updateOrderStatus.order.vehicleDetails.vin = updateOrderStatus.vin || ''; - return registry.update(updateOrderStatus.order); - }) - .then(function(){ - var updateOrderStatusEvent = factory.newEvent(NS_M, 'UpdateOrderStatusEvent'); - updateOrderStatusEvent.orderStatus = updateOrderStatus.order.orderStatus; - updateOrderStatusEvent.order = updateOrderStatus.order; - emit(updateOrderStatusEvent); - }); - + if (!updateOrderStatus.order.statusUpdates) { + updateOrderStatus.order.statusUpdates = []; + } + + updateOrderStatus.order.statusUpdates.push(updateOrderStatus); + + await orderRegistry.update(updateOrderStatus.order); + const updateOrderStatusEvent = factory.newEvent(NS_M, 'UpdateOrderStatusEvent'); + updateOrderStatusEvent.orderStatus = updateOrderStatus.order.orderStatus; + updateOrderStatusEvent.order = updateOrderStatus.order; + emit(updateOrderStatusEvent); } diff --git a/packages/vehicle-lifecycle-network/lib/setup.js b/packages/vehicle-lifecycle-network/lib/setup.js index 45876f1..2a14a57 100644 --- a/packages/vehicle-lifecycle-network/lib/setup.js +++ b/packages/vehicle-lifecycle-network/lib/setup.js @@ -12,21 +12,23 @@ * limitations under the License. */ +/* global getFactory getParticipantRegistry getAssetRegistry */ + /** * Setup the demo * @param {org.acme.vehicle.lifecycle.SetupDemo} setupDemo - the SetupDemo transaction * @transaction */ -function setupDemo(setupDemo) { +async function setupDemo(setupDemo) { // eslint-disable-line no-unused-vars console.log('setupDemo'); - var factory = getFactory(); - var NS_M = 'org.acme.vehicle.lifecycle.manufacturer'; - var NS = 'org.acme.vehicle.lifecycle'; - var NS_D = 'org.vda'; + const factory = getFactory(); + const NS_M = 'org.acme.vehicle.lifecycle.manufacturer'; + const NS = 'org.acme.vehicle.lifecycle'; + const NS_D = 'org.vda'; - var names = ['dan', 'simon', 'jake', 'anastasia', 'matthew', 'mark', 'fenglian', 'sam', 'james', 'nick', 'caroline', 'rachel', 'john', 'rob', 'tom', 'paul', 'ed', 'dave', 'anthony', 'toby', 'ant', 'matt', 'anna']; - var vehicles = { + const names = ['dan', 'simon', 'jake', 'anastasia', 'matthew', 'mark', 'fenglian', 'sam', 'james', 'nick', 'caroline', 'rachel', 'john', 'rob', 'tom', 'paul', 'ed', 'dave', 'anthony', 'toby', 'ant', 'matt', 'anna']; + const vehicles = { 'Arium': { 'Nova': [ { @@ -42,40 +44,40 @@ function setupDemo(setupDemo) { 'vehicleStatus': 'ACTIVE' } ] - }, + }, 'Morde': { 'Putt': [ { - 'vin': '6437956437', + 'vin': '6437956437', 'colour': 'black', - 'vehicleStatus': 'ACTIVE', + 'vehicleStatus': 'ACTIVE', 'suspiciousMessage': 'Mileage anomaly' }, { - 'vin': '857642213', + 'vin': '857642213', 'colour': 'red', 'vehicleStatus': 'ACTIVE' }, { - 'vin': '542376495', + 'vin': '542376495', 'colour': 'silver', 'vehicleStatus': 'ACTIVE' } ], 'Pluto': [ { - 'vin': '976431649', + 'vin': '976431649', 'colour': 'white', 'vehicleStatus': 'ACTIVE' }, { - 'vin': '564215468', + 'vin': '564215468', 'colour': 'green', - 'vehicleStatus': 'ACTIVE', + 'vehicleStatus': 'ACTIVE', 'suspiciousMessage': 'Insurance write-off but still active' }, { - 'vin': '784512464', + 'vin': '784512464', 'colour': 'grey', 'vehicleStatus': 'ACTIVE' } @@ -91,109 +93,89 @@ function setupDemo(setupDemo) { { 'vin': '312457645', 'colour': 'white', - 'vehicleStatus': 'ACTIVE', + 'vehicleStatus': 'ACTIVE', 'suspiciousMessage': 'Suspicious ownership sequence' }, { 'vin': '65235647', 'colour': 'silver', - 'vehicleStatus': 'ACTIVE', + 'vehicleStatus': 'ACTIVE', 'suspiciousMessage': 'Untaxed vehicle' } - ], + ], 'Rancher': [ { 'vin': '85654575', 'colour': 'blue', 'vehicleStatus': 'ACTIVE' - }, + }, { 'vin': '326548754', 'colour': 'white', - 'vehicleStatus': 'ACTIVE', + 'vehicleStatus': 'ACTIVE', 'suspiciousMessage': 'Uninsured vehicle' } ] } }; - - var manufacturers = []; - var privateOwners = []; - - for (var name in vehicles) { - var manufacturer = factory.newResource(NS_M, 'Manufacturer', name); - manufacturers.push(manufacturer); - } - for(var i=0; i { + return factory.newResource(NS_M, 'Manufacturer', name); + }); + const manufacturerRegistry = await getParticipantRegistry(NS_M + '.Manufacturer'); + await manufacturerRegistry.addAll(manufacturers); - var regulator = factory.newResource(NS, 'Regulator', 'regulator'); + // register private owners + const privateOwners = names.map(name => { + return factory.newResource(NS, 'PrivateOwner', name); + }); + const privateOwnerRegistry = await getParticipantRegistry(NS + '.PrivateOwner'); + await privateOwnerRegistry.addAll(privateOwners); + // register regulator + const regulator = factory.newResource(NS, 'Regulator', 'regulator'); + const regulatorRegistry = await getParticipantRegistry(NS + '.Regulator'); + await regulatorRegistry.add(regulator); - var privateOwnerRegistry; - var vehicleRegistry; + // register vehicles + const vs = []; + let carCount = 0; + for (const mName in vehicles) { + const manufacturer = vehicles[mName]; + for (const mModel in manufacturer) { + const model = manufacturer[mModel]; + for (let i = 0; i < model.length; i++) { + const vehicleTemplate = model[i]; + const vehicle = factory.newResource(NS_D, 'Vehicle', vehicleTemplate.vin); + vehicle.owner = factory.newRelationship(NS, 'PrivateOwner', names[carCount]); + vehicle.vehicleStatus = vehicleTemplate.vehicleStatus; + vehicle.vehicleDetails = factory.newConcept(NS_D, 'VehicleDetails'); + vehicle.vehicleDetails.make = mName; + vehicle.vehicleDetails.modelType = mModel; + vehicle.vehicleDetails.colour = vehicleTemplate.colour; + vehicle.vehicleDetails.vin = vehicleTemplate.vin; - return getParticipantRegistry(NS + '.Regulator') - .then(function(regulatorRegistry) { - return regulatorRegistry.add(regulator); - }) - .then(function() { - return getParticipantRegistry(NS_M + '.Manufacturer'); - }) - .then(function(manufacturerRegistry) { - return manufacturerRegistry.addAll(manufacturers); - }) - .then(function() { - return getParticipantRegistry(NS + '.PrivateOwner'); - }) - .then(function(privateOwnerRegistry) { - return privateOwnerRegistry.addAll(privateOwners); - }) - .then(function() { - return getAssetRegistry(NS_D + '.Vehicle'); - }) - .then(function(vehicleRegistry) { - var vs = []; - var carCount = 0; - for (var mName in vehicles) { - var manufacturer = vehicles[mName]; - for (var mModel in manufacturer) { - var model = manufacturer[mModel]; - for(var i=0; i=1 ) { - var factory = getFactory(); - var vehiclesToScrap = vehicles.filter(function(vehicle) { - return vehicle.vehicleStatus !== 'SCRAPPED'; - }); - for (var x = 0; x < vehiclesToScrap.length; x++) { - vehiclesToScrap[x].vehicleStatus = 'SCRAPPED'; - var scrapVehicleEvent = factory.newEvent(NS_D, 'ScrapVehicleEvent'); - scrapVehicleEvent.vehicle = vehiclesToScrap[x]; - emit(scrapVehicleEvent); - } - return assetRegistry.updateAll(vehiclesToScrap); - } + const NS_D = 'org.vda'; + const assetRegistry = await getAssetRegistry(NS_D + '.Vehicle'); + const vehicles = await query('selectAllCarsByColour', {'colour': scrapAllVehicles.colour}); + if (vehicles.length >= 1) { + const factory = getFactory(); + const vehiclesToScrap = vehicles.filter(function (vehicle) { + return vehicle.vehicleStatus !== 'SCRAPPED'; }); + for (let x = 0; x < vehiclesToScrap.length; x++) { + vehiclesToScrap[x].vehicleStatus = 'SCRAPPED'; + const scrapVehicleEvent = factory.newEvent(NS_D, 'ScrapVehicleEvent'); + scrapVehicleEvent.vehicle = vehiclesToScrap[x]; + emit(scrapVehicleEvent); + } + await assetRegistry.updateAll(vehiclesToScrap); + } } diff --git a/packages/vehicle-lifecycle-network/test/manufacturer.js b/packages/vehicle-lifecycle-network/test/manufacturer.js index d3e989f..4f16757 100644 --- a/packages/vehicle-lifecycle-network/test/manufacturer.js +++ b/packages/vehicle-lifecycle-network/test/manufacturer.js @@ -22,25 +22,21 @@ const NS = 'org.acme.vehicle.lifecycle'; const NS_M = 'org.acme.vehicle.lifecycle.manufacturer'; const NS_D = 'org.vda'; -describe('Manufacturer', function() { +describe('Manufacturer', () => { let businessNetworkConnection; let factory; - beforeEach(function() { - return Util.deployAndConnect() - .then(connection => { - businessNetworkConnection = connection; - factory = businessNetworkConnection.getBusinessNetwork().getFactory(); - }); + beforeEach(async () => { + businessNetworkConnection = await Util.deployAndConnect(); + factory = businessNetworkConnection.getBusinessNetwork().getFactory(); }); const orderId = '1000-1000-1000-1000'; /** * Place a vehicle order. - * @returns {Promise} resolved when the transaction is complete. */ - function placeOrder() { + async function placeOrder() { const placeOrder = factory.newTransaction(NS_M, 'PlaceOrder'); placeOrder.manufacturer = factory.newRelationship(NS_M, 'Manufacturer', 'manufacturer'); placeOrder.orderId = orderId; @@ -51,97 +47,66 @@ describe('Manufacturer', function() { vehicleDetails.colour = 'Red'; vehicleDetails.vin = ''; placeOrder.vehicleDetails = vehicleDetails; - return businessNetworkConnection.submitTransaction(placeOrder); + await businessNetworkConnection.submitTransaction(placeOrder); } /** * Update a vehicle order. * @returns {Promise} resolved when the transaction is complete. */ - function updateOrder() { + async function updateOrder() { const updateOrderStatus = factory.newTransaction(NS_M, 'UpdateOrderStatus'); updateOrderStatus.orderStatus = 'VIN_ASSIGNED'; updateOrderStatus.vin = 'VIN_NUMBER'; - return businessNetworkConnection.getAssetRegistry(NS_M + '.Order') - .then(function(orderRegistry) { - return orderRegistry.getAll(); - }) - .then(function(orders) { - const order = orders[0]; - updateOrderStatus.order = factory.newRelationship(NS_M, 'Order', order.getIdentifier()); - return businessNetworkConnection.submitTransaction(updateOrderStatus); - }); + const orderRegistry = await businessNetworkConnection.getAssetRegistry(NS_M + '.Order'); + const orders = await orderRegistry.getAll(); + const order = orders[0]; + updateOrderStatus.order = factory.newRelationship(NS_M, 'Order', order.getIdentifier()); + await businessNetworkConnection.submitTransaction(updateOrderStatus); } - describe('#placeOrder', function() { - it('should be able to place an order for a vehicle', function() { - return placeOrder() - .then(function() { - return businessNetworkConnection.getAssetRegistry(NS_M + '.Order'); - }) - .then(function(orderRegistry) { - return orderRegistry.get(orderId); - }) - .then(function(order) { - order.orderStatus.should.equal('PLACED'); - }); + describe('#placeOrder', () => { + it('should be able to place an order for a vehicle', async () => { + await placeOrder(); + const orderRegistry = await businessNetworkConnection.getAssetRegistry(NS_M + '.Order'); + const order = await orderRegistry.get(orderId); + order.orderStatus.should.equal('PLACED'); }); }); - describe('#updateOrderStatus', function() { - it('should create a vehicle and assign it a VIN number', function() { - return placeOrder() - .then(function() { - return updateOrder(); - }) - .then(function() { - return businessNetworkConnection.getAssetRegistry(NS_D + '.Vehicle'); - }) - .then(function(vehicleRegistry) { - return vehicleRegistry.get('VIN_NUMBER'); - }) - .then(function(vehicle) { - should.exist(vehicle); - vehicle.vehicleStatus.should.equal('OFF_THE_ROAD'); - vehicle.vehicleDetails.vin.should.equal('VIN_NUMBER'); - }); + describe('#updateOrderStatus', () => { + it('should create a vehicle and assign it a VIN number', async () => { + await placeOrder(); + await updateOrder(); + const vehicleRegistry = await businessNetworkConnection.getAssetRegistry(NS_D + '.Vehicle'); + const vehicle = await vehicleRegistry.get('VIN_NUMBER'); + should.exist(vehicle); + vehicle.vehicleStatus.should.equal('OFF_THE_ROAD'); + vehicle.vehicleDetails.vin.should.equal('VIN_NUMBER'); }); - it('should assign an owner to a vehicle and make it active', function() { + it('should assign an owner to a vehicle and make it active', async () => { const updateOrderStatus = factory.newTransaction(NS_M, 'UpdateOrderStatus'); updateOrderStatus.orderStatus = 'OWNER_ASSIGNED'; updateOrderStatus.vin = 'VIN_NUMBER'; updateOrderStatus.numberPlate = 'NUMBER_PLATE'; updateOrderStatus.v5c = 'V5C'; - return placeOrder() - .then(function() { - return updateOrder(); - }) - .then(function() { - return businessNetworkConnection.getAssetRegistry(NS_M + '.Order'); - }) - .then(function(orderRegistry) { - return orderRegistry.getAll(); - }) - .then(function(orders) { - const order = orders[0]; - updateOrderStatus.order = factory.newRelationship(NS_M, 'Order', order.getIdentifier()); - return businessNetworkConnection.submitTransaction(updateOrderStatus); - }) - .then(function() { - return businessNetworkConnection.getAssetRegistry(NS_D + '.Vehicle'); - }) - .then(function(vehicleRegistry) { - return vehicleRegistry.get('VIN_NUMBER'); - }) - .then(function(vehicle) { - should.exist(vehicle); - vehicle.vehicleStatus.should.equal('ACTIVE'); - vehicle.vehicleDetails.vin.should.equal(updateOrderStatus.vin); - vehicle.owner.getIdentifier().should.equal('dan'); - }); + await placeOrder(); + await updateOrder(); + const orderRegistry = await businessNetworkConnection.getAssetRegistry(NS_M + '.Order'); + const orders = await orderRegistry.getAll(); + const order = orders[0]; + updateOrderStatus.order = factory.newRelationship(NS_M, 'Order', order.getIdentifier()); + await businessNetworkConnection.submitTransaction(updateOrderStatus); + + const vehicleRegistry = await businessNetworkConnection.getAssetRegistry(NS_D + '.Vehicle'); + const vehicle = await vehicleRegistry.get('VIN_NUMBER'); + should.exist(vehicle); + vehicle.vehicleStatus.should.equal('ACTIVE'); + vehicle.vehicleDetails.vin.should.equal(updateOrderStatus.vin); + vehicle.owner.getIdentifier().should.equal('dan'); }); }); diff --git a/packages/vehicle-lifecycle-network/test/setup.js b/packages/vehicle-lifecycle-network/test/setup.js index f32268c..8acdadb 100644 --- a/packages/vehicle-lifecycle-network/test/setup.js +++ b/packages/vehicle-lifecycle-network/test/setup.js @@ -16,71 +16,52 @@ const Util = require('./util'); -const should = require('chai').should(); - const NS = 'org.acme.vehicle.lifecycle'; const NS_M = 'org.acme.vehicle.lifecycle.manufacturer'; const NS_D = 'org.vda'; -describe('Setup', function() { +describe('Setup', () => { let businessNetworkConnection; let factory; - beforeEach(function() { - return Util.deployAndConnect() - .then(connection => { - businessNetworkConnection = connection; - factory = businessNetworkConnection.getBusinessNetwork().getFactory(); - }); + beforeEach(async () => { + businessNetworkConnection = await Util.deployAndConnect(); + factory = businessNetworkConnection.getBusinessNetwork().getFactory(); }); - describe('Setup', function() { - describe('#setupDemo', function() { + describe('Setup', () => { + describe('#setupDemo', () => { /** * * @param {String} registry - name of a registry */ - function getAllFromRegistry(type, registry) { + async function getAllFromRegistry(type, registry) { const func = 'get' + type + 'Registry'; - return businessNetworkConnection[func](registry) - .then(function(registry) { - return registry.getAll(); - }); + const bnc = await businessNetworkConnection[func](registry); + return await bnc.getAll(); } - it('should create a scenario', function() { + it('should create a scenario', async () => { // submit the transaction const setupDemo = factory.newTransaction(NS, 'SetupDemo'); - return businessNetworkConnection.submitTransaction(setupDemo) - .then(function() { - // (participants) get regulator registry - return getAllFromRegistry('Participant', NS + '.Regulator'); - }) - .then(function(regulators) { - regulators.length.should.equal(1); - }) - .then(function() { - // (participants) get manufacturer registry - return getAllFromRegistry('Participant', NS_M + '.Manufacturer'); - }) - .then(function(manufacturers) { - manufacturers.length.should.be.above(1); - }) - .then(function() { - // (participants) get private owner registry - return getAllFromRegistry('Participant', NS + '.PrivateOwner'); - }) - .then(function(privateOwners) { - privateOwners.length.should.be.above(10); - }) - .then(function() { - // (assets) get vehicles registry - return getAllFromRegistry('Asset', NS_D + '.Vehicle'); - }) - .then(function(vehicles) { - vehicles.length.should.be.above(10); - }); + await businessNetworkConnection.submitTransaction(setupDemo); + + // (participants) get regulator registry + const regulators = await getAllFromRegistry('Participant', NS + '.Regulator'); + regulators.length.should.equal(1); + + // (participants) get manufacturer registry + const manufacturers = await getAllFromRegistry('Participant', NS_M + '.Manufacturer'); + manufacturers.length.should.be.above(1); + + // (participants) get private owner registry + const privateOwners = await getAllFromRegistry('Participant', NS + '.PrivateOwner'); + privateOwners.length.should.be.above(10); + + // (assets) get vehicles registry + const vehicles = await getAllFromRegistry('Asset', NS_D + '.Vehicle'); + vehicles.length.should.be.above(10); }); }); diff --git a/packages/vehicle-lifecycle-network/test/util.js b/packages/vehicle-lifecycle-network/test/util.js index 8b730fe..799dd8d 100644 --- a/packages/vehicle-lifecycle-network/test/util.js +++ b/packages/vehicle-lifecycle-network/test/util.js @@ -38,12 +38,9 @@ let adminConnection; * @param {String} type * @param {Resource} resource */ -module.exports.createAsset = function(businessNetworkConnection, NS, type, resource) { - var factory = businessNetworkConnection.getBusinessNetwork().getFactory(); - return businessNetworkConnection.getAssetRegistry(NS + '.' + type) - .then(function(registry) { - return registry.add(resource); - }); +module.exports.createAsset = async (businessNetworkConnection, NS, type, resource) => { + const registry = await businessNetworkConnection.getAssetRegistry(NS + '.' + type); + await registry.add(resource); }; /** @@ -53,24 +50,22 @@ module.exports.createAsset = function(businessNetworkConnection, NS, type, resou * @param {String} type * @param {Resource} resource */ -module.exports.createParticipant = function(businessNetworkConnection, NS, type, resource) { - return businessNetworkConnection.getParticipantRegistry(NS + '.' + resource.getIdentifier()) - .then(function(registry) { - return registry.add(resource); - }); +module.exports.createParticipant = async (businessNetworkConnection, NS, type, resource) => { + const registry = await businessNetworkConnection.getParticipantRegistry(NS + '.' + resource.getIdentifier()); + await registry.add(resource); }; /** * * @param {BusinessNetworkConnection} businessNetworkConnection */ -module.exports.setup = function(businessNetworkConnection) { - var factory = businessNetworkConnection.getBusinessNetwork().getFactory(); - var p1 = factory.newResource(NS, 'PrivateOwner', 'dan'); - var p2 = factory.newResource(NS, 'PrivateOwner', 'simon'); - var m1 = factory.newResource(NS_M, 'Manufacturer', 'manufacturer'); +module.exports.setup = async (businessNetworkConnection) => { + const factory = businessNetworkConnection.getBusinessNetwork().getFactory(); + const p1 = factory.newResource(NS, 'PrivateOwner', 'dan'); + const p2 = factory.newResource(NS, 'PrivateOwner', 'simon'); + const m1 = factory.newResource(NS_M, 'Manufacturer', 'manufacturer'); - var v = factory.newResource(NS_D, 'Vehicle', '123456789'); + const v = factory.newResource(NS_D, 'Vehicle', '123456789'); v.owner = factory.newRelationship(NS, 'PrivateOwner', 'dan'); v.vehicleStatus = 'ACTIVE'; v.numberPlate = 'NUMBER'; @@ -80,62 +75,44 @@ module.exports.setup = function(businessNetworkConnection) { v.vehicleDetails.colour = 'Beige'; v.vehicleDetails.vin = '123456789'; - return businessNetworkConnection.getParticipantRegistry(NS + '.PrivateOwner') - .then(function(pr) { - return pr.addAll([p1, p2]); - }) - .then(function() { - return businessNetworkConnection.getParticipantRegistry(NS_M + '.Manufacturer'); - }) - .then(function(pr) { - return pr.addAll([m1]); - }) - .then(function() { - return businessNetworkConnection.getAssetRegistry(NS_D + '.Vehicle'); - }) - .then(function(ar) { - return ar.addAll([v]); - }); + const pr = await businessNetworkConnection.getParticipantRegistry(NS + '.PrivateOwner'); + await pr.addAll([p1, p2]); + + const mr = await businessNetworkConnection.getParticipantRegistry(NS_M + '.Manufacturer'); + await mr.addAll([m1]); + + const vr = await businessNetworkConnection.getAssetRegistry(NS_D + '.Vehicle'); + await vr.addAll([v]); }; -module.exports.deployAndConnect = function() { - let adminConnection; - let businessNetworkDefinition; - let businessNetworkConnection; - - return getAdminConnection().then(connection => { - adminConnection = connection; - return BusinessNetworkDefinition.fromDirectory(path.resolve(__dirname, '..')); - }).then(definition => { - businessNetworkDefinition = definition; - return adminConnection.install(businessNetworkDefinition.getName()); - }).then(() => { - const startOptions = { - networkAdmins: [ - { - userName: 'admin', - enrollmentSecret: 'adminpw' - } - ] - }; - return adminConnection.start(businessNetworkDefinition, startOptions); - }).then(adminCards => { - return adminConnection.importCard(adminCardName, adminCards.get('admin')); - }).then(() => { - businessNetworkConnection = new BusinessNetworkConnection({ cardStore: cardStore }); - return businessNetworkConnection.connect(adminCardName); - }).then(() => { - return businessNetworkConnection; - }); +module.exports.deployAndConnect = async () => { + const adminConnection = await getAdminConnection(); + const businessNetworkDefinition = await BusinessNetworkDefinition.fromDirectory(path.resolve(__dirname, '..')); + + await adminConnection.install(businessNetworkDefinition.getName()); + const startOptions = { + networkAdmins: [ + { + userName: 'admin', + enrollmentSecret: 'adminpw' + } + ] + }; + const adminCards = await adminConnection.start(businessNetworkDefinition, startOptions); + await adminConnection.importCard(adminCardName, adminCards.get('admin')); + + const businessNetworkConnection = new BusinessNetworkConnection({ cardStore: cardStore }); + await businessNetworkConnection.connect(adminCardName); + return businessNetworkConnection; }; /** * Install required cards and create an admin connection. * @returns {Promise} Resolves with a AdminConnection. */ -function getAdminConnection() { +async function getAdminConnection() { if (adminConnection) { - return Promise.resolve(adminConnection); + return adminConnection; } const connectionProfile = { @@ -159,9 +136,7 @@ function getAdminConnection() { adminConnection = new AdminConnection({ cardStore: cardStore }); - return adminConnection.importCard(deployerCardName, deployerCard).then(() => { - return adminConnection.connect(deployerCardName); - }).then(() => { - return adminConnection; - }); + await adminConnection.importCard(deployerCardName, deployerCard); + await adminConnection.connect(deployerCardName); + return adminConnection; } diff --git a/packages/vehicle-lifecycle-network/test/vda.js b/packages/vehicle-lifecycle-network/test/vda.js index 14f0aa7..e831f76 100644 --- a/packages/vehicle-lifecycle-network/test/vda.js +++ b/packages/vehicle-lifecycle-network/test/vda.js @@ -19,101 +19,71 @@ const Util = require('./util'); const should = require('chai').should(); const NS = 'org.acme.vehicle.lifecycle'; -const NS_M = 'org.acme.vehicle.lifecycle.manufacturer'; const NS_D = 'org.vda'; -describe('VDA', function() { +describe('VDA', () => { let businessNetworkConnection; let factory; - beforeEach(function() { - return Util.deployAndConnect() - .then(connection => { - businessNetworkConnection = connection; - factory = businessNetworkConnection.getBusinessNetwork().getFactory(); - return Util.setup(businessNetworkConnection); - }); + beforeEach(async () => { + businessNetworkConnection = await Util.deployAndConnect(); + factory = businessNetworkConnection.getBusinessNetwork().getFactory(); + await Util.setup(businessNetworkConnection); }); - describe('#privateVehicleTransfer', function() { - it('should be able to transfer a vehicle between two private owners', function() { + describe('#privateVehicleTransfer', () => { + it('should be able to transfer a vehicle between two private owners', async () => { const vehicleToTransfer = '123456789'; - const owners = ['dan', 'simon']; - - let vehicleRegistry; - let privateOwnerRegistry; - let vehicle; - - return businessNetworkConnection.getAssetRegistry(NS_D + '.Vehicle') - .then(function(vr) { - vehicleRegistry = vr; - return vehicleRegistry.get(vehicleToTransfer); - }) - .then(function(v) { - vehicle = v; - should.not.exist(vehicle.logEntries); - vehicle.owner.getIdentifier().should.equal('dan'); - }) - .then(function() { - const privateVehicleTransfer = factory.newTransaction(NS_D, 'PrivateVehicleTransfer'); - privateVehicleTransfer.vehicle = factory.newRelationship(NS_D, 'Vehicle', vehicle.getIdentifier()); - privateVehicleTransfer.seller = vehicle.owner; - privateVehicleTransfer.buyer = factory.newRelationship(NS, 'PrivateOwner', 'simon'); - - return businessNetworkConnection.submitTransaction(privateVehicleTransfer); - }) - .then(function() { - return vehicleRegistry.get(vehicle.getIdentifier()); - }) - .then(function(newVehicle) { - newVehicle.owner.getIdentifier().should.equal('simon'); - should.exist(newVehicle.logEntries); - newVehicle.logEntries.length.should.equal(1); - newVehicle.logEntries[0].buyer.getIdentifier().should.equal('simon'); - newVehicle.logEntries[0].seller.getIdentifier().should.equal('dan'); - newVehicle.logEntries[0].vehicle.getIdentifier().should.equal(vehicleToTransfer); - }); + + const vehicleRegistry = await businessNetworkConnection.getAssetRegistry(NS_D + '.Vehicle'); + const vehicle = await vehicleRegistry.get(vehicleToTransfer); + + should.not.exist(vehicle.logEntries); + vehicle.owner.getIdentifier().should.equal('dan'); + + const privateVehicleTransfer = factory.newTransaction(NS_D, 'PrivateVehicleTransfer'); + privateVehicleTransfer.vehicle = factory.newRelationship(NS_D, 'Vehicle', vehicle.getIdentifier()); + privateVehicleTransfer.seller = vehicle.owner; + privateVehicleTransfer.buyer = factory.newRelationship(NS, 'PrivateOwner', 'simon'); + await businessNetworkConnection.submitTransaction(privateVehicleTransfer); + + const newVehicle = await vehicleRegistry.get(vehicle.getIdentifier()); + newVehicle.owner.getIdentifier().should.equal('simon'); + should.exist(newVehicle.logEntries); + newVehicle.logEntries.length.should.equal(1); + newVehicle.logEntries[0].buyer.getIdentifier().should.equal('simon'); + newVehicle.logEntries[0].seller.getIdentifier().should.equal('dan'); + newVehicle.logEntries[0].vehicle.getIdentifier().should.equal(vehicleToTransfer); }); }); - describe('ScrapVehicle', function() { - it('should change a vehicles status to SCRAPPED', function() { + describe('ScrapVehicle', () => { + it('should change a vehicles status to SCRAPPED', async () => { const vehicleToScrap = '123456789'; const scrapVehicle = factory.newTransaction(NS_D, 'ScrapVehicle'); scrapVehicle.vehicle = factory.newRelationship(NS_D, 'Vehicle', vehicleToScrap); - return businessNetworkConnection.submitTransaction(scrapVehicle) - .then(function() { - return businessNetworkConnection.getAssetRegistry(NS_D + '.Vehicle'); - }) - .then(function(assetRegistry) { - return assetRegistry.get(vehicleToScrap); - }) - .then(function(vehicle) { - vehicle.vehicleStatus.should.equal('SCRAPPED'); - }); + await businessNetworkConnection.submitTransaction(scrapVehicle); + + const assetRegistry = await businessNetworkConnection.getAssetRegistry(NS_D + '.Vehicle'); + const vehicle = await assetRegistry.get(vehicleToScrap); + vehicle.vehicleStatus.should.equal('SCRAPPED'); }); }); - describe('ScrapAllVehiclesByColour', function() { - it('should select vehicles by colour and change vehicles status to SCRAPPED', function() { + describe('ScrapAllVehiclesByColour', () => { + it('should select vehicles by colour and change vehicles status to SCRAPPED', async () => { // Vehicle with beige colour and id 123456789 resides in reposritory const vehicleId = '123456789'; const scrapVehicleTransaction = factory.newTransaction(NS_D, 'ScrapAllVehiclesByColour'); scrapVehicleTransaction.colour = 'Beige'; - return businessNetworkConnection.submitTransaction(scrapVehicleTransaction) - .then(function() { - return businessNetworkConnection.getAssetRegistry(NS_D + '.Vehicle'); - }) - .then(function(ar) { - const assetRegistry = ar; - return assetRegistry.get(vehicleId); - }) - .then(function(vehicle) { - vehicle.vehicleStatus.should.equal('SCRAPPED'); - }); + await businessNetworkConnection.submitTransaction(scrapVehicleTransaction); + + const assetRegistry = await businessNetworkConnection.getAssetRegistry(NS_D + '.Vehicle'); + const vehicle = await assetRegistry.get(vehicleId); + vehicle.vehicleStatus.should.equal('SCRAPPED'); }); });