From 664e25e9e58b608dbb7038aa2375296b312a7994 Mon Sep 17 00:00:00 2001 From: John Harrison Date: Tue, 6 Jun 2017 15:28:15 -0400 Subject: [PATCH] [FAB-4363] Increase fabric-sdk-java code coverage #3 This change improves the code coverage of: org.hyperledger.fabric_ca.sdk org.hyperledger.fabric_ca.sdk.exception org.hyperledger.fabric.sdk.exception Change-Id: Id4f3450c5e9c843437c66287e66a779f4f81285d Signed-off-by: John Harrison --- .../sdk/exception/FabricExceptionsTest.java | 334 ++++++++++++++++++ .../fabric_ca/sdk/HFCAClientTest.java | 47 ++- .../sdk/exception/FabricCAExceptionsTest.java | 127 +++++++ 3 files changed, 505 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/hyperledger/fabric/sdk/exception/FabricExceptionsTest.java create mode 100644 src/test/java/org/hyperledger/fabric_ca/sdk/exception/FabricCAExceptionsTest.java diff --git a/src/test/java/org/hyperledger/fabric/sdk/exception/FabricExceptionsTest.java b/src/test/java/org/hyperledger/fabric/sdk/exception/FabricExceptionsTest.java new file mode 100644 index 00000000..3966519c --- /dev/null +++ b/src/test/java/org/hyperledger/fabric/sdk/exception/FabricExceptionsTest.java @@ -0,0 +1,334 @@ +/* + * Copyright 2016, 2017 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.exception; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class FabricExceptionsTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testBaseException1() throws BaseException { + + thrown.expect(BaseException.class); + thrown.expectMessage("test"); + + throw new BaseException("test"); + + } + + @Test + public void testBaseException2() throws BaseException { + + thrown.expect(BaseException.class); + thrown.expectMessage("test"); + + throw new BaseException(new BaseException("test")); + + } + + @Test + public void testBaseException3() throws BaseException { + + thrown.expect(BaseException.class); + thrown.expectMessage("test"); + + throw new BaseException(new BaseException("test")); + + } + + @Test + public void testChaincodeEndorsementPolicyParseException1() throws ChaincodeEndorsementPolicyParseException { + + thrown.expect(ChaincodeEndorsementPolicyParseException.class); + thrown.expectMessage("test"); + + throw new ChaincodeEndorsementPolicyParseException("test"); + + } + + @Test + public void testChaincodeEndorsementPolicyParseException2() throws ChaincodeEndorsementPolicyParseException { + + thrown.expect(ChaincodeEndorsementPolicyParseException.class); + thrown.expectMessage("test"); + + throw new ChaincodeEndorsementPolicyParseException("test", + new ChaincodeEndorsementPolicyParseException("test")); + + } + + @Test + public void testChaincodeException() throws ChaincodeException { + BaseException baseException = new BaseException("test"); + thrown.expect(ChaincodeException.class); + thrown.expectMessage("test"); + + throw new ChaincodeException("test", baseException); + + } + + @Test + public void testCryptoException1() throws CryptoException { + + thrown.expect(CryptoException.class); + thrown.expectMessage("test"); + + throw new CryptoException("test"); + + } + + @Test + public void testCryptoException2() throws CryptoException { + + thrown.expect(CryptoException.class); + thrown.expectMessage("test"); + + throw new CryptoException("test", new CryptoException("test")); + + } + + @Test + public void testEventHubException1() throws EventHubException { + + thrown.expect(EventHubException.class); + thrown.expectMessage("test"); + + throw new EventHubException("test"); + + } + + @Test + public void testEventHubException2() throws EventHubException { + + thrown.expect(EventHubException.class); + thrown.expectMessage("test"); + + throw new EventHubException(new CryptoException("test")); + + } + + @Test + public void testEventHubException3() throws EventHubException { + + thrown.expect(EventHubException.class); + thrown.expectMessage("test"); + + throw new EventHubException("test", new CryptoException("test")); + + } + + @Test + public void testExecuteException1() throws ExecuteException { + + thrown.expect(ExecuteException.class); + thrown.expectMessage("test"); + + throw new ExecuteException("test"); + + } + + @Test + public void testExecuteException2() throws ExecuteException { + + thrown.expect(ExecuteException.class); + thrown.expectMessage("test"); + + throw new ExecuteException("test", new ExecuteException("test")); + + } + + @Test + public void testGetTCertBatchException() throws GetTCertBatchException { + + thrown.expect(GetTCertBatchException.class); + thrown.expectMessage("test"); + + throw new GetTCertBatchException("test", new ExecuteException("test")); + + } + + @Test + public void testInvalidArgumentException1() throws InvalidArgumentException { + + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("test"); + + throw new InvalidArgumentException("test"); + + } + + @Test + public void testInvalidArgumentException2() throws InvalidArgumentException { + + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("test"); + + throw new InvalidArgumentException(new InvalidArgumentException("test")); + + } + + @Test + public void testInvalidArgumentException3() throws InvalidArgumentException { + + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("test"); + + throw new InvalidArgumentException("test", new InvalidArgumentException("test")); + + } + + @Test + public void testInvalidTransactionException1() throws InvalidTransactionException { + + thrown.expect(InvalidTransactionException.class); + thrown.expectMessage("test"); + + throw new InvalidTransactionException("test"); + + } + + @Test + public void testInvalidTransactionException2() throws InvalidTransactionException { + + thrown.expect(InvalidTransactionException.class); + thrown.expectMessage("test"); + + throw new InvalidTransactionException("test", new InvalidTransactionException("test")); + + } + + @Test + public void testInvokeException() throws InvokeException { + + BaseException baseException = new BaseException("test"); + thrown.expect(InvokeException.class); + thrown.expectMessage("test"); + + throw new InvokeException("test", baseException); + + } + + @Test + public void testNoAvailableTCertException() throws NoAvailableTCertException { + + thrown.expect(NoAvailableTCertException.class); + thrown.expectMessage("test"); + + throw new NoAvailableTCertException("test"); + + } + + @Test + public void testNoValidPeerException() throws NoValidPeerException { + + thrown.expect(NoValidPeerException.class); + thrown.expectMessage("test"); + + throw new NoValidPeerException("test"); + + } + + @Test + public void testPeerException1() throws PeerException { + + thrown.expect(PeerException.class); + thrown.expectMessage("test"); + + throw new PeerException("test"); + + } + + @Test + public void testPeerException2() throws PeerException { + + thrown.expect(PeerException.class); + thrown.expectMessage("test"); + + throw new PeerException("test", new PeerException("test")); + + } + + @Test + public void testProposalException1() throws ProposalException { + + thrown.expect(ProposalException.class); + thrown.expectMessage("test"); + + throw new ProposalException("test"); + + } + + @Test + public void testProposalException2() throws ProposalException { + + thrown.expect(ProposalException.class); + thrown.expectMessage("test"); + + throw new ProposalException(new ProposalException("test")); + + } + + @Test + public void testProposalException3() throws ProposalException { + + thrown.expect(ProposalException.class); + thrown.expectMessage("test"); + + throw new ProposalException("test", new ProposalException("test")); + + } + + @Test + public void testQueryException() throws QueryException { + BaseException baseException = new BaseException("test"); + thrown.expect(QueryException.class); + thrown.expectMessage("test"); + + throw new QueryException("test", baseException); + + } + + @Test + public void testTransactionException1() throws TransactionException { + thrown.expect(TransactionException.class); + thrown.expectMessage("test"); + + throw new TransactionException("test"); + + } + + @Test + public void testTransactionException2() throws TransactionException { + thrown.expect(TransactionException.class); + thrown.expectMessage("test"); + + throw new TransactionException(new TransactionException("test")); + + } + + @Test + public void testTransactionException3() throws TransactionException { + thrown.expect(TransactionException.class); + thrown.expectMessage("test"); + + throw new TransactionException("test", new TransactionException("test")); + + } +} diff --git a/src/test/java/org/hyperledger/fabric_ca/sdk/HFCAClientTest.java b/src/test/java/org/hyperledger/fabric_ca/sdk/HFCAClientTest.java index 32ca4f96..124750d7 100644 --- a/src/test/java/org/hyperledger/fabric_ca/sdk/HFCAClientTest.java +++ b/src/test/java/org/hyperledger/fabric_ca/sdk/HFCAClientTest.java @@ -4,7 +4,7 @@ * 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 + * 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. @@ -16,13 +16,13 @@ import java.net.MalformedURLException; +import org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException; import org.junit.Assert; import org.junit.Test; public class HFCAClientTest { public static class MemberServicesFabricCAImplTest { - @Test public void testCOPCreation() { @@ -31,7 +31,6 @@ public void testCOPCreation() { Assert.assertNotNull(memberServices); Assert.assertSame(HFCAClient.class, memberServices.getClass()); - } catch (Exception e) { Assert.fail("Unexpected Exception " + e.getMessage()); } @@ -101,5 +100,47 @@ public void testBadURLQuery() { } } + + @Test + public void testNewInstanceNameUrlProperties() { + + try { + HFCAClient memberServices = HFCAClient.createNewInstance("name", "http://localhost:99", null); + Assert.assertNotNull(memberServices); + Assert.assertSame(HFCAClient.class, memberServices.getClass()); + + } catch (Exception e) { + Assert.assertSame(e.getClass(), IllegalArgumentException.class); + + } + } + + @Test + public void testNewInstanceNameUrlPropertiesSetNullName() { + + try { + HFCAClient.createNewInstance(null, "http://localhost:99", null); + Assert.fail("Expected exception when name is set to null"); + + } catch (Exception e) { + Assert.assertSame(e.getClass(), InvalidArgumentException.class); + Assert.assertEquals(e.getMessage(), "name must not be null or an empty string."); + + } + } + + @Test + public void testNewInstanceNameUrlPropertiesSetEmptyName() { + + try { + HFCAClient.createNewInstance("", "http://localhost:99", null); + Assert.fail("Expected exception when name is set to null"); + + } catch (Exception e) { + Assert.assertSame(e.getClass(), InvalidArgumentException.class); + Assert.assertEquals(e.getMessage(), "name must not be null or an empty string."); + + } + } } } diff --git a/src/test/java/org/hyperledger/fabric_ca/sdk/exception/FabricCAExceptionsTest.java b/src/test/java/org/hyperledger/fabric_ca/sdk/exception/FabricCAExceptionsTest.java new file mode 100644 index 00000000..b89cd2fa --- /dev/null +++ b/src/test/java/org/hyperledger/fabric_ca/sdk/exception/FabricCAExceptionsTest.java @@ -0,0 +1,127 @@ +/* + * Copyright 2016, 2017 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_ca.sdk.exception; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class FabricCAExceptionsTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testBaseException1() throws BaseException { + + thrown.expect(BaseException.class); + thrown.expectMessage("test"); + + throw new BaseException("test"); + + } + + @Test + public void testBaseException2() throws BaseException { + + thrown.expect(BaseException.class); + thrown.expectMessage("test"); + + throw new BaseException(new BaseException("test")); + + } + + @Test + public void testBaseException3() throws BaseException { + + thrown.expect(BaseException.class); + thrown.expectMessage("test"); + + throw new BaseException(new BaseException("test")); + + } + + @Test + public void testEnrollmentException1() throws EnrollmentException { + + thrown.expect(EnrollmentException.class); + thrown.expectMessage("test"); + + throw new EnrollmentException("test"); + + } + + @Test + public void testEnrollmentException2() throws EnrollmentException { + + thrown.expect(EnrollmentException.class); + thrown.expectMessage("test"); + + throw new EnrollmentException("test", new EnrollmentException("test")); + + } + + @Test + public void testInvalidArgumentException1() throws InvalidArgumentException { + + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("test"); + + throw new InvalidArgumentException("test"); + + } + + @Test + public void testInvalidArgumentException2() throws InvalidArgumentException { + + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("test"); + + throw new InvalidArgumentException(new InvalidArgumentException("test")); + + } + + @Test + public void testInvalidArgumentException3() throws InvalidArgumentException { + + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("test"); + + throw new InvalidArgumentException("test", new InvalidArgumentException("test")); + + } + + @Test + public void testRegistrationException() throws RegistrationException { + + BaseException baseException = new BaseException("test"); + thrown.expect(RegistrationException.class); + thrown.expectMessage("test"); + + throw new RegistrationException("test", baseException); + + } + + @Test + public void testRevocationException() throws RevocationException { + + BaseException baseException = new BaseException("test"); + thrown.expect(RevocationException.class); + thrown.expectMessage("test"); + + throw new RevocationException("test", baseException); + + } +}