|
|
|
@ -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();
|
|
|
|
|