Add certificate-mgt tests

4.x.x
Sameera Wickramasekara 7 years ago
parent 3697803e17
commit 0d3404a297

@ -17,7 +17,8 @@
~ under the License. ~ under the License.
--> -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
<groupId>org.wso2.carbon.devicemgt</groupId> <groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>certificate-mgt</artifactId> <artifactId>certificate-mgt</artifactId>
@ -105,36 +106,40 @@
</instructions> </instructions>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>2.18</version> <version>2.18</version>
<configuration> <configuration>
<systemPropertyVariables>
<log4j.configuration>file:src/test/resources/log4j.properties</log4j.configuration>
</systemPropertyVariables>
<suiteXmlFiles> <suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile> <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles> </suiteXmlFiles>
<systemPropertyVariables>
<jacoco-agent.destfile>${basedir}/target/coverage-reports/jacoco-unit.exec
</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.jacoco</groupId> <groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId> <artifactId>jacoco-maven-plugin</artifactId>
<configuration> <version>${jacoco.maven.plugin.version}</version>
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
</configuration>
<executions> <executions>
<execution> <execution>
<id>jacoco-initialize</id> <id>default-instrument</id>
<goals> <goals>
<goal>prepare-agent</goal> <goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals> </goals>
</execution> </execution>
<execution> <execution>
<id>jacoco-site</id> <id>jacoco-site</id>
<phase>test</phase> <phase>prepare-package</phase>
<goals> <goals>
<goal>report</goal> <goal>report</goal>
</goals> </goals>
@ -143,13 +148,24 @@
<outputDirectory>${basedir}/target/coverage-reports/site</outputDirectory> <outputDirectory>${basedir}/target/coverage-reports/site</outputDirectory>
</configuration> </configuration>
</execution> </execution>
<execution>
<id>default-report-integration</id>
<goals>
<goal>report-integration</goal>
</goals>
</execution>
</executions> </executions>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<dependencies> <dependencies>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>${jacoco.maven.plugin.version}</version>
<classifier>runtime</classifier>
</dependency>
<dependency> <dependency>
<groupId>org.eclipse.osgi</groupId> <groupId>org.eclipse.osgi</groupId>
<artifactId>org.eclipse.osgi</artifactId> <artifactId>org.eclipse.osgi</artifactId>

@ -0,0 +1,42 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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.wso2.carbon.certificate.mgt.core.impl;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.wso2.carbon.certificate.mgt.core.cache.CertificateCacheManager;
import org.wso2.carbon.certificate.mgt.core.common.BaseDeviceManagementCertificateTest;
import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOFactory;
/**
* This class tests CertificateCache manager methods
*/
public class CertificateCacheManagerImplTests extends BaseDeviceManagementCertificateTest{
private CertificateCacheManager manager;
@BeforeClass
@Override
public void init() throws Exception {
initDataSource();
CertificateManagementDAOFactory.init(this.getDataSource());
manager = org.wso2.carbon.certificate.mgt.core.cache.impl.CertificateCacheManagerImpl.getInstance();
Assert.assertNotNull(manager);
}
}

