Code formatting changes

4.x.x
Sameera Wickramasekara 7 years ago
parent ba6153bf1c
commit 59ca7e1795

@ -27,9 +27,10 @@ import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOFactory;
/** /**
* This class tests CertificateCache manager methods * This class tests CertificateCache manager methods
*/ */
public class CertificateCacheManagerImplTests extends BaseDeviceManagementCertificateTest{ public class CertificateCacheManagerImplTests extends BaseDeviceManagementCertificateTest {
private CertificateCacheManager manager; private CertificateCacheManager manager;
@BeforeClass @BeforeClass
@Override @Override
public void init() throws Exception { public void init() throws Exception {

@ -42,7 +42,12 @@ import org.wso2.carbon.certificate.mgt.core.util.CertificateManagementConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.io.File; 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.CertificateException;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.sql.SQLException; 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", @Test(description = "This test case tests behaviour when a certificate IO error occurs",
expectedExceptions = KeystoreException.class) expectedExceptions = KeystoreException.class)
public void negtiveTestGenerateCertificateFromCSR() throws Exception { public void negativeTestGenerateCertificateFromCSR() throws Exception {
CertificateGenerator generator = new CertificateGenerator(); CertificateGenerator generator = new CertificateGenerator();
//Prepare mock objects //Prepare mock objects
X509v3CertificateBuilder mock = Mockito.mock(X509v3CertificateBuilder.class); 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", @Test(description = "This test case tests behaviour when Certificate Operator creation error occurs",
expectedExceptions = KeystoreException.class) expectedExceptions = KeystoreException.class)
public void negtiveTestGenerateCertificateFromCSR2() throws Exception { public void negativeTestGenerateCertificateFromCSR2() throws Exception {
CertificateGenerator generator = new CertificateGenerator(); CertificateGenerator generator = new CertificateGenerator();
//Prepare mock objects //Prepare mock objects
JcaContentSignerBuilder mock = Mockito.mock(JcaContentSignerBuilder.class); JcaContentSignerBuilder mock = Mockito.mock(JcaContentSignerBuilder.class);
Mockito.when(mock.setProvider(Matchers.eq(CertificateManagementConstants.PROVIDER))).thenReturn(mock); 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" @Test(description = "This test case tests the behaviour when certificate exception occurs when verifying"
, expectedExceptions = KeystoreException.class) , expectedExceptions = KeystoreException.class)
public void negtiveTestGenerateCertificateFromCSR3() throws Exception { public void negativeTestGenerateCertificateFromCSR3() throws Exception {
CertificateGenerator generator = new CertificateGenerator(); CertificateGenerator generator = new CertificateGenerator();
//Prepare mock objects //Prepare mock objects
JcaX509CertificateConverter mock = Mockito.mock(JcaX509CertificateConverter.class); JcaX509CertificateConverter mock = Mockito.mock(JcaX509CertificateConverter.class);
Mockito.when(mock.setProvider(Matchers.eq(CertificateManagementConstants.PROVIDER))).thenReturn(mock); Mockito.when(mock.setProvider(Matchers.eq(CertificateManagementConstants.PROVIDER))).thenReturn(mock);
Mockito.when(mock.getCertificate(Matchers.any(X509CertificateHolder.class))).thenThrow(new CertificateException()); Mockito.when(mock.getCertificate(Matchers.any(X509CertificateHolder.class))).thenThrow(new CertificateException());
PowerMockito.whenNew(JcaX509CertificateConverter.class).withAnyArguments().thenReturn(mock); PowerMockito.whenNew(JcaX509CertificateConverter.class).withAnyArguments().thenReturn(mock);
//prepare input parameters //prepare input parameters
CSRGenerator csrGeneration = new CSRGenerator(); CSRGenerator csrGeneration = new CSRGenerator();
KeyStoreReader keyStoreReader = new KeyStoreReader(); 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" @Test(description = "This test case tests behaviour when the Certificate provider does not exist"
, expectedExceptions = KeystoreException.class) , expectedExceptions = KeystoreException.class)
public void negativeTestgenerateX509Certificate1() throws Exception { public void negativeTestGenerateX509Certificate1() throws Exception {
CertificateGenerator generator = new CertificateGenerator(); CertificateGenerator generator = new CertificateGenerator();
//prepare mock objects
X509Certificate mock = Mockito.mock(X509Certificate.class); X509Certificate mock = Mockito.mock(X509Certificate.class);
PowerMockito.doThrow(new NoSuchProviderException()).when(mock).verify(Matchers.any()); PowerMockito.doThrow(new NoSuchProviderException()).when(mock).verify(Matchers.any());
JcaX509CertificateConverter conv = Mockito.mock(JcaX509CertificateConverter.class); 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" @Test(description = "This test case tests behaviour when the Certificate Algorithm does not exist"
, expectedExceptions = KeystoreException.class) , expectedExceptions = KeystoreException.class)
public void negativeTestgenerateX509Certificate2() throws Exception { public void negativeTestGenerateX509Certificate2() throws Exception {
CertificateGenerator generator = new CertificateGenerator(); CertificateGenerator generator = new CertificateGenerator();
//prepare mock objects
X509Certificate mock = Mockito.mock(X509Certificate.class); X509Certificate mock = Mockito.mock(X509Certificate.class);
PowerMockito.doThrow(new NoSuchAlgorithmException()).when(mock).verify(Matchers.any()); PowerMockito.doThrow(new NoSuchAlgorithmException()).when(mock).verify(Matchers.any());
JcaX509CertificateConverter conv = Mockito.mock(JcaX509CertificateConverter.class); 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" @Test(description = "This test case tests behaviour when the Signature validation fails"
, expectedExceptions = KeystoreException.class) , expectedExceptions = KeystoreException.class)
public void negativeTestgenerateX509Certificate3() throws Exception { public void negativeTestGenerateX509Certificate3() throws Exception {
CertificateGenerator generator = new CertificateGenerator(); CertificateGenerator generator = new CertificateGenerator();
//prepare mock objects
X509Certificate mock = Mockito.mock(X509Certificate.class); X509Certificate mock = Mockito.mock(X509Certificate.class);
PowerMockito.doThrow(new SignatureException()).when(mock).verify(Matchers.any()); PowerMockito.doThrow(new SignatureException()).when(mock).verify(Matchers.any());
JcaX509CertificateConverter conv = Mockito.mock(JcaX509CertificateConverter.class); 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" @Test(description = "This test case tests behaviour when the Certificate exception occurs"
, expectedExceptions = KeystoreException.class) , expectedExceptions = KeystoreException.class)
public void negativeTestgenerateX509Certificate4() throws Exception { public void negativeTestGenerateX509Certificate4() throws Exception {
CertificateGenerator generator = new CertificateGenerator(); CertificateGenerator generator = new CertificateGenerator();
//prepare mock objects
X509Certificate mock = Mockito.mock(X509Certificate.class); X509Certificate mock = Mockito.mock(X509Certificate.class);
PowerMockito.doThrow(new CertificateException()).when(mock).verify(Matchers.any()); PowerMockito.doThrow(new CertificateException()).when(mock).verify(Matchers.any());
JcaX509CertificateConverter conv = Mockito.mock(JcaX509CertificateConverter.class); 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" @Test(description = "This test case tests behaviour when the Certificate key is invalid"
, expectedExceptions = KeystoreException.class) , expectedExceptions = KeystoreException.class)
public void negativeTestgenerateX509Certificate5() throws Exception { public void negativeTestGenerateX509Certificate5() throws Exception {
CertificateGenerator generator = new CertificateGenerator(); CertificateGenerator generator = new CertificateGenerator();
//prepare mock objects
X509Certificate mock = Mockito.mock(X509Certificate.class); X509Certificate mock = Mockito.mock(X509Certificate.class);
PowerMockito.doThrow(new InvalidKeyException()).when(mock).verify(Matchers.any()); PowerMockito.doThrow(new InvalidKeyException()).when(mock).verify(Matchers.any());
JcaX509CertificateConverter conv = Mockito.mock(JcaX509CertificateConverter.class); 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" @Test(description = "This test case tests behavior when the CA certificate is null"
, expectedExceptions = KeystoreException.class) , expectedExceptions = KeystoreException.class)
public void negativeTestgetRootCertificates1() throws KeystoreException { public void negativeTestGetRootCertificates1() throws KeystoreException {
CertificateGenerator generator = new CertificateGenerator(); CertificateGenerator generator = new CertificateGenerator();
generator.getRootCertificates(null, new byte[10]); generator.getRootCertificates(null, new byte[10]);
} }
@Test(description = "This test case tests behavior when the CA certificate is null", @Test(description = "This test case tests behavior when the CA certificate is null",
expectedExceptions = KeystoreException.class) expectedExceptions = KeystoreException.class)
public void negativeTestgetRootCertificates2() throws KeystoreException { public void negativeTestGetRootCertificates2() throws KeystoreException {
CertificateGenerator generator = new CertificateGenerator(); CertificateGenerator generator = new CertificateGenerator();
generator.getRootCertificates(new byte[10], null); generator.getRootCertificates(new byte[10], null);
} }

@ -19,7 +19,6 @@
package org.wso2.carbon.certificate.mgt.core.impl; package org.wso2.carbon.certificate.mgt.core.impl;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.mockito.Matchers;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito; import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore; 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.base.MultitenantConstants;
import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOFactory; 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.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.exception.TransactionManagementException;
import org.wso2.carbon.certificate.mgt.core.service.CertificateManagementServiceImpl; import org.wso2.carbon.certificate.mgt.core.service.CertificateManagementServiceImpl;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.io.File; import java.io.File;
import java.security.NoSuchProviderException;
import java.security.cert.X509Certificate;
import java.sql.SQLException; import java.sql.SQLException;
/** /**
@ -49,12 +45,12 @@ import java.sql.SQLException;
@PowerMockIgnore({"java.net.ssl", "javax.security.auth.x500.X500Principal"}) @PowerMockIgnore({"java.net.ssl", "javax.security.auth.x500.X500Principal"})
@PrepareForTest({CertificateManagementServiceImpl.class, JcaX509CertificateConverter.class, CertificateGenerator.class, @PrepareForTest({CertificateManagementServiceImpl.class, JcaX509CertificateConverter.class, CertificateGenerator.class,
CertificateManagementDAOFactory.class}) CertificateManagementDAOFactory.class})
public class CertificateManagementServiceImplNegativeTests extends PowerMockTestCase{ public class CertificateManagementServiceImplNegativeTests extends PowerMockTestCase {
private CertificateManagementServiceImpl instance; private CertificateManagementServiceImpl instance;
private DataSource daoExceptionDatasource; private DataSource daoExceptionDatasource;
private static final String MOCK_SERIAL="1234"; private static final String MOCK_SERIAL = "1234";
private static final String MOCK_DATASOURCE="H2"; private static final String MOCK_DATASOURCE = "H2";
@BeforeClass @BeforeClass
public void init() throws SQLException { 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" @Test(description = "This test case tests behaviour when an error occurs when opening the data source"
,expectedExceptions = CertificateManagementException.class) , expectedExceptions = CertificateManagementException.class)
public void negativeTestretrieveCertificate2() throws Exception { public void negativeTestRetrieveCertificate2() throws Exception {
PowerMockito.mockStatic(CertificateManagementDAOFactory.class); PowerMockito.mockStatic(CertificateManagementDAOFactory.class);
PowerMockito.doThrow(new SQLException()).when(CertificateManagementDAOFactory.class,"openConnection"); PowerMockito.doThrow(new SQLException()).when(CertificateManagementDAOFactory.class, "openConnection");
instance.retrieveCertificate(MOCK_SERIAL); instance.retrieveCertificate(MOCK_SERIAL);
} }
@Test(description = "This test case tests behaviour when an error occurs when looking for a certificate with " + @Test(description = "This test case tests behaviour when an error occurs when looking for a certificate with " +
"a serial number",expectedExceptions = CertificateManagementException.class) "a serial number", expectedExceptions = CertificateManagementException.class)
public void negativeTestretrieveCertificate() throws Exception { public void negativeTestRetrieveCertificate() throws Exception {
CertificateManagementDAOFactory.init(daoExceptionDatasource); CertificateManagementDAOFactory.init(daoExceptionDatasource);
CertificateManagementServiceImpl instance1 = CertificateManagementServiceImpl.getInstance(); CertificateManagementServiceImpl instance1 = CertificateManagementServiceImpl.getInstance();
instance1.retrieveCertificate(MOCK_SERIAL); 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", @Test(description = "This test case tests behaviour when an error occurs when opening the data source",
expectedExceptions = CertificateManagementException.class) expectedExceptions = CertificateManagementException.class)
public void negativeTestgetAllCertificates() throws Exception { public void negativeTestGetAllCertificates() throws Exception {
PowerMockito.mockStatic(CertificateManagementDAOFactory.class); PowerMockito.mockStatic(CertificateManagementDAOFactory.class);
PowerMockito.doThrow(new SQLException()).when(CertificateManagementDAOFactory.class,"openConnection"); PowerMockito.doThrow(new SQLException()).when(CertificateManagementDAOFactory.class, "openConnection");
instance.getAllCertificates(1,2); instance.getAllCertificates(1, 2);
} }
@Test(description = "This test case tests behaviour when an error occurs getting the list of certificates from repository" @Test(description = "This test case tests behaviour when an error occurs getting the list of certificates from repository"
,expectedExceptions = CertificateManagementException.class) , expectedExceptions = CertificateManagementException.class)
public void negativeTestgetAllCertificates2() throws Exception { public void negativeTestGetAllCertificates2() throws Exception {
CertificateManagementDAOFactory.init(daoExceptionDatasource); CertificateManagementDAOFactory.init(daoExceptionDatasource);
CertificateManagementServiceImpl instance1 = CertificateManagementServiceImpl.getInstance(); 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" @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 { public void negativeTestRemoveCertificate() throws Exception {
PowerMockito.mockStatic(CertificateManagementDAOFactory.class); PowerMockito.mockStatic(CertificateManagementDAOFactory.class);
PowerMockito.doThrow(new TransactionManagementException()).when(CertificateManagementDAOFactory.class,"beginTransaction"); PowerMockito.doThrow(new TransactionManagementException()).when(CertificateManagementDAOFactory.class, "beginTransaction");
instance.removeCertificate(MOCK_SERIAL); instance.removeCertificate(MOCK_SERIAL);
} }
@Test(description = "This test case tests behaviour when an error occurs while removing the certificate from the certificate " + @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 { public void negativeTestRemoveCertificate2() throws Exception {
CertificateManagementDAOFactory.init(daoExceptionDatasource); CertificateManagementDAOFactory.init(daoExceptionDatasource);
CertificateManagementServiceImpl instance1 = CertificateManagementServiceImpl.getInstance(); CertificateManagementServiceImpl instance1 = CertificateManagementServiceImpl.getInstance();
@ -145,12 +141,12 @@ public class CertificateManagementServiceImplNegativeTests extends PowerMockTest
expectedExceptions = CertificateManagementException.class) expectedExceptions = CertificateManagementException.class)
public void negativeTestGetCertificates() throws Exception { public void negativeTestGetCertificates() throws Exception {
PowerMockito.mockStatic(CertificateManagementDAOFactory.class); PowerMockito.mockStatic(CertificateManagementDAOFactory.class);
PowerMockito.doThrow(new SQLException()).when(CertificateManagementDAOFactory.class,"openConnection"); PowerMockito.doThrow(new SQLException()).when(CertificateManagementDAOFactory.class, "openConnection");
instance.getCertificates(); instance.getCertificates();
} }
@Test(description = "This test case tests behaviour when an error occurs while looking up for the list of certificates" @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 { public void negativeTestGetCertificates2() throws CertificateManagementException {
CertificateManagementDAOFactory.init(daoExceptionDatasource); CertificateManagementDAOFactory.init(daoExceptionDatasource);
CertificateManagementServiceImpl instance1 = CertificateManagementServiceImpl.getInstance(); CertificateManagementServiceImpl instance1 = CertificateManagementServiceImpl.getInstance();
@ -161,12 +157,12 @@ public class CertificateManagementServiceImplNegativeTests extends PowerMockTest
expectedExceptions = CertificateManagementException.class) expectedExceptions = CertificateManagementException.class)
public void negativeTestSearchCertificates() throws Exception { public void negativeTestSearchCertificates() throws Exception {
PowerMockito.mockStatic(CertificateManagementDAOFactory.class); PowerMockito.mockStatic(CertificateManagementDAOFactory.class);
PowerMockito.doThrow(new SQLException()).when(CertificateManagementDAOFactory.class,"openConnection"); PowerMockito.doThrow(new SQLException()).when(CertificateManagementDAOFactory.class, "openConnection");
instance.searchCertificates(MOCK_SERIAL); instance.searchCertificates(MOCK_SERIAL);
} }
@Test(description = "This test case tests behaviour when an error occurs while searching for the certificate by the 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 { public void negativeTestSearchCertificates2() throws CertificateManagementException {
CertificateManagementDAOFactory.init(daoExceptionDatasource); CertificateManagementDAOFactory.init(daoExceptionDatasource);
CertificateManagementServiceImpl instance1 = CertificateManagementServiceImpl.getInstance(); CertificateManagementServiceImpl instance1 = CertificateManagementServiceImpl.getInstance();

@ -33,7 +33,7 @@ public class CertificateTests {
private static int TENANT_ID = 1234; private static int TENANT_ID = 1234;
@Test(description = "This test case tests the Certificate object getters and setters") @Test(description = "This test case tests the Certificate object getters and setters")
public void certificateCreationTest(){ public void certificateCreationTest() {
Certificate certificate = new Certificate(); Certificate certificate = new Certificate();
certificate.setSerial(SERIAL); certificate.setSerial(SERIAL);
@ -41,9 +41,9 @@ public class CertificateTests {
certificate.setTenantDomain(TENANT_DOMAIN); certificate.setTenantDomain(TENANT_DOMAIN);
certificate.setTenantId(TENANT_ID); certificate.setTenantId(TENANT_ID);
Assert.assertEquals(certificate.getCertificate(),new DummyCertificate()); Assert.assertEquals(certificate.getCertificate(), new DummyCertificate());
Assert.assertEquals(certificate.getSerial(),SERIAL); Assert.assertEquals(certificate.getSerial(), SERIAL);
Assert.assertEquals(certificate.getTenantDomain(),TENANT_DOMAIN); Assert.assertEquals(certificate.getTenantDomain(), TENANT_DOMAIN);
Assert.assertEquals(certificate.getTenantId(),TENANT_ID); Assert.assertEquals(certificate.getTenantId(), TENANT_ID);
} }
} }

Loading…
Cancel
Save