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

Commit 819afce

Browse files
John Harrisoncr22rc
authored andcommitted
[FAB-4363] Increase fabric-sdk-java code coverage #5
This change improves the code coverage of: ChannelConfiguration.class Orderer.class Peer.class Change-Id: Iee85392eda3a31562189b4360f395454d419d767 Signed-off-by: John Harrison <harrijk63@gmail.com>
1 parent 13cbafa commit 819afce

File tree

5 files changed

+165
-20
lines changed

5 files changed

+165
-20
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void setChannel(Channel channel) throws InvalidArgumentException {
9090
}
9191

9292
if (null != this.channel && this.channel != channel) {
93-
throw new InvalidArgumentException(format("Can not add orderer %s to channel %s because it already belongs to channel %s.",
93+
throw new InvalidArgumentException(format("Can not add orderer %s to channel %s because it already belongs to channel %s.",
9494
name, channel.getName(), this.channel.getName()));
9595
}
9696

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2016 DTCC, Fujitsu Australia Software Technology, IBM - All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package org.hyperledger.fabric.sdk;
16+
17+
import org.junit.Assert;
18+
import org.junit.Test;
19+
20+
public class ChannelConfigurationTest {
21+
private static final String TEST_BYTES_1 = "0A205E87B04D3B137E4F";
22+
private static final String TEST_BYTES_2 = "00112233445566778899";
23+
24+
@Test
25+
public void testChannelConfigurationByeArray() {
26+
// Test empty constructor
27+
new ChannelConfiguration();
28+
29+
// Test byte array constructor
30+
ChannelConfiguration testChannelConfig = new ChannelConfiguration(TEST_BYTES_1.getBytes());
31+
testChannelConfig.setChannelConfiguration(TEST_BYTES_2.getBytes());
32+
Assert.assertEquals(TEST_BYTES_2, new String(testChannelConfig.getChannelConfigurationAsBytes()));
33+
}
34+
}

src/test/java/org/hyperledger/fabric/sdk/EndpointTest.java

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@
1414

1515
package org.hyperledger.fabric.sdk;
1616

17+
import java.util.Properties;
18+
1719
import org.junit.Assert;
20+
import org.junit.Rule;
1821
import org.junit.Test;
22+
import org.junit.rules.ExpectedException;
1923

2024
public class EndpointTest {
25+
26+
@Rule
27+
public ExpectedException thrown = ExpectedException.none();
28+
2129
@Test
2230
public void testEndpointNonPEM() {
2331
Endpoint ep = new Endpoint("grpc://localhost:524", null);
@@ -28,32 +36,92 @@ public void testEndpointNonPEM() {
2836
Assert.assertEquals("localhost", ep.getHost());
2937

3038
try {
31-
ep = new Endpoint("grpcs2://localhost:524", null);
39+
new Endpoint("grpcs2://localhost:524", null);
3240
Assert.fail("protocol grpcs2 should have been invalid");
3341
} catch (RuntimeException rex) {
3442
Assert.assertEquals("Invalid protocol expected grpc or grpcs and found grpcs2.", rex.getMessage());
3543
}
3644

3745
try {
38-
ep = new Endpoint("grpcs://localhost", null);
46+
new Endpoint("grpcs://localhost", null);
3947
Assert.fail("should have thrown error as there is no port in the url");
4048
} catch (RuntimeException rex) {
4149
Assert.assertEquals("URL must be of the format protocol://host:port", rex.getMessage());
4250
}
4351

4452
try {
45-
ep = new Endpoint("", null);
53+
new Endpoint("", null);
4654
Assert.fail("should have thrown error as url is empty");
4755
} catch (RuntimeException rex) {
4856
Assert.assertEquals("URL cannot be null or empty", rex.getMessage());
4957
}
5058

5159
try {
52-
ep = new Endpoint(null, null);
60+
new Endpoint(null, null);
5361
Assert.fail("should have thrown error as url is empty");
5462
} catch (RuntimeException rex) {
5563
Assert.assertEquals("URL cannot be null or empty", rex.getMessage());
5664
}
5765
}
5866

67+
@Test
68+
public void testNullPropertySslProvider() {
69+
thrown.expect(RuntimeException.class);
70+
thrown.expectMessage("Property of sslProvider expected");
71+
72+
Properties testprops = new Properties();
73+
testprops.setProperty("hostnameOverride", "override");
74+
75+
new Endpoint("grpcs://localhost:594", testprops);
76+
}
77+
78+
@Test
79+
public void testEmptyPropertySslProvider() {
80+
thrown.expect(RuntimeException.class);
81+
thrown.expectMessage("Property of sslProvider has to be either openSSL or JDK");
82+
83+
Properties testprops = new Properties();
84+
testprops.setProperty("sslProvider", "");
85+
testprops.setProperty("hostnameOverride", "override");
86+
87+
new Endpoint("grpcs://localhost:594", testprops);
88+
}
89+
90+
@Test
91+
public void testNullPropertyNegotiationType() {
92+
thrown.expect(RuntimeException.class);
93+
thrown.expectMessage("Property of negotiationType expected");
94+
95+
Properties testprops = new Properties();
96+
testprops.setProperty("sslProvider", "openSSL");
97+
testprops.setProperty("hostnameOverride", "override");
98+
99+
new Endpoint("grpcs://localhost:594", testprops);
100+
}
101+
102+
@Test
103+
public void testEmptyPropertyNegotiationType() {
104+
thrown.expect(RuntimeException.class);
105+
thrown.expectMessage("Property of negotiationType has to be either TLS or plainText");
106+
107+
Properties testprops = new Properties();
108+
testprops.setProperty("sslProvider", "openSSL");
109+
testprops.setProperty("hostnameOverride", "override");
110+
testprops.setProperty("negotiationType", "");
111+
112+
new Endpoint("grpcs://localhost:594", testprops);
113+
}
114+
115+
@Test
116+
public void testExtractCommonName() {
117+
118+
Properties testprops = new Properties();
119+
testprops.setProperty("trustServerCertificate", "true");
120+
testprops.setProperty("pemFile", System.getProperty("user.dir") + "/src/test/resources/keypair-signed.crt");
121+
testprops.setProperty("sslProvider", "openSSL");
122+
testprops.setProperty("hostnameOverride", "override");
123+
testprops.setProperty("negotiationType", "TLS");
124+
125+
Assert.assertSame(new Endpoint("grpcs://localhost:594", testprops).getClass(), Endpoint.class);
126+
}
59127
}

src/test/java/org/hyperledger/fabric/sdk/OrdererTest.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,26 @@
2121
import org.junit.Assert;
2222
import org.junit.BeforeClass;
2323
import org.junit.Ignore;
24+
import org.junit.Rule;
2425
import org.junit.Test;
26+
import org.junit.rules.ExpectedException;
2527

2628

2729
public class OrdererTest {
2830
static HFClient hfclient = null;
2931
static Orderer orderer = null;
3032
static File tempFile;
3133

34+
static final String DEFAULT_CHANNEL_NAME = "channel";
35+
static final String ORDERER_NAME = "testorderer";
36+
37+
@Rule
38+
public ExpectedException thrown = ExpectedException.none();
39+
3240
@BeforeClass
3341
public static void setupClient() throws Exception {
3442
hfclient = TestHFClient.newInstance();
35-
orderer = hfclient.newOrderer("myorder", "grpc://localhost:5151");
43+
orderer = hfclient.newOrderer(ORDERER_NAME, "grpc://localhost:5151");
3644
}
3745

3846
@AfterClass
@@ -43,11 +51,29 @@ public static void cleanUp() {
4351
}
4452
}
4553

54+
@Test
55+
public void testSetDuplicateChannnel() throws InvalidArgumentException {
56+
thrown.expect(InvalidArgumentException.class);
57+
thrown.expectMessage("Can not add orderer " + ORDERER_NAME + " to channel channel2 because it already belongs to channel " + DEFAULT_CHANNEL_NAME + ".");
58+
59+
Channel channel2 = hfclient.newChannel("channel2");
60+
orderer.setChannel(channel2);
61+
orderer.setChannel(channel2);
62+
}
63+
64+
@Test
65+
public void testSetNullChannel() throws InvalidArgumentException {
66+
thrown.expect(InvalidArgumentException.class);
67+
thrown.expectMessage("setChannel Channel can not be null");
68+
69+
orderer.setChannel(null);
70+
}
71+
4672
@Test
4773
public void testSetChannel() {
4874

4975
try {
50-
Channel channel = hfclient.newChannel("channel");
76+
Channel channel = hfclient.newChannel(DEFAULT_CHANNEL_NAME);
5177
orderer.setChannel(channel);
5278
Assert.assertTrue(channel == orderer.getChannel());
5379

@@ -56,10 +82,12 @@ public void testSetChannel() {
5682
}
5783
}
5884

59-
@Test(expected = InvalidArgumentException.class)
60-
public void testSetNullChannel() throws InvalidArgumentException {
61-
orderer.setChannel(null);
62-
Assert.fail("Expected null channel to throw exception.");
85+
@Test
86+
public void testNullOrdererName() throws InvalidArgumentException {
87+
thrown.expect(InvalidArgumentException.class);
88+
thrown.expectMessage("Invalid name for orderer");
89+
90+
new Orderer(null, "url", null);
6391
}
6492

6593
@Test(expected = InvalidArgumentException.class)
@@ -77,19 +105,19 @@ public void testMissingAddress() throws InvalidArgumentException {
77105
@Ignore
78106
public void testGetChannel() {
79107
try {
80-
Channel channel = hfclient.newChannel("channel");
81-
orderer = hfclient.newOrderer("odererName", "grpc://localhost:5151");
108+
Channel channel = hfclient.newChannel(DEFAULT_CHANNEL_NAME);
109+
orderer = hfclient.newOrderer("ordererName", "grpc://localhost:5151");
82110
channel.addOrderer(orderer);
83111
} catch (Exception e) {
84112
Assert.fail("Unexpected Exception " + e.getMessage());
85113
}
86-
Assert.assertTrue("Test passed - ", orderer.getChannel().getName().equalsIgnoreCase("channel"));
114+
Assert.assertTrue("Test passed - ", orderer.getChannel().getName().equalsIgnoreCase(DEFAULT_CHANNEL_NAME));
87115
}
88116

89117
@Test(expected = Exception.class)
90118
public void testSendNullTransactionThrowsException() throws Exception {
91119
try {
92-
orderer = hfclient.newOrderer("orderertest", "grpc://localhost:5151");
120+
orderer = hfclient.newOrderer(ORDERER_NAME, "grpc://localhost:5151");
93121
} catch (InvalidArgumentException e) {
94122
Assert.fail("Failed to create new orderer: " + e);
95123
}

src/test/java/org/hyperledger/fabric/sdk/PeerTest.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,24 @@
1919
import org.hyperledger.fabric.sdk.exception.PeerException;
2020
import org.junit.Assert;
2121
import org.junit.BeforeClass;
22+
import org.junit.Rule;
2223
import org.junit.Test;
24+
import org.junit.rules.ExpectedException;
2325

2426
public class PeerTest {
2527
static HFClient hfclient = null;
2628
static Peer peer = null;
2729

30+
static final String PEER_NAME = "peertest";
31+
32+
@Rule
33+
public ExpectedException thrown = ExpectedException.none();
34+
2835
@BeforeClass
2936
public static void setupClient() {
3037
try {
3138
hfclient = TestHFClient.newInstance();
32-
peer = hfclient.newPeer("peer_", "grpc://localhost:7051");
39+
peer = hfclient.newPeer(PEER_NAME, "grpc://localhost:7051");
3340
} catch (Exception e) {
3441
e.printStackTrace();
3542
Assert.fail("Unexpected Exception " + e.getMessage());
@@ -38,11 +45,10 @@ public static void setupClient() {
3845

3946
@Test
4047
public void testGetName() {
41-
final String peerName = "peertest";
4248
Assert.assertTrue(peer != null);
4349
try {
44-
peer = new Peer(peerName, "grpc://localhost:4", null);
45-
Assert.assertEquals(peerName, peer.getName());
50+
peer = new Peer(PEER_NAME, "grpc://localhost:4", null);
51+
Assert.assertEquals(PEER_NAME, peer.getName());
4652
} catch (InvalidArgumentException e) {
4753
Assert.fail("Unexpected Exeception " + e.getMessage());
4854
}
@@ -81,8 +87,17 @@ public void testSendAsyncNullProposal() throws PeerException, InvalidArgumentExc
8187

8288
@Test(expected = InvalidArgumentException.class)
8389
public void testBadURL() throws InvalidArgumentException {
84-
hfclient.newPeer("peer_", " ");
90+
hfclient.newPeer(PEER_NAME, " ");
8591
Assert.fail("Expected peer with no channel throw exception");
8692
}
8793

94+
@Test
95+
public void testDuplicateChannel() throws InvalidArgumentException {
96+
thrown.expect(InvalidArgumentException.class);
97+
thrown.expectMessage("Can not add peer " + PEER_NAME + " to channel duplicate because it already belongs to channel duplicate.");
98+
99+
Channel duplicate = hfclient.newChannel("duplicate");
100+
peer.setChannel(duplicate);
101+
peer.setChannel(duplicate);
102+
}
88103
}

0 commit comments

Comments
 (0)