use of org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisChangeLifecyclePost.
/**
* Change the lifecycle state of an API
*
* @param action lifecycle action
* @param apiId UUID of API
* @param lifecycleChecklist lifecycle check list items
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return 200 OK if the operation is succesful
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response apisChangeLifecyclePost(String action, String apiId, String lifecycleChecklist, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
Map<String, Boolean> lifecycleChecklistMap = new HashMap<>();
WorkflowResponseDTO response = null;
try {
if (lifecycleChecklist != null) {
String[] checkList = lifecycleChecklist.split(",");
for (String checkList1 : checkList) {
StringTokenizer attributeTokens = new StringTokenizer(checkList1, ":");
String attributeName = attributeTokens.nextToken();
Boolean attributeValue = Boolean.valueOf(attributeTokens.nextToken());
lifecycleChecklistMap.put(attributeName, attributeValue);
}
}
if (action.trim().equals(APIMgtConstants.CHECK_LIST_ITEM_CHANGE_EVENT)) {
RestAPIPublisherUtil.getApiPublisher(username).updateCheckListItem(apiId, action, lifecycleChecklistMap);
WorkflowResponse workflowResponse = new GeneralWorkflowResponse();
// since workflows are not defined for checklist items
workflowResponse.setWorkflowStatus(WorkflowStatus.APPROVED);
response = MappingUtil.toWorkflowResponseDTO(workflowResponse);
return Response.ok().entity(response).build();
} else {
WorkflowResponse workflowResponse = RestAPIPublisherUtil.getApiPublisher(username).updateAPIStatus(apiId, action, lifecycleChecklistMap);
response = MappingUtil.toWorkflowResponseDTO(workflowResponse);
// be in either pending or approved state) send back the workflow response
if (WorkflowStatus.CREATED == workflowResponse.getWorkflowStatus()) {
URI location = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId);
return Response.status(Response.Status.ACCEPTED).header(RestApiConstants.LOCATION_HEADER, location).entity(response).build();
} else {
return Response.ok().entity(response).build();
}
}
} catch (APIManagementException e) {
String errorMessage = "Error while updating lifecycle of API" + apiId + " to " + action;
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (URISyntaxException e) {
String errorMessage = "Error while adding location header in response for api : " + apiId;
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler, paramList);
log.error(errorMessage, e);
return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse in project carbon-apimgt by wso2.
the class ApplicationsApiServiceImplTestCase method testApplicationsApplicationIdPutErrorCase.
@Test
public void testApplicationsApplicationIdPutErrorCase() throws APIManagementException, NotFoundException {
TestUtil.printTestMethodName();
String applicationId = UUID.randomUUID().toString();
String accessToken = UUID.randomUUID().toString();
String clientID = UUID.randomUUID().toString();
String clientSecret = UUID.randomUUID().toString();
ApplicationsApiServiceImpl applicationsApiService = new ApplicationsApiServiceImpl();
APIStore apiStore = Mockito.mock(APIStoreImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
Request request = getRequest();
PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
ApplicationTokenDTO applicationTokenDTO = new ApplicationTokenDTO();
applicationTokenDTO.setAccessToken(accessToken);
applicationTokenDTO.setTokenScopes("SCOPE1");
applicationTokenDTO.setValidityTime((long) 100000);
List<String> grantTypes = new ArrayList<>();
grantTypes.add("password");
grantTypes.add("jwt");
ApplicationKeysDTO applicationKeysDTO = new ApplicationKeysDTO();
applicationKeysDTO.setConsumerKey(clientID);
applicationKeysDTO.setConsumerSecret(clientSecret);
applicationKeysDTO.setKeyType(ApplicationKeysDTO.KeyTypeEnum.PRODUCTION);
applicationKeysDTO.setCallbackUrl(null);
applicationKeysDTO.setSupportedGrantTypes(grantTypes);
List<ApplicationKeysDTO> applicationKeysDTOList = new ArrayList<>();
applicationKeysDTOList.add(applicationKeysDTO);
ApplicationDTO applicationDTO = new ApplicationDTO();
applicationDTO.setApplicationId(applicationId);
applicationDTO.setDescription("sample application");
applicationDTO.setName("app1");
applicationDTO.setSubscriber("subscriber");
applicationDTO.setPermission("permission");
applicationDTO.setLifeCycleStatus("APPROVED");
applicationDTO.setThrottlingTier("UNLIMITED");
applicationDTO.setToken(applicationTokenDTO);
applicationDTO.setKeys(applicationKeysDTOList);
WorkflowResponse workflowResponse = new GeneralWorkflowResponse();
workflowResponse.setWorkflowStatus(WorkflowStatus.APPROVED);
Mockito.when(apiStore.getApplication(applicationId, USER)).thenReturn(getSampleApplication(applicationId));
Mockito.doThrow(new APIManagementException("Error Occurred", ExceptionCodes.INTERNAL_ERROR)).when(apiStore).updateApplication(applicationId, getSampleApplication(applicationId));
Response response = applicationsApiService.applicationsApplicationIdPut(applicationId, applicationDTO, null, null, request);
Assert.assertEquals(500, response.getStatus());
}
use of org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse in project carbon-apimgt by wso2.
the class ApplicationsApiServiceImplTestCase method testApplicationsApplicationIdDelete.
@Test
public void testApplicationsApplicationIdDelete() throws APIManagementException, NotFoundException {
TestUtil.printTestMethodName();
String applicationId = UUID.randomUUID().toString();
ApplicationsApiServiceImpl applicationsApiService = new ApplicationsApiServiceImpl();
APIStore apiStore = Mockito.mock(APIStoreImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
Request request = getRequest();
PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
WorkflowResponse workflowResponse = new GeneralWorkflowResponse();
workflowResponse.setWorkflowStatus(WorkflowStatus.APPROVED);
Mockito.when(apiStore.deleteApplication(applicationId)).thenReturn(workflowResponse);
Response response = applicationsApiService.applicationsApplicationIdDelete(applicationId, null, null, request);
Assert.assertEquals(200, response.getStatus());
}
use of org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse in project carbon-apimgt by wso2.
the class APIStoreImplTestCase method testAddApplicationUpdateWorkflowReject.
@Test(description = "Test Application update workflow reject")
public void testAddApplicationUpdateWorkflowReject() throws APIManagementException {
/*
* This test is to validate the rollback the application to its previous state for application
* update request rejection
*/
ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
Policy policy = Mockito.mock(Policy.class);
APIStore apiStore = getApiStoreImpl(applicationDAO, policyDAO, workflowDAO);
Application application = new Application(APP_NAME, USER_NAME);
application.setStatus(ApplicationStatus.APPLICATION_APPROVED);
application.setPolicy(new ApplicationPolicy(TIER));
application.setId(UUID);
application.setPermissionString("[{\"groupId\": \"testGroup\",\"permission\":[\"READ\",\"UPDATE\",\"DELETE\",\"SUBSCRIPTION\"]}]");
Mockito.when(applicationDAO.isApplicationNameExists(APP_NAME)).thenReturn(false);
Mockito.when(policyDAO.getPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, TIER)).thenReturn(policy);
// following section mock the workflow callback api
DefaultWorkflowExecutor executor = Mockito.mock(DefaultWorkflowExecutor.class);
APIGateway apiGateway = Mockito.mock(APIGateway.class);
Workflow workflow = new ApplicationUpdateWorkflow(applicationDAO, workflowDAO, apiGateway);
workflow.setWorkflowReference(application.getId());
workflow.setExternalWorkflowReference(UUID);
// validate the rejection flow
// here we assume the application is an approve state before update
// this attribute is set internally based on the workflow data
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_EXISTIN_APP_STATUS, ApplicationStatus.APPLICATION_APPROVED);
WorkflowResponse response = new GeneralWorkflowResponse();
response.setWorkflowStatus(WorkflowStatus.REJECTED);
Mockito.when(executor.complete(workflow)).thenReturn(response);
apiStore.completeWorkflow(executor, workflow);
Mockito.verify(applicationDAO, Mockito.times(1)).updateApplicationState(application.getId(), ApplicationStatus.APPLICATION_APPROVED);
// here we assume the application is an rejected state before update.
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_EXISTIN_APP_STATUS, ApplicationStatus.APPLICATION_REJECTED);
apiStore.completeWorkflow(executor, workflow);
Mockito.verify(applicationDAO, Mockito.times(1)).updateApplicationState(application.getId(), ApplicationStatus.APPLICATION_REJECTED);
}
use of org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse in project carbon-apimgt by wso2.
the class APIStoreImplTestCase method testAddSubscriptionWorkflowReject.
@Test(description = "Test Subscription workflow rejection")
public void testAddSubscriptionWorkflowReject() throws APIManagementException {
ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APIGateway apiGateway = Mockito.mock(APIGateway.class);
WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
Policy policy = new SubscriptionPolicy(UUID, TIER);
APIStore apiStore = getApiStoreImpl(apiDAO, applicationDAO, apiSubscriptionDAO, workflowDAO, apiGateway, policyDAO);
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, TIER)).thenReturn(policy);
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI();
apiBuilder.lifeCycleStatus(APIStatus.PUBLISHED.getStatus());
API api = apiBuilder.build();
String apiId = api.getId();
Application application = new Application("TestApp", USER_ID);
application.setId(UUID);
Mockito.when(apiDAO.getAPI(apiId)).thenReturn(api);
Mockito.when(applicationDAO.getApplication(UUID)).thenReturn(application);
SubscriptionResponse response = apiStore.addApiSubscription(apiId, UUID, TIER);
DefaultWorkflowExecutor executor = Mockito.mock(DefaultWorkflowExecutor.class);
Workflow workflow = new SubscriptionCreationWorkflow(apiSubscriptionDAO, workflowDAO, apiGateway);
workflow.setWorkflowType(APIMgtConstants.WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
workflow.setWorkflowReference(response.getSubscriptionUUID());
WorkflowResponse workflowResponse = new GeneralWorkflowResponse();
workflowResponse.setWorkflowStatus(WorkflowStatus.REJECTED);
Mockito.when(executor.complete(workflow)).thenReturn(workflowResponse);
apiStore.completeWorkflow(executor, workflow);
Mockito.verify(apiSubscriptionDAO, Mockito.times(1)).updateSubscriptionStatus(response.getSubscriptionUUID(), SubscriptionStatus.REJECTED);
}
Aggregations