From 0d3404a297d7670b04e10e5b025894c251f129d9 Mon Sep 17 00:00:00 2001 From: Sameera Wickramasekara Date: Fri, 13 Oct 2017 17:07:43 +0530 Subject: [PATCH] Add certificate-mgt tests --- .../pom.xml | 42 ++- .../CertificateCacheManagerImplTests.java | 42 +++ .../CertificateGeneratorNegativeTests.java | 242 ++++++++++++++++++ ...ateManagementServiceImplNegativeTests.java | 180 +++++++++++++ .../mgt/core/impl/CertificateTests.java | 49 ++++ .../src/test/resources/testng.xml | 4 + 6 files changed, 546 insertions(+), 13 deletions(-) create mode 100644 components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateCacheManagerImplTests.java create mode 100644 components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGeneratorNegativeTests.java create mode 100644 components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateManagementServiceImplNegativeTests.java create mode 100644 components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateTests.java diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml index 351a0f589fa..9b1aee851d9 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml @@ -17,7 +17,8 @@ ~ under the License. --> - + org.wso2.carbon.devicemgt certificate-mgt @@ -60,7 +61,7 @@ org.apache.commons.logging, javax.security.auth.x500, javax.xml.*, - javax.xml.parsers;version="${javax.xml.parsers.import.pkg.version}";resolution:=optional, + javax.xml.parsers;version="${javax.xml.parsers.import.pkg.version}";resolution:=optional, org.apache.commons.codec.binary;version="${commons-codec.wso2.osgi.version.range}", org.bouncycastle.asn1, org.bouncycastle.asn1.x500, @@ -105,36 +106,40 @@ - org.apache.maven.plugins maven-surefire-plugin 2.18 - - file:src/test/resources/log4j.properties - src/test/resources/testng.xml + + ${basedir}/target/coverage-reports/jacoco-unit.exec + + org.jacoco jacoco-maven-plugin - - ${basedir}/target/coverage-reports/jacoco-unit.exec - + ${jacoco.maven.plugin.version} - jacoco-initialize + default-instrument + + instrument + + + + default-restore-instrumented-classes - prepare-agent + restore-instrumented-classes jacoco-site - test + prepare-package report @@ -143,13 +148,24 @@ ${basedir}/target/coverage-reports/site + + default-report-integration + + report-integration + + - + + org.jacoco + org.jacoco.agent + ${jacoco.maven.plugin.version} + runtime + org.eclipse.osgi org.eclipse.osgi 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 new file mode 100644 index 00000000000..2e6b24004ce --- /dev/null +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateCacheManagerImplTests.java @@ -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); + } + +} 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 new file mode 100644 index 00000000000..5b7885fca0f --- /dev/null +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGeneratorNegativeTests.java @@ -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()); + } +} 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 new file mode 100644 index 00000000000..83f4f233783 --- /dev/null +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateManagementServiceImplNegativeTests.java @@ -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(); + } +} 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 new file mode 100644 index 00000000000..9d8f1d010eb --- /dev/null +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateTests.java @@ -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); + } +} diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/resources/testng.xml b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/resources/testng.xml index b76295f1086..13b59143201 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/resources/testng.xml +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/resources/testng.xml @@ -27,6 +27,10 @@ + + + +