Skip to content

Commit

Permalink
[FAB-15823] Fix getStringPayload npe
Browse files Browse the repository at this point in the history
getStringPayload will now return null if the payload is
null instead of throwing a null pointer exception

Also fixes build break introduced with new Context argument
on transaction methods

Change-Id: I15dc4403a7a4782de6a22da7885d4b5baf40e4d7
Signed-off-by: James Taylor <[email protected]>
  • Loading branch information
jt-nti committed Jun 27, 2019
1 parent 7a60cb6 commit 9077581
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

package org.hyperledger.fabric.shim;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.util.HashMap;
import java.util.Map;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Defines methods that all chaincodes must implement.
*/
Expand Down Expand Up @@ -70,7 +70,7 @@ public byte[] getPayload() {
}

public String getStringPayload() {
return new String(payload, UTF_8);
return (payload==null) ? null : new String(payload, UTF_8);
}

/**
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
public String t5(Context ctx) {
doSomeWork();
System.out.println("SampleContract::T5 Done");
return null;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;

Expand Down Expand Up @@ -178,6 +179,32 @@ public void testInvokeTxnThatDoesNotExist() {
assertThat(SampleContract.t1Invoked, is(0));
}

@Test
public void testInvokeTxnThatReturnsNullString() {
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:t5");
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(nullValue()));
assertThat(SampleContract.beforeInvoked, is(1));
assertThat(SampleContract.afterInvoked, is(1));
assertThat(SampleContract.doWorkInvoked, is(1));
assertThat(SampleContract.t1Invoked, is(0));
}

@Test
public void testInvokeTxnThatThrowsAnException() {
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 @@ -12,6 +12,7 @@
import java.lang.reflect.Method;
import java.security.Permission;

import org.hyperledger.fabric.contract.Context;
import org.hyperledger.fabric.contract.ContractInterface;
import org.hyperledger.fabric.contract.ContractRuntimeException;
import org.hyperledger.fabric.contract.annotation.Contract;
Expand Down Expand Up @@ -89,7 +90,7 @@ public void duplicateTransaction() throws NoSuchMethodException, SecurityExcepti
ContractDefinition cf = new ContractDefinitionImpl(SampleContract.class);

ContractInterface contract = new SampleContract();
Method m = contract.getClass().getMethod("t2", new Class[] {});
Method m = contract.getClass().getMethod("t2", new Class[] { Context.class });

thrown.expect(ContractRuntimeException.class);
thrown.expectMessage("Duplicate transaction method t2");
Expand Down

0 comments on commit 9077581

Please sign in to comment.