use of org.wso2.carbon.apimgt.core.exception.WorkflowException in project carbon-apimgt by wso2.
the class APIProviderImplTest method testChangeLifeCycleStatus.
@Test
public void testChangeLifeCycleStatus() throws RegistryException, UserStoreException, APIManagementException, FaultGatewaysException, WorkflowException, XMLStreamException {
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
prepareForChangeLifeCycleStatus(apiProvider, apimgtDAO, apiId, artifact);
APIStateChangeResponse response1 = apiProvider.changeLifeCycleStatus(apiId, APIConstants.API_LC_ACTION_DEPRECATE, "org1");
Assert.assertEquals("APPROVED", response1.getStateChangeStatus());
}
use of org.wso2.carbon.apimgt.core.exception.WorkflowException in project carbon-apimgt by wso2.
the class APIProviderImplTest method testChangeLifeCycleStatus_FaultyGWException.
@Test(expected = FaultGatewaysException.class)
public void testChangeLifeCycleStatus_FaultyGWException() throws RegistryException, UserStoreException, APIManagementException, FaultGatewaysException, WorkflowException, XMLStreamException {
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
prepareForChangeLifeCycleStatus(apiProvider, apimgtDAO, apiId, artifact);
GovernanceException exception = new GovernanceException(new APIManagementException("FaultGatewaysException:" + "{\"PUBLISHED\":{\"PROD\":\"Error\"}}"));
Mockito.when(artifact.getLifecycleState()).thenThrow(exception);
apiProvider.changeLifeCycleStatus(apiId, APIConstants.API_LC_ACTION_DEPRECATE, "org1");
}
use of org.wso2.carbon.apimgt.core.exception.WorkflowException in project carbon-apimgt by wso2.
the class UserSignUpWSWorkflowExecutorTest method testFailureToExecuteUserSignUpWSWorkflow.
@Test
public void testFailureToExecuteUserSignUpWSWorkflow() throws Exception {
userSignUpWSWorkflowExecutor.setUsername(username);
userSignUpWSWorkflowExecutor.setPassword(password.toCharArray());
PowerMockito.doNothing().when(apiMgtDAO).addWorkflowEntry(workflowDTO);
// Test failure to execute user sign up workflow, when APIManagementException has been thrown while persisting
// workflow entry in database
PowerMockito.doThrow(new APIManagementException("Error while persisting workflow")).when(apiMgtDAO).addWorkflowEntry(workflowDTO);
try {
userSignUpWSWorkflowExecutor.execute(workflowDTO);
Assert.fail("Expected WorkflowException has not occurred while executing user sign up workflow");
} catch (WorkflowException e) {
Assert.assertEquals(e.getMessage(), "Error while persisting workflow");
}
// Test failure to execute user sign up workflow, when AxisFault has been thrown while sending the message out
PowerMockito.doThrow(new AxisFault("Error sending out message")).when(serviceClient).fireAndForget((OMElement) Mockito.anyObject());
try {
userSignUpWSWorkflowExecutor.execute(workflowDTO);
Assert.fail("Expected WorkflowException has not occurred while executing user sign up workflow");
} catch (WorkflowException e) {
Assert.assertEquals(e.getMessage(), "Error sending out message");
}
// Test failure to execute user sign up workflow, when XMLStreamException has been thrown while building payload
PowerMockito.mockStatic(AXIOMUtil.class);
PowerMockito.when(AXIOMUtil.stringToOM(Mockito.anyString())).thenThrow(new XMLStreamException("Error " + "converting String to OMElement"));
try {
userSignUpWSWorkflowExecutor.execute(workflowDTO);
Assert.fail("Expected WorkflowException has not occurred while executing user sign up workflow");
} catch (WorkflowException e) {
Assert.assertEquals(e.getMessage(), "Error converting String to OMElement");
}
}
use of org.wso2.carbon.apimgt.core.exception.WorkflowException in project carbon-apimgt by wso2.
the class APIProviderImpl method cleanUpPendingAPIStateChangeTask.
private void cleanUpPendingAPIStateChangeTask(int apiId, boolean isAPIProduct) throws WorkflowException, APIManagementException {
// Run cleanup task for workflow
WorkflowExecutor apiStateChangeWFExecutor;
WorkflowDTO wfDTO;
if (isAPIProduct) {
apiStateChangeWFExecutor = getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE);
wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(apiId), WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE);
} else {
apiStateChangeWFExecutor = getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_STATE);
wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(apiId), WorkflowConstants.WF_TYPE_AM_API_STATE);
}
if (wfDTO != null && WorkflowStatus.CREATED == wfDTO.getStatus()) {
apiStateChangeWFExecutor.cleanUpPendingTask(wfDTO.getExternalWorkflowReference());
}
}
use of org.wso2.carbon.apimgt.core.exception.WorkflowException in project carbon-apimgt by wso2.
the class APIProviderImpl method deleteAPI.
public void deleteAPI(String apiUuid, String organization) throws APIManagementException {
boolean isError = false;
int apiId = -1;
API api = null;
// get api object by uuid
try {
api = getAPIbyUUID(apiUuid, organization);
} catch (APIManagementException e) {
log.error("Error while getting API by uuid for deleting API " + apiUuid + " on organization " + organization);
log.debug("Following steps will be skipped while deleting API " + apiUuid + "on organization " + organization + " due to api being null. " + "deleting Resource Registration from key managers, deleting on external API stores, " + "event publishing to gateways, logging audit message, extracting API details for " + "the recommendation system. ");
isError = true;
}
// get api id from db
try {
apiId = apiMgtDAO.getAPIID(apiUuid);
} catch (APIManagementException e) {
log.error("Error while getting API ID from DB for deleting API " + apiUuid + " on organization " + organization, e);
log.debug("Following steps will be skipped while deleting the API " + apiUuid + " on organization " + organization + "due to api id being null. cleanup workflow tasks of the API, " + "delete event publishing to gateways");
isError = true;
}
// DB delete operations
if (!isError && api != null) {
try {
deleteAPIRevisions(apiUuid, organization);
deleteAPIFromDB(api);
if (log.isDebugEnabled()) {
String logMessage = "API Name: " + api.getId().getApiName() + ", API Version " + api.getId().getVersion() + " successfully removed from the database.";
log.debug(logMessage);
}
} catch (APIManagementException e) {
log.error("Error while executing API delete operations on DB for API " + apiUuid + " on organization " + organization, e);
isError = true;
}
}
// Deleting Resource Registration from key managers
if (api != null && api.getId() != null && api.getId().toString() != null) {
Map<String, KeyManagerDto> tenantKeyManagers = KeyManagerHolder.getTenantKeyManagers(tenantDomain);
for (Map.Entry<String, KeyManagerDto> keyManagerDtoEntry : tenantKeyManagers.entrySet()) {
KeyManager keyManager = keyManagerDtoEntry.getValue().getKeyManager();
if (keyManager != null) {
try {
keyManager.deleteRegisteredResourceByAPIId(api.getId().toString());
log.debug("API " + apiUuid + " on organization " + organization + " has successfully removed from the Key Manager " + keyManagerDtoEntry.getKey());
} catch (APIManagementException e) {
log.error("Error while deleting Resource Registration for API " + apiUuid + " on organization " + organization + " in Key Manager " + keyManagerDtoEntry.getKey(), e);
}
}
}
}
try {
GatewayArtifactsMgtDAO.getInstance().deleteGatewayArtifacts(apiUuid);
log.debug("API " + apiUuid + " on organization " + organization + " has successfully removed from the gateway artifacts.");
} catch (APIManagementException e) {
log.error("Error while executing API delete operation on gateway artifacts for API " + apiUuid, e);
isError = true;
}
try {
apiPersistenceInstance.deleteAPI(new Organization(organization), apiUuid);
log.debug("API " + apiUuid + " on organization " + organization + " has successfully removed from the persistence instance.");
} catch (APIPersistenceException e) {
log.error("Error while executing API delete operation on persistence instance for API " + apiUuid + " on organization " + organization, e);
isError = true;
}
// Deleting on external API stores
if (api != null) {
// gatewayType check is required when API Management is deployed on
// other servers to avoid synapse
// Check if there are already published external APIStores.If yes,removing APIs from them.
Set<APIStore> apiStoreSet;
try {
apiStoreSet = getPublishedExternalAPIStores(apiUuid);
WSO2APIPublisher wso2APIPublisher = new WSO2APIPublisher();
if (apiStoreSet != null && !apiStoreSet.isEmpty()) {
for (APIStore store : apiStoreSet) {
wso2APIPublisher.deleteFromStore(api.getId(), APIUtil.getExternalAPIStore(store.getName(), tenantId));
}
}
} catch (APIManagementException e) {
log.error("Error while executing API delete operation on external API stores for API " + apiUuid + " on organization " + organization, e);
isError = true;
}
}
if (apiId != -1) {
try {
cleanUpPendingAPIStateChangeTask(apiId, false);
} catch (WorkflowException | APIManagementException e) {
log.error("Error while executing API delete operation on cleanup workflow tasks for API " + apiUuid + " on organization " + organization, e);
isError = true;
}
}
// Delete event publishing to gateways
if (api != null && apiId != -1) {
APIEvent apiEvent = new APIEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.API_DELETE.name(), tenantId, tenantDomain, api.getId().getApiName(), apiId, api.getUuid(), api.getId().getVersion(), api.getType(), api.getContext(), APIUtil.replaceEmailDomainBack(api.getId().getProviderName()), api.getStatus());
APIUtil.sendNotification(apiEvent, APIConstants.NotifierType.API.name());
} else {
log.debug("Event has not published to gateways due to API id has failed to retrieve from DB for API " + apiUuid + " on organization " + organization);
}
// Logging audit message for API delete
if (api != null) {
JSONObject apiLogObject = new JSONObject();
apiLogObject.put(APIConstants.AuditLogConstants.NAME, api.getId().getApiName());
apiLogObject.put(APIConstants.AuditLogConstants.VERSION, api.getId().getVersion());
apiLogObject.put(APIConstants.AuditLogConstants.PROVIDER, api.getId().getProviderName());
APIUtil.logAuditMessage(APIConstants.AuditLogConstants.API, apiLogObject.toString(), APIConstants.AuditLogConstants.DELETED, this.username);
}
// Extracting API details for the recommendation system
if (api != null && recommendationEnvironment != null) {
RecommenderEventPublisher extractor = new RecommenderDetailsExtractor(api, tenantDomain, APIConstants.DELETE_API);
Thread recommendationThread = new Thread(extractor);
recommendationThread.start();
}
// if one of the above has failed throw an error
if (isError) {
throw new APIManagementException("Error while deleting the API " + apiUuid + " on organization " + organization);
}
}
Aggregations