use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetIcon.
@Test
public void testGetIcon() throws APIManagementException, org.wso2.carbon.user.api.UserStoreException, RegistryException {
APIIdentifier identifier = new APIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
int tenantId = -1234;
UserRegistry registry = Mockito.mock(UserRegistry.class);
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapperExtended(genericArtifactManager, registryService, registry, tenantManager);
Mockito.when(tenantManager.getTenantId(Mockito.anyString())).thenThrow(UserStoreException.class).thenReturn(tenantId);
try {
abstractAPIManager.getIcon(identifier);
Assert.fail("User store exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Error while loading API icon of API"));
}
Mockito.when(registryService.getGovernanceSystemRegistry(Mockito.anyInt())).thenThrow(RegistryException.class).thenReturn(registry);
try {
abstractAPIManager.getIcon(identifier);
Assert.fail("User store exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Error while loading API icon of API"));
}
Assert.assertNull(abstractAPIManager.getIcon(identifier));
AbstractAPIManager abstractAPIManager1 = new AbstractAPIManagerWrapper(genericArtifactManager, registryService, registry, tenantManager);
Mockito.when(registryService.getGovernanceUserRegistry(Mockito.anyString(), Mockito.anyInt())).thenReturn(registry);
Assert.assertNull(abstractAPIManager1.getIcon(identifier));
abstractAPIManager1.tenantDomain = SAMPLE_TENANT_DOMAIN_1;
Mockito.when(registry.resourceExists(Mockito.anyString())).thenReturn(true);
Resource resource = new ResourceImpl();
resource.setContent("sample conetent");
resource.setMediaType("api");
Mockito.when(registry.get(Mockito.anyString())).thenReturn(resource);
Assert.assertTrue(abstractAPIManager1.getIcon(identifier).getContentType().equals("api"));
}
use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.
the class UserAwareAPIProviderTest method testDeleteApiSpecificMediationPolicy.
/**
* This method checks the behaviour of Delete API specific mediation policy method for a non-existing mediation
* policy.
*
* @throws APIManagementException API Management Exception.
*/
@Test
public void testDeleteApiSpecificMediationPolicy() throws APIManagementException {
Identifier identifier = Mockito.mock(Identifier.class);
Assert.assertFalse(userAwareAPIProvider.deleteApiSpecificMediationPolicy(identifier, "test", "test"));
}
use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetSwaggerDefinitionTimeStamps.
@Test
public void testGetSwaggerDefinitionTimeStamps() throws Exception {
APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
UserRegistry registry = Mockito.mock(UserRegistry.class);
Mockito.when(tenantManager.getTenantId(Mockito.anyString())).thenThrow(UserStoreException.class).thenReturn(-1234);
PowerMockito.mockStatic(OASParserUtil.class);
Mockito.when(registryService.getGovernanceUserRegistry(Mockito.anyString(), Mockito.anyInt())).thenThrow(RegistryException.class).thenReturn(registry);
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(null, registryService, registry, tenantManager);
Assert.assertNull(abstractAPIManager.getSwaggerDefinitionTimeStamps(identifier));
Assert.assertNull(abstractAPIManager.getSwaggerDefinitionTimeStamps(identifier));
abstractAPIManager.tenantDomain = SAMPLE_TENANT_DOMAIN_1;
Map<String, String> result = new HashMap<String, String>();
result.put("swagger1", "scopes:apim_create,resources:{get:/*}");
result.put("swagger2", "scopes:apim_view,resources:{get:/menu}");
// Mockito.when(apiDefinitionFromOpenAPISpec.getAPIOpenAPIDefinitionTimeStamps((APIIdentifier) Mockito.any(),
// (org.wso2.carbon.registry.api.Registry) Mockito.any())).thenReturn(result);
// Assert.assertEquals(abstractAPIManager.getSwaggerDefinitionTimeStamps(identifier).size(),2);
// abstractAPIManager.tenantDomain = SAMPLE_TENANT_DOMAIN;
// result.put("swagger3","");
// Assert.assertEquals(abstractAPIManager.getSwaggerDefinitionTimeStamps(identifier).size(),3);
}
use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.
the class APIProviderImpl method checkIfAPIExists.
/**
* Function returns true if the specified API already exists in the registry
*
* @param identifier
* @return
* @throws APIManagementException
*/
public boolean checkIfAPIExists(APIIdentifier identifier) throws APIManagementException {
String apiPath = APIUtil.getAPIPath(identifier);
try {
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
Registry registry;
if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
int id = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(id);
} else {
if (this.tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(this.tenantDomain)) {
registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceUserRegistry(identifier.getProviderName(), MultitenantConstants.SUPER_TENANT_ID);
} else {
if (this.tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(this.tenantDomain)) {
registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceUserRegistry(identifier.getProviderName(), MultitenantConstants.SUPER_TENANT_ID);
} else {
registry = this.registry;
}
}
}
return registry.resourceExists(apiPath);
} catch (RegistryException e) {
handleException("Failed to get API from : " + apiPath, e);
return false;
} catch (UserStoreException e) {
handleException("Failed to get API from : " + apiPath, e);
return false;
}
}
use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.
the class APIProviderImpl method deleteAPIProduct.
public void deleteAPIProduct(APIProduct apiProduct) throws APIManagementException {
APIProductIdentifier identifier = apiProduct.getId();
try {
// int apiId = apiMgtDAO.getAPIID(identifier, null);
long subsCount = apiMgtDAO.getAPISubscriptionCountByAPI(identifier);
if (subsCount > 0) {
// Logging as a WARN since this isn't an error scenario.
String message = "Cannot remove the API Product as active subscriptions exist.";
log.warn(message);
throw new APIManagementException(message);
}
// gatewayType check is required when API Management is deployed on
// other servers to avoid synapse
deleteAPIProductRevisions(apiProduct.getUuid(), apiProduct.getOrganization());
apiPersistenceInstance.deleteAPIProduct(new Organization(apiProduct.getOrganization()), apiProduct.getUuid());
apiMgtDAO.deleteAPIProduct(identifier);
cleanUpPendingAPIStateChangeTask(apiProduct.getProductId(), true);
if (log.isDebugEnabled()) {
String logMessage = "API Product Name: " + identifier.getName() + ", API Product Version " + identifier.getVersion() + " successfully removed from the database.";
log.debug(logMessage);
}
JSONObject apiLogObject = new JSONObject();
apiLogObject.put(APIConstants.AuditLogConstants.NAME, identifier.getName());
apiLogObject.put(APIConstants.AuditLogConstants.VERSION, identifier.getVersion());
apiLogObject.put(APIConstants.AuditLogConstants.PROVIDER, identifier.getProviderName());
APIUtil.logAuditMessage(APIConstants.AuditLogConstants.API_PRODUCT, apiLogObject.toString(), APIConstants.AuditLogConstants.DELETED, this.username);
GatewayArtifactsMgtDAO.getInstance().deleteGatewayArtifacts(apiProduct.getUuid());
} catch (APIPersistenceException e) {
handleException("Failed to remove the API product", e);
} catch (WorkflowException e) {
handleException("Error while removing the pending workflows of API Product", e);
}
}
Aggregations