use of org.wso2.carbon.apimgt.core.models.SubscriptionResponse in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsPost.
/**
* Adds a new subscription
*
* @param body Subscription details to be added
* @param request msf4j request object
* @return Newly added subscription as the response
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response subscriptionsPost(SubscriptionDTO body, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
SubscriptionDTO subscriptionDTO = null;
URI location = null;
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
String applicationId = body.getApplicationId();
String apiId = body.getApiIdentifier();
String tier = body.getPolicy();
Application application = apiStore.getApplicationByUuid(applicationId);
if (application != null && !ApplicationStatus.APPLICATION_APPROVED.equals(application.getStatus())) {
String errorMessage = "Application " + applicationId + " is not active";
ExceptionCodes exceptionCode = ExceptionCodes.APPLICATION_INACTIVE;
APIManagementException e = new APIManagementException(errorMessage, exceptionCode);
Map<String, String> paramList = new HashMap<>();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
if (application != null) {
SubscriptionResponse addSubResponse = apiStore.addApiSubscription(apiId, applicationId, tier);
String subscriptionId = addSubResponse.getSubscriptionUUID();
Subscription subscription = apiStore.getSubscriptionByUUID(subscriptionId);
location = new URI(RestApiConstants.RESOURCE_PATH_SUBSCRIPTION + "/" + subscriptionId);
subscriptionDTO = SubscriptionMappingUtil.fromSubscriptionToDTO(subscription);
// be in either pending or approved state) send back the workflow response
if (SubscriptionStatus.ON_HOLD == subscription.getStatus()) {
WorkflowResponseDTO workflowResponse = MiscMappingUtil.fromWorkflowResponseToDTO(addSubResponse.getWorkflowResponse());
return Response.status(Response.Status.ACCEPTED).header(RestApiConstants.LOCATION_HEADER, location).entity(workflowResponse).build();
}
} else {
String errorMessage = null;
ExceptionCodes exceptionCode = null;
exceptionCode = ExceptionCodes.APPLICATION_NOT_FOUND;
errorMessage = "Application not found";
APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, exceptionCode);
Map<String, String> paramList = new HashMap<>();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
} catch (GatewayException e) {
String errorMessage = "Failed to add subscription of API : " + body.getApiIdentifier() + " to gateway";
log.error(errorMessage, e);
return Response.status(Response.Status.ACCEPTED).build();
} catch (APIManagementException e) {
String errorMessage = "Error while adding subscriptions";
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, body.getApiIdentifier());
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, body.getApplicationId());
paramList.put(APIMgtConstants.ExceptionsConstants.TIER, body.getPolicy());
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 subscription : " + body.getSubscriptionId();
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.SUBSCRIPTION_ID, body.getSubscriptionId());
ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler, paramList);
log.error(errorMessage, e);
return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
}
return Response.status(Response.Status.CREATED).header(RestApiConstants.LOCATION_HEADER, location).entity(subscriptionDTO).build();
}
use of org.wso2.carbon.apimgt.core.models.SubscriptionResponse in project carbon-apimgt by wso2.
the class APIStoreImpl method addApiSubscription.
@Override
public SubscriptionResponse addApiSubscription(String apiId, String applicationId, String tier) throws APIManagementException {
SubscriptionResponse subScriptionResponse;
// Generate UUID for application
String subscriptionId = UUID.randomUUID().toString();
try {
API api = getAPIbyUUID(apiId);
Application application = getApplicationByUuid(applicationId);
if (application == null) {
String errorMsg = "Cannot find an application for given applicationId - " + applicationId;
log.error(errorMsg);
throw new APIManagementException(errorMsg, ExceptionCodes.APPLICATION_NOT_FOUND);
}
Policy policy = getPolicyDAO().getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, tier);
if (policy == null) {
String errorMsg = "Cannot find an subscription policy for given policy name - " + tier;
log.error(errorMsg);
throw new APIManagementException(errorMsg, ExceptionCodes.POLICY_NOT_FOUND);
}
getApiSubscriptionDAO().addAPISubscription(subscriptionId, apiId, applicationId, policy.getUuid(), APIMgtConstants.SubscriptionStatus.ON_HOLD);
WorkflowExecutor addSubscriptionWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
// Instead of quering the db, we create same subscription object
Subscription subscription = new Subscription(subscriptionId, application, api, policy);
subscription.setStatus(APIMgtConstants.SubscriptionStatus.ON_HOLD);
SubscriptionCreationWorkflow workflow = new SubscriptionCreationWorkflow(getApiSubscriptionDAO(), getWorkflowDAO(), getApiGateway());
workflow.setCreatedTime(LocalDateTime.now());
workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
workflow.setWorkflowReference(subscriptionId);
workflow.setWorkflowType(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
workflow.setSubscription(subscription);
workflow.setSubscriber(getUsername());
String workflowDescription = "API [ " + subscription.getApi().getName() + " - " + subscription.getApi().getVersion() + " ] subscription creation request from subscriber - " + getUsername() + " for the application - " + subscription.getApplication().getName() + "";
workflow.setWorkflowDescription(workflowDescription);
WorkflowResponse response = addSubscriptionWFExecutor.execute(workflow);
workflow.setStatus(response.getWorkflowStatus());
if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
completeWorkflow(addSubscriptionWFExecutor, workflow);
} else {
// only add entry to workflow table if it is a pending task
addWorkflowEntries(workflow);
}
subScriptionResponse = new SubscriptionResponse(subscriptionId, response);
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while adding api subscription for api - " + apiId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
return subScriptionResponse;
}
use of org.wso2.carbon.apimgt.core.models.SubscriptionResponse 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);
}
use of org.wso2.carbon.apimgt.core.models.SubscriptionResponse in project carbon-apimgt by wso2.
the class APIStoreImplTestCase method testAddSubscriptionForInvalidApplication.
@Test(description = "Add subscription without a valid app", expectedExceptions = APIManagementException.class)
public void testAddSubscriptionForInvalidApplication() 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);
APIStore apiStore = getApiStoreImpl(apiDAO, applicationDAO, apiSubscriptionDAO, workflowDAO, apiGateway);
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI();
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(null);
SubscriptionResponse subscriptionResponse = apiStore.addApiSubscription(apiId, UUID, TIER);
String subscriptionId = subscriptionResponse.getSubscriptionUUID();
Assert.assertNotNull(subscriptionId);
// subscription should not be added
Mockito.verify(apiSubscriptionDAO, Mockito.times(0)).addAPISubscription(subscriptionId, apiId, UUID, TIER, APIMgtConstants.SubscriptionStatus.ON_HOLD);
}
use of org.wso2.carbon.apimgt.core.models.SubscriptionResponse in project carbon-apimgt by wso2.
the class APIStoreImplTestCase method testAddSubscription.
@Test(description = "Add subscription to an application")
public void testAddSubscription() 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);
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, TIER)).thenReturn(policy);
APIStore apiStore = getApiStoreImpl(apiDAO, applicationDAO, apiSubscriptionDAO, workflowDAO, apiGateway, policyDAO);
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 subscriptionResponse = apiStore.addApiSubscription(apiId, UUID, TIER);
String subscriptionId = subscriptionResponse.getSubscriptionUUID();
Assert.assertNotNull(subscriptionId);
// before workflow add subscription with blocked state
Mockito.verify(apiSubscriptionDAO, Mockito.times(1)).addAPISubscription(subscriptionId, apiId, UUID, policy.getUuid(), APIMgtConstants.SubscriptionStatus.ON_HOLD);
// after workflow change the state
Mockito.verify(apiSubscriptionDAO, Mockito.times(1)).updateSubscriptionStatus(subscriptionId, APIMgtConstants.SubscriptionStatus.ACTIVE);
}
Aggregations