use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class APIUtilTest method testGetAPIWithGovernanceArtifact.
@Test
public void testGetAPIWithGovernanceArtifact() throws Exception {
System.setProperty("carbon.home", APIUtilTest.class.getResource("/").getFile());
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
API expectedAPI = getUniqueAPI();
final String provider = expectedAPI.getId().getProviderName();
final String tenantDomain = org.wso2.carbon.utils.multitenancy.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
final int tenantId = -1234;
System.setProperty("carbon.home", "");
File siteConfFile = new File(Thread.currentThread().getContextClassLoader().getResource("tenant-conf.json").getFile());
String tenantConfValue = FileUtils.readFileToString(siteConfFile);
GovernanceArtifact artifact = Mockito.mock(GovernanceArtifact.class);
Registry registry = Mockito.mock(Registry.class);
ApiMgtDAO apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
Resource resource = Mockito.mock(Resource.class);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
RealmService realmService = Mockito.mock(RealmService.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
ThrottleProperties throttleProperties = Mockito.mock(ThrottleProperties.class);
SubscriptionPolicy policy = Mockito.mock(SubscriptionPolicy.class);
SubscriptionPolicy[] policies = new SubscriptionPolicy[] { policy };
QuotaPolicy quotaPolicy = Mockito.mock(QuotaPolicy.class);
RequestCountLimit limit = Mockito.mock(RequestCountLimit.class);
PrivilegedCarbonContext carbonContext = Mockito.mock(PrivilegedCarbonContext.class);
RegistryService registryService = Mockito.mock(RegistryService.class);
UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
PowerMockito.mockStatic(ApiMgtDAO.class);
PowerMockito.mockStatic(GovernanceUtils.class);
PowerMockito.mockStatic(MultitenantUtils.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
Mockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
Mockito.when(apiMgtDAO.getAPIID(Mockito.any(String.class))).thenReturn(123);
Mockito.when(apiMgtDAO.getPolicyNames(PolicyConstants.POLICY_LEVEL_SUB, provider)).thenReturn(new String[] { "Unlimited" });
Mockito.when(artifact.getId()).thenReturn("");
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER)).thenReturn(provider);
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_CACHE_TIMEOUT)).thenReturn("15");
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_TIER)).thenReturn("Unlimited");
Mockito.when(MultitenantUtils.getTenantDomain(provider)).thenReturn(tenantDomain);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
APIMConfigService apimConfigService = Mockito.mock(APIMConfigService.class);
Mockito.when(serviceReferenceHolder.getApimConfigService()).thenReturn(apimConfigService);
Mockito.when(apimConfigService.getTenantConfig(tenantDomain)).thenReturn(tenantConfValue);
Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
Mockito.when(tenantManager.getTenantId(tenantDomain)).thenReturn(tenantId);
Mockito.when(registryService.getConfigSystemRegistry(tenantId)).thenReturn(userRegistry);
String artifactPath = "";
Mockito.when(GovernanceUtils.getArtifactPath(registry, "")).thenReturn(artifactPath);
Mockito.when(registry.get(artifactPath)).thenReturn(resource);
Mockito.when(resource.getLastModified()).thenReturn(expectedAPI.getLastUpdated());
Mockito.when(resource.getCreatedTime()).thenReturn(expectedAPI.getLastUpdated());
Mockito.when(resource.getContent()).thenReturn(tenantConfValue.getBytes());
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
Mockito.when(apiManagerConfiguration.getThrottleProperties()).thenReturn(throttleProperties);
Mockito.when(apiMgtDAO.getSubscriptionPolicies(tenantId)).thenReturn(policies);
Mockito.when(policy.getDefaultQuotaPolicy()).thenReturn(quotaPolicy);
Mockito.when(quotaPolicy.getLimit()).thenReturn(limit);
Mockito.when(registry.getTags(artifactPath)).thenReturn(getTagsFromSet(expectedAPI.getTags()));
ArrayList<URITemplate> urlList = getURLTemplateList(expectedAPI.getUriTemplates());
Mockito.when(apiMgtDAO.getAllURITemplates(Mockito.anyString(), Mockito.anyString())).thenReturn(urlList);
CORSConfiguration corsConfiguration = expectedAPI.getCorsConfiguration();
Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.CORS_CONFIGURATION_ACCESS_CTL_ALLOW_HEADERS)).thenReturn(corsConfiguration.getAccessControlAllowHeaders().toString());
Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.CORS_CONFIGURATION_ACCESS_CTL_ALLOW_METHODS)).thenReturn(corsConfiguration.getAccessControlAllowMethods().toString());
Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.CORS_CONFIGURATION_ACCESS_CTL_ALLOW_ORIGIN)).thenReturn(corsConfiguration.getAccessControlAllowOrigins().toString());
API api = APIUtil.getAPI(artifact);
Assert.assertNotNull(api);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class APIUtilTest method testGetCustomInSequence.
@Test
public void testGetCustomInSequence() throws Exception {
APIIdentifier apiIdentifier = Mockito.mock(APIIdentifier.class);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
RegistryService registryService = Mockito.mock(RegistryService.class);
UserRegistry registry = Mockito.mock(UserRegistry.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(registryService.getGovernanceSystemRegistry(eq(1))).thenReturn(registry);
Collection collection = Mockito.mock(Collection.class);
String artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getApiName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getVersion();
String path = artifactPath + RegistryConstants.PATH_SEPARATOR + "in" + RegistryConstants.PATH_SEPARATOR;
Mockito.when(registry.get(eq(path))).thenReturn(collection);
String[] childPaths = { "test" };
Mockito.when(collection.getChildren()).thenReturn(childPaths);
InputStream sampleSequence = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("sampleSequence.xml").getFile());
Resource resource = Mockito.mock(Resource.class);
Mockito.when(registry.get(eq("test"))).thenReturn(resource);
Mockito.when(resource.getContentStream()).thenReturn(sampleSequence);
OMElement customSequence = APIUtil.getCustomSequence("sample", 1, "in", apiIdentifier);
Assert.assertNotNull(customSequence);
sampleSequence.close();
}
use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class APIUtilTest method testGetArtifactManager.
@Test
public void testGetArtifactManager() {
PowerMockito.mockStatic(GenericArtifactManager.class);
System.setProperty("carbon.home", APIUtilTest.class.getResource("/").getFile());
Registry registry = Mockito.mock(UserRegistry.class);
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
PowerMockito.mockStatic(GovernanceUtils.class);
PowerMockito.doNothing().when(GovernanceUtils.class, "loadGovernanceArtifacts", (UserRegistry) registry);
Mockito.when(GovernanceUtils.findGovernanceArtifactConfiguration(APIConstants.API_KEY, registry)).thenReturn(Mockito.mock(GovernanceArtifactConfiguration.class)).thenReturn(null).thenThrow(RegistryException.class);
GenericArtifactManager genericArtifactManager = Mockito.mock(GenericArtifactManager.class);
PowerMockito.whenNew(GenericArtifactManager.class).withArguments(registry, APIConstants.API_KEY).thenReturn(genericArtifactManager);
GenericArtifactManager retrievedGenericArtifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
Assert.assertEquals(genericArtifactManager, retrievedGenericArtifactManager);
APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
Assert.fail();
} catch (APIManagementException ex) {
Assert.assertTrue(ex.getMessage().contains("Failed to initialize GenericArtifactManager"));
} catch (org.wso2.carbon.registry.core.exceptions.RegistryException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class GatewayUtils method deleteRegistryProperty.
/**
* Delete the given registry property from the given tenant registry path
*
* @param propertyName property name
* @param path resource path
* @param tenantDomain
* @throws AxisFault
*/
public static void deleteRegistryProperty(String propertyName, String path, String tenantDomain) throws AxisFault {
try {
UserRegistry registry = getRegistry(tenantDomain);
PrivilegedCarbonContext.startTenantFlow();
if (tenantDomain != null && StringUtils.isNotEmpty(tenantDomain)) {
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
} else {
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true);
}
Resource resource = registry.get(path);
if (resource != null && resource.getProperty(propertyName) != null) {
resource.removeProperty(propertyName);
registry.put(resource.getPath(), resource);
resource.discard();
}
} catch (RegistryException | APIManagementException e) {
String msg = "Failed to delete secure endpoint password alias " + e.getMessage();
throw new AxisFault(msg, e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class GatewayUtils method setRegistryProperty.
/**
* Add/Update the given registry property from the given tenant registry
* path
*
* @param propertyName property name
* @param propertyValue property value
* @param path resource path
* @param tenantDomain
* @throws APIManagementException
*/
public static void setRegistryProperty(String propertyName, String propertyValue, String path, String tenantDomain) throws APIManagementException {
UserRegistry registry = getRegistry(tenantDomain);
PrivilegedCarbonContext.startTenantFlow();
if (tenantDomain != null && StringUtils.isNotEmpty(tenantDomain)) {
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
} else {
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true);
}
try {
Resource resource = registry.get(path);
// add or update property
if (resource.getProperty(propertyName) != null) {
resource.setProperty(propertyName, propertyValue);
} else {
resource.addProperty(propertyName, propertyValue);
}
registry.put(resource.getPath(), resource);
resource.discard();
} catch (RegistryException e) {
throw new APIManagementException("Error while reading registry resource " + path + " for tenant " + tenantDomain);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
Aggregations