Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit 53f6955

Browse files
committed
FAB-4596 Improve code coverage
Improve code coverage for the fabric-sdk-java code base Change-Id: I5d549d02b3accbeb97463ca31328a10287df4f4b Signed-off-by: rickr <cr22rc@gmail.com>
1 parent 819afce commit 53f6955

File tree

13 files changed

+939
-500
lines changed

13 files changed

+939
-500
lines changed

src/main/java/org/hyperledger/fabric/sdk/Channel.java

Lines changed: 167 additions & 285 deletions
Large diffs are not rendered by default.

src/main/java/org/hyperledger/fabric/sdk/GetTCertBatchRequest.java

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/main/java/org/hyperledger/fabric/sdk/HFClient.java

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
3333
import org.hyperledger.fabric.sdk.exception.ProposalException;
3434
import org.hyperledger.fabric.sdk.exception.TransactionException;
35-
import org.hyperledger.fabric.sdk.helper.Utils;
3635
import org.hyperledger.fabric.sdk.security.CryptoSuite;
3736

3837
import static java.lang.String.format;
38+
import static org.hyperledger.fabric.sdk.User.userContextCheck;
3939

4040
public class HFClient {
4141

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

8686
}
87+
88+
cryptoSuite.init();
8789
this.cryptoSuite = cryptoSuite;
88-
this.cryptoSuite.init();
90+
8991
}
9092

9193
/**
@@ -106,6 +108,7 @@ public static HFClient createNewInstance() {
106108
*/
107109

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

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

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

168172
public Peer newPeer(String name, String grpcURL, Properties properties) throws InvalidArgumentException {
173+
clientCheck();
169174
return Peer.createNewInstance(name, grpcURL, properties);
170175
}
171176

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

181186
public Peer newPeer(String name, String grpcURL) throws InvalidArgumentException {
187+
clientCheck();
182188
return Peer.createNewInstance(name, grpcURL, null);
183189
}
184190

