use of org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO 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.api.dto.CertificateMetadataDTO 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.api.dto.CertificateMetadataDTO 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.api.dto.CertificateMetadataDTO in project carbon-apimgt by wso2.
the class EndpointCertificatesApiServiceImpl method getEndpointCertificates.
public Response getEndpointCertificates(Integer limit, Integer offset, String alias, String endpoint, MessageContext messageContext) {
limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
List<CertificateMetadataDTO> certificates;
String userName = RestApiCommonUtil.getLoggedInUsername();
int tenantId = APIUtil.getTenantId(userName);
String query = CertificateRestApiUtils.buildQueryString("alias", alias, "endpoint", endpoint);
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
if (StringUtils.isNotEmpty(alias) || StringUtils.isNotEmpty(endpoint)) {
if (log.isDebugEnabled()) {
log.debug(String.format("Call the search certificate api to get the filtered certificates for " + "tenant id : %d, alias : %s, and endpoint : %s", tenantId, alias, endpoint));
}
certificates = apiProvider.searchCertificates(tenantId, alias, endpoint);
} else {
if (log.isDebugEnabled()) {
log.debug(String.format("There is no query parameters provided. So, retrieve all the certificates" + " belongs to the tenantId : %d", tenantId));
}
certificates = apiProvider.getCertificates(userName);
}
CertificatesDTO certificatesDTO = CertificateRestApiUtils.getPaginatedCertificates(certificates, limit, offset, query);
return Response.status(Response.Status.OK).entity(certificatesDTO).build();
} catch (APIManagementException e) {
RestApiUtil.handleInternalServerError("Error while retrieving the certificates.", e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO in project carbon-apimgt by wso2.
the class EndpointCertificatesApiServiceImpl method updateEndpointCertificateByAlias.
public Response updateEndpointCertificateByAlias(String alias, InputStream certificateInputStream, Attachment certificateDetail, MessageContext messageContext) {
try {
if (StringUtils.isEmpty(alias)) {
RestApiUtil.handleBadRequest("The alias should not be empty", log);
}
ContentDisposition contentDisposition = certificateDetail.getContentDisposition();
String fileName = contentDisposition.getParameter(RestApiConstants.CONTENT_DISPOSITION_FILENAME);
if (StringUtils.isBlank(fileName)) {
RestApiUtil.handleBadRequest("Certificate update failed. The Certificate should not be empty", log);
}
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String userName = RestApiCommonUtil.getLoggedInUsername();
int tenantId = APIUtil.getTenantId(userName);
if (!apiProvider.isCertificatePresent(tenantId, alias)) {
if (log.isDebugEnabled()) {
log.debug(String.format("Could not find a certificate in truststore which belongs to tenant : %d " + "and with alias : %s. Hence the operation is terminated.", tenantId, alias));
}
RestApiUtil.handleResourceNotFoundError("Could not update the certificate. " + "The alias '" + alias + "' not found.", log);
}
String base64EncodedCert = CertificateRestApiUtils.generateEncodedCertificate(certificateInputStream);
int responseCode = apiProvider.updateCertificate(base64EncodedCert, alias);
List<CertificateMetadataDTO> updatedCertificate = apiProvider.searchCertificates(tenantId, alias, null);
if (ResponseCode.SUCCESS.getResponseCode() == responseCode && updatedCertificate.size() > 0) {
CertificateMetadataDTO certificateMetadata = updatedCertificate.get(0);
CertMetadataDTO certificateDTO = new CertMetadataDTO();
certificateDTO.setAlias(certificateMetadata.getAlias());
certificateDTO.setEndpoint(certificateMetadata.getEndpoint());
URI updatedCertUri = new URI(RestApiConstants.CERTS_BASE_PATH + "?alias=" + alias);
return Response.ok(updatedCertUri).entity(certificateDTO).build();
}
if (ResponseCode.INTERNAL_SERVER_ERROR.getResponseCode() == responseCode) {
RestApiUtil.handleInternalServerError("Error while updating the certificate due to an internal " + "server error", log);
} else if (ResponseCode.CERTIFICATE_NOT_FOUND.getResponseCode() == responseCode) {
RestApiUtil.handleResourceNotFoundError("", log);
} else if (ResponseCode.CERTIFICATE_EXPIRED.getResponseCode() == responseCode) {
RestApiUtil.handleBadRequest("Error while updating the certificate. Certificate Expired.", log);
}
} catch (APIManagementException e) {
RestApiUtil.handleInternalServerError("Error while adding the certificate due to an internal server " + "error", log);
} catch (IOException e) {
RestApiUtil.handleInternalServerError("Error while encoding certificate", log);
} catch (URISyntaxException e) {
RestApiUtil.handleInternalServerError("Error while generating the resource location URI for alias '" + alias + "'", log);
}
return null;
}
Aggregations