use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionList in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImplTestCase method testSubscriptionsGetfromApplication.
@Test
public void testSubscriptionsGetfromApplication() throws APIManagementException, NotFoundException {
TestUtil.printTestMethodName();
String applicationId = UUID.randomUUID().toString();
String subsID1 = UUID.randomUUID().toString();
String subsID2 = UUID.randomUUID().toString();
SubscriptionsApiServiceImpl subscriptionsApiService = new SubscriptionsApiServiceImpl();
APIStore apiStore = Mockito.mock(APIStoreImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
Request request = TestUtil.getRequest();
PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
Application application = SampleTestObjectCreator.createDefaultApplication();
List<Subscription> subscriptionList = new ArrayList<>();
subscriptionList.add(SampleTestObjectCreator.createSubscription(subsID1));
subscriptionList.add(SampleTestObjectCreator.createSubscription(subsID2));
Mockito.when(apiStore.getApplicationByUuid(applicationId)).thenReturn(application);
Mockito.when(apiStore.getAPISubscriptionsByApplication(application, ApiType.STANDARD)).thenReturn(subscriptionList);
Response response = subscriptionsApiService.subscriptionsGet(null, applicationId, ApiType.STANDARD.toString(), 0, 10, null, request);
Assert.assertEquals(200, response.getStatus());
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionList in project carbon-apimgt by wso2.
the class SubscriptionDataLoaderImpl method getSubscriptionById.
@Override
public Subscription getSubscriptionById(String apiId, String appId) throws DataLoadingException {
String endPoint = APIConstants.SubscriptionValidationResources.SUBSCRIPTIONS + "?apiId=" + apiId + "&appId=" + appId;
Subscription subscription = null;
String responseString;
try {
responseString = invokeService(endPoint, null);
} catch (IOException e) {
String msg = "Error while executing the http client " + endPoint;
log.error(msg, e);
throw new DataLoadingException(msg, e);
}
if (responseString != null && !responseString.isEmpty()) {
SubscriptionList list = new Gson().fromJson(responseString, SubscriptionList.class);
if (list.getList() != null && !list.getList().isEmpty()) {
subscription = list.getList().get(0);
}
}
return subscription;
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionList in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsGet.
@Override
public Response subscriptionsGet(String xWSO2Tenant, Integer apiId, Integer appId, String apiUUID, String applicationUUID, MessageContext messageContext) throws APIManagementException {
Response result;
SubscriptionValidationDAO subscriptionValidationDAO = new SubscriptionValidationDAO();
List<Subscription> subscriptionList = new ArrayList<>();
xWSO2Tenant = SubscriptionValidationDataUtil.validateTenantDomain(xWSO2Tenant, messageContext);
if (StringUtils.isNotEmpty(applicationUUID) && StringUtils.isNotEmpty(apiUUID)) {
Subscription subscription = subscriptionValidationDAO.getSubscription(apiUUID, applicationUUID);
if (subscription != null) {
subscriptionList.add(subscription);
}
result = Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionToSubscriptionListDTO(subscriptionList)).build();
} else if (apiId != null && appId != null) {
Subscription subscription = subscriptionValidationDAO.getSubscription(apiId, appId);
if (subscription != null) {
subscriptionList.add(subscription);
}
result = Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionToSubscriptionListDTO(subscriptionList)).build();
} else if (StringUtils.isNotEmpty(xWSO2Tenant)) {
result = Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionToSubscriptionListDTO(subscriptionValidationDAO.getAllSubscriptions(xWSO2Tenant))).build();
} else {
result = Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionToSubscriptionListDTO(subscriptionValidationDAO.getAllSubscriptions())).build();
}
return result;
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionList in project carbon-apimgt by wso2.
the class APIStateChangeWorkflow method updateAPIStatusForWorkflowComplete.
private void updateAPIStatusForWorkflowComplete(String apiId, String status, String updatedBy, LocalDateTime time) throws APIManagementException {
boolean requireReSubscriptions = false;
boolean deprecateOlderVersion = false;
try {
API api = apiDAO.getAPI(apiId);
API.APIBuilder apiBuilder = new API.APIBuilder(api);
apiBuilder.lastUpdatedTime(time);
apiBuilder.updatedBy(updatedBy);
LifecycleState currentState = apiLifecycleManager.getLifecycleDataForState(apiBuilder.getLifecycleInstanceId(), apiBuilder.getLifeCycleStatus());
apiBuilder.lifecycleState(currentState);
if (APIMgtConstants.APILCWorkflowStatus.PENDING.toString().equals(api.getWorkflowStatus())) {
apiBuilder.workflowStatus(APIMgtConstants.APILCWorkflowStatus.APPROVED.toString());
apiDAO.updateAPIWorkflowStatus(apiId, APIMgtConstants.APILCWorkflowStatus.APPROVED);
}
List<CheckItemBean> list = currentState.getCheckItemBeanList();
for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
CheckItemBean checkItemBean = (CheckItemBean) iterator.next();
if (APIMgtConstants.DEPRECATE_PREVIOUS_VERSIONS.equals(checkItemBean.getName())) {
deprecateOlderVersion = checkItemBean.isValue();
} else if (APIMgtConstants.REQUIRE_RE_SUBSCRIPTIONS.equals(checkItemBean.getName())) {
requireReSubscriptions = checkItemBean.isValue();
}
}
API originalAPI = apiBuilder.build();
apiLifecycleManager.executeLifecycleEvent(api.getLifeCycleStatus(), status, apiBuilder.getLifecycleInstanceId(), updatedBy, originalAPI);
if (deprecateOlderVersion) {
if (StringUtils.isNotEmpty(api.getCopiedFromApiId())) {
API oldAPI = apiDAO.getAPI(api.getCopiedFromApiId());
if (oldAPI != null) {
API.APIBuilder previousAPI = new API.APIBuilder(oldAPI);
previousAPI.setLifecycleStateInfo(apiLifecycleManager.getLifecycleDataForState(previousAPI.getLifecycleInstanceId(), previousAPI.getLifeCycleStatus()));
if (APIUtils.validateTargetState(previousAPI.getLifecycleState(), APIStatus.DEPRECATED.getStatus())) {
apiLifecycleManager.executeLifecycleEvent(previousAPI.getLifeCycleStatus(), APIStatus.DEPRECATED.getStatus(), previousAPI.getLifecycleInstanceId(), updatedBy, previousAPI.build());
}
}
}
}
if (!requireReSubscriptions) {
if (StringUtils.isNotEmpty(api.getCopiedFromApiId())) {
List<Subscription> subscriptions = apiSubscriptionDAO.getAPISubscriptionsByAPI(api.getCopiedFromApiId());
List<Subscription> subscriptionList = new ArrayList<>();
for (Subscription subscription : subscriptions) {
if (api.getPolicies().contains(subscription.getPolicy())) {
if (!APIMgtConstants.SubscriptionStatus.ON_HOLD.equals(subscription.getStatus())) {
subscriptionList.add(new Subscription(UUID.randomUUID().toString(), subscription.getApplication(), subscription.getApi(), subscription.getPolicy()));
}
}
apiSubscriptionDAO.copySubscriptions(subscriptionList);
}
}
}
// publish API state change to gateway
apiGateway.changeAPIState(originalAPI, status);
} catch (APIMgtDAOException e) {
String errorMsg = "Couldn't change the status of api ID " + apiId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
} catch (LifecycleException e) {
String errorMsg = "Couldn't change the status of api ID " + apiId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_LIFECYCLE_EXCEPTION);
} catch (GatewayException e) {
String message = "Error occurred while changing the state of api ID: " + apiId + " to " + status + "in gateway";
log.error(message, e);
throw new APIManagementException(message, ExceptionCodes.GATEWAY_EXCEPTION);
}
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionList in project carbon-apimgt by wso2.
the class APISubscriptionDAOImpl method createSubscriptionsWithApiInformationOnly.
private List<Subscription> createSubscriptionsWithApiInformationOnly(ResultSet rs) throws APIMgtDAOException {
List<Subscription> subscriptionList = new ArrayList<>();
try {
Subscription subscription;
while (rs.next()) {
String subscriptionId = rs.getString("SUBS_UUID");
String subscriptionTier = rs.getString("SUBS_POLICY");
API.APIBuilder apiBuilder = new API.APIBuilder(rs.getString("API_PROVIDER"), rs.getString("API_NAME"), rs.getString("API_VERSION"));
apiBuilder.id(rs.getString("API_ID"));
apiBuilder.context(rs.getString("API_CONTEXT"));
API api = apiBuilder.build();
subscription = new Subscription(subscriptionId, null, api, new SubscriptionPolicy(subscriptionTier));
subscription.setStatus(APIMgtConstants.SubscriptionStatus.valueOf(rs.getString("SUB_STATUS")));
subscriptionList.add(subscription);
}
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "creating subscriptions api information", e);
}
return subscriptionList;
}
Aggregations