use of org.wso2.carbon.apimgt.rest.api.admin.dto.WorkflowDTO in project carbon-apimgt by wso2.
the class SubscriptionCreationWSWorkflowExecutorTest method testWorkflowExecuteWithoutExecutorParam.
@Test
public void testWorkflowExecuteWithoutExecutorParam() throws Exception {
SubscriptionWorkflowDTO workflowDTO = new SubscriptionWorkflowDTO();
workflowDTO.setApiContext("/test");
workflowDTO.setApiName("TestAPI");
workflowDTO.setApiVersion("1.0");
workflowDTO.setApiProvider("admin");
workflowDTO.setSubscriber("admin");
workflowDTO.setApplicationName("TestApp");
workflowDTO.setTierName("Gold");
workflowDTO.setWorkflowReference("1");
workflowDTO.setExternalWorkflowReference(UUID.randomUUID().toString());
workflowDTO.setCallbackUrl("http://test");
PowerMockito.doNothing().when(apiMgtDAO).updateSubscriptionStatus(Integer.parseInt(workflowDTO.getWorkflowReference()), APIConstants.SubscriptionStatus.REJECTED);
ServiceReferenceHolderMockCreator serviceRefMock = new ServiceReferenceHolderMockCreator(-1234);
ServiceReferenceHolderMockCreator.initContextService();
PowerMockito.whenNew(ServiceClient.class).withArguments(Mockito.any(ConfigurationContext.class), Mockito.any(AxisService.class)).thenReturn(serviceClient);
subscriptionCreationWSWorkflowExecutor.setUsername(null);
subscriptionCreationWSWorkflowExecutor.setPassword(null);
try {
// shouldn't fail. this checks for unsecured enpoint use case
Assert.assertNotNull(subscriptionCreationWSWorkflowExecutor.execute(workflowDTO));
} catch (WorkflowException e) {
Assert.fail("Unexpected WorkflowException occurred while executing Subscription creation ws workflow");
}
// empty values
subscriptionCreationWSWorkflowExecutor.setUsername("");
subscriptionCreationWSWorkflowExecutor.setPassword("".toCharArray());
try {
// shouldn't fail. this checks for unsecured enpoint use case
Assert.assertNotNull(subscriptionCreationWSWorkflowExecutor.execute(workflowDTO));
} catch (WorkflowException e) {
Assert.fail("Unexpected WorkflowException occurred while executing Subscription creation ws workflow");
}
// one empty value and other null
subscriptionCreationWSWorkflowExecutor.setUsername("");
subscriptionCreationWSWorkflowExecutor.setPassword(null);
try {
// shouldn't fail. this checks for unsecured enpoint use case
Assert.assertNotNull(subscriptionCreationWSWorkflowExecutor.execute(workflowDTO));
} catch (WorkflowException e) {
Assert.fail("Unexpected WorkflowException occurred while executing Subscription creation ws workflow");
}
subscriptionCreationWSWorkflowExecutor.setUsername(null);
subscriptionCreationWSWorkflowExecutor.setPassword("".toCharArray());
try {
// shouldn't fail. this checks for unsecured enpoint use case
Assert.assertNotNull(subscriptionCreationWSWorkflowExecutor.execute(workflowDTO));
} catch (WorkflowException e) {
Assert.fail("Unexpected WorkflowException occurred while executing Subscription creation ws workflow");
}
// without a password
subscriptionCreationWSWorkflowExecutor.setUsername("admin");
subscriptionCreationWSWorkflowExecutor.setPassword("".toCharArray());
try {
// shouldn't fail. this checks for unsecured enpoint use case.
Assert.assertNotNull(subscriptionCreationWSWorkflowExecutor.execute(workflowDTO));
} catch (WorkflowException e) {
Assert.fail("Unexpected WorkflowException occurred while executing Subscription creation ws workflow");
}
subscriptionCreationWSWorkflowExecutor.setUsername("admin");
subscriptionCreationWSWorkflowExecutor.setPassword(null);
try {
// shouldn't fail. this checks for unsecured enpoint use case.
Assert.assertNotNull(subscriptionCreationWSWorkflowExecutor.execute(workflowDTO));
} catch (WorkflowException e) {
Assert.fail("Unexpected WorkflowException occurred while executing Subscription creation ws workflow");
}
}
use of org.wso2.carbon.apimgt.rest.api.admin.dto.WorkflowDTO in project carbon-apimgt by wso2.
the class WorkflowMappingUtil method toWorkflowDTO.
/**
* Map Workflow to WorkflowDTO
* @param response WorkflowResponse object
* @return WorkflowResponseDTO mapped WorkflowResponseDTO
*/
public static WorkflowDTO toWorkflowDTO(Workflow response) {
WorkflowDTO workflowDTO = new WorkflowDTO();
if (response != null) {
workflowDTO.setCreatedTime(response.getCreatedTime().toString());
workflowDTO.setDescription(response.getWorkflowDescription());
workflowDTO.setType(response.getWorkflowType());
workflowDTO.setReferenceId(response.getExternalWorkflowReference());
workflowDTO.setWorkflowStatus(response.getStatus().toString());
}
return workflowDTO;
}
use of org.wso2.carbon.apimgt.rest.api.admin.dto.WorkflowDTO in project carbon-apimgt by wso2.
the class AbstractAPIManager method populateAPIInformation.
protected void populateAPIInformation(String uuid, String organization, API api) throws APIManagementException, OASPersistenceException, ParseException, AsyncSpecPersistenceException {
Organization org = new Organization(organization);
// UUID
if (api.getUuid() == null) {
api.setUuid(uuid);
}
api.setOrganization(organization);
// environment
String environmentString = null;
if (api.getEnvironments() != null) {
environmentString = String.join(",", api.getEnvironments());
}
api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environmentString, organization));
// workflow status
APIIdentifier apiId = api.getId();
WorkflowDTO workflow;
String currentApiUuid = uuid;
if (api.isRevision() && api.getRevisionedApiId() != null) {
currentApiUuid = api.getRevisionedApiId();
}
workflow = APIUtil.getAPIWorkflowStatus(currentApiUuid, WF_TYPE_AM_API_STATE);
if (workflow != null) {
WorkflowStatus status = workflow.getStatus();
api.setWorkflowStatus(status.toString());
}
// TODO try to use a single query to get info from db
int internalId = apiMgtDAO.getAPIID(currentApiUuid);
apiId.setId(internalId);
apiMgtDAO.setServiceStatusInfoToAPI(api, internalId);
// api level tier
String apiLevelTier;
if (api.isRevision()) {
apiLevelTier = apiMgtDAO.getAPILevelTier(api.getRevisionedApiId(), api.getUuid());
} else {
apiLevelTier = apiMgtDAO.getAPILevelTier(internalId);
}
api.setApiLevelPolicy(apiLevelTier);
// available tier
String tiers = null;
Set<Tier> tiersSet = api.getAvailableTiers();
Set<String> tierNameSet = new HashSet<String>();
for (Tier t : tiersSet) {
tierNameSet.add(t.getName());
}
if (api.getAvailableTiers() != null) {
tiers = String.join("||", tierNameSet);
}
Map<String, Tier> definedTiers = APIUtil.getTiers(APIUtil.getInternalOrganizationId(organization));
Set<Tier> availableTier = APIUtil.getAvailableTiers(definedTiers, tiers, api.getId().getApiName());
api.setAvailableTiers(availableTier);
// Scopes
Map<String, Scope> scopeToKeyMapping = APIUtil.getAPIScopes(currentApiUuid, organization);
api.setScopes(new LinkedHashSet<>(scopeToKeyMapping.values()));
// templates
String resourceConfigsString;
if (api.getSwaggerDefinition() != null) {
resourceConfigsString = api.getSwaggerDefinition();
} else {
resourceConfigsString = apiPersistenceInstance.getOASDefinition(org, uuid);
}
api.setSwaggerDefinition(resourceConfigsString);
if (resourceConfigsString == null) {
if (api.getAsyncApiDefinition() != null) {
resourceConfigsString = api.getAsyncApiDefinition();
} else {
resourceConfigsString = apiPersistenceInstance.getAsyncDefinition(org, uuid);
}
api.setAsyncApiDefinition(resourceConfigsString);
}
if (api.getType() != null && APIConstants.APITransportType.GRAPHQL.toString().equals(api.getType())) {
api.setGraphQLSchema(getGraphqlSchemaDefinition(uuid, organization));
}
JSONParser jsonParser = new JSONParser();
JSONObject paths = null;
if (resourceConfigsString != null) {
JSONObject resourceConfigsJSON = (JSONObject) jsonParser.parse(resourceConfigsString);
paths = (JSONObject) resourceConfigsJSON.get(APIConstants.SWAGGER_PATHS);
}
Set<URITemplate> uriTemplates = apiMgtDAO.getURITemplatesOfAPI(api.getUuid());
for (URITemplate uriTemplate : uriTemplates) {
String uTemplate = uriTemplate.getUriTemplate();
String method = uriTemplate.getHTTPVerb();
List<Scope> oldTemplateScopes = uriTemplate.retrieveAllScopes();
List<Scope> newTemplateScopes = new ArrayList<>();
if (!oldTemplateScopes.isEmpty()) {
for (Scope templateScope : oldTemplateScopes) {
Scope scope = scopeToKeyMapping.get(templateScope.getKey());
newTemplateScopes.add(scope);
}
}
uriTemplate.addAllScopes(newTemplateScopes);
uriTemplate.setResourceURI(api.getUrl());
uriTemplate.setResourceSandboxURI(api.getSandboxUrl());
// AWS Lambda: set arn & timeout to URI template
if (paths != null) {
JSONObject path = (JSONObject) paths.get(uTemplate);
if (path != null) {
JSONObject operation = (JSONObject) path.get(method.toLowerCase());
if (operation != null) {
if (operation.containsKey(APIConstants.SWAGGER_X_AMZN_RESOURCE_NAME)) {
uriTemplate.setAmznResourceName((String) operation.get(APIConstants.SWAGGER_X_AMZN_RESOURCE_NAME));
}
if (operation.containsKey(APIConstants.SWAGGER_X_AMZN_RESOURCE_TIMEOUT)) {
uriTemplate.setAmznResourceTimeout(((Long) operation.get(APIConstants.SWAGGER_X_AMZN_RESOURCE_TIMEOUT)).intValue());
}
}
}
}
}
if (APIConstants.IMPLEMENTATION_TYPE_INLINE.equalsIgnoreCase(api.getImplementation())) {
for (URITemplate template : uriTemplates) {
template.setMediationScript(template.getAggregatedMediationScript());
}
}
api.setUriTemplates(uriTemplates);
// CORS . if null is returned, set default config from the configuration
if (api.getCorsConfiguration() == null) {
api.setCorsConfiguration(APIUtil.getDefaultCorsConfiguration());
}
// set category
List<APICategory> categories = api.getApiCategories();
if (categories != null) {
List<String> categoriesOfAPI = new ArrayList<String>();
for (APICategory apiCategory : categories) {
categoriesOfAPI.add(apiCategory.getName());
}
List<APICategory> categoryList = new ArrayList<>();
if (!categoriesOfAPI.isEmpty()) {
// category array retrieved from artifact has only the category name, therefore we need to fetch
// categories
// and fill out missing attributes before attaching the list to the api
List<APICategory> allCategories = APIUtil.getAllAPICategoriesOfOrganization(organization);
// todo-category: optimize this loop with breaks
for (String categoryName : categoriesOfAPI) {
for (APICategory category : allCategories) {
if (categoryName.equals(category.getName())) {
categoryList.add(category);
break;
}
}
}
}
api.setApiCategories(categoryList);
}
}
use of org.wso2.carbon.apimgt.rest.api.admin.dto.WorkflowDTO in project carbon-apimgt by wso2.
the class ApplicationRegistrationSimpleWorkflowExecutorTest method testFailureWhileExecutingApplicationRegistrationWorkFlow.
@Test
public void testFailureWhileExecutingApplicationRegistrationWorkFlow() throws APIManagementException {
PowerMockito.doNothing().when(apiMgtDAO).createApplicationRegistrationEntry(workflowDTO, false);
oAuthApplicationInfo.setJsonString("{\"client_credentials\":\"Client Credentials\"}");
Mockito.when(keyManager.createApplication(oAuthAppRequest)).thenThrow(new APIManagementException(""));
try {
applicationRegistrationSimpleWorkflowExecutor.execute(workflowDTO);
Assert.fail("Expected WorkflowException is not thrown while executing application registration simple " + "workflow");
} catch (WorkflowException e) {
Assert.assertTrue(e.getMessage().contains("Error occurred while executing SubscriberKeyMgtClient."));
}
}
use of org.wso2.carbon.apimgt.rest.api.admin.dto.WorkflowDTO in project carbon-apimgt by wso2.
the class SampleWorkFlowExecutor method execute.
@Override
public WorkflowResponse execute(WorkflowDTO workflowDTO) throws WorkflowException {
workflowDTO.setStatus(WorkflowStatus.APPROVED);
WorkflowResponse workflowResponse = complete(workflowDTO);
if (workflowDTO instanceof ApplicationRegistrationWorkflowDTO) {
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
((ApplicationRegistrationWorkflowDTO) workflowDTO).setApplicationInfo(oAuthApplicationInfo);
((ApplicationRegistrationWorkflowDTO) workflowDTO).setAccessTokenInfo(accessTokenInfo);
}
return workflowResponse;
}
Aggregations