@ -0,0 +1,242 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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.wso2.carbon.certificate.mgt.core.impl;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.cert.CertIOException;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.X509v3CertificateBuilder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.operator.OperatorCreationException;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.annotations.BeforeClass;
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.KeystoreException;
import org.wso2.carbon.certificate.mgt.core.util.CSRGenerator;
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.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.sql.SQLException;
/**
* This class has the negative tests for CertificateGenerator class
*/
@PowerMockIgnore({"java.net.ssl", "javax.security.auth.x500.X500Principal"})
@PrepareForTest({CertificateGenerator.class})
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 {
CertificateGenerator generator = new CertificateGenerator();
//Prepare mock objects
X509v3CertificateBuilder mock = Mockito.mock(X509v3CertificateBuilder.class);
Mockito.when(mock.addExtension(Matchers.any(ASN1ObjectIdentifier.class), Matchers.anyBoolean(),
Matchers.any(ASN1Encodable.class))).thenThrow(new CertIOException("CERTIO"));
PowerMockito.whenNew(X509v3CertificateBuilder.class).withAnyArguments().thenReturn(mock);
//prepare input parameters
CSRGenerator csrGeneration = new CSRGenerator();
KeyStoreReader keyStoreReader = new KeyStoreReader();
KeyPair keyPair = csrGeneration.generateKeyPair("RSA", 1024);
byte[] csrData = csrGeneration.generateCSR("SHA256WithRSA", keyPair);
PKCS10CertificationRequest certificationRequest;
PrivateKey privateKeyCA = keyStoreReader.getCAPrivateKey();
X509Certificate certCA = (X509Certificate) keyStoreReader.getCACertificate();
certificationRequest = new PKCS10CertificationRequest(csrData);
generator.generateCertificateFromCSR(privateKeyCA, certificationRequest, certCA.getIssuerX500Principal().getName());
}
@Test(description = "This test case tests behaviour when Certificate Operator creation error occurs",
expectedExceptions = KeystoreException.class)
public void negtiveTestGenerateCertificateFromCSR2() 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);
Mockito.when(mock.build(Matchers.any(PrivateKey.class))).thenThrow(new OperatorCreationException("OPERATOR"));
PowerMockito.whenNew(JcaContentSignerBuilder.class).withAnyArguments().thenReturn(mock);
//prepare input parameters
CSRGenerator csrGeneration = new CSRGenerator();
KeyStoreReader keyStoreReader = new KeyStoreReader();
KeyPair keyPair = csrGeneration.generateKeyPair("RSA", 1024);
byte[] csrData = csrGeneration.generateCSR("SHA256WithRSA", keyPair);
PKCS10CertificationRequest certificationRequest;
PrivateKey privateKeyCA = keyStoreReader.getCAPrivateKey();
X509Certificate certCA = (X509Certificate) keyStoreReader.getCACertificate();
certificationRequest = new PKCS10CertificationRequest(csrData);
generator.generateCertificateFromCSR(privateKeyCA, certificationRequest, certCA.getIssuerX500Principal().getName());
}
@Test(description = "This test case tests the behaviour when certificate exception occurs when verifying"
, expectedExceptions = KeystoreException.class)
public void negtiveTestGenerateCertificateFromCSR3() 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();
KeyPair keyPair = csrGeneration.generateKeyPair("RSA", 1024);
byte[] csrData = csrGeneration.generateCSR("SHA256WithRSA", keyPair);
PKCS10CertificationRequest certificationRequest;
PrivateKey privateKeyCA = keyStoreReader.getCAPrivateKey();
X509Certificate certCA = (X509Certificate) keyStoreReader.getCACertificate();
certificationRequest = new PKCS10CertificationRequest(csrData);
generator.generateCertificateFromCSR(privateKeyCA, certificationRequest, certCA.getIssuerX500Principal().getName());
}
@Test(description = "This test case tests behaviour when the Certificate provider does not exist"
, expectedExceptions = KeystoreException.class)
public void negativeTestgenerateX509Certificate1() throws Exception {
CertificateGenerator generator = new CertificateGenerator();
X509Certificate mock = Mockito.mock(X509Certificate.class);
PowerMockito.doThrow(new NoSuchProviderException()).when(mock).verify(Matchers.any());
JcaX509CertificateConverter conv = Mockito.mock(JcaX509CertificateConverter.class);
Mockito.when(conv.setProvider(Mockito.anyString())).thenReturn(conv);
Mockito.when(conv.getCertificate(Mockito.any())).thenReturn(mock);
PowerMockito.whenNew(JcaX509CertificateConverter.class).withNoArguments().thenReturn(conv);
generator.generateX509Certificate();
}
@Test(description = "This test case tests behaviour when the Certificate Algorithm does not exist"
, expectedExceptions = KeystoreException.class)
public void negativeTestgenerateX509Certificate2() throws Exception {
CertificateGenerator generator = new CertificateGenerator();
X509Certificate mock = Mockito.mock(X509Certificate.class);
PowerMockito.doThrow(new NoSuchAlgorithmException()).when(mock).verify(Matchers.any());
JcaX509CertificateConverter conv = Mockito.mock(JcaX509CertificateConverter.class);
Mockito.when(conv.setProvider(Mockito.anyString())).thenReturn(conv);
Mockito.when(conv.getCertificate(Mockito.any())).thenReturn(mock);
PowerMockito.whenNew(JcaX509CertificateConverter.class).withNoArguments().thenReturn(conv);
generator.generateX509Certificate();
}
@Test(description = "This test case tests behaviour when the Signature validation fails"
, expectedExceptions = KeystoreException.class)
public void negativeTestgenerateX509Certificate3() throws Exception {
CertificateGenerator generator = new CertificateGenerator();
X509Certificate mock = Mockito.mock(X509Certificate.class);
PowerMockito.doThrow(new SignatureException()).when(mock).verify(Matchers.any());
JcaX509CertificateConverter conv = Mockito.mock(JcaX509CertificateConverter.class);
Mockito.when(conv.setProvider(Mockito.anyString())).thenReturn(conv);
Mockito.when(conv.getCertificate(Mockito.any())).thenReturn(mock);
PowerMockito.whenNew(JcaX509CertificateConverter.class).withNoArguments().thenReturn(conv);
generator.generateX509Certificate();
}
@Test(description = "This test case tests behaviour when the Certificate exception occurs"
, expectedExceptions = KeystoreException.class)
public void negativeTestgenerateX509Certificate4() throws Exception {
CertificateGenerator generator = new CertificateGenerator();
X509Certificate mock = Mockito.mock(X509Certificate.class);
PowerMockito.doThrow(new CertificateException()).when(mock).verify(Matchers.any());
JcaX509CertificateConverter conv = Mockito.mock(JcaX509CertificateConverter.class);
Mockito.when(conv.setProvider(Mockito.anyString())).thenReturn(conv);
Mockito.when(conv.getCertificate(Mockito.any())).thenReturn(mock);
PowerMockito.whenNew(JcaX509CertificateConverter.class).withNoArguments().thenReturn(conv);
generator.generateX509Certificate();
}
@Test(description = "This test case tests behaviour when the Certificate key is invalid"
, expectedExceptions = KeystoreException.class)
public void negativeTestgenerateX509Certificate5() throws Exception {
CertificateGenerator generator = new CertificateGenerator();
X509Certificate mock = Mockito.mock(X509Certificate.class);
PowerMockito.doThrow(new InvalidKeyException()).when(mock).verify(Matchers.any());
JcaX509CertificateConverter conv = Mockito.mock(JcaX509CertificateConverter.class);
Mockito.when(conv.setProvider(Mockito.anyString())).thenReturn(conv);
Mockito.when(conv.getCertificate(Mockito.any())).thenReturn(mock);
PowerMockito.whenNew(JcaX509CertificateConverter.class).withNoArguments().thenReturn(conv);
generator.generateX509Certificate();
}
@Test(description = "This test case tests behavior when the CA certificate is null"
, expectedExceptions = KeystoreException.class)
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 {
CertificateGenerator generator = new CertificateGenerator();
generator.getRootCertificates(new byte[10], null);
}
@BeforeClass
public void init() throws SQLException {
if (System.getProperty("carbon.home") == null) {
File file = new File("src/test/resources/carbon-home");
if (file.exists()) {
System.setProperty("carbon.home", file.getAbsolutePath());
}
file = new File("../resources/carbon-home");
if (file.exists()) {
System.setProperty("carbon.home", file.getAbsolutePath());
}
file = new File("../../resources/carbon-home");
if (file.exists()) {
System.setProperty("carbon.home", file.getAbsolutePath());
}
file = new File("../../../resources/carbon-home");
if (file.exists()) {
System.setProperty("carbon.home", file.getAbsolutePath());
}
}
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants
.SUPER_TENANT_DOMAIN_NAME);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
DataSource normalDatasource = Mockito.mock(DataSource.class, Mockito.RETURNS_DEEP_STUBS);
DataSource daoExceptionDatasource = Mockito.mock(DataSource.class, Mockito.RETURNS_DEEP_STUBS);
Mockito.when(normalDatasource.getConnection().getMetaData().getDatabaseProductName()).thenReturn("H2");
CertificateManagementDAOFactory.init(normalDatasource);
Mockito.when(daoExceptionDatasource.getConnection().getMetaData().getDatabaseProductName()).thenReturn("H2");
Mockito.when(daoExceptionDatasource.getConnection().prepareStatement(Mockito.anyString())).thenThrow(new SQLException());
}
}

