Skip to content

Commit

Permalink
FAB-4454 Increase junit coverage transaction
Browse files Browse the repository at this point in the history
. Increased junit coverage for the fabric.sdk.transaction package
. Added new unit tests for various classes
. Removed some assert statements
. Removed ProtoUtils.createChaincodeSpec as it was unused
. Removed TransactionContext.attrs property as it was unused
. Removed some other minor dead code and minor refactoring

Change-Id: I70c179dc96069160365cbd1ba1a5ca1b8203643e
Signed-off-by: Chris Murphy <chrism@fast.au.fujitsu.com>
  • Loading branch information
chrism28282828 authored and cr22rc committed Jun 13, 2017
1 parent 53f6955 commit 8232a06
Show file tree
Hide file tree
Showing 9 changed files with 464 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.hyperledger.fabric.sdk.transaction;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
Expand Down Expand Up @@ -92,21 +93,21 @@ private void constructInstallProposal() throws ProposalException {

createNetModeTransaction();

} catch (Exception exp) {
} catch (IOException exp) {
logger.error(exp);
throw new ProposalException("IO Error while creating install proposal", exp);
}
}

private void createNetModeTransaction() throws Exception {
private void createNetModeTransaction() throws IOException {
logger.debug("createNetModeTransaction");

if (null == chaincodeSource && chaincodeInputStream == null) {
throw new IllegalArgumentException("Missing chaincode source or chaincode inputstream in InstallRequest");
throw new IllegalArgumentException("Missing chaincodeSource or chaincodeInputStream in InstallRequest");
}

if (null != chaincodeSource && chaincodeInputStream != null) {
throw new IllegalArgumentException("Both chaincode source and chaincode inputstream in InstallRequest were set. Specify on or the other.");
throw new IllegalArgumentException("Both chaincodeSource and chaincodeInputStream in InstallRequest were set. Specify one or the other");
}

final Type ccType;
Expand All @@ -117,10 +118,14 @@ private void createNetModeTransaction() throws Exception {
switch (chaincodeLanguage) {
case GO_LANG:

// chaincodePath is mandatory
// chaincodeSource may be a File or InputStream

// Verify that chaincodePath is being passed
if (Utils.isNullOrEmpty(chaincodePath)) {
throw new IllegalArgumentException("Missing chaincodePath in InstallRequest");
}

dplang = "Go";
ccType = Type.GOLANG;
if (null != chaincodeSource) {
Expand All @@ -131,11 +136,20 @@ private void createNetModeTransaction() throws Exception {
break;

case JAVA:

// chaincodePath is not applicable and must be null
// chaincodeSource may be a File or InputStream

// Verify that chaincodePath is null
if (null != chaincodePath) {

This comment has been minimized.

Copy link
@alacambra

alacambra Aug 25, 2017

There is a bug here. It is not possible to set it to null, if we do not patch something. ChaincodeId does not allow null values and per default it is an empty String!

throw new IllegalArgumentException("chaincodePath must be null for Java chaincode");
}

dplang = "Java";
ccType = Type.JAVA;
if (null != chaincodeSource) {
targetPathPrefix = "src";
projectSourceDir = Paths.get(chaincodeSource.toString(), chaincodePath).toFile();
projectSourceDir = Paths.get(chaincodeSource.toString()).toFile();
}
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private void constructInstantiateProposal() throws ProposalException {
}
}

private void createNetModeTransaction() throws Exception {
private void createNetModeTransaction() {
logger.debug("NetModeTransaction");

List<String> modlist = new LinkedList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class ProposalBuilder {
protected TransactionRequest request;
protected ChaincodeSpec.Type ccType = ChaincodeSpec.Type.GOLANG;
protected Map<String, byte[]> transientMap = null;

// The channel that is being targeted . note blank string means no specific channel
private String channelID;

protected ProposalBuilder() {
Expand Down Expand Up @@ -92,16 +94,6 @@ public ProposalBuilder request(TransactionRequest request) {
return this;
}

/**
* The channel that is being targeted . note blank string means no specific channel.
*
* @param channelID
*/

public void channelID(String channelID) {
this.channelID = channelID;
}

public FabricProposal.Proposal build() throws ProposalException {
if (request != null && request.noChannelID()) {
channelID = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,6 @@ public static ChaincodeDeploymentSpec createDeploymentSpec(Type ccType, String n

}

public static ChaincodeSpec createChaincodeSpec(String name, ChaincodeSpec.Type ccType, Object... args) {

ChaincodeID chaincodeID = ChaincodeID.newBuilder().setName(name).build();

List<ByteString> argList = new ArrayList<>(args.length);

for (Object arg : args) {
if (arg instanceof String) {
arg = ByteString.copyFrom(((String) arg).getBytes(UTF_8));
} else if (arg instanceof byte[]) {
arg = ByteString.copyFrom((byte[]) arg);
}
argList.add((ByteString) arg);
}

ChaincodeInput chaincodeInput = ChaincodeInput.newBuilder()
.addAllArgs(argList)
.build();

return ChaincodeSpec.newBuilder().setType(ccType).setChaincodeId(chaincodeID)
.setInput(chaincodeInput)
.build();

}

// static CryptoSuite suite = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package org.hyperledger.fabric.sdk.transaction;

import java.util.List;

import com.google.protobuf.ByteString;
import com.google.protobuf.Timestamp;
Expand Down Expand Up @@ -54,7 +53,7 @@ public CryptoSuite getCryptoPrimitives() {

private final String txID;

private List<String> attrs;
//private List<String> attrs;
private long proposalWaitTime = config.getProposalWaitTime();
private final Identities.SerializedIdentity identity;

Expand Down Expand Up @@ -114,18 +113,18 @@ public Channel getChannel() {
*
* @return the attributes.
*/
public List<String> getAttrs() {
return this.attrs;
}
//public List<String> getAttrs() {
// return this.attrs;
//}

/**
* Set the attributes for this transaction context.
*
* @param attrs the attributes.
*/
public void setAttrs(List<String> attrs) {
this.attrs = attrs;
}
//public void setAttrs(List<String> attrs) {
// this.attrs = attrs;
//}

/**
* Gets the timeout for a single proposal request to endorser in milliseconds.
Expand Down Expand Up @@ -196,15 +195,6 @@ public ASN1Primitive toASN1Primitive() {
}
}

String getMSPID() {
return user.getMspId();
}

String getCreator() {
return getUser().getEnrollment().getCert();

}

public String getChannelID() {
return getChannel().getName();
}
Expand All @@ -222,14 +212,15 @@ public ByteString signByteString(byte[] b) throws CryptoException {
}

public ByteString signByteStrings(ByteString... bs) throws CryptoException {
assert bs != null;
if (bs == null) {
return null;
}
assert bs.length != 0;
if (bs.length == 0) {
return null;
}
if (bs.length == 1 && bs[0] == null) {
return null;
}

ByteString f = bs[0];
for (int i = 1; i < bs.length; ++i) {
Expand All @@ -240,19 +231,19 @@ public ByteString signByteStrings(ByteString... bs) throws CryptoException {
}

public ByteString[] signByteStrings(User[] users, ByteString... bs) throws CryptoException {
assert bs != null;
if (bs == null) {
return null;
}
assert bs.length != 0;
if (bs.length == 0) {
return null;
}
if (bs.length == 1 && bs[0] == null) {
return null;
}

ByteString f = bs[0];
for (int i = 1; i < bs.length; ++i) {
f = f.concat(bs[i]);

}

final byte[] signbytes = f.toByteArray();
Expand Down
Loading

0 comments on commit 8232a06

Please sign in to comment.