Search in sources :

Example 46 with Tier

use of org.wso2.carbon.apimgt.api.model.Tier in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testGetSubscribedAPIsWithApp.

@Test
public void testGetSubscribedAPIsWithApp() throws APIManagementException {
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
    Set<SubscribedAPI> originalSubscribedAPIs = new HashSet<SubscribedAPI>();
    SubscribedAPI subscribedAPI = Mockito.mock(SubscribedAPI.class);
    originalSubscribedAPIs.add(subscribedAPI);
    Subscriber subscriber = new Subscriber("Subscriber");
    Tier tier = Mockito.mock(Tier.class);
    when(apiMgtDAO.getSubscribedAPIs(subscriber, "testApplication", "testID")).thenReturn(originalSubscribedAPIs);
    when(subscribedAPI.getTier()).thenReturn(tier);
    when(tier.getName()).thenReturn("tier");
    assertNotNull(apiConsumer.getSubscribedAPIs(subscriber, "testApplication", "testID"));
}
Also used : Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) Tier(org.wso2.carbon.apimgt.api.model.Tier) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 47 with Tier

use of org.wso2.carbon.apimgt.api.model.Tier in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testGetSubscribedIdentifiers.

@Test
public void testGetSubscribedIdentifiers() throws APIManagementException {
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
    Set<SubscribedAPI> originalSubscribedAPIs = new HashSet<>();
    SubscribedAPI subscribedAPI = Mockito.mock(SubscribedAPI.class);
    originalSubscribedAPIs.add(subscribedAPI);
    Subscriber subscriber = new Subscriber("Subscriber");
    APIIdentifier apiId1 = new APIIdentifier(API_PROVIDER, SAMPLE_API_NAME, SAMPLE_API_VERSION);
    Tier tier = Mockito.mock(Tier.class);
    when(apiMgtDAO.getSubscribedAPIs("testorg", subscriber, "testID")).thenReturn(originalSubscribedAPIs);
    when(subscribedAPI.getTier()).thenReturn(tier);
    when(tier.getName()).thenReturn("tier");
    when(subscribedAPI.getApiId()).thenReturn(apiId1);
    Application app = Mockito.mock(Application.class);
    when(app.getId()).thenReturn(1);
    when(subscribedAPI.getApplication()).thenReturn(app);
    Set<APIKey> apiKeys = new HashSet<>();
    APIKey apiKey = new APIKey();
    apiKey.setType("Production");
    apiKeys.add(apiKey);
    Mockito.when(apiMgtDAO.getKeyMappingsFromApplicationId(Mockito.anyInt())).thenReturn(apiKeys);
    AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
    accessTokenInfo.setAccessToken(UUID.randomUUID().toString());
    Mockito.when(keyManager.getAccessTokenByConsumerKey(Mockito.anyString())).thenReturn(accessTokenInfo);
    assertNotNull(apiConsumer.getSubscribedIdentifiers(subscriber, apiId1, "testID", "testorg"));
}
Also used : APIKey(org.wso2.carbon.apimgt.api.model.APIKey) AccessTokenInfo(org.wso2.carbon.apimgt.api.model.AccessTokenInfo) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) Tier(org.wso2.carbon.apimgt.api.model.Tier) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Application(org.wso2.carbon.apimgt.api.model.Application) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 48 with Tier

use of org.wso2.carbon.apimgt.api.model.Tier in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testAddSubscriptionInvalidTier.

