Skip to content

Commit

Permalink
Sort sample business networks - npm changes - Issue 1465 (hyperledger…
Browse files Browse the repository at this point in the history
…-archives#1542)

* Sort list of sample business networks
Closes #1465

* Merge branch 'master' of github.com:/hyperledger/composer into issue-1456
Sort sample networks - npm changes

* half done test

* completed tests

* Sorting final

* Removed accidental file changes

* Removed accidental file changes2
  • Loading branch information
KaiUsher authored and cazfletch committed Jul 25, 2017
1 parent 5274211 commit 476e733
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 43 deletions.
59 changes: 59 additions & 0 deletions packages/composer-playground-api/routes/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ module.exports = (app) => {

LOG.exit(method, data);
return callback(null, data);

});
}

Expand Down Expand Up @@ -167,6 +168,63 @@ module.exports = (app) => {
return callback(null, options);
}

/**
* Sort the samples into the correct order
* @param {Array} samples the array of samples
* @param {function} callback the function to call when done
* @returns {Array} The array of sorted samples
*/
function sortSamples (samples, callback) {
const method = 'sortSamples';

// Define primary networks.
let primarySampleNames = ['basic-sample-network'];
let sorted = [];

// Append primary networks to the list.
for (let i = 0; i < primarySampleNames.length; i++) {
let primaryName = primarySampleNames[i];
for (let j = 0; j < samples.length; j++) {
let network = samples[j];
if (primaryName === network.name) {
sorted.push(network);
}
}
}

// Create an array of networks that are not classed as primary.
let nonPrimaryNetworks = [];
for (let i = 0; i < samples.length; i++) {
let network = samples[i];
if (primarySampleNames.indexOf(network.name) === -1) {
nonPrimaryNetworks.push(network);
}
}

// Sort non primary networks alphabetically.
let sortedNonPrimarySamples = nonPrimaryNetworks.sort((a, b) => {
let businessNetworkA = a.name.toLowerCase();
let businessNetworkB = b.name.toLowerCase();
if (businessNetworkA < businessNetworkB) {
return -1;
}
if (businessNetworkA > businessNetworkB) {
return 1;
} else {
return 0;
}
});

// Append non primary networks to list.
for (let i = 0; i < sortedNonPrimarySamples.length; i++) {
let network = sortedNonPrimarySamples[i];
sorted.push(network);
}

LOG.exit(method, sorted);
return callback(null, sorted);
}

