use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIKeyValidator method mapToAPIInfo.
private APIInfoDTO mapToAPIInfo(ArrayList<URITemplate> uriTemplates, String context, String apiVersion) {
APIInfoDTO apiInfoDTO = new APIInfoDTO();
apiInfoDTO.setApiName(context);
apiInfoDTO.setContext(context);
apiInfoDTO.setVersion(apiVersion);
apiInfoDTO.setResources(new LinkedHashSet<ResourceInfoDTO>());
ResourceInfoDTO resourceInfoDTO = null;
VerbInfoDTO verbInfoDTO = null;
// The following map is used to retrieve already created ResourceInfoDTO rather than iterating -
// the resource Set in apiInfoDTO.
LinkedHashMap<String, ResourceInfoDTO> resourcesMap = new LinkedHashMap<String, ResourceInfoDTO>();
for (URITemplate uriTemplate : uriTemplates) {
resourceInfoDTO = resourcesMap.get(uriTemplate.getUriTemplate());
if (null == resourceInfoDTO) {
resourceInfoDTO = new ResourceInfoDTO();
resourceInfoDTO.setUrlPattern(uriTemplate.getUriTemplate());
resourceInfoDTO.setHttpVerbs(new LinkedHashSet());
apiInfoDTO.getResources().add(resourceInfoDTO);
resourcesMap.put(uriTemplate.getUriTemplate(), resourceInfoDTO);
}
verbInfoDTO = new VerbInfoDTO();
verbInfoDTO.setHttpVerb(uriTemplate.getHTTPVerb());
verbInfoDTO.setAuthType(uriTemplate.getAuthType());
verbInfoDTO.setThrottling(uriTemplate.getThrottlingTier());
verbInfoDTO.setContentAware(uriTemplate.checkContentAwareFromThrottlingTiers());
verbInfoDTO.setThrottlingConditions(uriTemplate.getThrottlingConditions());
verbInfoDTO.setConditionGroups(uriTemplate.getConditionGroups());
verbInfoDTO.setApplicableLevel(uriTemplate.getApplicableLevel());
resourceInfoDTO.getHttpVerbs().add(verbInfoDTO);
}
return apiInfoDTO;
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIUtilTest method testGetAPIWithGovernanceArtifactAdvancedThrottlingDisabled.
@Test
public void testGetAPIWithGovernanceArtifactAdvancedThrottlingDisabled() 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);
RegistryService registryService = Mockito.mock(RegistryService.class);
UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
APIMConfigService apimConfigService = Mockito.mock(APIMConfigService.class);
Mockito.when(serviceReferenceHolder.getApimConfigService()).thenReturn(apimConfigService);
Mockito.when(apimConfigService.getTenantConfig(tenantDomain)).thenReturn(tenantConfValue);
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);
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.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIUtilTest method getUniqueAPI.
private API getUniqueAPI() {
APIIdentifier apiIdentifier = new APIIdentifier(UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString());
API api = new API(apiIdentifier);
api.setStatus(APIConstants.CREATED);
api.setContext(UUID.randomUUID().toString());
Set<String> environments = new HashSet<String>();
environments.add(UUID.randomUUID().toString());
URITemplate uriTemplate = new URITemplate();
uriTemplate.setAuthType("None");
uriTemplate.setHTTPVerb("GET");
uriTemplate.setThrottlingTier("Unlimited");
uriTemplate.setUriTemplate("/*");
Set<URITemplate> uriTemplates = new HashSet<URITemplate>();
uriTemplates.add(uriTemplate);
uriTemplate = new URITemplate();
uriTemplate.setAuthType("None");
uriTemplate.setHTTPVerb("GET");
uriTemplate.setThrottlingTier("Unlimited");
uriTemplate.setUriTemplate("/get");
uriTemplates.add(uriTemplate);
uriTemplate = new URITemplate();
uriTemplate.setAuthType("None");
uriTemplate.setHTTPVerb("POST");
uriTemplate.setThrottlingTier("Unlimited");
uriTemplate.setUriTemplate("/*");
uriTemplates.add(uriTemplate);
uriTemplate = new URITemplate();
uriTemplate.setAuthType("None");
uriTemplate.setHTTPVerb("POST");
uriTemplate.setThrottlingTier("Unlimited");
uriTemplate.setUriTemplate("/post");
uriTemplates.add(uriTemplate);
uriTemplate = new URITemplate();
uriTemplate.setAuthType("None");
uriTemplate.setHTTPVerb("DELETE");
uriTemplate.setThrottlingTier("Unlimited");
uriTemplate.setUriTemplate("/*");
uriTemplates.add(uriTemplate);
uriTemplate = new URITemplate();
uriTemplate.setAuthType("None");
uriTemplate.setHTTPVerb("PUT");
uriTemplate.setThrottlingTier("Unlimited");
uriTemplate.setUriTemplate("/*");
uriTemplates.add(uriTemplate);
uriTemplate = new URITemplate();
uriTemplate.setAuthType("None");
uriTemplate.setHTTPVerb("PUT");
uriTemplate.setThrottlingTier("Unlimited");
uriTemplate.setUriTemplate("/put");
uriTemplates.add(uriTemplate);
api.setUriTemplates(uriTemplates);
api.setEnvironments(environments);
api.setUUID(UUID.randomUUID().toString());
api.setThumbnailUrl(UUID.randomUUID().toString());
api.setVisibility(UUID.randomUUID().toString());
api.setVisibleRoles(UUID.randomUUID().toString());
api.setVisibleTenants(UUID.randomUUID().toString());
api.setTransports(UUID.randomUUID().toString());
api.setInSequence(UUID.randomUUID().toString());
api.setOutSequence(UUID.randomUUID().toString());
api.setFaultSequence(UUID.randomUUID().toString());
api.setDescription(UUID.randomUUID().toString());
api.setRedirectURL(UUID.randomUUID().toString());
api.setBusinessOwner(UUID.randomUUID().toString());
api.setApiOwner(UUID.randomUUID().toString());
api.setAdvertiseOnly(true);
CORSConfiguration corsConfiguration = new CORSConfiguration(true, Arrays.asList("*"), true, Arrays.asList("*"), Arrays.asList("*"));
api.setCorsConfiguration(corsConfiguration);
api.setLastUpdated(new Date());
api.setCreatedTime(new Date().toString());
Set<Tier> tierSet = new HashSet<Tier>();
tierSet.add(new Tier("Unlimited"));
tierSet.add(new Tier("Gold"));
api.addAvailableTiers(tierSet);
Set<String> tags = new HashSet<String>();
tags.add("stuff");
api.addTags(tags);
return api;
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class OASTestBase method testGetURITemplates.
public void testGetURITemplates(APIDefinition parser, String content) throws Exception {
JSONObject jsonObject = new JSONObject(content);
URITemplate exUriTemplate = new URITemplate();
exUriTemplate.setUriTemplate("/pets");
exUriTemplate.setAuthType("Application & Application User");
exUriTemplate.setAuthTypes("Application & Application User");
exUriTemplate.setHTTPVerb("GET");
exUriTemplate.setHttpVerbs("GET");
exUriTemplate.setThrottlingTier("Unlimited");
exUriTemplate.setThrottlingTiers("Unlimited");
exUriTemplate.setScope(extensionScope);
exUriTemplate.setScopes(extensionScope);
String scopesOnlyInSecurity = jsonObject.getJSONObject("scopesOnlyInSecurity").toString();
Set<URITemplate> uriTemplates = parser.getURITemplates(scopesOnlyInSecurity);
Assert.assertEquals(1, uriTemplates.size());
Assert.assertTrue(uriTemplates.contains(petGet));
String scopesOnlyInExtension = jsonObject.getJSONObject("scopesOnlyInExtension").toString();
uriTemplates = parser.getURITemplates(scopesOnlyInExtension);
Assert.assertEquals(1, uriTemplates.size());
Assert.assertTrue(uriTemplates.contains(exUriTemplate));
String scopesInExtensionAndSec = jsonObject.getJSONObject("scopesInExtensionAndSec").toString();
uriTemplates = parser.getURITemplates(scopesInExtensionAndSec);
Assert.assertEquals(1, uriTemplates.size());
Assert.assertTrue(uriTemplates.contains(petGet));
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class OASTestBase method getUriTemplate.
protected URITemplate getUriTemplate(String httpVerb, String authType, String uriTemplateString) {
URITemplate uriTemplate = new URITemplate();
uriTemplate.setAuthTypes(authType);
uriTemplate.setAuthType(authType);
uriTemplate.setHTTPVerb(httpVerb);
uriTemplate.setHttpVerbs(httpVerb);
uriTemplate.setUriTemplate(uriTemplateString);
uriTemplate.setThrottlingTier("Unlimited");
uriTemplate.setThrottlingTiers("Unlimited");
uriTemplate.setScope(null);
return uriTemplate;
}
Aggregations