Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
Added unit tests and fixed vehicle lifecycle network (#76)
Browse files Browse the repository at this point in the history
* Added animal tracking unit tests

* Fix bugs
  • Loading branch information
Liam Grace authored and nklincoln committed Jul 19, 2017
1 parent 9b66138 commit 9733595
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 118 deletions.
1 change: 0 additions & 1 deletion packages/animaltracking-network/.eslintrc.yml
Expand Up @@ -28,7 +28,6 @@ rules:
eqeqeq: error
no-throw-literal: error
strict: error
no-var: error
dot-notation: error
no-tabs: error
no-trailing-spaces: error
Expand Down
220 changes: 110 additions & 110 deletions packages/animaltracking-network/lib/mozart.cto.js
Expand Up @@ -23,33 +23,33 @@
* @transaction
*/
function onAnimalMovementDeparture(movementDeparture) {
console.log('onAnimalMovementDeparture');
if (movementDeparture.animal.movementStatus !== 'IN_FIELD') {
throw new Error('Animal is already IN_TRANSIT');
}
console.log('onAnimalMovementDeparture');
if (movementDeparture.animal.movementStatus !== 'IN_FIELD') {
throw new Error('Animal is already IN_TRANSIT');
}

// set the movement status of the animal
movementDeparture.animal.movementStatus = 'IN_TRANSIT';
movementDeparture.animal.movementStatus = 'IN_TRANSIT';

// save the animal
return getAssetRegistry('com.biz.Animal')
return getAssetRegistry('com.biz.Animal')
.then(function(ar) {
return ar.update(movementDeparture.animal);
return ar.update(movementDeparture.animal);
})
.then(function() {
// add the animal to the incoming animals of the
// destination business
if (movementDeparture.to.incomingAnimals) {
movementDeparture.to.incomingAnimals.push(movementDeparture.animal);
} else {
movementDeparture.to.incomingAnimals = [movementDeparture.animal];
}
if (movementDeparture.to.incomingAnimals) {
movementDeparture.to.incomingAnimals.push(movementDeparture.animal);
} else {
movementDeparture.to.incomingAnimals = [movementDeparture.animal];
}

// save the business
return getAssetRegistry('com.biz.Business')
return getAssetRegistry('com.biz.Business');
})
.then(function(br) {
return br.update(movementDeparture.to);
return br.update(movementDeparture.to);
});
}

Expand All @@ -59,44 +59,44 @@ function onAnimalMovementDeparture(movementDeparture) {
* @transaction
*/
function onAnimalMovementArrival(movementArrival) {
console.log('onAnimalMovementArrival');
console.log('onAnimalMovementArrival');

if (movementArrival.animal.movementStatus !== 'IN_TRANSIT') {
throw new Error('Animal is not IN_TRANSIT');
}
if (movementArrival.animal.movementStatus !== 'IN_TRANSIT') {
throw new Error('Animal is not IN_TRANSIT');
}

// set the movement status of the animal
movementArrival.animal.movementStatus = 'IN_FIELD';
movementArrival.animal.movementStatus = 'IN_FIELD';

// set the new owner of the animal
// to the owner of the 'to' business
movementArrival.animal.owner = movementArrival.to.owner;
movementArrival.animal.owner = movementArrival.to.owner;

// set the new location of the animal
movementArrival.animal.location = movementArrival.arrivalField;
movementArrival.animal.location = movementArrival.arrivalField;

// save the animal
return getAssetRegistry('com.biz.Animal')
return getAssetRegistry('com.biz.Animal')
.then(function(ar) {
return ar.update(movementArrival.animal);
return ar.update(movementArrival.animal);
})
.then(function() {
// remove the animal from the incoming animals
// of the 'to' business
if (!movementArrival.to.incomingAnimals) {
throw new Error('Incoming business should have incomingAnimals on AnimalMovementArrival.');
}
if (!movementArrival.to.incomingAnimals) {
throw new Error('Incoming business should have incomingAnimals on AnimalMovementArrival.');
}

movementArrival.to.incomingAnimals = movementArrival.to.incomingAnimals
movementArrival.to.incomingAnimals = movementArrival.to.incomingAnimals
.filter(function(animal) {
return animal.animalId !== movementArrival.animal.animalId;
return animal.animalId !== movementArrival.animal.animalId;
});

// save the business
return getAssetRegistry('com.biz.Business');
return getAssetRegistry('com.biz.Business');
})
.then(function(br) {
return br.update(movementArrival.to);
return br.update(movementArrival.to);
});
}

Expand All @@ -106,103 +106,103 @@ function onAnimalMovementArrival(movementArrival) {
* @transaction
*/
function setupDemo(setupDemo) {
var factory = getFactory();
var NS = 'com.biz';

var farmers = [
factory.newResource(NS, 'Farmer', 'FARMER_1'),
factory.newResource(NS, 'Farmer', 'FARMER_2')
];

var businesses = [
factory.newResource(NS, 'Business', 'BUSINESS_1'),
factory.newResource(NS, 'Business', 'BUSINESS_2')
];

var fields = [
factory.newResource(NS, 'Field','FIELD_1'),
factory.newResource(NS, 'Field','FIELD_2'),
factory.newResource(NS, 'Field','FIELD_3'),
factory.newResource(NS, 'Field','FIELD_4')
];

var animals = [
factory.newResource(NS, 'Animal', 'ANIMAL_1'),
factory.newResource(NS, 'Animal', 'ANIMAL_2'),
factory.newResource(NS, 'Animal', 'ANIMAL_3'),
factory.newResource(NS, 'Animal', 'ANIMAL_4'),
factory.newResource(NS, 'Animal', 'ANIMAL_5'),
factory.newResource(NS, 'Animal', 'ANIMAL_6'),
factory.newResource(NS, 'Animal', 'ANIMAL_7'),
factory.newResource(NS, 'Animal', 'ANIMAL_8')
];
return getParticipantRegistry(NS + '.Regulator')
var factory = getFactory();
var NS = 'com.biz';

var farmers = [
factory.newResource(NS, 'Farmer', 'FARMER_1'),
factory.newResource(NS, 'Farmer', 'FARMER_2')
];

var businesses = [
factory.newResource(NS, 'Business', 'BUSINESS_1'),
factory.newResource(NS, 'Business', 'BUSINESS_2')
];

var fields = [
factory.newResource(NS, 'Field','FIELD_1'),
factory.newResource(NS, 'Field','FIELD_2'),
factory.newResource(NS, 'Field','FIELD_3'),
factory.newResource(NS, 'Field','FIELD_4')
];

var animals = [
factory.newResource(NS, 'Animal', 'ANIMAL_1'),
factory.newResource(NS, 'Animal', 'ANIMAL_2'),
factory.newResource(NS, 'Animal', 'ANIMAL_3'),
factory.newResource(NS, 'Animal', 'ANIMAL_4'),
factory.newResource(NS, 'Animal', 'ANIMAL_5'),
factory.newResource(NS, 'Animal', 'ANIMAL_6'),
factory.newResource(NS, 'Animal', 'ANIMAL_7'),
factory.newResource(NS, 'Animal', 'ANIMAL_8')
];
return getParticipantRegistry(NS + '.Regulator')
.then(function(regulatorRegistry) {
var regulator = factory.newResource(NS, 'Regulator', 'REGULATOR');
regulator.email = 'REGULATOR';
regulator.firstName = 'Ronnie';
regulator.lastName = 'Regulator';
return regulatorRegistry.addAll([regulator]);
var regulator = factory.newResource(NS, 'Regulator', 'REGULATOR');
regulator.email = 'REGULATOR';
regulator.firstName = 'Ronnie';
regulator.lastName = 'Regulator';
return regulatorRegistry.addAll([regulator]);
})
.then(function() {
return getParticipantRegistry(NS + '.Farmer');
return getParticipantRegistry(NS + '.Farmer');
})
.then(function(farmerRegistry) {
farmers.forEach(function(farmer) {
var sbi = 'BUSINESS_' + farmer.getIdentifier().split('_')[1];
farmer.firstName = farmer.getIdentifier();
farmer.lastName = '';
farmer.address1 = 'Address1';
farmer.address2 = 'Address2';
farmer.county = 'County';
farmer.postcode = 'PO57C0D3';
farmer.business = factory.newResource(NS, 'Business', sbi);
});
return farmerRegistry.addAll(farmers);
farmers.forEach(function(farmer) {
var sbi = 'BUSINESS_' + farmer.getIdentifier().split('_')[1];
farmer.firstName = farmer.getIdentifier();
farmer.lastName = '';
farmer.address1 = 'Address1';
farmer.address2 = 'Address2';
farmer.county = 'County';
farmer.postcode = 'PO57C0D3';
farmer.business = factory.newResource(NS, 'Business', sbi);
});
return farmerRegistry.addAll(farmers);
})
.then(function() {
return getAssetRegistry(NS + '.Business');
return getAssetRegistry(NS + '.Business');
})
.then(function(businessRegistry) {
businesses.forEach(function(business, index) {
var cph = 'FIELD_' + (index + 1);
var farmer = 'FARMER_' + (index + 1);
business.address1 = 'Address1';
business.address2 = 'Address2';
business.county = 'County';
business.postcode = 'PO57C0D3';
business.owner = factory.newRelationship(NS, 'Farmer', farmer);
});

return businessRegistry.addAll(businesses);
businesses.forEach(function(business, index) {
var cph = 'FIELD_' + (index + 1);
var farmer = 'FARMER_' + (index + 1);
business.address1 = 'Address1';
business.address2 = 'Address2';
business.county = 'County';
business.postcode = 'PO57C0D3';
business.owner = factory.newRelationship(NS, 'Farmer', farmer);
});

return businessRegistry.addAll(businesses);
})
.then(function() {
return getAssetRegistry(NS + '.Field');
return getAssetRegistry(NS + '.Field');
})
.then(function(fieldRegistry) {
fields.forEach(function(field, index) {
var business = 'BUSINESS_' + (((index + 1) % 2) + 1);
field.name = 'FIELD_' + (index + 1);
field.business = factory.newRelationship(NS, 'Business', business);
});
return fieldRegistry.addAll(fields);
fields.forEach(function(field, index) {
var business = 'BUSINESS_' + (((index + 1) % 2) + 1);
field.name = 'FIELD_' + (index + 1);
field.business = factory.newRelationship(NS, 'Business', business);
});
return fieldRegistry.addAll(fields);
})
.then(function() {
return getAssetRegistry(NS + '.Animal');
return getAssetRegistry(NS + '.Animal');
})
.then(function(animalRegistry) {
animals.forEach(function(animal, index) {
var field = 'FIELD_' + (((index + 1) % 2) + 1);
var farmer = 'FARMER_' + (((index + 1) % 2) + 1);
animal.species = 'SHEEP_GOAT';
animal.movementStatus = 'IN_FIELD';
animal.productionType = 'MEAT';
animal.location = factory.newRelationship(NS, 'Field', field);
animal.owner = factory.newRelationship(NS, 'Farmer', farmer);
});
return animalRegistry.addAll(animals);
animals.forEach(function(animal, index) {
var field = 'FIELD_' + (((index + 1) % 2) + 1);
var farmer = 'FARMER_' + (((index + 1) % 2) + 1);
animal.species = 'SHEEP_GOAT';
animal.movementStatus = 'IN_FIELD';
animal.productionType = 'MEAT';
animal.location = factory.newRelationship(NS, 'Field', field);
animal.owner = factory.newRelationship(NS, 'Farmer', farmer);
});
return animalRegistry.addAll(animals);
});
}
}

/*eslint-enable no-unused-vars*/
/*eslint-enable no-undef*/
2 changes: 1 addition & 1 deletion packages/vehicle-lifecycle-network/lib/manufacturer.js
Expand Up @@ -81,7 +81,7 @@ function updateOrderStatus(updateOrderStatus) {
vehicle.owner = factory.newRelationship('org.acme.vehicle.lifecycle', 'PrivateOwner', updateOrderStatus.order.orderer.email);
vehicle.numberPlate = updateOrderStatus.numberPlate || '';
vehicle.vehicleDetails.numberPlate = updateOrderStatus.numberPlate || '';
vehicle.v5c = updateOrderStatus.v5c || '';
vehicle.vehicleDetails.v5c = updateOrderStatus.v5c || '';
if (!vehicle.logEntries) {
vehicle.logEntries = [];
}
Expand Down
9 changes: 4 additions & 5 deletions packages/vehicle-lifecycle-network/lib/vda.js
Expand Up @@ -22,7 +22,7 @@ function privateVehicleTransfer(privateVehicleTransfer) {
console.log('privateVehicleTransfer');

var currentParticipant = getCurrentParticipant();


var NS_M = 'org.acme.vehicle.lifecycle.manufacturer';
var NS = 'org.acme.vehicle.lifecycle';
Expand All @@ -38,7 +38,6 @@ function privateVehicleTransfer(privateVehicleTransfer) {

//PrivateVehicleTransaction for log
var vehicleTransferLogEntry = factory.newConcept(NS_D, 'VehicleTransferLogEntry');
vehicleTransferLogEntry.transactionId = privateVehicleTransfer.transactionId;
vehicleTransferLogEntry.vehicle = factory.newRelationship(NS_D, 'Vehicle', vehicle.getIdentifier());
vehicleTransferLogEntry.seller = factory.newRelationship(NS, 'PrivateOwner', seller.getIdentifier());
vehicleTransferLogEntry.buyer = factory.newRelationship(NS, 'PrivateOwner', buyer.getIdentifier());
Expand All @@ -63,10 +62,10 @@ function privateVehicleTransfer(privateVehicleTransfer) {
function scrapVehicle(scrapVehicle) {
console.log('scrapVehicle');

var NS_D = 'org.vda';
var assetRegistry;
var NS_D = 'org.vda';
var assetRegistry;

return getAssetRegistry(NS_D + '.Vehicle')
return getAssetRegistry(NS_D + '.Vehicle')
.then(function(ar) {
assetRegistry = ar;
return assetRegistry.get(scrapVehicle.vehicle.getIdentifier());
Expand Down
2 changes: 1 addition & 1 deletion packages/vehicle-lifecycle-network/package.json
Expand Up @@ -30,7 +30,7 @@
"author": "Hyperledger Composer",
"license": "Apache-2.0",
"dependencies": {
"vehicle-lifecycle-model": "^0.1.0"
"vehicle-lifecycle-model": "latest"
},
"devDependencies": {
"browserfs": "^1.2.0",
Expand Down

0 comments on commit 9733595

Please sign in to comment.