/**
* Check if environment has client id and secret in
*/
Expand All @@ -177,6 +235,7 @@ module.exports = (app) => {
getSampleNames,
getSampleMetaData,
downloadMetaData,
sortSamples
], function (err, result) {
if (err) {
return res.status(httpstatus.INTERNAL_SERVER_ERROR).json({error : err});
Expand Down
131 changes: 127 additions & 4 deletions packages/composer-playground-api/test/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,37 @@ RegClient.prototype.fetch = function (url, options, callback) {
}
};

let forceSearchFail = false, forceMetadataFail = false;
let forceSearchFail = false, forceMetadataFail = false, sortableList = false;

// class methods
RegClient.prototype.get = function (url, options, callback) {
let getSampleNames = {
objects : [{
package : {
name : 'bob'
}
},
}]
};

let getUnsortedSampleNames = {
objects : [{
package : {
name : 'cat',
}
},
{
package : {
name : 'ant',
}
},
{
package : {
name : 'bat',
}
}
]
};

let getMetaData = {
versions : {
'0.1.0' : {
Expand Down Expand Up @@ -123,19 +142,97 @@ RegClient.prototype.get = function (url, options, callback) {
}
};

let getMetaDataUnsortedCat = {
versions : {
'0.1.0' : {
engines : {
composer : '0.9.0'
},
name : 'cat',
description : 'cat package',
version : '1.0',
dist : {
tarball : 'my tar'
}
},
}
};

let getMetaDataUnsortedAnt = {
versions : {
'0.1.0' : {
engines : {
composer : '0.9.0'
},
name : 'ant',
description : 'ant package',
version : '1.0',
dist : {
tarball : 'my tar'
}
},
}
};

let getMetaDataUnsortedBat = {
versions : {
'0.1.0' : {
engines : {
composer : '0.9.0'
},
name : 'bat',
description : 'bat package',
version : '1.0',
dist : {
tarball : 'my tar'
}
},
}
};

if (url.startsWith('https://registry.npmjs.org/-/v1/search') && forceSearchFail) {
forceSearchFail = false;
return callback('some error');
} else if (url.startsWith('https://registry.npmjs.org/-/v1/search') && sortableList) {
sortableList = false;
return callback(null, getUnsortedSampleNames);
} else if (url.startsWith('https://registry.npmjs.org/-/v1/search')) {
return callback(null, getSampleNames);
}

if (url.startsWith('https://registry.npmjs.org/bob') && forceMetadataFail) {
forceMetadataFail = false;
return callback('some error');
} else if (url.startsWith('https://registry.npmjs.org/bob')) {
} else if (url.startsWith('https://registry.npmjs.org/bob') && !sortableList) {
return callback(null, getMetaData);
}

if (url.startsWith('https://registry.npmjs.org/cat') && forceMetadataFail) {
forceMetadataFail = false;
return callback('some error');
} else if (url.startsWith('https://registry.npmjs.org/cat')) {
return callback(null, getMetaDataUnsortedCat);
}

if (url.startsWith('https://registry.npmjs.org/ant') && forceMetadataFail) {
forceMetadataFail = false;
return callback('some error');
} else if (url.startsWith('https://registry.npmjs.org/ant')) {
return callback(null, getMetaDataUnsortedAnt);
}

if (url.startsWith('https://registry.npmjs.org/bat') && forceMetadataFail) {
forceMetadataFail = false;
return callback('some error');
} else if (url.startsWith('https://registry.npmjs.org/bat')) {
return callback(null, getMetaDataUnsortedBat);
}

if (sortableList) {
sortableList = false;
return callback(null, getUnsortedSampleNames);
}

};

describe('npm routes', () => {
Expand Down Expand Up @@ -165,7 +262,6 @@ describe('npm routes', () => {

app.listen();


});

describe('GET /api/getSampleList', () => {
Expand All @@ -185,6 +281,33 @@ describe('npm routes', () => {
});
});

it('should sort the list correctly', () => {
sortableList = true;
return chai.request(app)
.get('/api/getSampleList')
.then((res) => {
res.should.have.status(200);
res.should.be.json;
res.body.should.be.an('array');
res.body.should.deep.equal([{
name : 'ant',
description : 'ant package',
version : '1.0',
tarball : 'my tar'
}, {
name : 'bat',
description : 'bat package',
version : '1.0',
tarball : 'my tar'
}, {
name : 'cat',
description : 'cat package',
version : '1.0',
tarball : 'my tar'
}]);
});
});

it('should handle error in getting list', () => {
forceSearchFail = true;
return chai.request(app)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ import-modal {

.github-spinner {
height: 200px;
width: 465px;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,37 +406,20 @@ describe('ImportComponent', () => {
});
});

describe('orderGitHubNetworks', () => {
describe('addEmptyNetworkOption', () => {

const BASIC_SAMPLE = 'basic-sample-network';
const CAR_AUCTION = 'carauction-network';
const FOO = 'foo';
const BAR = 'bar';
const primaryNetworkNames = [BASIC_SAMPLE, CAR_AUCTION];

it('should return an array only allowing an empty project to be created when an empty array is input', () => {
let result = component.orderGitHubProjects([]);
result.length.should.equal(1);
result[0].name.should.equal('Empty Business Network');
});

it('should order the list of networks correctly if a list of networks is passed in', () => {
let INPUT_NETWORKS = [{name: FOO}, {name: BASIC_SAMPLE}, {name: CAR_AUCTION}];
let result = component.orderGitHubProjects(INPUT_NETWORKS);
it('should correctly add an empty network option to the start of the list', () => {
let INPUT_NETWORKS = [{name: BASIC_SAMPLE}, {name: BAR}, {name: FOO}];
let result = component.addEmptyNetworkOption(INPUT_NETWORKS);
result.length.should.equal(4);
result[0].name.should.equal(EMPTY_NETWORK.name);
result[1].name.should.equal(BASIC_SAMPLE);
result[2].name.should.equal(CAR_AUCTION);
result[3].name.should.equal(FOO);
});

it('should order the list of networks correctly if a list of networks is passed in without the primary network names', () => {
let INPUT_NETWORKS = [{name: FOO}, {name: BAR}];
let result = component.orderGitHubProjects(INPUT_NETWORKS);
result.length.should.equal(3);
result[0].name.should.equal(EMPTY_NETWORK.name);
result[1].name.should.equal(FOO);
result[2].name.should.equal(BAR);
result[3].name.should.equal(FOO);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class ImportComponent implements OnInit {
private deployInProgress: boolean = false;
private gitHubInProgress: boolean = false;
private sampleNetworks = [];
private primaryNetworkNames = ['basic-sample-network', 'carauction-network'];
private primaryNetworkNames = ['basic-sample-network'];
private chosenNetwork = null;
private expandInput: boolean = false;

Expand Down Expand Up @@ -75,7 +75,7 @@ export class ImportComponent implements OnInit {
this.gitHubInProgress = true;
this.sampleBusinessNetworkService.getSampleList()
.then((sampleNetworkList) => {
this.sampleNetworks = this.orderGitHubProjects(sampleNetworkList);
this.sampleNetworks = this.addEmptyNetworkOption(sampleNetworkList);
this.gitHubInProgress = false;

})
Expand All @@ -85,26 +85,17 @@ export class ImportComponent implements OnInit {
});
}

orderGitHubProjects(networks: any[]): any[] {
addEmptyNetworkOption(networks: any[]): any[] {

let newOrder = [];

// Append new network option to the list.
newOrder.push(this.EMPTY_BIZNET);

for (let i = 0; i < this.primaryNetworkNames.length; i++) {
let primaryName = this.primaryNetworkNames[i];
for (let j = 0; j < networks.length; j++) {
let network = networks[j];
if (primaryName === network.name) {
newOrder.push(network);
}
}
}
for (let i = 0; i < networks.length; i++) {
let network = networks[i];
if (this.primaryNetworkNames.indexOf(network.name) === -1) {
newOrder.push(network);
}
newOrder.push(networks[i]);
}

return newOrder;
}

Expand Down

0 comments on commit 476e733

Please sign in to comment.