use of org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException in project carbon-apimgt by wso2.
the class CertificateManagerImplTest method testRemoveFromPublisherWithInternalServerErrorWhenDeleting.
@Test
public void testRemoveFromPublisherWithInternalServerErrorWhenDeleting() {
PowerMockito.stub(PowerMockito.method(CertificateMgtUtils.class, "removeCertificateFromTrustStore", String.class)).toReturn(ResponseCode.INTERNAL_SERVER_ERROR);
try {
Mockito.when(certificateMgtDAO.deleteCertificate("testRemoveFromPublisherWithInternalServerErrorWhenDeleting", "testRemoveFromPublisherWithInternalServerErrorWhenDeleting", TENANT_ID)).thenReturn(true);
Mockito.when(certificateMgtDAO.addCertificate(BASE64_ENCODED_CERT, "testRemoveFromPublisherWithInternalServerErrorWhenDeleting", "testRemoveFromPublisherWithInternalServerErrorWhenDeleting", TENANT_ID)).thenReturn(true);
CertificateMetadataDTO certificateMetadataDTO = new CertificateMetadataDTO();
certificateMetadataDTO.setEndpoint("testRemoveFromPublisherWithInternalServerErrorWhenDeleting");
certificateMetadataDTO.setCertificate(BASE64_ENCODED_CERT);
certificateMetadataDTO.setAlias("testRemoveFromPublisherWithInternalServerErrorWhenDeleting");
Mockito.when(certificateMgtDAO.getCertificates("testRemoveFromPublisherWithInternalServerErrorWhenDeleting", null, TENANT_ID)).thenReturn(Arrays.asList(certificateMetadataDTO));
} catch (CertificateManagementException | CertificateAliasExistsException e) {
e.printStackTrace();
}
ResponseCode responseCode = certificateManager.deleteCertificateFromParentNode("testRemoveFromPublisherWithInternalServerErrorWhenDeleting", "testRemoveFromPublisherWithInternalServerErrorWhenDeleting", TENANT_ID);
Assert.assertEquals(ResponseCode.INTERNAL_SERVER_ERROR, responseCode);
}
use of org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException in project carbon-apimgt by wso2.
the class CertificateManagerImplTest method testRemoveFromPublisherCertificateManagementException.
@Test
public void testRemoveFromPublisherCertificateManagementException() {
PowerMockito.stub(PowerMockito.method(CertificateMgtUtils.class, "removeCertificateFromTrustStore", String.class)).toReturn(ResponseCode.INTERNAL_SERVER_ERROR);
try {
Mockito.when(certificateMgtDAO.deleteCertificate("testRemoveFromPublisherCertificateManagementException", "testRemoveFromPublisherCertificateManagementException", TENANT_ID)).thenReturn(true);
Mockito.when(certificateMgtDAO.addCertificate(BASE64_ENCODED_CERT, "testRemoveFromPublisherCertificateManagementException", "testRemoveFromPublisherCertificateManagementException", TENANT_ID)).thenThrow(CertificateManagementException.class);
CertificateMetadataDTO certificateMetadataDTO = new CertificateMetadataDTO();
certificateMetadataDTO.setEndpoint("testRemoveFromPublisherCertificateManagementException");
certificateMetadataDTO.setCertificate(BASE64_ENCODED_CERT);
certificateMetadataDTO.setAlias("testRemoveFromPublisherCertificateManagementException");
List<CertificateMetadataDTO> certificateMetadataDTOList = new ArrayList<>();
certificateMetadataDTOList.add(certificateMetadataDTO);
Mockito.when(certificateMgtDAO.getCertificates("testRemoveFromPublisherCertificateManagementException", null, TENANT_ID)).thenReturn(certificateMetadataDTOList);
} catch (CertificateManagementException | CertificateAliasExistsException e) {
e.printStackTrace();
}
ResponseCode responseCode = certificateManager.deleteCertificateFromParentNode("testRemoveFromPublisherCertificateManagementException", "testRemoveFromPublisherCertificateManagementException", TENANT_ID);
Assert.assertEquals(ResponseCode.INTERNAL_SERVER_ERROR, responseCode);
}
use of org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException in project carbon-apimgt by wso2.
the class CertificateManagerImplTest method testGetCertificate.
@Test
public void testGetCertificate() throws APIManagementException, CertificateManagementException {
String alias = "testGetCertificate";
String endpoint = "testGetCertificate";
int tenant = 1234;
CertificateMetadataDTO certificateMetadata = generateMetadata();
List<CertificateMetadataDTO> certificateMetadataList = new ArrayList<>();
certificateMetadataList.add(certificateMetadata);
Mockito.when(certificateMgtDAO.getCertificates(alias, endpoint, tenant)).thenReturn(certificateMetadataList);
List<CertificateMetadataDTO> result = certificateManager.getCertificates(tenant, alias, endpoint);
Assert.assertNotNull(result);
Assert.assertEquals(certificateMetadataList, result);
}
use of org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException in project carbon-apimgt by wso2.
the class CertificateManagerImplTest method testIsCertificatePresent.
@Test
public void testIsCertificatePresent() throws APIManagementException, CertificateManagementException {
CertificateMetadataDTO certificateMetadataDTO = generateMetadata();
List<CertificateMetadataDTO> certificateMetadataList = new ArrayList<>();
certificateMetadataList.add(certificateMetadataDTO);
Mockito.when(certificateMgtDAO.getCertificates(Mockito.anyString(), Mockito.anyString(), Mockito.anyInt())).thenReturn(certificateMetadataList);
boolean result = certificateManager.isCertificatePresent(TENANT_ID, ALIAS);
Assert.assertTrue(result);
}
use of org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException in project carbon-apimgt by wso2.
the class CertificateMgtDAO method getClientCertificates.
/**
* Method to retrieve certificate metadata from db for specific tenant which matches alias or api identifier.
* Both alias and api identifier are optional
*
* @param tenantId : The id of the tenant which the certificate belongs to.
* @param alias : Alias for the certificate. (Optional)
* @param apiIdentifier : The API which the certificate is mapped to. (Optional)
* @param organization : Organization
* @return : A CertificateMetadataDTO object if the certificate is retrieved successfully, null otherwise.
*/
public List<ClientCertificateDTO> getClientCertificates(int tenantId, String alias, APIIdentifier apiIdentifier, String organization) throws CertificateManagementException {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<ClientCertificateDTO> clientCertificateDTOS = new ArrayList<>();
int apiId = 0;
int index = 1;
String selectQuery = SQLConstants.ClientCertificateConstants.SELECT_CERTIFICATE_FOR_TENANT;
if (StringUtils.isNotEmpty(alias) && apiIdentifier != null) {
selectQuery = SQLConstants.ClientCertificateConstants.SELECT_CERTIFICATE_FOR_TENANT_ALIAS_APIID;
} else if (StringUtils.isNotEmpty(alias)) {
selectQuery = SQLConstants.ClientCertificateConstants.SELECT_CERTIFICATE_FOR_TENANT_ALIAS;
} else if (apiIdentifier != null) {
selectQuery = SQLConstants.ClientCertificateConstants.SELECT_CERTIFICATE_FOR_TENANT_APIID;
}
try {
connection = APIMgtDBUtil.getConnection();
if (apiIdentifier != null) {
String apiUuid;
if (apiIdentifier.getUUID() != null) {
apiUuid = apiIdentifier.getUUID();
APIRevision apiRevision = ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(apiUuid);
if (apiRevision != null && apiRevision.getApiUUID() != null) {
apiUuid = apiRevision.getApiUUID();
}
} else {
apiUuid = ApiMgtDAO.getInstance().getUUIDFromIdentifier(apiIdentifier, organization);
}
apiId = ApiMgtDAO.getInstance().getAPIID(apiUuid, connection);
}
preparedStatement = connection.prepareStatement(selectQuery);
preparedStatement.setBoolean(index, false);
index++;
preparedStatement.setInt(index, tenantId);
index++;
if (alias != null) {
preparedStatement.setString(index, alias);
index++;
}
if (apiIdentifier != null) {
preparedStatement.setInt(index, apiId);
}
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
alias = resultSet.getString("ALIAS");
ClientCertificateDTO clientCertificateDTO = new ClientCertificateDTO();
clientCertificateDTO.setTierName(resultSet.getString("TIER_NAME"));
clientCertificateDTO.setAlias(alias);
clientCertificateDTO.setCertificate(APIMgtDBUtil.getStringFromInputStream(resultSet.getBinaryStream("CERTIFICATE")));
if (apiIdentifier == null) {
apiIdentifier = new APIIdentifier(APIUtil.replaceEmailDomain(resultSet.getString("API_PROVIDER")), resultSet.getString("API_NAME"), resultSet.getString("API_VERSION"));
}
clientCertificateDTO.setApiIdentifier(apiIdentifier);
clientCertificateDTOS.add(clientCertificateDTO);
}
} catch (SQLException e) {
handleException("Error while searching client certificate details for the tenant " + tenantId, e);
} catch (APIManagementException e) {
handleException("API Management Exception while searching client certificate details for the tenant " + tenantId, e);
} finally {
APIMgtDBUtil.closeAllConnections(preparedStatement, connection, resultSet);
}
return clientCertificateDTOS;
}
Aggregations