@@ -245,37 +251,14 @@ public QueryByChaincodeRequest newQueryProposalRequest() {
245251

246252
public void setUserContext(User userContext) throws InvalidArgumentException {
247253

248-
if (userContext == null) {
249-
throw new InvalidArgumentException("setUserContext is null");
250-
}
251-
final String userName = userContext.getName();
252-
if (Utils.isNullOrEmpty(userName)) {
253-
throw new InvalidArgumentException("setUserContext user's name is missing");
254-
}
255-
256-
Enrollment enrollment = userContext.getEnrollment();
257-
if (enrollment == null) {
258-
throw new InvalidArgumentException(format("setUserContext for user %s has no Enrollment set", userName));
259-
}
260-
261-
if (Utils.isNullOrEmpty(userContext.getMspId())) {
262-
throw new InvalidArgumentException(format("setUserContext for user %s has user's MSPID is missing", userName));
263-
}
264-
265-
if (Utils.isNullOrEmpty(userContext.getName())) {
266-
throw new InvalidArgumentException("setUserContext user's name is missing");
267-
}
268-
269-
if (Utils.isNullOrEmpty(enrollment.getCert())) {
270-
throw new InvalidArgumentException(format("setUserContext for user %s Enrollment missing user certificate.", userName));
271-
}
272-
if (null == enrollment.getKey()) {
273-
throw new InvalidArgumentException(format("setUserContext for user %s has Enrollment missing signing key", userName));
254+
if (null == cryptoSuite) {
255+
throw new InvalidArgumentException("No cryptoSuite has been set.");
274256
}
257+
userContextCheck(userContext);
258+
this.userContext = userContext;
275259

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

278-
this.userContext = userContext;
279262
}
280263

281264
/**
@@ -311,6 +294,7 @@ public void setUserContext(User userContext) throws InvalidArgumentException {
311294
*/
312295

313296
public EventHub newEventHub(String name, String grpcURL, Properties properties) throws InvalidArgumentException {
297+
clientCheck();
314298
return EventHub.createNewInstance(name, grpcURL, executorService, properties);
315299
}
316300

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

326310
public EventHub newEventHub(String name, String grpcURL) throws InvalidArgumentException {
311+
clientCheck();
327312
return newEventHub(name, grpcURL, null);
328313
}
329314

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

339324
public Orderer newOrderer(String name, String grpcURL) throws InvalidArgumentException {
325+
clientCheck();
340326
return newOrderer(name, grpcURL, null);
341327
}
342328

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

375361
public Orderer newOrderer(String name, String grpcURL, Properties properties) throws InvalidArgumentException {
362+
clientCheck();
376363
return Orderer.createNewInstance(name, grpcURL, properties);
377364
}
378365

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

389-
if (userContext == null) {
390-
throw new InvalidArgumentException("UserContext has not been set.");
391-
}
376+
clientCheck();
377+
392378
if (null == peer) {
393379

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

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

424-
if (userContext == null) {
425-
throw new InvalidArgumentException("UserContext has not been set.");
426-
}
410+
clientCheck();
411+
427412
if (null == peer) {
428413

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

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

442+
clientCheck();
443+
457444
Channel systemChannel = Channel.newSystemChannel(this);
458445
return systemChannel.getChannelConfigurationSignature(channelConfiguration, signer);
459446

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

462+
clientCheck();
463+
475464
installProposalRequest.setSubmitted();
476465
Channel systemChannel = Channel.newSystemChannel(this);
477466

478467
return systemChannel.sendInstallProposal(installProposalRequest, peers);
479468

480469
}
481470

471+
private void clientCheck() throws InvalidArgumentException {
472+
473+
if (null == cryptoSuite) {
474+
throw new InvalidArgumentException("No cryptoSuite has been set.");
475+
}
476+
477+
userContextCheck(userContext);
478+
479+
}
480+
482481
}

src/main/java/org/hyperledger/fabric/sdk/Peer.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ void setChannel(Channel channel) throws InvalidArgumentException {
9292

9393
}
9494

95+
void unsetChannel() {
96+
channel = null;
97+
98+
}
99+
95100
/**
96101
* The channel the peer is set on.
97102
*
@@ -155,9 +160,6 @@ ListenableFuture<FabricProposalResponse.ProposalResponse> sendProposalAsync(Fabr
155160

156161
try {
157162
return localEndorserClient.sendProposalAsync(proposal);
158-
} catch (PeerException e) { //Any error start with a clean connection.
159-
endorserClent = null;
160-
throw e;
161163
} catch (Throwable t) {
162164
endorserClent = null;
163165
throw t;
@@ -179,9 +181,6 @@ FabricProposalResponse.ProposalResponse sendProposal(FabricProposal.SignedPropos
179181

180182
try {
181183
return localEndorserClient.sendProposal(proposal);
182-
} catch (PeerException e) { //Any error start with a clean connection.
183-
endorserClent = null;
184-
throw e;
185184
} catch (Throwable t) {
186185
endorserClent = null;
187186
throw t;

src/main/java/org/hyperledger/fabric/sdk/TransactionRequest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,12 @@ public boolean isSubmitted() {
271271
}
272272

273273
void setSubmitted() throws InvalidArgumentException {
274+
274275
if (submitted) {
275276
// Has already been submitted.
276277
throw new InvalidArgumentException("Request has been already submitted and can not be reused.");
277278
}
279+
User.userContextCheck(userContext);
278280
this.submitted = true;
279281
}
280282

src/main/java/org/hyperledger/fabric/sdk/TransactionResponse.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/main/java/org/hyperledger/fabric/sdk/User.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
import java.util.Set;
1818

19+
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
20+
import org.hyperledger.fabric.sdk.helper.Utils;
21+
22+
import static java.lang.String.format;
23+
1924
/**
2025
* User - Is the interface needed to be implemented by embedding application of the SDK
2126
*/
@@ -58,9 +63,38 @@ public interface User {
5863
Enrollment getEnrollment();
5964

6065
/**
61-
* Get the ID provided by the user's organization.
66+
* Get the Membership Service Provider Identifier provided by the user's organization.
6267
*
63-
* @return msp ID.
68+
* @return MSP Id.
6469
*/
6570
String getMspId();
71+
72+
static void userContextCheck(User userContext) throws InvalidArgumentException {
73+
74+
if (userContext == null) {
75+
throw new InvalidArgumentException("UserContext is null");
76+
}
77+
final String userName = userContext.getName();
78+
if (Utils.isNullOrEmpty(userName)) {
79+
throw new InvalidArgumentException("UserContext user's name missing.");
80+
}
81+
82+
Enrollment enrollment = userContext.getEnrollment();
83+
if (enrollment == null) {
84+
throw new InvalidArgumentException(format("UserContext for user %s has no enrollment set.", userName));
85+
}
86+
87+
if (Utils.isNullOrEmpty(userContext.getMspId())) {
88+
throw new InvalidArgumentException(format("UserContext for user %s has user's MSPID missing.", userName));
89+
}
90+
91+
if (Utils.isNullOrEmpty(enrollment.getCert())) {
92+
throw new InvalidArgumentException(format("UserContext for user %s enrollment missing user certificate.", userName));
93+
}
94+
if (null == enrollment.getKey()) {
95+
throw new InvalidArgumentException(format("UserContext for user %s has Enrollment missing signing key", userName));
96+
}
97+
98+
}
99+
66100
}

0 commit comments

Comments
 (0)