Skip to content

Commit

Permalink
FABN-929: Allow function chaining on Transaction
Browse files Browse the repository at this point in the history
Return this from Transaction.setEventHandlerStrategy() and
Transaction.setTransient() to allow chaining of calls, for example:

await contract.createTransaction('name').setTransient(data).submit();

Change-Id: I859f452a9c902ce7de63bdd6e647eaf1edb22d08
Signed-off-by: Mark S. Lewis <mark_lewis@uk.ibm.com>
  • Loading branch information
bestbeforetoday committed Nov 6, 2018
1 parent 0713ad6 commit 163e5e0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
4 changes: 4 additions & 0 deletions fabric-network/lib/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ class Transaction {
* Set the event handler strategy to be used for this transaction invocation
* instead of the default handler configured in the gateway options.
* @param {Function} factoryFunction Event handler factory function.
* @returns {Transaction} This object, to allow function chaining.
*/
setEventHandlerStrategy(factoryFunction) {
this._createTxEventHandler = factoryFunction;
return this;
}

/**
Expand All @@ -84,9 +86,11 @@ class Transaction {
* private data to a transaction function.
* @param {Object} transientMap Object with String property names and
* Buffer property values.
* @returns {Transaction} This object, to allow function chaining.
*/
setTransient(transientMap) {
this._transientMap = transientMap;
return this;
}

/**
Expand Down
26 changes: 22 additions & 4 deletions fabric-network/test/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ describe('Transaction', () => {
});
});

describe('#setEventHandlerStrategy', () => {
it('returns this', () => {
const transaction = new Transaction(stubContract, 'name');
const stubEventHandler = sinon.createStubInstance(TransactionEventHandler);
const stubEventHandlerFactoryFn = () => stubEventHandler;

const result = transaction.setEventHandlerStrategy(stubEventHandlerFactoryFn);

expect(result).to.equal(transaction);
});
});

describe('#setTransient', () => {
it('returns this', () => {
const transaction = new Transaction(stubContract, 'name');
const result = transaction.setTransient(new Map());
expect(result).to.equal(transaction);
});
});

describe('#submit', () => {
const transactionName = 'TRANSACTION_NAME';
const expectedResult = Buffer.from('42');
Expand Down Expand Up @@ -182,8 +202,7 @@ describe('Transaction', () => {
const stubEventHandlerFactoryFn = sinon.stub();
stubEventHandlerFactoryFn.withArgs(txId, network, options).returns(stubEventHandler);

transaction.setEventHandlerStrategy(stubEventHandlerFactoryFn);
await transaction.submit();
await transaction.setEventHandlerStrategy(stubEventHandlerFactoryFn).submit();

sinon.assert.called(stubEventHandler.startListening);
sinon.assert.called(stubEventHandler.waitForEvents);
Expand All @@ -193,8 +212,7 @@ describe('Transaction', () => {
const transientMap = {key1: 'value1', key2: 'value2'};
expectedProposal.transientMap = transientMap;

transaction.setTransient(transientMap);
await transaction.submit();
await transaction.setTransient(transientMap).submit();

sinon.assert.calledWith(channel.sendTransactionProposal, sinon.match(expectedProposal));
});
Expand Down
3 changes: 1 addition & 2 deletions test/integration/network-e2e/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,7 @@ test('\n\n***** Network End-to-end flow: invoke transaction with transient data
key1: Buffer.from('value1'),
key2: Buffer.from('value2')
};
transaction.setTransient(transientMap);
const response = await transaction.submit();
const response = await transaction.setTransient(transientMap).submit();
t.pass('Got response: ' + response.toString('utf8'));
const result = JSON.parse(response.toString('utf8'));

Expand Down
3 changes: 1 addition & 2 deletions test/integration/network-e2e/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ test('\n\n***** Network End-to-end flow: evaluate transaction with transient dat
key1: Buffer.from('value1'),
key2: Buffer.from('value2')
};
transaction.setTransient(transientMap);
const response = await transaction.evaluate();
const response = await transaction.setTransient(transientMap).evaluate();

t.pass('Got response: ' + response.toString('utf8'));
const result = JSON.parse(response.toString('utf8'));
Expand Down

0 comments on commit 163e5e0

Please sign in to comment.