Skip to content

Commit

Permalink
[FAB-15632] Add name element to transaction annotation
Browse files Browse the repository at this point in the history
Change-Id: I895c55449ee6924a836144d9bee4d86424f46e4a
Signed-off-by: James Taylor <[email protected]>
  • Loading branch information
jt-nti committed Jun 13, 2019
1 parent d726175 commit f87de8e
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@
* @return
*/
boolean submit() default true;

/**
* The name of the callable transaction if it should be different to the method
* name
*
* @return the transaction name
*/
String name() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class TxFunctionImpl implements TxFunction {
private static Logger logger = Logger.getLogger(TxFunctionImpl.class);

private Method method;
private String name;
private TransactionType type;
private Routing routing;
private TypeSchema returnSchema;
Expand Down Expand Up @@ -77,6 +78,14 @@ public TxFunctionImpl(Method m, ContractDefinition contract) {
this.type = TransactionType.QUERY;
}

String txnName = m.getAnnotation(Transaction.class).name();
if (!txnName.isEmpty()) {
this.name = txnName;
}
}

if (name == null) {
this.name = m.getName();
}

this.routing = new RoutingImpl(m, contract.getContractImpl());
Expand Down Expand Up @@ -108,7 +117,7 @@ public TxFunctionImpl(Method m, ContractDefinition contract) {

@Override
public String getName() {
return this.method.getName();
return name;
}

@Override
Expand All @@ -133,7 +142,7 @@ public TransactionType getType() {

@Override
public String toString() {
return this.method.getName() + " @" + Integer.toHexString(System.identityHashCode(this));
return name + " @" + Integer.toHexString(System.identityHashCode(this));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public SystemContract() {

}

@Transaction(submit = false)
public String GetMetadata() {
@Transaction(submit = false, name = "GetMetadata")
public String getMetadata() {
String jsonmetadata = MetadataBuilder.getMetadata();
return jsonmetadata;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public class SampleContract implements ContractInterface {
static public int t1Invoked = 0;
static public int i1Invoked = 0;

@Transaction(name = "t4")
public String tFour() {

System.out.println("SampleContract::T4 Done");
return "Transaction 4";
}

@Transaction
public String t3() {
throw new RuntimeException("T3 fail!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,58 @@ public void testInvokeTxnThatExists() {
assertThat(SampleContract.t1Invoked, is(1));
}

@Test
public void testInvokeTxnWithDefinedName() {
ContractRouter r = new ContractRouter(new String[] { "-a", "127.0.0.1:7052", "-i", "testId" });
r.findAllContracts();
ChaincodeStub s = new ChaincodeStubNaiveImpl();

List<String> args = new ArrayList<>();
args.add("samplecontract:t4");
args.add("asdf");
((ChaincodeStubNaiveImpl) s).setStringArgs(args);

SampleContract.beforeInvoked = 0;
SampleContract.afterInvoked = 0;
SampleContract.doWorkInvoked = 0;
SampleContract.t1Invoked = 0;

Chaincode.Response response = r.invoke(s);
assertThat(response, is(notNullValue()));
assertThat(response.getStatus(), is(Chaincode.Response.Status.SUCCESS));
assertThat(response.getStringPayload(), is(equalTo("Transaction 4")));
assertThat(SampleContract.beforeInvoked, is(1));
assertThat(SampleContract.afterInvoked, is(1));
assertThat(SampleContract.doWorkInvoked, is(0));
assertThat(SampleContract.t1Invoked, is(0));
}

@Test
public void testInvokeTxnWithDefinedNameUsingMethodName() {
ContractRouter r = new ContractRouter(new String[] { "-a", "127.0.0.1:7052", "-i", "testId" });
r.findAllContracts();
ChaincodeStub s = new ChaincodeStubNaiveImpl();

List<String> args = new ArrayList<>();
args.add("samplecontract:tFour");
args.add("asdf");
((ChaincodeStubNaiveImpl) s).setStringArgs(args);

SampleContract.beforeInvoked = 0;
SampleContract.afterInvoked = 0;
SampleContract.doWorkInvoked = 0;
SampleContract.t1Invoked = 0;

Chaincode.Response response = r.invoke(s);
assertThat(response, is(notNullValue()));
assertThat(response.getStatus(), is(Chaincode.Response.Status.INTERNAL_SERVER_ERROR));
assertThat(response.getStringPayload(), is(startsWith("java.lang.IllegalStateException")));
assertThat(SampleContract.beforeInvoked, is(0));
assertThat(SampleContract.afterInvoked, is(0));
assertThat(SampleContract.doWorkInvoked, is(0));
assertThat(SampleContract.t1Invoked, is(0));
}

@Test
public void testInvokeTxnThatDoesNotExist() {
ContractRouter r = new ContractRouter(new String[] { "-a", "127.0.0.1:7052", "-i", "testId" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@
*/
package org.hyperledger.fabric.contract.metadata;

import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.hyperledger.fabric.contract.systemcontract.SystemContract;
import org.junit.Before;
Expand Down Expand Up @@ -48,7 +42,7 @@ public void systemContract() {

// access the system contract to extract the metadata
SystemContract system = new SystemContract();
String metadatacompressed = system.GetMetadata();
String metadatacompressed = system.getMetadata();

}

Expand Down

0 comments on commit f87de8e

Please sign in to comment.