use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.
the class RestApiUtilTestCase method testGetErrorDTO1.
@Test(description = "Test get Error DTO")
public void testGetErrorDTO1() throws Exception {
Map<String, String> paramList = new HashMap<>();
ErrorDTO errorDTOExpected = new ErrorDTO();
errorDTOExpected.setCode((long) 900300);
errorDTOExpected.setMessage("Lifecycle exception occurred");
errorDTOExpected.setDescription("Error occurred while changing lifecycle state");
ErrorHandler errorHandler = Mockito.mock(ErrorHandler.class);
when(errorHandler.getErrorCode()).thenReturn((long) 900300);
when(errorHandler.getErrorMessage()).thenReturn("Lifecycle exception occurred");
when(errorHandler.getErrorDescription()).thenReturn("Error occurred while changing lifecycle state");
ErrorDTO errorDTO1 = RestApiUtil.getErrorDTO(errorHandler, paramList);
Assert.assertEquals(errorDTO1.getCode(), errorDTOExpected.getCode());
Assert.assertEquals(errorDTO1.getMessage(), errorDTOExpected.getMessage());
Assert.assertEquals(errorDTO1.getMoreInfo(), new HashMap<String, String>());
}
use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.
the class APIGatewayPublisherImpl method changeAPIState.
@Override
public void changeAPIState(API api, String status) throws GatewayException {
// create the message to be sent to the gateway. This contains the basic details of the API and target
// lifecycle state.
APIEvent gatewayDTO = new APIEvent(APIMgtConstants.GatewayEventTypes.API_STATE_CHANGE);
gatewayDTO.setLabels(api.getLabels());
gatewayDTO.setApiSummary(toAPISummary(api));
publishToPublisherTopic(gatewayDTO);
}
use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.
the class APIStateChangeWorkflow method completeWorkflow.
@Override
public WorkflowResponse completeWorkflow(WorkflowExecutor workflowExecutor) throws APIManagementException {
WorkflowResponse response = workflowExecutor.complete(this);
setStatus(response.getWorkflowStatus());
if (WorkflowStatus.APPROVED == response.getWorkflowStatus()) {
if (log.isDebugEnabled()) {
log.debug("API state change workflow complete: Approved");
}
String invoker = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_LC_INVOKER);
String currentState = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_CUR_STATE);
String targetState = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_TARGET_STATE);
boolean hasOwnGateway = Boolean.valueOf(getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_HAS_OWN_GATEWAY));
String label = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_AUTOGEN_LABEL);
if (hasOwnGateway) {
// (CREATED to DEPRECATED)
if ((currentState.equalsIgnoreCase(APIStatus.CREATED.getStatus()) || currentState.equalsIgnoreCase(APIStatus.MAINTENANCE.getStatus()) || currentState.equalsIgnoreCase(APIStatus.PROTOTYPED.getStatus())) && (targetState.equalsIgnoreCase(APIStatus.PUBLISHED.getStatus()) || targetState.equalsIgnoreCase(APIStatus.PROTOTYPED.getStatus()) || targetState.equalsIgnoreCase(APIStatus.DEPRECATED.getStatus()))) {
try {
// No need to auto-generate the label again As hasOwnGateway is true.
// create the gateway
API api = apiDAO.getAPI(getWorkflowReference());
apiGateway.createContainerBasedGateway(label, api);
} catch (ContainerBasedGatewayException e) {
// Revert already added labels
DedicatedGateway dedicatedGateway = new DedicatedGateway();
dedicatedGateway.setEnabled(false);
dedicatedGateway.setApiId(getWorkflowReference());
dedicatedGateway.setUpdatedBy(invoker);
List<String> labels = new ArrayList<>();
labels.add(labelDAO.getLabelIdByNameAndType(APIMgtConstants.DEFAULT_LABEL_NAME, APIMgtConstants.LABEL_TYPE_GATEWAY));
labels.add(labelDAO.getLabelIdByNameAndType(APIMgtConstants.DEFAULT_LABEL_NAME, APIMgtConstants.LABEL_TYPE_STORE));
apiDAO.updateDedicatedGateway(dedicatedGateway, labels);
throw new APIManagementException("Error while updating lifecycle state in Private Jet Mode", e, ExceptionCodes.DEDICATED_CONTAINER_GATEWAY_CREATION_FAILED);
}
}
}
String localTime = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_LAST_UPTIME);
LocalDateTime time = LocalDateTime.parse(localTime);
updateAPIStatusForWorkflowComplete(getWorkflowReference(), targetState, invoker, time);
// After publishing the state change to the Gateway, remove the gateway for following occasions.
if (hasOwnGateway) {
if ((currentState.equalsIgnoreCase(APIStatus.PUBLISHED.getStatus()) || currentState.equalsIgnoreCase(APIStatus.PROTOTYPED.getStatus()) || currentState.equalsIgnoreCase(APIStatus.DEPRECATED.getStatus())) && (targetState.equalsIgnoreCase(APIStatus.CREATED.getStatus()) || targetState.equalsIgnoreCase(APIStatus.MAINTENANCE.getStatus()) || targetState.equalsIgnoreCase(APIStatus.PROTOTYPED.getStatus())) || targetState.equalsIgnoreCase(APIStatus.RETIRED.getStatus())) {
// remove gateway
API api = apiDAO.getAPI(getWorkflowReference());
apiGateway.removeContainerBasedGateway(label, api);
}
}
} else if (WorkflowStatus.REJECTED == response.getWorkflowStatus()) {
if (log.isDebugEnabled()) {
log.debug("API state change workflow complete: Rejected");
}
apiDAO.updateAPIWorkflowStatus(getWorkflowReference(), APIMgtConstants.APILCWorkflowStatus.REJECTED);
}
updateWorkflowEntries(this);
return response;
}
use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testTemovePendingLifecycleWorkflowTaskForAPIForAPIWithoutPendingLCState.
@Test(description = "Exception when removing api pending lc status change request for an api", expectedExceptions = APIManagementException.class)
public void testTemovePendingLifecycleWorkflowTaskForAPIForAPIWithoutPendingLCState() throws APIManagementException, LifecycleException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiLifecycleManager, apiDAO, workflowDAO, gateway);
APIBuilder builder = SampleTestObjectCreator.createDefaultAPI();
builder.workflowStatus(APILCWorkflowStatus.PENDING.toString());
API api = builder.build();
String uuid = api.getId();
Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
/*
Mockito.doThrow(new APIMgtDAOException("Error while executing sql query")).when(workflowDAO)
.getExternalWorkflowReferenceForPendingTask(uuid, WorkflowConstants.WF_TYPE_AM_API_STATE);
*/
Mockito.when(workflowDAO.getExternalWorkflowReferenceForPendingTask(uuid, WorkflowConstants.WF_TYPE_AM_API_STATE)).thenThrow(new APIMgtDAOException("Error occurred while changing api lifecycle workflow status"));
apiPublisher.removePendingLifecycleWorkflowTaskForAPI(uuid);
Mockito.verify(workflowDAO, Mockito.times(1)).getExternalWorkflowReferenceForPendingTask(uuid, WorkflowConstants.WF_TYPE_AM_API_STATE);
}
use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testGetLifeCycleEventsExceptionFindingAPILifeCycleHistory.
@Test(description = "Exception finding API LifeCycle History when getting lifecycle events list of an API", expectedExceptions = APIManagementException.class)
public void testGetLifeCycleEventsExceptionFindingAPILifeCycleHistory() throws APIManagementException, LifecycleException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gateway);
API api = SampleTestObjectCreator.createDefaultAPI().build();
String uuid = api.getId();
String lifecycleId = api.getLifecycleInstanceId();
Mockito.when(apiDAO.getAPISummary(uuid)).thenReturn(api);
Mockito.doThrow(new LifecycleException("Couldn't find APILifecycle History for ID " + uuid)).when(apiLifecycleManager).getLifecycleHistory(lifecycleId);
apiPublisher.getLifeCycleEvents(uuid);
}
Aggregations