Search in sources :

Example 41 with LifeCycle

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>());
}
Also used : ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 42 with LifeCycle

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);
}
Also used : APIEvent(org.wso2.carbon.apimgt.core.models.events.APIEvent)

Example 43 with LifeCycle

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;
}
Also used : LocalDateTime(java.time.LocalDateTime) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) API(org.wso2.carbon.apimgt.core.models.API) ArrayList(java.util.ArrayList) List(java.util.List) DedicatedGateway(org.wso2.carbon.apimgt.core.models.DedicatedGateway)

Example 44 with LifeCycle

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);
}
Also used : WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) API(org.wso2.carbon.apimgt.core.models.API) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 45 with LifeCycle

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);
}
Also used : APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) LifecycleException(org.wso2.carbon.lcm.core.exception.LifecycleException) API(org.wso2.carbon.apimgt.core.models.API) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)20 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)20 API (org.wso2.carbon.apimgt.core.models.API)20 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)19 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)13 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)11 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)11 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)11 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)10 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)10 LifecycleException (org.wso2.carbon.lcm.core.exception.LifecycleException)10 JSONObject (org.json.simple.JSONObject)8 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)8 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)7 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)7 List (java.util.List)6 IOException (java.io.IOException)5 Map (java.util.Map)5 FaultGatewaysException (org.wso2.carbon.apimgt.api.FaultGatewaysException)4