Skip to content

Commit ac70a6f

Browse files
[FAB-10032] Adding unit test
Added unit test for all classes FVT for chaincode will be added in next commit (FAB-11839) Change-Id: I6c07cad7d3fc28c9f28687fbc0becb201df7cc9b Signed-off-by: gennady <gennady@il.ibm.com>
1 parent ac6c906 commit ac70a6f

File tree

20 files changed

+836
-258
lines changed

20 files changed

+836
-258
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ subprojects {
3131
testCompile 'junit:junit:4.12'
3232
testCompile 'org.hamcrest:hamcrest-library:1.3'
3333
testCompile 'org.mockito:mockito-core:2.+'
34+
testCompile 'com.github.stefanbirkner:system-rules:1.17.0'
3435
}
3536
}

fabric-chaincode-shim/build.gradle

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,18 @@ jacocoTestCoverageVerification {
7171
violationRules {
7272
rule {
7373
element = 'CLASS'
74-
//FIXME: after all unit test added, remove this property
75-
includes = ['org.hyperledger.fabric.shim.ledger.*',
76-
'org.hyperledger.fabric.shim.impl.ChaincodeStubImpl']
74+
excludes = ['org.hyperledger.fabric.shim.helper.Channel',
75+
'org.hyperledger.fabric.shim.impl.Handler',
76+
'org.hyperledger.fabric.shim.impl.Handler.1',
77+
'org.hyperledger.fabric.shim.impl.Handler.CCState',
78+
'org.hyperledger.fabric.shim.impl.ChaincodeSupportStream',
79+
'org.hyperledger.fabric.shim.impl.ChaincodeSupportStream.1',
80+
'org.hyperledger.fabric.shim.impl.ChaincodeSupportStream.2',
81+
'org.hyperledger.fabric.shim.impl.ChaincodeSupportClient',
82+
'org.hyperledger.fabric.shim.impl.QueryResultsIteratorImpl',
83+
'org.hyperledger.fabric.shim.impl.QueryResultsIteratorImpl.1']
7784
limit {
78-
minimum = 0.9
85+
minimum = 0.86
7986
}
8087
}
8188
}
@@ -93,7 +100,7 @@ task licenseCheck {
93100
def missing = new LinkedList<File>()
94101
sourceSets.forEach {
95102
sourceSet ->
96-
sourceSet.allSource.findAll { !it.path.contains("build") }.each {
103+
sourceSet.allSource.findAll { !it.path.contains("build") && !it.path.contains("test/resources")}.each {
97104
file ->
98105
BufferedReader r = new BufferedReader(new FileReader(file))
99106
def line, hasSPDX = false, hasTraditional = false

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeBase.java

Lines changed: 76 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939

4040
public abstract class ChaincodeBase implements Chaincode {
4141

42+
public static final String CORE_CHAINCODE_LOGGING_SHIM = "CORE_CHAINCODE_LOGGING_SHIM";
43+
public static final String CORE_CHAINCODE_LOGGING_LEVEL = "CORE_CHAINCODE_LOGGING_LEVEL";
44+
4245
@Override
4346
public abstract Response init(ChaincodeStub stub);
4447

@@ -59,9 +62,9 @@ public abstract class ChaincodeBase implements Chaincode {
5962

6063
private String id;
6164

62-
private final static String CORE_CHAINCODE_ID_NAME = "CORE_CHAINCODE_ID_NAME";
63-
private final static String CORE_PEER_ADDRESS = "CORE_PEER_ADDRESS";
64-
private final static String CORE_PEER_TLS_ENABLED = "CORE_PEER_TLS_ENABLED";
65+
private static final String CORE_CHAINCODE_ID_NAME = "CORE_CHAINCODE_ID_NAME";
66+
private static final String CORE_PEER_ADDRESS = "CORE_PEER_ADDRESS";
67+
private static final String CORE_PEER_TLS_ENABLED = "CORE_PEER_TLS_ENABLED";
6568
private static final String CORE_PEER_TLS_ROOTCERT_FILE = "CORE_PEER_TLS_ROOTCERT_FILE";
6669
private static final String ENV_TLS_CLIENT_KEY_PATH = "CORE_TLS_CLIENT_KEY_PATH";
6770
private static final String ENV_TLS_CLIENT_CERT_PATH = "CORE_TLS_CLIENT_CERT_PATH";
@@ -76,22 +79,21 @@ public abstract class ChaincodeBase implements Chaincode {
7679
* @param args command line arguments
7780
*/
7881
public void start(String[] args) {
79-
processEnvironmentOptions();
80-
processCommandLineOptions(args);
81-
initializeLogging();
8282
try {
83+
processEnvironmentOptions();
84+
processCommandLineOptions(args);
85+
initializeLogging();
8386
validateOptions();
8487
final ChaincodeID chaincodeId = ChaincodeID.newBuilder().setName(this.id).build();
8588
final ManagedChannelBuilder<?> channelBuilder = newChannelBuilder();
8689
final Handler handler = new Handler(chaincodeId, this);
8790
new ChaincodeSupportStream(channelBuilder, handler::onChaincodeMessage, handler::nextOutboundChaincodeMessage);
88-
89-
} catch (IllegalArgumentException e) {
91+
} catch (Exception e) {
9092
logger.fatal("Chaincode could not start", e);
9193
}
9294
}
9395

94-
private void initializeLogging() {
96+
void initializeLogging() {
9597
System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tH:%1$tM:%1$tS:%1$tL %4$-7.7s %2$s %5$s%6$s%n");
9698
final Logger rootLogger = Logger.getLogger("");
9799
for (java.util.logging.Handler handler : rootLogger.getHandlers()) {
@@ -110,32 +112,35 @@ public synchronized String format(LogRecord record) {
110112
});
111113
}
112114
// set logging level of shim logger
113-
Logger.getLogger("org.hyperledger.fabric.shim").setLevel(mapLevel(System.getenv("CORE_CHAINCODE_LOGGING_SHIM")));
115+
Level shimLogLevel = mapLevel(System.getenv(CORE_CHAINCODE_LOGGING_SHIM));
116+
Logger.getLogger(ChaincodeBase.class.getPackage().getName()).setLevel(shimLogLevel);
114117

115118
// set logging level of chaincode logger
116-
Logger.getLogger(this.getClass().getPackage().getName()).setLevel(mapLevel(System.getenv("CORE_CHAINCODE_LOGGING_LEVEL")));
119+
Level chaincodeLogLevel = mapLevel(System.getenv(CORE_CHAINCODE_LOGGING_LEVEL));
120+
Logger.getLogger(this.getClass().getPackage().getName()).setLevel(chaincodeLogLevel);
117121

118122
}
119123

120124
private Level mapLevel(String level) {
121-
switch (level) {
122-
case "CRITICAL":
123-
case "ERROR":
124-
return Level.SEVERE;
125-
case "WARNING":
126-
return Level.WARNING;
127-
case "INFO":
128-
return Level.INFO;
129-
case "NOTICE":
130-
return Level.CONFIG;
131-
case "DEBUG":
132-
return Level.FINEST;
133-
default:
134-
return Level.INFO;
125+
if (level != null) {
126+
switch (level) {
127+
case "CRITICAL":
128+
case "ERROR":
129+
return Level.SEVERE;
130+
case "WARNING":
131+
return Level.WARNING;
132+
case "INFO":
133+
return Level.INFO;
134+
case "NOTICE":
135+
return Level.CONFIG;
136+
case "DEBUG":
137+
return Level.FINEST;
138+
}
135139
}
140+
return Level.INFO;
136141
}
137142

138-
private void validateOptions() {
143+
void validateOptions() {
139144
if (this.id == null) {
140145
throw new IllegalArgumentException(format("The chaincode id must be specified using either the -i or --i command line options or the %s environment variable.", CORE_CHAINCODE_ID_NAME));
141146
}
@@ -152,7 +157,7 @@ private void validateOptions() {
152157
}
153158
}
154159

155-
private void processCommandLineOptions(String[] args) {
160+
void processCommandLineOptions(String[] args) {
156161
Options options = new Options();
157162
options.addOption("a", "peer.address", true, "Address of peer to connect to");
158163
options.addOption(null, "peerAddress", true, "Address of peer to connect to");
@@ -182,7 +187,6 @@ private void processCommandLineOptions(String[] args) {
182187
}
183188
} catch (Exception e) {
184189
logger.warn("cli parsing failed with exception", e);
185-
186190
}
187191

188192
logger.info("<<<<<<<<<<<<<CommandLine options>>>>>>>>>>>>");
@@ -194,7 +198,7 @@ private void processCommandLineOptions(String[] args) {
194198
logger.info("CORE_TLS_CLIENT_CERT_PATH" + this.tlsClientCertPath);
195199
}
196200

197-
private void processEnvironmentOptions() {
201+
void processEnvironmentOptions() {
198202
if (System.getenv().containsKey(CORE_CHAINCODE_ID_NAME)) {
199203
this.id = System.getenv(CORE_CHAINCODE_ID_NAME);
200204
}
@@ -224,35 +228,31 @@ private void processEnvironmentOptions() {
224228
logger.info("CORE_TLS_CLIENT_CERT_PATH" + this.tlsClientCertPath);
225229
}
226230

227-
private ManagedChannelBuilder<?> newChannelBuilder() {
231+
ManagedChannelBuilder<?> newChannelBuilder() throws IOException {
228232
final NettyChannelBuilder builder = NettyChannelBuilder.forAddress(host, port);
229233
logger.info("Configuring channel connection to peer.");
230234

231235
if (tlsEnabled) {
232-
logger.info("TLS is enabled");
233-
try {
234-
byte ckb[] = Files.readAllBytes(Paths.get(this.tlsClientKeyPath));
235-
byte ccb[] = Files.readAllBytes(Paths.get(this.tlsClientCertPath));
236-
237-
238-
final SslContext sslContext = GrpcSslContexts.forClient()
239-
.trustManager(new File(this.tlsClientRootCertPath))
240-
.keyManager(
241-
new ByteArrayInputStream(Base64.getDecoder().decode(ccb)),
242-
new ByteArrayInputStream(Base64.getDecoder().decode(ckb)))
243-
.build();
244-
builder.negotiationType(NegotiationType.TLS);
245-
builder.sslContext(sslContext);
246-
logger.info("TLS context built: " + sslContext);
247-
} catch (IOException e) {
248-
logger.fatal("failed connect to peer", e);
249-
}
236+
builder.negotiationType(NegotiationType.TLS);
237+
builder.sslContext(createSSLContext());
250238
} else {
251239
builder.usePlaintext(true);
252240
}
253241
return builder;
254242
}
255243

244+
SslContext createSSLContext() throws IOException {
245+
byte ckb[] = Files.readAllBytes(Paths.get(this.tlsClientKeyPath));
246+
byte ccb[] = Files.readAllBytes(Paths.get(this.tlsClientCertPath));
247+
248+
return GrpcSslContexts.forClient()
249+
.trustManager(new File(this.tlsClientRootCertPath))
250+
.keyManager(
251+
new ByteArrayInputStream(Base64.getDecoder().decode(ccb)),
252+
new ByteArrayInputStream(Base64.getDecoder().decode(ckb)))
253+
.build();
254+
}
255+
256256
protected static Response newSuccessResponse(String message, byte[] payload) {
257257
return new Response(SUCCESS, message, payload);
258258
}
@@ -295,4 +295,32 @@ private static byte[] printStackTrace(Throwable throwable) {
295295
throwable.printStackTrace(new PrintWriter(buffer));
296296
return buffer.toString().getBytes(StandardCharsets.UTF_8);
297297
}
298+
299+
String getHost() {
300+
return host;
301+
}
302+
303+
int getPort() {
304+
return port;
305+
}
306+
307+
boolean isTlsEnabled() {
308+
return tlsEnabled;
309+
}
310+
311+
String getTlsClientKeyPath() {
312+
return tlsClientKeyPath;
313+
}
314+
315+
String getTlsClientCertPath() {
316+
return tlsClientCertPath;
317+
}
318+
319+
String getTlsClientRootCertPath() {
320+
return tlsClientRootCertPath;
321+
}
322+
323+
String getId() {
324+
return id;
325+
}
298326
}

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/Handler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,4 +565,8 @@ public enum CCState {
565565
READY
566566
}
567567

568+
CCState getState() {
569+
return this.state;
570+
}
571+
568572
}

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/KeyModificationImpl.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,9 @@ public boolean equals(Object obj) {
6868
if (getClass() != obj.getClass()) return false;
6969
KeyModificationImpl other = (KeyModificationImpl) obj;
7070
if (deleted != other.deleted) return false;
71-
if (timestamp == null) {
72-
if (other.timestamp != null) return false;
73-
} else if (!timestamp.equals(other.timestamp)) return false;
74-
if (txId == null) {
75-
if (other.txId != null) return false;
76-
} else if (!txId.equals(other.txId)) return false;
77-
if (value == null) {
78-
if (other.value != null) return false;
79-
} else if (!value.equals(other.value)) return false;
71+
if (!timestamp.equals(other.timestamp)) return false;
72+
if (!txId.equals(other.txId)) return false;
73+
if (!value.equals(other.value)) return false;
8074
return true;
8175
}
8276

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/KeyValueImpl.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,8 @@ public boolean equals(Object obj) {
4949
if (obj == null) return false;
5050
if (getClass() != obj.getClass()) return false;
5151
KeyValueImpl other = (KeyValueImpl) obj;
52-
if (key == null) {
53-
if (other.key != null) return false;
54-
} else if (!key.equals(other.key)) return false;
55-
if (value == null) {
56-
if (other.value != null) return false;
57-
} else if (!value.equals(other.value)) return false;
52+
if (!key.equals(other.key)) return false;
53+
if (!value.equals(other.value)) return false;
5854
return true;
5955
}
6056

0 commit comments

Comments
 (0)