Skip to content

Commit

Permalink
Use new connect API in Yo generated business network unit tests (hype…
Browse files Browse the repository at this point in the history
…rledger-archives#2733)

Signed-off-by: Mark S. Lewis <[email protected]>
  • Loading branch information
bestbeforetoday authored and nklincoln committed Nov 15, 2017
1 parent 7ad3db6 commit 20791dd
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"composer-admin": "latest",
"composer-client": "latest",
"composer-connector-embedded": "latest",
"browserfs": "latest",
"chai": "latest",
"eslint": "latest",
"istanbul": "latest",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,89 +3,111 @@
* Write the unit tests for your transction processor functions here
*/

var AdminConnection = require('composer-admin').AdminConnection;
var BrowserFS = require('browserfs/dist/node/index');
var BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection;
var BusinessNetworkDefinition = require('composer-common').BusinessNetworkDefinition;
var path = require('path');
var fs = require('fs');
const AdminConnection = require('composer-admin').AdminConnection;
const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection;
const BusinessNetworkDefinition = require('composer-common').BusinessNetworkDefinition;
const IdCard = require('composer-common').IdCard;
const MemoryCardStore = require('composer-common').MemoryCardStore;

const path = require('path');

require('chai').should();

var bfs_fs = BrowserFS.BFSRequire('fs');
var NS = '<%= namespace%>';
const NS = '<%= namespace%>';
const assetType = 'SampleAsset';

var assetType = 'SampleAsset';
describe('#' + NS, () => {
let businessNetworkConnection;

describe('#'+NS, function() {
before(() => {
// Embedded connection used for local testing
const connectionProfile = {
name: 'embedded',
type: 'embedded'
};
// Embedded connection does not need real credentials
const credentials = {
certificate: 'FAKE CERTIFICATE',
privateKey: 'FAKE PRIVATE KEY'
};

var businessNetworkConnection;
// Identity used with the admin connection to deploy business networks
const deployerMetadata = {
version: 1,
userName: 'PeerAdmin',
roles: [ 'PeerAdmin', 'ChannelAdmin' ]
};
const deployerCard = new IdCard(deployerMetadata, connectionProfile);
deployerCard.setCredentials(credentials);

before(function() {
BrowserFS.initialize(new BrowserFS.FileSystem.InMemory());
var adminConnection = new AdminConnection({ fs: bfs_fs });
return adminConnection.createProfile('defaultProfile', {
type: 'embedded'
})
.then(function() {
return adminConnection.connectWithDetails('defaultProfile', 'admin', 'Xurw3yU9zI0l');
})
.then(function() {
// Identity used to connect to business networks
const userMetadata = {
version: 1,
userName: 'admin',
businessNetwork: '<%= appname%>'
};
const userCard = new IdCard(userMetadata, connectionProfile);
userCard.setCredentials(credentials);

const deployerCardName = 'deployer';
const userCardName = 'user';

// In-memory card store for testing so cards are not persisted to the file system
const cardStore = new MemoryCardStore();
const adminConnection = new AdminConnection({ cardStore: cardStore });

return adminConnection.importCard(deployerCardName, deployerCard).then(() => {
return adminConnection.importCard(userCardName, userCard);
}).then(() => {
return adminConnection.connect(deployerCardName);
}).then(() => {
return BusinessNetworkDefinition.fromDirectory(path.resolve(__dirname, '..'));
})
.then(function(businessNetworkDefinition) {
}).then(businessNetworkDefinition => {
return adminConnection.deploy(businessNetworkDefinition);
})
.then(function() {
businessNetworkConnection = new BusinessNetworkConnection({ fs: bfs_fs });
return businessNetworkConnection.connectWithDetails('defaultProfile', '<%= appname%>', 'admin', 'Xurw3yU9zI0l');
}).then(() => {
businessNetworkConnection = new BusinessNetworkConnection({ cardStore: cardStore });
return businessNetworkConnection.connect(userCardName);
});
});

describe('ChangeAssetValue()', function() {

describe('ChangeAssetValue()', () => {
it('should change the value property of ' + assetType + ' to newValue', () => {
const factory = businessNetworkConnection.getBusinessNetwork().getFactory();

var factory = businessNetworkConnection.getBusinessNetwork().getFactory();

// create a user
var user = factory.newResource(NS, 'User', '<%= appauthor%>');
// Create a user participant
const user = factory.newResource(NS, 'User', '<%= appauthor%>');

// create the asset
var asset = factory.newResource(NS, assetType, 'ASSET_001');
// Create the asset
const asset = factory.newResource(NS, assetType, 'ASSET_001');
asset.value = 'old-value';

var changeAssetValue = factory.newTransaction(NS, 'ChangeAssetValue');
// Create a transaction to change the asset's value property
const changeAssetValue = factory.newTransaction(NS, 'ChangeAssetValue');
changeAssetValue.relatedAsset = factory.newRelationship(NS, assetType, asset.$identifier);
changeAssetValue.newValue = 'new-value';

// Get the asset registry.
return businessNetworkConnection.getAssetRegistry(NS + '.' + assetType)
.then(function(registry) {

// Add the Asset to the asset registry.
return registry.add(asset)
.then(function() {
return businessNetworkConnection.getParticipantRegistry(NS + '.User');
})
.then(function(userRegistry) {
return userRegistry.add(user);
})
.then(function() {
// submit the transaction
return businessNetworkConnection.submitTransaction(changeAssetValue);
})
.then(function() {
return businessNetworkConnection.getAssetRegistry(NS + '.' + assetType);
})
.then(function(registry) {
// get the listing
return registry.get(asset.$identifier);
})
.then(function(newAsset) {
newAsset.value.should.equal('new-value');
});
let assetRegistry;

return businessNetworkConnection.getAssetRegistry(NS + '.' + assetType).then(registry => {
assetRegistry = registry;
// Add the asset to the appropriate asset registry
return registry.add(asset);
}).then(() => {
return businessNetworkConnection.getParticipantRegistry(NS + '.User');
}).then(userRegistry => {
// Add the user to the appropriate participant registry
return userRegistry.add(user);
}).then(() => {
// Submit the transaction
return businessNetworkConnection.submitTransaction(changeAssetValue);
}).then(registry => {
// Get the asset
return assetRegistry.get(asset.$identifier);
}).then(newAsset => {
// Assert that the asset has the new value property
newAsset.value.should.equal(changeAssetValue.newValue);
});
});
});
});

});

0 comments on commit 20791dd

Please sign in to comment.