Skip to content

Commit

Permalink
[FAB-4363] Increase fabric-sdk-java code coverage #5
Browse files Browse the repository at this point in the history
This change improves the code coverage of:
ChannelConfiguration.class
Orderer.class
Peer.class

Change-Id: Iee85392eda3a31562189b4360f395454d419d767
Signed-off-by: John Harrison <harrijk63@gmail.com>
  • Loading branch information
John Harrison authored and cr22rc committed Jun 12, 2017
1 parent 13cbafa commit 819afce
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/hyperledger/fabric/sdk/Orderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void setChannel(Channel channel) throws InvalidArgumentException {
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2016 DTCC, Fujitsu Australia Software Technology, IBM - All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.hyperledger.fabric.sdk;

import org.junit.Assert;
import org.junit.Test;

public class ChannelConfigurationTest {
private static final String TEST_BYTES_1 = "0A205E87B04D3B137E4F";
private static final String TEST_BYTES_2 = "00112233445566778899";

@Test
public void testChannelConfigurationByeArray() {
// Test empty constructor
new ChannelConfiguration();

// Test byte array constructor
ChannelConfiguration testChannelConfig = new ChannelConfiguration(TEST_BYTES_1.getBytes());
testChannelConfig.setChannelConfiguration(TEST_BYTES_2.getBytes());
Assert.assertEquals(TEST_BYTES_2, new String(testChannelConfig.getChannelConfigurationAsBytes()));
}
}
76 changes: 72 additions & 4 deletions src/test/java/org/hyperledger/fabric/sdk/EndpointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@

package org.hyperledger.fabric.sdk;

import java.util.Properties;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class EndpointTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testEndpointNonPEM() {
Endpoint ep = new Endpoint("grpc://localhost:524", null);
Expand All @@ -28,32 +36,92 @@ public void testEndpointNonPEM() {
Assert.assertEquals("localhost", ep.getHost());

try {
ep = new Endpoint("grpcs2://localhost:524", null);
new Endpoint("grpcs2://localhost:524", null);
Assert.fail("protocol grpcs2 should have been invalid");
} catch (RuntimeException rex) {
Assert.assertEquals("Invalid protocol expected grpc or grpcs and found grpcs2.", rex.getMessage());
}

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

try {
ep = new Endpoint("", null);
new Endpoint("", null);
Assert.fail("should have thrown error as url is empty");
} catch (RuntimeException rex) {
Assert.assertEquals("URL cannot be null or empty", rex.getMessage());
}

try {
ep = new Endpoint(null, null);
new Endpoint(null, null);
Assert.fail("should have thrown error as url is empty");
} catch (RuntimeException rex) {
Assert.assertEquals("URL cannot be null or empty", rex.getMessage());
}
}

@Test
public void testNullPropertySslProvider() {
thrown.expect(RuntimeException.class);
thrown.expectMessage("Property of sslProvider expected");

Properties testprops = new Properties();
testprops.setProperty("hostnameOverride", "override");

new Endpoint("grpcs://localhost:594", testprops);
}

@Test
public void testEmptyPropertySslProvider() {
thrown.expect(RuntimeException.class);
thrown.expectMessage("Property of sslProvider has to be either openSSL or JDK");

Properties testprops = new Properties();
testprops.setProperty("sslProvider", "");
testprops.setProperty("hostnameOverride", "override");

new Endpoint("grpcs://localhost:594", testprops);
}

@Test
public void testNullPropertyNegotiationType() {
thrown.expect(RuntimeException.class);
thrown.expectMessage("Property of negotiationType expected");

Properties testprops = new Properties();
testprops.setProperty("sslProvider", "openSSL");
testprops.setProperty("hostnameOverride", "override");

new Endpoint("grpcs://localhost:594", testprops);
}

@Test
public void testEmptyPropertyNegotiationType() {
thrown.expect(RuntimeException.class);
thrown.expectMessage("Property of negotiationType has to be either TLS or plainText");

Properties testprops = new Properties();
testprops.setProperty("sslProvider", "openSSL");
testprops.setProperty("hostnameOverride", "override");
testprops.setProperty("negotiationType", "");

new Endpoint("grpcs://localhost:594", testprops);
}

@Test
public void testExtractCommonName() {

Properties testprops = new Properties();
testprops.setProperty("trustServerCertificate", "true");
testprops.setProperty("pemFile", System.getProperty("user.dir") + "/src/test/resources/keypair-signed.crt");
testprops.setProperty("sslProvider", "openSSL");
testprops.setProperty("hostnameOverride", "override");
testprops.setProperty("negotiationType", "TLS");

Assert.assertSame(new Endpoint("grpcs://localhost:594", testprops).getClass(), Endpoint.class);
}
}
48 changes: 38 additions & 10 deletions src/test/java/org/hyperledger/fabric/sdk/OrdererTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,26 @@
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;


public class OrdererTest {
static HFClient hfclient = null;
static Orderer orderer = null;
static File tempFile;

static final String DEFAULT_CHANNEL_NAME = "channel";
static final String ORDERER_NAME = "testorderer";

@Rule
public ExpectedException thrown = ExpectedException.none();

@BeforeClass
public static void setupClient() throws Exception {
hfclient = TestHFClient.newInstance();
orderer = hfclient.newOrderer("myorder", "grpc://localhost:5151");
orderer = hfclient.newOrderer(ORDERER_NAME, "grpc://localhost:5151");
}

@AfterClass
Expand All @@ -43,11 +51,29 @@ public static void cleanUp() {
}
}

@Test
public void testSetDuplicateChannnel() throws InvalidArgumentException {
thrown.expect(InvalidArgumentException.class);
thrown.expectMessage("Can not add orderer " + ORDERER_NAME + " to channel channel2 because it already belongs to channel " + DEFAULT_CHANNEL_NAME + ".");

Channel channel2 = hfclient.newChannel("channel2");
orderer.setChannel(channel2);
orderer.setChannel(channel2);
}

@Test
public void testSetNullChannel() throws InvalidArgumentException {
thrown.expect(InvalidArgumentException.class);
thrown.expectMessage("setChannel Channel can not be null");

orderer.setChannel(null);
}

@Test
public void testSetChannel() {

try {
Channel channel = hfclient.newChannel("channel");
Channel channel = hfclient.newChannel(DEFAULT_CHANNEL_NAME);
orderer.setChannel(channel);
Assert.assertTrue(channel == orderer.getChannel());

Expand All @@ -56,10 +82,12 @@ public void testSetChannel() {
}
}

@Test(expected = InvalidArgumentException.class)
public void testSetNullChannel() throws InvalidArgumentException {
orderer.setChannel(null);
Assert.fail("Expected null channel to throw exception.");
@Test
public void testNullOrdererName() throws InvalidArgumentException {
thrown.expect(InvalidArgumentException.class);
thrown.expectMessage("Invalid name for orderer");

new Orderer(null, "url", null);
}

@Test(expected = InvalidArgumentException.class)
Expand All @@ -77,19 +105,19 @@ public void testMissingAddress() throws InvalidArgumentException {
@Ignore
public void testGetChannel() {
try {
Channel channel = hfclient.newChannel("channel");
orderer = hfclient.newOrderer("odererName", "grpc://localhost:5151");
Channel channel = hfclient.newChannel(DEFAULT_CHANNEL_NAME);
orderer = hfclient.newOrderer("ordererName", "grpc://localhost:5151");
channel.addOrderer(orderer);
} catch (Exception e) {
Assert.fail("Unexpected Exception " + e.getMessage());
}
Assert.assertTrue("Test passed - ", orderer.getChannel().getName().equalsIgnoreCase("channel"));
Assert.assertTrue("Test passed - ", orderer.getChannel().getName().equalsIgnoreCase(DEFAULT_CHANNEL_NAME));
}

@Test(expected = Exception.class)
public void testSendNullTransactionThrowsException() throws Exception {
try {
orderer = hfclient.newOrderer("orderertest", "grpc://localhost:5151");
orderer = hfclient.newOrderer(ORDERER_NAME, "grpc://localhost:5151");
} catch (InvalidArgumentException e) {
Assert.fail("Failed to create new orderer: " + e);
}
Expand Down
25 changes: 20 additions & 5 deletions src/test/java/org/hyperledger/fabric/sdk/PeerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,24 @@
import org.hyperledger.fabric.sdk.exception.PeerException;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class PeerTest {
static HFClient hfclient = null;
static Peer peer = null;

static final String PEER_NAME = "peertest";

@Rule
public ExpectedException thrown = ExpectedException.none();

@BeforeClass
public static void setupClient() {
try {
hfclient = TestHFClient.newInstance();
peer = hfclient.newPeer("peer_", "grpc://localhost:7051");
peer = hfclient.newPeer(PEER_NAME, "grpc://localhost:7051");
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Unexpected Exception " + e.getMessage());
Expand All @@ -38,11 +45,10 @@ public static void setupClient() {

@Test
public void testGetName() {
final String peerName = "peertest";
Assert.assertTrue(peer != null);
try {
peer = new Peer(peerName, "grpc://localhost:4", null);
Assert.assertEquals(peerName, peer.getName());
peer = new Peer(PEER_NAME, "grpc://localhost:4", null);
Assert.assertEquals(PEER_NAME, peer.getName());
} catch (InvalidArgumentException e) {
Assert.fail("Unexpected Exeception " + e.getMessage());
}
Expand Down Expand Up @@ -81,8 +87,17 @@ public void testSendAsyncNullProposal() throws PeerException, InvalidArgumentExc

@Test(expected = InvalidArgumentException.class)
public void testBadURL() throws InvalidArgumentException {
hfclient.newPeer("peer_", " ");
hfclient.newPeer(PEER_NAME, " ");
Assert.fail("Expected peer with no channel throw exception");
}

@Test
public void testDuplicateChannel() throws InvalidArgumentException {
thrown.expect(InvalidArgumentException.class);
thrown.expectMessage("Can not add peer " + PEER_NAME + " to channel duplicate because it already belongs to channel duplicate.");

Channel duplicate = hfclient.newChannel("duplicate");
peer.setChannel(duplicate);
peer.setChannel(duplicate);
}
}

0 comments on commit 819afce

Please sign in to comment.