diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateCacheManagerImplTests.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateCacheManagerImplTests.java index 2e6b24004ce..8eb45f394e5 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateCacheManagerImplTests.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateCacheManagerImplTests.java @@ -27,9 +27,10 @@ import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOFactory; /** * This class tests CertificateCache manager methods */ -public class CertificateCacheManagerImplTests extends BaseDeviceManagementCertificateTest{ +public class CertificateCacheManagerImplTests extends BaseDeviceManagementCertificateTest { private CertificateCacheManager manager; + @BeforeClass @Override public void init() throws Exception { diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGeneratorNegativeTests.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGeneratorNegativeTests.java index 5b7885fca0f..8fe7aee6bae 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGeneratorNegativeTests.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGeneratorNegativeTests.java @@ -42,7 +42,12 @@ import org.wso2.carbon.certificate.mgt.core.util.CertificateManagementConstants; import org.wso2.carbon.context.PrivilegedCarbonContext; import javax.sql.DataSource; import java.io.File; -import java.security.*; +import java.security.KeyPair; +import java.security.PrivateKey; +import java.security.NoSuchProviderException; +import java.security.NoSuchAlgorithmException; +import java.security.SignatureException; +import java.security.InvalidKeyException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.sql.SQLException; @@ -56,7 +61,7 @@ public class CertificateGeneratorNegativeTests extends PowerMockTestCase { @Test(description = "This test case tests behaviour when a certificate IO error occurs", expectedExceptions = KeystoreException.class) - public void negtiveTestGenerateCertificateFromCSR() throws Exception { + public void negativeTestGenerateCertificateFromCSR() throws Exception { CertificateGenerator generator = new CertificateGenerator(); //Prepare mock objects X509v3CertificateBuilder mock = Mockito.mock(X509v3CertificateBuilder.class); @@ -78,9 +83,8 @@ public class CertificateGeneratorNegativeTests extends PowerMockTestCase { @Test(description = "This test case tests behaviour when Certificate Operator creation error occurs", expectedExceptions = KeystoreException.class) - public void negtiveTestGenerateCertificateFromCSR2() throws Exception { + public void negativeTestGenerateCertificateFromCSR2() throws Exception { CertificateGenerator generator = new CertificateGenerator(); - //Prepare mock objects JcaContentSignerBuilder mock = Mockito.mock(JcaContentSignerBuilder.class); Mockito.when(mock.setProvider(Matchers.eq(CertificateManagementConstants.PROVIDER))).thenReturn(mock); @@ -100,15 +104,13 @@ public class CertificateGeneratorNegativeTests extends PowerMockTestCase { @Test(description = "This test case tests the behaviour when certificate exception occurs when verifying" , expectedExceptions = KeystoreException.class) - public void negtiveTestGenerateCertificateFromCSR3() throws Exception { + public void negativeTestGenerateCertificateFromCSR3() throws Exception { CertificateGenerator generator = new CertificateGenerator(); - //Prepare mock objects JcaX509CertificateConverter mock = Mockito.mock(JcaX509CertificateConverter.class); Mockito.when(mock.setProvider(Matchers.eq(CertificateManagementConstants.PROVIDER))).thenReturn(mock); Mockito.when(mock.getCertificate(Matchers.any(X509CertificateHolder.class))).thenThrow(new CertificateException()); PowerMockito.whenNew(JcaX509CertificateConverter.class).withAnyArguments().thenReturn(mock); - //prepare input parameters CSRGenerator csrGeneration = new CSRGenerator(); KeyStoreReader keyStoreReader = new KeyStoreReader(); @@ -124,9 +126,9 @@ public class CertificateGeneratorNegativeTests extends PowerMockTestCase { @Test(description = "This test case tests behaviour when the Certificate provider does not exist" , expectedExceptions = KeystoreException.class) - public void negativeTestgenerateX509Certificate1() throws Exception { + public void negativeTestGenerateX509Certificate1() throws Exception { CertificateGenerator generator = new CertificateGenerator(); - + //prepare mock objects X509Certificate mock = Mockito.mock(X509Certificate.class); PowerMockito.doThrow(new NoSuchProviderException()).when(mock).verify(Matchers.any()); JcaX509CertificateConverter conv = Mockito.mock(JcaX509CertificateConverter.class); @@ -138,9 +140,9 @@ public class CertificateGeneratorNegativeTests extends PowerMockTestCase { @Test(description = "This test case tests behaviour when the Certificate Algorithm does not exist" , expectedExceptions = KeystoreException.class) - public void negativeTestgenerateX509Certificate2() throws Exception { + public void negativeTestGenerateX509Certificate2() throws Exception { CertificateGenerator generator = new CertificateGenerator(); - + //prepare mock objects X509Certificate mock = Mockito.mock(X509Certificate.class); PowerMockito.doThrow(new NoSuchAlgorithmException()).when(mock).verify(Matchers.any()); JcaX509CertificateConverter conv = Mockito.mock(JcaX509CertificateConverter.class); @@ -152,9 +154,9 @@ public class CertificateGeneratorNegativeTests extends PowerMockTestCase { @Test(description = "This test case tests behaviour when the Signature validation fails" , expectedExceptions = KeystoreException.class) - public void negativeTestgenerateX509Certificate3() throws Exception { + public void negativeTestGenerateX509Certificate3() throws Exception { CertificateGenerator generator = new CertificateGenerator(); - + //prepare mock objects X509Certificate mock = Mockito.mock(X509Certificate.class); PowerMockito.doThrow(new SignatureException()).when(mock).verify(Matchers.any()); JcaX509CertificateConverter conv = Mockito.mock(JcaX509CertificateConverter.class); @@ -166,9 +168,9 @@ public class CertificateGeneratorNegativeTests extends PowerMockTestCase { @Test(description = "This test case tests behaviour when the Certificate exception occurs" , expectedExceptions = KeystoreException.class) - public void negativeTestgenerateX509Certificate4() throws Exception { + public void negativeTestGenerateX509Certificate4() throws Exception { CertificateGenerator generator = new CertificateGenerator(); - + //prepare mock objects X509Certificate mock = Mockito.mock(X509Certificate.class); PowerMockito.doThrow(new CertificateException()).when(mock).verify(Matchers.any()); JcaX509CertificateConverter conv = Mockito.mock(JcaX509CertificateConverter.class); @@ -180,9 +182,9 @@ public class CertificateGeneratorNegativeTests extends PowerMockTestCase { @Test(description = "This test case tests behaviour when the Certificate key is invalid" , expectedExceptions = KeystoreException.class) - public void negativeTestgenerateX509Certificate5() throws Exception { + public void negativeTestGenerateX509Certificate5() throws Exception { CertificateGenerator generator = new CertificateGenerator(); - + //prepare mock objects X509Certificate mock = Mockito.mock(X509Certificate.class); PowerMockito.doThrow(new InvalidKeyException()).when(mock).verify(Matchers.any()); JcaX509CertificateConverter conv = Mockito.mock(JcaX509CertificateConverter.class); @@ -194,14 +196,14 @@ public class CertificateGeneratorNegativeTests extends PowerMockTestCase { @Test(description = "This test case tests behavior when the CA certificate is null" , expectedExceptions = KeystoreException.class) - public void negativeTestgetRootCertificates1() throws KeystoreException { + public void negativeTestGetRootCertificates1() throws KeystoreException { CertificateGenerator generator = new CertificateGenerator(); generator.getRootCertificates(null, new byte[10]); } @Test(description = "This test case tests behavior when the CA certificate is null", expectedExceptions = KeystoreException.class) - public void negativeTestgetRootCertificates2() throws KeystoreException { + public void negativeTestGetRootCertificates2() throws KeystoreException { CertificateGenerator generator = new CertificateGenerator(); generator.getRootCertificates(new byte[10], null); } diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateManagementServiceImplNegativeTests.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateManagementServiceImplNegativeTests.java index d5599dcb8af..f0c0e765f33 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateManagementServiceImplNegativeTests.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateManagementServiceImplNegativeTests.java @@ -19,7 +19,6 @@ package org.wso2.carbon.certificate.mgt.core.impl; import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; -import org.mockito.Matchers; import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; @@ -32,15 +31,12 @@ import org.testng.annotations.Test; import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOFactory; import org.wso2.carbon.certificate.mgt.core.exception.CertificateManagementException; -import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException; import org.wso2.carbon.certificate.mgt.core.exception.TransactionManagementException; import org.wso2.carbon.certificate.mgt.core.service.CertificateManagementServiceImpl; import org.wso2.carbon.context.PrivilegedCarbonContext; import javax.sql.DataSource; import java.io.File; -import java.security.NoSuchProviderException; -import java.security.cert.X509Certificate; import java.sql.SQLException; /** @@ -49,12 +45,12 @@ import java.sql.SQLException; @PowerMockIgnore({"java.net.ssl", "javax.security.auth.x500.X500Principal"}) @PrepareForTest({CertificateManagementServiceImpl.class, JcaX509CertificateConverter.class, CertificateGenerator.class, CertificateManagementDAOFactory.class}) -public class CertificateManagementServiceImplNegativeTests extends PowerMockTestCase{ +public class CertificateManagementServiceImplNegativeTests extends PowerMockTestCase { private CertificateManagementServiceImpl instance; private DataSource daoExceptionDatasource; - private static final String MOCK_SERIAL="1234"; - private static final String MOCK_DATASOURCE="H2"; + private static final String MOCK_SERIAL = "1234"; + private static final String MOCK_DATASOURCE = "H2"; @BeforeClass public void init() throws SQLException { @@ -94,16 +90,16 @@ public class CertificateManagementServiceImplNegativeTests extends PowerMockTest @Test(description = "This test case tests behaviour when an error occurs when opening the data source" - ,expectedExceptions = CertificateManagementException.class) - public void negativeTestretrieveCertificate2() throws Exception { + , expectedExceptions = CertificateManagementException.class) + public void negativeTestRetrieveCertificate2() throws Exception { PowerMockito.mockStatic(CertificateManagementDAOFactory.class); - PowerMockito.doThrow(new SQLException()).when(CertificateManagementDAOFactory.class,"openConnection"); + PowerMockito.doThrow(new SQLException()).when(CertificateManagementDAOFactory.class, "openConnection"); instance.retrieveCertificate(MOCK_SERIAL); } @Test(description = "This test case tests behaviour when an error occurs when looking for a certificate with " + - "a serial number",expectedExceptions = CertificateManagementException.class) - public void negativeTestretrieveCertificate() throws Exception { + "a serial number", expectedExceptions = CertificateManagementException.class) + public void negativeTestRetrieveCertificate() throws Exception { CertificateManagementDAOFactory.init(daoExceptionDatasource); CertificateManagementServiceImpl instance1 = CertificateManagementServiceImpl.getInstance(); instance1.retrieveCertificate(MOCK_SERIAL); @@ -111,30 +107,30 @@ public class CertificateManagementServiceImplNegativeTests extends PowerMockTest @Test(description = "This test case tests behaviour when an error occurs when opening the data source", expectedExceptions = CertificateManagementException.class) - public void negativeTestgetAllCertificates() throws Exception { + public void negativeTestGetAllCertificates() throws Exception { PowerMockito.mockStatic(CertificateManagementDAOFactory.class); - PowerMockito.doThrow(new SQLException()).when(CertificateManagementDAOFactory.class,"openConnection"); - instance.getAllCertificates(1,2); + PowerMockito.doThrow(new SQLException()).when(CertificateManagementDAOFactory.class, "openConnection"); + instance.getAllCertificates(1, 2); } @Test(description = "This test case tests behaviour when an error occurs getting the list of certificates from repository" - ,expectedExceptions = CertificateManagementException.class) - public void negativeTestgetAllCertificates2() throws Exception { + , expectedExceptions = CertificateManagementException.class) + public void negativeTestGetAllCertificates2() throws Exception { CertificateManagementDAOFactory.init(daoExceptionDatasource); CertificateManagementServiceImpl instance1 = CertificateManagementServiceImpl.getInstance(); - instance1.getAllCertificates(1,2); + instance1.getAllCertificates(1, 2); } @Test(description = "This test case tests behaviour when data source transaction error occurs when removing the certificate" - ,expectedExceptions = CertificateManagementException.class) + , expectedExceptions = CertificateManagementException.class) public void negativeTestRemoveCertificate() throws Exception { PowerMockito.mockStatic(CertificateManagementDAOFactory.class); - PowerMockito.doThrow(new TransactionManagementException()).when(CertificateManagementDAOFactory.class,"beginTransaction"); + PowerMockito.doThrow(new TransactionManagementException()).when(CertificateManagementDAOFactory.class, "beginTransaction"); instance.removeCertificate(MOCK_SERIAL); } @Test(description = "This test case tests behaviour when an error occurs while removing the certificate from the certificate " + - "repository",expectedExceptions = CertificateManagementException.class) + "repository", expectedExceptions = CertificateManagementException.class) public void negativeTestRemoveCertificate2() throws Exception { CertificateManagementDAOFactory.init(daoExceptionDatasource); CertificateManagementServiceImpl instance1 = CertificateManagementServiceImpl.getInstance(); @@ -145,12 +141,12 @@ public class CertificateManagementServiceImplNegativeTests extends PowerMockTest expectedExceptions = CertificateManagementException.class) public void negativeTestGetCertificates() throws Exception { PowerMockito.mockStatic(CertificateManagementDAOFactory.class); - PowerMockito.doThrow(new SQLException()).when(CertificateManagementDAOFactory.class,"openConnection"); + PowerMockito.doThrow(new SQLException()).when(CertificateManagementDAOFactory.class, "openConnection"); instance.getCertificates(); } @Test(description = "This test case tests behaviour when an error occurs while looking up for the list of certificates" - ,expectedExceptions = CertificateManagementException.class) + , expectedExceptions = CertificateManagementException.class) public void negativeTestGetCertificates2() throws CertificateManagementException { CertificateManagementDAOFactory.init(daoExceptionDatasource); CertificateManagementServiceImpl instance1 = CertificateManagementServiceImpl.getInstance(); @@ -161,12 +157,12 @@ public class CertificateManagementServiceImplNegativeTests extends PowerMockTest expectedExceptions = CertificateManagementException.class) public void negativeTestSearchCertificates() throws Exception { PowerMockito.mockStatic(CertificateManagementDAOFactory.class); - PowerMockito.doThrow(new SQLException()).when(CertificateManagementDAOFactory.class,"openConnection"); + PowerMockito.doThrow(new SQLException()).when(CertificateManagementDAOFactory.class, "openConnection"); instance.searchCertificates(MOCK_SERIAL); } @Test(description = "This test case tests behaviour when an error occurs while searching for the certificate by the serial" - ,expectedExceptions = CertificateManagementException.class) + , expectedExceptions = CertificateManagementException.class) public void negativeTestSearchCertificates2() throws CertificateManagementException { CertificateManagementDAOFactory.init(daoExceptionDatasource); CertificateManagementServiceImpl instance1 = CertificateManagementServiceImpl.getInstance(); diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateTests.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateTests.java index 9d8f1d010eb..3c47eea758e 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateTests.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateTests.java @@ -33,7 +33,7 @@ public class CertificateTests { private static int TENANT_ID = 1234; @Test(description = "This test case tests the Certificate object getters and setters") - public void certificateCreationTest(){ + public void certificateCreationTest() { Certificate certificate = new Certificate(); certificate.setSerial(SERIAL); @@ -41,9 +41,9 @@ public class CertificateTests { certificate.setTenantDomain(TENANT_DOMAIN); certificate.setTenantId(TENANT_ID); - Assert.assertEquals(certificate.getCertificate(),new DummyCertificate()); - Assert.assertEquals(certificate.getSerial(),SERIAL); - Assert.assertEquals(certificate.getTenantDomain(),TENANT_DOMAIN); - Assert.assertEquals(certificate.getTenantId(),TENANT_ID); + Assert.assertEquals(certificate.getCertificate(), new DummyCertificate()); + Assert.assertEquals(certificate.getSerial(), SERIAL); + Assert.assertEquals(certificate.getTenantDomain(), TENANT_DOMAIN); + Assert.assertEquals(certificate.getTenantId(), TENANT_ID); } }