Skip to content

Commit

Permalink
Add headless tests to increase coverage
Browse files Browse the repository at this point in the history
Using the code coverage report,
Client headless tests are added and additional
error checking is added to Client.js.
In Chain.js removed duplicate required name check
and unnecessary debug logging.

Patch set 2: Rebase and fix lint errors.

Change-Id: Iac51f0daf6f8265a0629868299154aa8174ab9b5
Signed-off-by: cdaughtr <[email protected]>
  • Loading branch information
cdaughtr committed Jan 18, 2017
1 parent d4b315b commit 7eef633
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 15 deletions.
10 changes: 0 additions & 10 deletions hfc/lib/Chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ var Chain = class {
throw new Error('Failed to create Chain. Missing requirement "clientContext" parameter.');
}

// naming for the chain will be enforced later by the orderer when
// "initialize()" gets called
this._name = name;

// Security enabled flag
Expand Down Expand Up @@ -214,7 +212,6 @@ var Chain = class {
return;
}
}
logger.debug('Did not find a peer to remove with url "%s".', url);
}

/**
Expand Down Expand Up @@ -262,7 +259,6 @@ var Chain = class {
return;
}
}
logger.debug('Did not find an orderer to remove with url "%s".', url);
}

/**
Expand Down Expand Up @@ -405,12 +401,6 @@ var Chain = class {
return Promise.reject(new Error('no user defined'));
}

// verify that we have a name configured
if(!this._name) {
logger.error('initializeChain - no chain id defined');
return Promise.reject(new Error('Chain name is not defined'));
}

// verify that we have a transactionid configured
if(!this._initial_transaction_id) {
logger.error('initializeChain - no transaction id defined');
Expand Down
14 changes: 11 additions & 3 deletions hfc/lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ var Client = class {
var self = this;
logger.debug('saveUserToStateStore, userContext: ' + self._userContext);
return new Promise(function(resolve, reject) {
if (self._userContext && self._userContext._name) {
if (self._userContext && self._userContext._name && self._stateStore) {
logger.debug('saveUserToStateStore, begin promise stateStore.setValue');
self._stateStore.setValue(self._userContext._name, self._userContext.toString())
.then(
Expand Down Expand Up @@ -177,8 +177,16 @@ var Client = class {
}
);
} else {
logger.debug('saveUserToStateStore Promise rejected');
reject(new Error('Cannot save null userContext name to stateStore.'));
if (self._userContext == null) {
logger.debug('saveUserToStateStore Promise rejected, Cannot save user to state store when userContext is null.');
reject(new Error('Cannot save user to state store when userContext is null.'));
} else if (self._userContext._name == null) {
logger.debug('saveUserToStateStore Promise rejected, Cannot save user to state store when userContext has no name.');
reject(new Error('Cannot save user to state store when userContext has no name.'));
} else {
logger.debug('saveUserToStateStore Promise rejected, Cannot save user to state store when stateStore is null.');
reject(new Error('Cannot save user to state store when stateStore is null.'));
}
}
});
}
Expand Down
97 changes: 95 additions & 2 deletions test/unit/headless-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,81 @@ test('\n\n ** index.js **\n\n', function (t) {
var Client = hfc;
var client = new Client();
var chainKeyValStorePath = 'tmp/chainKeyValStorePath';

test('\n\n ** lib/Client.js **\n\n', function (t) {

t.equals(client.getCryptoSuite(), null, 'Client getCryptoSuite should initially be null');
client.setCryptoSuite(utils.getCryptoSuite());
if (client.getCryptoSuite() != null) t.pass('Client getCryptoSuite should not be null after setCryptoSuite');

client.getUserContext()
.then(function(response){
if (response === null)
t.pass('Client tests: getUserContext successful null user name.');
else t.fail('Client tests: getUserContext failed null name check');
}, function(error){
t.fail('Client tests: Unexpected error, getUserContext null name check. ' + error);
});

client.saveUserToStateStore()
.then(function(response){
t.fail('Client tests: got response, but should throw "Cannot save user to state store when userContext is null."');
}, function(error){
if (error.message === 'Cannot save user to state store when userContext is null.')
t.pass('Client tests: Should throw "Cannot save user to state store when userContext is null."');
else t.fail('Client tests: Unexpected error message thrown, should throw "Cannot save user to state store when userContext is null." ' + error);
});

client.setUserContext(null)
.then(function(response){
t.fail('Client tests: got response, but should throw "Cannot save null userContext."');
}, function(error){
if (error.message === 'Cannot save null userContext.')
t.pass('Client tests: Should throw "Cannot save null userContext."');
else t.fail('Client tests: Unexpected error message thrown, should throw "Cannot save null userContext." ' + error);
});

client.getUserContext('someUser')
.then(function(response){
if (response == null)
t.pass('Client tests: getUserContext with no context in memory or persisted returns null');
else t.fail('Client tests: getUserContext with no context in memory or persisted did not return null');
}, function(error){
t.fail('Client tests: getUserContext with no context in memory or persisted did not returned error. ' + error);
});

client.setUserContext(new User('someUser'), true)
.then(function(response){
if (response && response.getName() === 'someUser') {
t.pass('Client tests: successfully setUserContext with skipPersistence.');
debugger;//check out this return
return response;
}
else t.fail('Client tests: failed name check after setUserContext with skipPersistence.');
}, function(error){
t.fail('Client tests: Unexpected error, failed setUserContext with skipPersistence. ' + error);
})
.then(function(response){
client.getUserContext('someUser')
.then(function(response){
if (response && response.getName() === 'someUser')
t.pass('Client tests: getUserContext not persisted/skipPersistence was successful.');
else t.fail('Client tests: getUserContext not persisted/skipPersistence was not successful.');
}, function(error){
t.fail('Client tests: Unexpected error, getUserContext not persisted/skipPersistence. ' + error);
});
});

client.setUserContext(new User('someUser'))
.then(function(result){
t.fail('Client tests: setUserContext without skipPersistence and no stateStore should not return result.');
},
function(error){
if (error.message === 'Cannot save user to state store when stateStore is null.')
t.pass('Client tests: Should throw "Cannot save user to state store when stateStore is null"');
else t.fail('Client tests: Unexpected error message thrown, should throw "Cannot save user to state store when stateStore is null." ' + error);
});

var chain = client.newChain('someChain');
t.equals(chain.getName(), 'someChain', 'Checking chain names match');
t.throws(
Expand All @@ -150,6 +224,13 @@ test('\n\n ** lib/Client.js **\n\n', function (t) {
null,
'Client tests: getChain()');

t.throws(
function () {
client.getChain('someOtherChain');
},
/^Error: Chain not found for name someOtherChain./,
'Client tests: Should throw Error: Chain not found for name someOtherChain.');

t.throws(
function() {
client.setStateStore({});
Expand Down Expand Up @@ -187,7 +268,7 @@ test('\n\n ** lib/Client.js **\n\n', function (t) {
t.fail('Client getStateStore test: Failed to set value, reason: ' + reason);
t.end();
}
);
);
});
});

Expand Down Expand Up @@ -624,7 +705,19 @@ test('\n\n ** Chain - method tests **\n\n', function (t) {
t.equals(error.toString(),'Error: Initial transaction id is not defined','Chain tests: transaction id is required when initializing');
}
}
); t.end();
);
var client3 = new Client();
var test_chain3 = new Chain('someTestChain3', client3);
test_chain3.addOrderer(new Orderer('grpc://somehost.com:1234'));
test_chain3.initializeChain().then(function(response){
t.fail('Chain tests: no user defined should, throw error, response '+response);
},function(error){
if (error && error.message && error.message === 'no user defined')
t.pass('Chain tests: no user defined, should throw error');
else t.fail('Chain tests: no user defined, should have thrown error "no user defined"');
});

t.end();
});

// User tests /////////
Expand Down

0 comments on commit 7eef633

Please sign in to comment.