Skip to content

Commit

Permalink
FAB-4596 Improve code coverage
Browse files Browse the repository at this point in the history
Improve code coverage for the fabric-sdk-java code base

Change-Id: I5d549d02b3accbeb97463ca31328a10287df4f4b
Signed-off-by: rickr <cr22rc@gmail.com>
  • Loading branch information
cr22rc committed Jun 13, 2017
1 parent 819afce commit 53f6955
Show file tree
Hide file tree
Showing 13 changed files with 939 additions and 500 deletions.
452 changes: 167 additions & 285 deletions src/main/java/org/hyperledger/fabric/sdk/Channel.java

Large diffs are not rendered by default.

65 changes: 0 additions & 65 deletions src/main/java/org/hyperledger/fabric/sdk/GetTCertBatchRequest.java

This file was deleted.

69 changes: 34 additions & 35 deletions src/main/java/org/hyperledger/fabric/sdk/HFClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ProposalException;
import org.hyperledger.fabric.sdk.exception.TransactionException;
import org.hyperledger.fabric.sdk.helper.Utils;
import org.hyperledger.fabric.sdk.security.CryptoSuite;

import static java.lang.String.format;
import static org.hyperledger.fabric.sdk.User.userContextCheck;

public class HFClient {

Expand Down Expand Up @@ -84,8 +84,10 @@ public void setCryptoSuite(CryptoSuite cryptoSuite) throws CryptoException, Inva
throw new InvalidArgumentException("CryptoSuite may only be set once.");

}

cryptoSuite.init();
this.cryptoSuite = cryptoSuite;
this.cryptoSuite.init();

}

/**
Expand All @@ -106,6 +108,7 @@ public static HFClient createNewInstance() {
*/

