use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testRemoveSubscription.
@Test
public void testRemoveSubscription() throws APIManagementException, WorkflowException, APIPersistenceException {
String uuid = UUID.randomUUID().toString();
String apiUUID = UUID.randomUUID().toString();
Subscriber subscriber = new Subscriber("sub1");
Application application = new Application("app1", subscriber);
application.setId(1);
APIIdentifier identifier = new APIIdentifier(API_PROVIDER, SAMPLE_API_NAME, SAMPLE_API_VERSION);
identifier.setUuid(apiUUID);
SubscribedAPI subscribedAPIOld = new SubscribedAPI(subscriber, identifier);
subscribedAPIOld.setApplication(application);
Mockito.when(apiMgtDAO.isAppAllowed(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString())).thenReturn(true);
PowerMockito.mockStatic(ApiMgtDAO.class);
PowerMockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
Mockito.when(apiMgtDAO.checkAPIUUIDIsARevisionUUID(Mockito.anyString())).thenReturn(null);
DevPortalAPI devPortalAPI = Mockito.mock(DevPortalAPI.class);
Mockito.when(apiPersistenceInstance.getDevPortalAPI(any(Organization.class), any(String.class))).thenReturn(devPortalAPI);
SubscribedAPI subscribedAPINew = new SubscribedAPI(subscriber, identifier);
subscribedAPINew.setUUID(uuid);
subscribedAPINew.setApplication(application);
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO, apiPersistenceInstance);
apiConsumer.removeSubscription(subscribedAPINew, "org1");
Mockito.verify(apiMgtDAO, Mockito.times(1)).getApplicationNameFromId(Mockito.anyInt());
String workflowExtRef = "test_wf_ref";
String workflowExtRef1 = "complete_wf_ref";
Mockito.when(apiMgtDAO.getExternalWorkflowReferenceForSubscription((APIIdentifier) Mockito.any(), Mockito.anyInt(), Mockito.anyString())).thenReturn(workflowExtRef, workflowExtRef1);
SubscriptionWorkflowDTO subscriptionWorkflowDTO = new SubscriptionWorkflowDTO();
subscriptionWorkflowDTO.setWorkflowReference("1");
Mockito.when(apiMgtDAO.retrieveWorkflow(workflowExtRef)).thenReturn(subscriptionWorkflowDTO);
SubscribedAPI subscribedAPI = new SubscribedAPI("api1");
subscribedAPI.setTier(new Tier("Gold"));
Mockito.when(apiMgtDAO.getSubscriptionById(Mockito.anyInt())).thenReturn(subscribedAPI);
Mockito.when(apiMgtDAO.getSubscriptionStatus(uuid, 1)).thenReturn(APIConstants.SubscriptionStatus.ON_HOLD);
apiConsumer.removeSubscription(subscribedAPINew, "org1");
Mockito.when(apiMgtDAO.retrieveWorkflow(workflowExtRef1)).thenReturn(subscriptionWorkflowDTO);
PowerMockito.when(MultitenantUtils.getTenantDomain(Mockito.anyString())).thenReturn("abc.org");
apiConsumer.removeSubscription(subscribedAPINew, "org1");
Mockito.verify(apiMgtDAO, Mockito.times(2)).retrieveWorkflow(Mockito.anyString());
}
use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.
the class APIProviderImpl method updateAPIforStateChange.
public boolean updateAPIforStateChange(API api, String currentStatus, String newStatus) throws APIManagementException, FaultGatewaysException {
boolean isSuccess = false;
String provider = api.getId().getProviderName();
String providerTenantMode = api.getId().getProviderName();
provider = APIUtil.replaceEmailDomain(provider);
String name = api.getId().getApiName();
String version = api.getId().getVersion();
boolean isTenantFlowStarted = false;
try {
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(providerTenantMode));
if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
isTenantFlowStarted = true;
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
}
// API api = getAPI(apiId);
if (api != null) {
if (!currentStatus.equals(newStatus)) {
api.setStatus(newStatus);
// If API status changed to publish we should add it to recently added APIs list
// this should happen in store-publisher cluster domain if deployment is distributed
// IF new API published we will add it to recently added APIs
Caching.getCacheManager(APIConstants.API_MANAGER_CACHE_MANAGER).getCache(APIConstants.RECENTLY_ADDED_API_CACHE_NAME).removeAll();
api.setAsPublishedDefaultVersion(api.getId().getVersion().equals(apiMgtDAO.getPublishedDefaultVersion(api.getId())));
if (APIConstants.RETIRED.equals(newStatus)) {
cleanUpPendingSubscriptionCreationProcessesByAPI(api.getUuid());
apiMgtDAO.removeAllSubscriptions(api.getUuid());
deleteAPIRevisions(api.getUuid(), tenantDomain);
}
// updateApiArtifactNew(api, false, false);
PublisherAPI publisherAPI = APIMapper.INSTANCE.toPublisherApi(api);
try {
apiPersistenceInstance.updateAPI(new Organization(api.getOrganization()), publisherAPI);
} catch (APIPersistenceException e) {
handleException("Error while persisting the updated API ", e);
}
}
isSuccess = true;
} else {
handleException("Couldn't find an API with the name-" + name + "version-" + version);
}
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
return isSuccess;
}
use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.
the class APIProviderImpl method searchPaginatedAPIs.
@Override
public Map<String, Object> searchPaginatedAPIs(String searchQuery, String organization, int start, int end, String sortBy, String sortOrder) throws APIManagementException {
Map<String, Object> result = new HashMap<String, Object>();
if (log.isDebugEnabled()) {
log.debug("Original search query received : " + searchQuery);
}
Organization org = new Organization(organization);
String[] roles = APIUtil.getFilteredUserRoles(userNameWithoutChange);
Map<String, Object> properties = APIUtil.getUserProperties(userNameWithoutChange);
UserContext userCtx = new UserContext(userNameWithoutChange, org, properties, roles);
try {
PublisherAPISearchResult searchAPIs = apiPersistenceInstance.searchAPIsForPublisher(org, searchQuery, start, end, userCtx, sortBy, sortOrder);
if (log.isDebugEnabled()) {
log.debug("searched APIs for query : " + searchQuery + " :-->: " + searchAPIs.toString());
}
Set<Object> apiSet = new LinkedHashSet<>();
if (searchAPIs != null) {
List<PublisherAPIInfo> list = searchAPIs.getPublisherAPIInfoList();
List<Object> apiList = new ArrayList<>();
for (PublisherAPIInfo publisherAPIInfo : list) {
API mappedAPI = APIMapper.INSTANCE.toApi(publisherAPIInfo);
populateAPIStatus(mappedAPI);
populateDefaultVersion(mappedAPI);
apiList.add(mappedAPI);
}
apiSet.addAll(apiList);
result.put("apis", apiSet);
result.put("length", searchAPIs.getTotalAPIsCount());
result.put("isMore", true);
} else {
result.put("apis", apiSet);
result.put("length", 0);
result.put("isMore", false);
}
} catch (APIPersistenceException e) {
throw new APIManagementException("Error while searching the api ", e);
}
return result;
}
use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.
the class APIConsumerImpl method getAPIorAPIProductByUUID.
@Override
public ApiTypeWrapper getAPIorAPIProductByUUID(String uuid, String organization) throws APIManagementException {
try {
Organization org = new Organization(organization);
DevPortalAPI devPortalApi = apiPersistenceInstance.getDevPortalAPI(org, uuid);
if (devPortalApi != null) {
checkVisibilityPermission(userNameWithoutChange, devPortalApi.getVisibility(), devPortalApi.getVisibleRoles());
if (APIConstants.API_PRODUCT.equalsIgnoreCase(devPortalApi.getType())) {
APIProduct apiProduct = APIMapper.INSTANCE.toApiProduct(devPortalApi);
apiProduct.setID(new APIProductIdentifier(devPortalApi.getProviderName(), devPortalApi.getApiName(), devPortalApi.getVersion()));
populateAPIProductInformation(uuid, organization, apiProduct);
populateAPIStatus(apiProduct);
apiProduct = addTiersToAPI(apiProduct, organization);
return new ApiTypeWrapper(apiProduct);
} else {
API api = APIMapper.INSTANCE.toApi(devPortalApi);
populateDevPortalAPIInformation(uuid, organization, api);
populateDefaultVersion(api);
populateAPIStatus(api);
api = addTiersToAPI(api, organization);
return new ApiTypeWrapper(api);
}
} else {
String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist";
throw new APIMgtResourceNotFoundException(msg);
}
} catch (APIPersistenceException | OASPersistenceException | ParseException e) {
String msg = "Failed to get API";
throw new APIManagementException(msg, e);
}
}
use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.
the class APIConsumerImpl method getLightweightAPIByUUID.
/**
* Get minimal details of API by registry artifact id
*
* @param uuid Registry artifact id
* @param organization identifier of the organization
* @return API of the provided artifact id
* @throws APIManagementException
*/
@Override
public API getLightweightAPIByUUID(String uuid, String organization) throws APIManagementException {
try {
Organization org = new Organization(organization);
DevPortalAPI devPortalApi = apiPersistenceInstance.getDevPortalAPI(org, uuid);
if (devPortalApi != null) {
checkVisibilityPermission(userNameWithoutChange, devPortalApi.getVisibility(), devPortalApi.getVisibleRoles());
API api = APIMapper.INSTANCE.toApi(devPortalApi);
// / populate relavant external info
// environment
String environmentString = null;
if (api.getEnvironments() != null) {
environmentString = String.join(",", api.getEnvironments());
}
api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environmentString, organization));
// CORS . if null is returned, set default config from the configuration
if (api.getCorsConfiguration() == null) {
api.setCorsConfiguration(APIUtil.getDefaultCorsConfiguration());
}
return api;
} else {
String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist";
throw new APIMgtResourceNotFoundException(msg);
}
} catch (APIPersistenceException e) {
String msg = "Failed to get API with uuid " + uuid;
throw new APIManagementException(msg, e);
}
}
Aggregations