@Test
public void testAddSubscriptionInvalidTier() throws APIManagementException {
    API api = new API(new APIIdentifier(API_PROVIDER, "published_api", SAMPLE_API_VERSION));
    api.setSubscriptionAvailability(APIConstants.SUBSCRIPTION_TO_ALL_TENANTS);
    Application application = new Application(1);
    api.setStatus(APIConstants.PUBLISHED);
    Set<Tier> tiers = new HashSet<>();
    tiers.add(new Tier("tier1"));
    api.setAvailableTiers(tiers);
    ApiTypeWrapper apiTypeWrapper = new ApiTypeWrapper(api);
    apiTypeWrapper.setTier("tier2");
    Mockito.when(apiMgtDAO.addSubscription(Mockito.eq(apiTypeWrapper), Mockito.eq(application), Mockito.anyString(), Mockito.anyString())).thenReturn(1);
    SubscribedAPI subscribedAPI = new SubscribedAPI(UUID.randomUUID().toString());
    Mockito.when(apiMgtDAO.getSubscriptionById(1)).thenReturn(subscribedAPI);
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO, SAMPLE_TENANT_DOMAIN_1);
    try {
        apiConsumer.addSubscription(apiTypeWrapper, "sub1", application);
        Assert.fail("Invalid Tier error not thrown.");
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getErrorHandler().getErrorCode(), ExceptionCodes.SUBSCRIPTION_TIER_NOT_ALLOWED.getErrorCode());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Tier(org.wso2.carbon.apimgt.api.model.Tier) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Application(org.wso2.carbon.apimgt.api.model.Application) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 49 with Tier

use of org.wso2.carbon.apimgt.api.model.Tier in project carbon-apimgt by wso2.

the class APIProviderImpl method checkResourceThrottlingTiersInURITemplates.

private void checkResourceThrottlingTiersInURITemplates(Set<URITemplate> uriTemplates, String tenantDomain) throws APIManagementException {
    Map<String, Tier> tierMap = APIUtil.getTiers(APIConstants.TIER_RESOURCE_TYPE, tenantDomain);
    if (tierMap != null) {
        for (URITemplate template : uriTemplates) {
            if (template.getThrottlingTier() != null && !tierMap.containsKey(template.getThrottlingTier())) {
                String message = "Invalid x-throttling tier " + template.getThrottlingTier() + " found in api definition for resource " + template.getHTTPVerb() + " " + template.getUriTemplate();
                log.error(message);
                throw new APIManagementException(message);
            }
        }
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Tier(org.wso2.carbon.apimgt.api.model.Tier) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate)

Example 50 with Tier

use of org.wso2.carbon.apimgt.api.model.Tier in project carbon-apimgt by wso2.

the class APIKeyValidationService method getAllURITemplates.

/**
 * Return the URI Templates for an API
 *
 * @param context Requested context
 * @param version API Version
 * @return APIKeyValidationInfoDTO with authorization info and tier info if authorized. If it is not
 * authorized, tier information will be <pre>null</pre>
 */
public ArrayList<URITemplate> getAllURITemplates(String context, String version) throws APIManagementException {
    Timer timer6 = MetricManager.timer(org.wso2.carbon.metrics.manager.Level.INFO, MetricManager.name(APIConstants.METRICS_PREFIX, this.getClass().getSimpleName(), "GET_URI_TEMPLATE"));
    Timer.Context timerContext6 = timer6.start();
    if (log.isDebugEnabled()) {
        log.debug("getAllURITemplates request from gateway to keymanager: requestTime=" + new SimpleDateFormat("[yyyy.MM.dd HH:mm:ss,SSS zzz]").format(new Date()) + " ,for:" + context);
    }
    ArrayList<URITemplate> templates = getTemplates(context, version);
    if (log.isDebugEnabled()) {
        log.debug("getAllURITemplates response from keyManager to gateway for:" + context + " at " + new SimpleDateFormat("[yyyy.MM.dd HH:mm:ss,SSS zzz]").format(new Date()));
    }
    timerContext6.stop();
    return templates;
}
Also used : Timer(org.wso2.carbon.metrics.manager.Timer) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

Tier (org.wso2.carbon.apimgt.api.model.Tier)108 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)53 ArrayList (java.util.ArrayList)42 Test (org.junit.Test)40 HashSet (java.util.HashSet)39 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)37 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)37 API (org.wso2.carbon.apimgt.api.model.API)33 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)32 HashMap (java.util.HashMap)28 Application (org.wso2.carbon.apimgt.api.model.Application)26 Test (org.testng.annotations.Test)22 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)22 Application (org.wso2.carbon.apimgt.core.models.Application)22 LinkedHashSet (java.util.LinkedHashSet)21 JSONObject (org.json.simple.JSONObject)20 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)20 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)20 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)20 BeforeTest (org.testng.annotations.BeforeTest)19