public Channel newChannel(String name) throws InvalidArgumentException {
clientCheck();
logger.trace("Creating channel :" + name);
Channel newChannel = Channel.createNewInstance(name, this);
channels.put(name, newChannel);
Expand All @@ -127,6 +130,7 @@ public Channel newChannel(String name) throws InvalidArgumentException {

public Channel newChannel(String name, Orderer orderer, ChannelConfiguration channelConfiguration, byte[]... channelConfigurationSignatures) throws TransactionException, InvalidArgumentException {

clientCheck();
logger.trace("Creating channel :" + name);
Channel newChannel = Channel.createNewInstance(name, this, orderer, channelConfiguration, channelConfigurationSignatures);
channels.put(name, newChannel);
Expand Down Expand Up @@ -166,6 +170,7 @@ public Channel newChannel(String name, Orderer orderer, ChannelConfiguration cha
*/

public Peer newPeer(String name, String grpcURL, Properties properties) throws InvalidArgumentException {
clientCheck();
return Peer.createNewInstance(name, grpcURL, properties);
}

Expand All @@ -179,6 +184,7 @@ public Peer newPeer(String name, String grpcURL, Properties properties) throws I
*/

public Peer newPeer(String name, String grpcURL) throws InvalidArgumentException {
clientCheck();
return Peer.createNewInstance(name, grpcURL, null);
}

Expand Down Expand Up @@ -245,37 +251,14 @@ public QueryByChaincodeRequest newQueryProposalRequest() {

public void setUserContext(User userContext) throws InvalidArgumentException {

if (userContext == null) {
throw new InvalidArgumentException("setUserContext is null");
}
final String userName = userContext.getName();
if (Utils.isNullOrEmpty(userName)) {
throw new InvalidArgumentException("setUserContext user's name is missing");
}

Enrollment enrollment = userContext.getEnrollment();
if (enrollment == null) {
throw new InvalidArgumentException(format("setUserContext for user %s has no Enrollment set", userName));
}

if (Utils.isNullOrEmpty(userContext.getMspId())) {
throw new InvalidArgumentException(format("setUserContext for user %s has user's MSPID is missing", userName));
}

if (Utils.isNullOrEmpty(userContext.getName())) {
throw new InvalidArgumentException("setUserContext user's name is missing");
}

if (Utils.isNullOrEmpty(enrollment.getCert())) {
throw new InvalidArgumentException(format("setUserContext for user %s Enrollment missing user certificate.", userName));
}
if (null == enrollment.getKey()) {
throw new InvalidArgumentException(format("setUserContext for user %s has Enrollment missing signing key", userName));
if (null == cryptoSuite) {
throw new InvalidArgumentException("No cryptoSuite has been set.");
}
userContextCheck(userContext);
this.userContext = userContext;

logger.debug(format("Setting user context to MSPID: %s user: %s", userContext.getMspId(), userContext.getName()));

this.userContext = userContext;
}

/**
Expand Down Expand Up @@ -311,6 +294,7 @@ public void setUserContext(User userContext) throws InvalidArgumentException {
*/

public EventHub newEventHub(String name, String grpcURL, Properties properties) throws InvalidArgumentException {
clientCheck();
return EventHub.createNewInstance(name, grpcURL, executorService, properties);
}

Expand All @@ -324,6 +308,7 @@ public EventHub newEventHub(String name, String grpcURL, Properties properties)
*/

public EventHub newEventHub(String name, String grpcURL) throws InvalidArgumentException {
clientCheck();
return newEventHub(name, grpcURL, null);
}

Expand All @@ -337,6 +322,7 @@ public EventHub newEventHub(String name, String grpcURL) throws InvalidArgumentE
*/

public Orderer newOrderer(String name, String grpcURL) throws InvalidArgumentException {
clientCheck();
return newOrderer(name, grpcURL, null);
}

Expand Down Expand Up @@ -373,6 +359,7 @@ public Orderer newOrderer(String name, String grpcURL) throws InvalidArgumentExc
*/

public Orderer newOrderer(String name, String grpcURL, Properties properties) throws InvalidArgumentException {
clientCheck();
return Orderer.createNewInstance(name, grpcURL, properties);
}

Expand All @@ -386,9 +373,8 @@ public Orderer newOrderer(String name, String grpcURL, Properties properties) th
*/
public Set<String> queryChannels(Peer peer) throws InvalidArgumentException, ProposalException {

if (userContext == null) {
throw new InvalidArgumentException("UserContext has not been set.");
}
clientCheck();

if (null == peer) {

throw new InvalidArgumentException("peer set to null");
Expand Down Expand Up @@ -421,9 +407,8 @@ public Set<String> queryChannels(Peer peer) throws InvalidArgumentException, Pro

public List<ChaincodeInfo> queryInstalledChaincodes(Peer peer) throws InvalidArgumentException, ProposalException {

if (userContext == null) {
throw new InvalidArgumentException("UserContext has not been set.");
}
clientCheck();

if (null == peer) {

throw new InvalidArgumentException("peer set to null");
Expand Down Expand Up @@ -454,6 +439,8 @@ public List<ChaincodeInfo> queryInstalledChaincodes(Peer peer) throws InvalidArg

public byte[] getChannelConfigurationSignature(ChannelConfiguration channelConfiguration, User signer) throws InvalidArgumentException {

clientCheck();

Channel systemChannel = Channel.newSystemChannel(this);
return systemChannel.getChannelConfigurationSignature(channelConfiguration, signer);

Expand All @@ -472,11 +459,23 @@ public byte[] getChannelConfigurationSignature(ChannelConfiguration channelConfi
public Collection<ProposalResponse> sendInstallProposal(InstallProposalRequest installProposalRequest, Collection<Peer> peers)
throws ProposalException, InvalidArgumentException {

clientCheck();

installProposalRequest.setSubmitted();
Channel systemChannel = Channel.newSystemChannel(this);

return systemChannel.sendInstallProposal(installProposalRequest, peers);

}

private void clientCheck() throws InvalidArgumentException {

if (null == cryptoSuite) {
throw new InvalidArgumentException("No cryptoSuite has been set.");
}

userContextCheck(userContext);

}

}
11 changes: 5 additions & 6 deletions src/main/java/org/hyperledger/fabric/sdk/Peer.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ void setChannel(Channel channel) throws InvalidArgumentException {

}

void unsetChannel() {
channel = null;

}

/**
* The channel the peer is set on.
*
Expand Down Expand Up @@ -155,9 +160,6 @@ ListenableFuture<FabricProposalResponse.ProposalResponse> sendProposalAsync(Fabr

try {
return localEndorserClient.sendProposalAsync(proposal);
} catch (PeerException e) { //Any error start with a clean connection.
endorserClent = null;
throw e;
} catch (Throwable t) {
endorserClent = null;
throw t;
Expand All @@ -179,9 +181,6 @@ FabricProposalResponse.ProposalResponse sendProposal(FabricProposal.SignedPropos

try {
return localEndorserClient.sendProposal(proposal);
} catch (PeerException e) { //Any error start with a clean connection.
endorserClent = null;
throw e;
} catch (Throwable t) {
endorserClent = null;
throw t;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,12 @@ public boolean isSubmitted() {
}

void setSubmitted() throws InvalidArgumentException {

if (submitted) {
// Has already been submitted.
throw new InvalidArgumentException("Request has been already submitted and can not be reused.");
}
User.userContextCheck(userContext);
this.submitted = true;
}

Expand Down
22 changes: 0 additions & 22 deletions src/main/java/org/hyperledger/fabric/sdk/TransactionResponse.java

This file was deleted.

38 changes: 36 additions & 2 deletions src/main/java/org/hyperledger/fabric/sdk/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

import java.util.Set;

import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.helper.Utils;

import static java.lang.String.format;

/**
* User - Is the interface needed to be implemented by embedding application of the SDK
*/
Expand Down Expand Up @@ -58,9 +63,38 @@ public interface User {
Enrollment getEnrollment();

/**
* Get the ID provided by the user's organization.
* Get the Membership Service Provider Identifier provided by the user's organization.
*
* @return msp ID.
* @return MSP Id.
*/
String getMspId();

static void userContextCheck(User userContext) throws InvalidArgumentException {

if (userContext == null) {
throw new InvalidArgumentException("UserContext is null");
}
final String userName = userContext.getName();
if (Utils.isNullOrEmpty(userName)) {
throw new InvalidArgumentException("UserContext user's name missing.");
}

Enrollment enrollment = userContext.getEnrollment();
if (enrollment == null) {
throw new InvalidArgumentException(format("UserContext for user %s has no enrollment set.", userName));
}

if (Utils.isNullOrEmpty(userContext.getMspId())) {
throw new InvalidArgumentException(format("UserContext for user %s has user's MSPID missing.", userName));
}

if (Utils.isNullOrEmpty(enrollment.getCert())) {
throw new InvalidArgumentException(format("UserContext for user %s enrollment missing user certificate.", userName));
}
if (null == enrollment.getKey()) {
throw new InvalidArgumentException(format("UserContext for user %s has Enrollment missing signing key", userName));
}

}

}
Loading

0 comments on commit 53f6955

Please sign in to comment.