@ -0,0 +1,180 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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.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;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.IObjectFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.ObjectFactory;
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;
/**
* This class covers the negative tests for CertificateManagementServiceImpl class
*/
@PowerMockIgnore({"java.net.ssl", "javax.security.auth.x500.X500Principal"})
@PrepareForTest({CertificateManagementServiceImpl.class, JcaX509CertificateConverter.class, CertificateGenerator.class,
CertificateManagementDAOFactory.class})
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";
@BeforeClass
public void init() throws SQLException {
if (System.getProperty("carbon.home") == null) {
File file = new File("src/test/resources/carbon-home");
if (file.exists()) {
System.setProperty("carbon.home", file.getAbsolutePath());
}
file = new File("../resources/carbon-home");
if (file.exists()) {
System.setProperty("carbon.home", file.getAbsolutePath());
}
file = new File("../../resources/carbon-home");
if (file.exists()) {
System.setProperty("carbon.home", file.getAbsolutePath());
}
file = new File("../../../resources/carbon-home");
if (file.exists()) {
System.setProperty("carbon.home", file.getAbsolutePath());
}
}
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants
.SUPER_TENANT_DOMAIN_NAME);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
DataSource normalDatasource = Mockito.mock(DataSource.class, Mockito.RETURNS_DEEP_STUBS);
Mockito.when(normalDatasource.getConnection().getMetaData().getDatabaseProductName()).thenReturn(MOCK_DATASOURCE);
CertificateManagementDAOFactory.init(normalDatasource);
//configure datasource to throw dao exception
daoExceptionDatasource = Mockito.mock(DataSource.class, Mockito.RETURNS_DEEP_STUBS);
Mockito.when(daoExceptionDatasource.getConnection().getMetaData().getDatabaseProductName()).thenReturn(MOCK_DATASOURCE);
Mockito.when(daoExceptionDatasource.getConnection().prepareStatement(Mockito.anyString())).thenThrow(new SQLException());
//save as class variable
instance = CertificateManagementServiceImpl.getInstance();
}
@Test(description = "This test case tests behaviour when an error occurs when opening the data source"
,expectedExceptions = CertificateManagementException.class)
public void negativeTestretrieveCertificate2() throws Exception {
PowerMockito.mockStatic(CertificateManagementDAOFactory.class);
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 {
CertificateManagementDAOFactory.init(daoExceptionDatasource);
CertificateManagementServiceImpl instance1 = CertificateManagementServiceImpl.getInstance();
instance1.retrieveCertificate(MOCK_SERIAL);
}
@Test(description = "This test case tests behaviour when an error occurs when opening the data source",
expectedExceptions = CertificateManagementException.class)
public void negativeTestgetAllCertificates() throws Exception {
PowerMockito.mockStatic(CertificateManagementDAOFactory.class);
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 {
CertificateManagementDAOFactory.init(daoExceptionDatasource);
CertificateManagementServiceImpl instance1 = CertificateManagementServiceImpl.getInstance();
instance1.getAllCertificates(1,2);
}
@Test(description = "This test case tests behaviour when data source transaction error occurs when removing the certificate"
,expectedExceptions = CertificateManagementException.class)
public void negativeTestRemoveCertificate() throws Exception {
PowerMockito.mockStatic(CertificateManagementDAOFactory.class);
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)
public void negativeTestRemoveCertificate2() throws Exception {
CertificateManagementDAOFactory.init(daoExceptionDatasource);
CertificateManagementServiceImpl instance1 = CertificateManagementServiceImpl.getInstance();
instance1.removeCertificate(MOCK_SERIAL);
}
@Test(description = "This test case tests behaviour when an error occurs when opening the data source",
expectedExceptions = CertificateManagementException.class)
public void negativeTestGetCertificates() throws Exception {
PowerMockito.mockStatic(CertificateManagementDAOFactory.class);
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)
public void negativeTestGetCertificates2() throws CertificateManagementException {
CertificateManagementDAOFactory.init(daoExceptionDatasource);
CertificateManagementServiceImpl instance1 = CertificateManagementServiceImpl.getInstance();
instance1.getCertificates();
}
@Test(description = "This test case tests behaviour when an error occurs when opening the data source",
expectedExceptions = CertificateManagementException.class)
public void negativeTestSearchCertificates() throws Exception {
PowerMockito.mockStatic(CertificateManagementDAOFactory.class);
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)
public void negativeTestSearchCertificates2() throws CertificateManagementException {
CertificateManagementDAOFactory.init(daoExceptionDatasource);
CertificateManagementServiceImpl instance1 = CertificateManagementServiceImpl.getInstance();
instance1.searchCertificates(MOCK_SERIAL);
}
//Powermockito requirement
@ObjectFactory
public IObjectFactory getObjectFactory() {
return new org.powermock.modules.testng.PowerMockObjectFactory();
}
}

@ -0,0 +1,49 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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.wso2.carbon.certificate.mgt.core.impl;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.wso2.carbon.certificate.mgt.core.bean.Certificate;
import org.wso2.carbon.certificate.mgt.core.util.DummyCertificate;
/**
* This class tests the DTO for certificates
*/
public class CertificateTests {
private static String SERIAL = "1234";
private static String TENANT_DOMAIN = "tenant_domain";
private static int TENANT_ID = 1234;
@Test(description = "This test case tests the Certificate object getters and setters")
public void certificateCreationTest(){
Certificate certificate = new Certificate();
certificate.setSerial(SERIAL);
certificate.setCertificate(new DummyCertificate());
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);
}
}

@ -27,6 +27,10 @@
<class name="org.wso2.carbon.certificate.mgt.core.util.CommonUtil"/> <class name="org.wso2.carbon.certificate.mgt.core.util.CommonUtil"/>
<class name="org.wso2.carbon.certificate.mgt.core.impl.CertificateGeneratorTests"/> <class name="org.wso2.carbon.certificate.mgt.core.impl.CertificateGeneratorTests"/>
<class name="org.wso2.carbon.certificate.mgt.core.impl.KeyGeneratorTests"/> <class name="org.wso2.carbon.certificate.mgt.core.impl.KeyGeneratorTests"/>
<class name="org.wso2.carbon.certificate.mgt.core.impl.CertificateManagementServiceImplNegativeTests"/>
<class name="org.wso2.carbon.certificate.mgt.core.impl.CertificateGeneratorNegativeTests"/>
<class name="org.wso2.carbon.certificate.mgt.core.impl.CertificateTests"/>
<class name="org.wso2.carbon.certificate.mgt.core.impl.CertificateCacheManagerImplTests"/>
</classes> </classes>
</test> </test>
</suite> </suite>

Loading…
Cancel
Save