Search in sources :

Example 41 with Subscriber

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

the class APIConsumerImplTest method testGetApplicationsWithPagination.

@Test
public void testGetApplicationsWithPagination() throws APIManagementException {
    Application[] applications = new Application[] { new Application(1), new Application(2) };
    Mockito.when(apiMgtDAO.getApplicationsWithPagination((Subscriber) Mockito.any(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(applications);
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
    Assert.assertEquals(apiConsumer.getApplicationsWithPagination(new Subscriber("sub1"), "1", 0, 5, "", "", "ASC", "testorg").length, 2);
}
Also used : Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) Application(org.wso2.carbon.apimgt.api.model.Application) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 42 with Subscriber

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

the class APIMgtDAOTest method testDeleteSubscriptionsForapiId.

@Test
public void testDeleteSubscriptionsForapiId() throws Exception {
    Subscriber subscriber = new Subscriber("testCreateApplicationRegistrationEntry");
    String organization = "testOrg";
    subscriber.setTenantId(-1234);
    subscriber.setEmail("abc@wso2.com");
    subscriber.setSubscribedDate(new Date(System.currentTimeMillis()));
    apiMgtDAO.addSubscriber(subscriber, null);
    Policy applicationPolicy = getApplicationPolicy("testCreateApplicationRegistrationEntry");
    SubscriptionPolicy subscriptionPolicy = (SubscriptionPolicy) getSubscriptionPolicy("testCreateApplicationRegistrationEntry");
    subscriptionPolicy.setMonetizationPlan(APIConstants.Monetization.FIXED_RATE);
    apiMgtDAO.addSubscriptionPolicy((SubscriptionPolicy) subscriptionPolicy);
    applicationPolicy.setTenantId(-1234);
    apiMgtDAO.addApplicationPolicy((ApplicationPolicy) applicationPolicy);
    Application application = new Application("testCreateApplicationRegistrationEntry", subscriber);
    application.setTier("testCreateApplicationRegistrationEntry");
    application.setId(apiMgtDAO.addApplication(application, "testCreateApplicationRegistrationEntry", organization));
    application.setDescription("updated description");
    apiMgtDAO.updateApplication(application);
    assertEquals(apiMgtDAO.getApplicationById(application.getId()).getDescription(), "updated description");
    APIIdentifier apiId = new APIIdentifier("testCreateApplicationRegistrationEntry", "testCreateApplicationRegistrationEntry", "1.0.0");
    API api = new API(apiId);
    api.setContext("/testCreateApplicationRegistrationEntry");
    api.setContextTemplate("/testCreateApplicationRegistrationEntry/{version}");
    api.setVersionTimestamp(String.valueOf(System.currentTimeMillis()));
    APIPolicy apiPolicy = (APIPolicy) getPolicyAPILevelPerUser("testCreateApplicationRegistrationEntry");
    api.setApiLevelPolicy(apiPolicy.getPolicyName());
    api.setUUID(UUID.randomUUID().toString());
    api.getId().setId(apiMgtDAO.addAPI(api, -1234, organization));
    apiId.setTier(subscriptionPolicy.getPolicyName());
    ApiTypeWrapper apiTypeWrapper = new ApiTypeWrapper(api);
    int subsId = apiMgtDAO.addSubscription(apiTypeWrapper, application, APIConstants.SubscriptionStatus.ON_HOLD, subscriber.getName());
    assertTrue(apiMgtDAO.getApplicationsByTier(subscriptionPolicy.getPolicyName()).length > 0);
    String subStatus = apiMgtDAO.getSubscriptionStatusById(subsId);
    assertEquals(subStatus, APIConstants.SubscriptionStatus.ON_HOLD);
    SubscribedAPI subscribedAPI = apiMgtDAO.getSubscriptionById(subsId);
    String clientIdProduction = UUID.randomUUID().toString();
    String clientIdSandbox = UUID.randomUUID().toString();
    apiMgtDAO.createApplicationKeyTypeMappingForManualClients(APIConstants.API_KEY_TYPE_PRODUCTION, application.getId(), clientIdProduction, "Default", UUID.randomUUID().toString());
    apiMgtDAO.createApplicationKeyTypeMappingForManualClients(APIConstants.API_KEY_TYPE_SANDBOX, application.getId(), clientIdSandbox, "Default", UUID.randomUUID().toString());
    assertTrue(apiMgtDAO.getSubscriptionCount(subscriber, application.getName(), null) > 0);
    OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
    Mockito.when(keyManager.retrieveApplication(clientIdProduction)).thenReturn(oAuthApplicationInfo);
    Mockito.when(keyManager.retrieveApplication(clientIdSandbox)).thenReturn(oAuthApplicationInfo);
    assertTrue(apiMgtDAO.getSubscribedAPIs(organization, subscriber, null).size() > 0);
    assertEquals(subscribedAPI.getSubCreatedStatus(), APIConstants.SubscriptionCreatedStatus.SUBSCRIBE);
    assertEquals(subscribedAPI.getApiId(), apiId);
    assertEquals(subscribedAPI.getApplication().getId(), application.getId());
    SubscribedAPI subscribedAPIFromUuid = apiMgtDAO.getSubscriptionByUUID(subscribedAPI.getUUID());
    assertEquals(subscribedAPIFromUuid.getSubCreatedStatus(), APIConstants.SubscriptionCreatedStatus.SUBSCRIBE);
    assertEquals(subscribedAPIFromUuid.getApiId(), apiId);
    assertEquals(subscribedAPIFromUuid.getApplication().getId(), application.getId());
    apiMgtDAO.updateApplicationStatus(application.getId(), APIConstants.ApplicationStatus.APPLICATION_APPROVED);
    String status = apiMgtDAO.getApplicationStatus("testCreateApplicationRegistrationEntry", "testCreateApplicationRegistrationEntry");
    assertEquals(status, APIConstants.ApplicationStatus.APPLICATION_APPROVED);
    boolean applicationExist = apiMgtDAO.isApplicationExist(application.getName(), subscriber.getName(), null, organization);
    assertTrue(applicationExist);
    Set<SubscribedAPI> subscribedAPIS = apiMgtDAO.getSubscribedAPIs(subscriber, application.getName(), null);
    assertEquals(subscribedAPIS.size(), 1);
    apiMgtDAO.updateSubscription(apiId, APIConstants.SubscriptionStatus.BLOCKED, application.getId(), organization);
    subscribedAPI.setSubStatus(APIConstants.SubscriptionStatus.REJECTED);
    apiMgtDAO.updateSubscription(subscribedAPI);
    assertTrue(apiMgtDAO.hasSubscription(subscriptionPolicy.getPolicyName(), subscriber.getName(), PolicyConstants.POLICY_LEVEL_SUB));
    assertTrue(apiMgtDAO.hasSubscription(applicationPolicy.getPolicyName(), subscriber.getName(), PolicyConstants.POLICY_LEVEL_APP));
    assertTrue(apiMgtDAO.hasSubscription(apiPolicy.getPolicyName(), subscriber.getName(), PolicyConstants.POLICY_LEVEL_API));
    assertTrue(apiPolicy.getPolicyName().equals(apiMgtDAO.getAPILevelTier(apiMgtDAO.getAPIID(api.getUuid()))));
    apiMgtDAO.recordAPILifeCycleEvent(api.getUuid(), "CREATED", "PUBLISHED", "testCreateApplicationRegistrationEntry", -1234);
    apiMgtDAO.updateDefaultAPIPublishedVersion(apiId);
    apiMgtDAO.removeAllSubscriptions(api.getUuid());
    assertTrue(apiMgtDAO.getAPINamesMatchingContext(api.getContext()).size() > 0);
    apiMgtDAO.deleteAPI(api.getUuid());
    apiMgtDAO.deleteApplication(application);
    apiMgtDAO.removeThrottlePolicy(PolicyConstants.POLICY_LEVEL_APP, "testCreateApplicationRegistrationEntry", -1234);
    apiMgtDAO.deleteApplicationKeyMappingByConsumerKey(clientIdProduction);
    apiMgtDAO.deleteApplicationMappingByConsumerKey(clientIdSandbox);
    deleteSubscriber(subscriber.getId());
}
Also used : QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.api.model.policy.Policy) GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) Date(java.util.Date) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) API(org.wso2.carbon.apimgt.api.model.API) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) Application(org.wso2.carbon.apimgt.api.model.Application) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 43 with Subscriber

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

the class APIConsumerImplTest method testGetSubscriptionCount.

@Test
public void testGetSubscriptionCount() throws APIManagementException {
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper();
    Subscriber subscriber = new Subscriber("Subscriber");
    apiConsumer.apiMgtDAO = apiMgtDAO;
    when(apiMgtDAO.getSubscriptionCount((Subscriber) Mockito.anyObject(), Mockito.anyString(), Mockito.anyString())).thenReturn(10);
    apiConsumer.apiMgtDAO = apiMgtDAO;
    assertEquals((Integer) 10, apiConsumer.getSubscriptionCount(subscriber, "testApplication", "testId"));
}
Also used : Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 44 with Subscriber

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

the class APIProviderImpl method getSubscriberClaims.

/**
 * Returns the claims of subscriber for the given subscriber.
 *
 * @param subscriber The name of the subscriber to be returned
 * @return The looked up claims of the subscriber or null if the requested subscriber does not exist
 * @throws APIManagementException if failed to get Subscriber
 */
@Override
public Map<String, String> getSubscriberClaims(String subscriber) throws APIManagementException {
    String tenantDomain = MultitenantUtils.getTenantDomain(subscriber);
    int tenantId = 0;
    Map<String, String> claimMap = new HashMap<>();
    Map<String, String> subscriberClaims = null;
    String configuredClaims = "";
    try {
        tenantId = getTenantId(tenantDomain);
        UserStoreManager userStoreManager = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId).getUserStoreManager();
        if (userStoreManager.isExistingUser(subscriber)) {
            subscriberClaims = APIUtil.getClaims(subscriber, tenantId, ClaimsRetriever.DEFAULT_DIALECT_URI);
            APIManagerConfiguration configuration = getAPIManagerConfiguration();
            configuredClaims = configuration.getFirstProperty(APIConstants.API_PUBLISHER_SUBSCRIBER_CLAIMS);
        }
        for (String claimURI : configuredClaims.split(",")) {
            if (subscriberClaims != null) {
                claimMap.put(claimURI, subscriberClaims.get(claimURI));
            }
        }
    } catch (UserStoreException e) {
        throw new APIManagementException("Error while retrieving tenant id for tenant domain " + tenantDomain, e);
    }
    return claimMap;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) UserStoreException(org.wso2.carbon.user.api.UserStoreException) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager)

Example 45 with Subscriber

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

the class DefaultKeyValidationHandlerTest method testValidateScopes.

@Test
public void testValidateScopes() throws APIKeyMgtException {
    API api = new API();
    api.setApiId(1);
    api.setApiProvider(USER_NAME);
    api.setApiName(API_NAME);
    api.setApiVersion(API_VERSION);
    api.setContext(API_CONTEXT);
    URLMapping urlMapping = new URLMapping();
    urlMapping.addScope(SCOPES);
    urlMapping.setHttpMethod(HTTP_VERB);
    urlMapping.setUrlPattern(RESOURCE);
    api.addResource(urlMapping);
    Map<String, API> apiMap = new HashMap<>();
    String key = API_CONTEXT + ":" + API_VERSION;
    apiMap.put(key, api);
    APIKeyValidationInfoDTO dto = new APIKeyValidationInfoDTO();
    dto.setSubscriber(SUBSCRIBER);
    dto.setApplicationName(APPLICATION_NAME);
    dto.setApplicationId(APPLICATION_ID);
    dto.setApplicationTier(TIER);
    Set<String> scopeSet = new HashSet<>();
    scopeSet.add(SCOPES);
    dto.setScopes(scopeSet);
    dto.setSubscriberTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
    dto.setUserType(APIConstants.ACCESS_TOKEN_USER_TYPE_APPLICATION);
    // TokenValidationContext for non default API
    TokenValidationContext param1 = new TokenValidationContext();
    param1.setValidationInfoDTO(dto);
    param1.setContext(API_CONTEXT);
    param1.setVersion(API_VERSION);
    param1.setAccessToken(ACCESS_TOKEN);
    param1.setMatchingResource(RESOURCE);
    param1.setHttpVerb(HTTP_VERB);
    // TokenValidationContext for default API version
    TokenValidationContext param2 = new TokenValidationContext();
    param2.setValidationInfoDTO(dto);
    param2.setContext(API_CONTEXT);
    param2.setVersion(DEFAULT_API_VERSION);
    param2.setAccessToken(ACCESS_TOKEN);
    param2.setMatchingResource(RESOURCE);
    param2.setHttpVerb(HTTP_VERB);
    Mockito.when(SubscriptionDataHolder.getInstance()).thenReturn(subscriptionDataHolder);
    Mockito.when(privilegedCarbonContext.getTenantDomain()).thenReturn(TENANT_DOMAIN);
    Mockito.when(subscriptionDataHolder.getTenantSubscriptionStore(eq(TENANT_DOMAIN))).thenReturn(tenantSubscriptionStore);
    Mockito.when(tenantSubscriptionStore.getApiByContextAndVersion(eq(API_CONTEXT), eq(API_VERSION))).thenReturn(api);
    DefaultKeyValidationHandler defaultKeyValidationHandler = new DefaultKeyValidationHandler();
    boolean isScopeValidated = defaultKeyValidationHandler.validateScopes(param1);
    boolean isScopeValidated_default = defaultKeyValidationHandler.validateScopes(param2);
    Assert.assertTrue("Scope validation fails for API " + API_NAME, isScopeValidated);
    Assert.assertTrue("Scope validation fails for default API " + API_NAME, isScopeValidated_default);
}
Also used : URLMapping(org.wso2.carbon.apimgt.api.model.subscription.URLMapping) TokenValidationContext(org.wso2.carbon.apimgt.keymgt.service.TokenValidationContext) HashMap(java.util.HashMap) API(org.wso2.carbon.apimgt.keymgt.model.entity.API) APIKeyValidationInfoDTO(org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)98 Test (org.junit.Test)64 Application (org.wso2.carbon.apimgt.api.model.Application)63 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)60 PreparedStatement (java.sql.PreparedStatement)39 SQLException (java.sql.SQLException)39 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)39 ResultSet (java.sql.ResultSet)37 Connection (java.sql.Connection)31 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)28 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)25 Tier (org.wso2.carbon.apimgt.api.model.Tier)20 ArrayList (java.util.ArrayList)19 HashSet (java.util.HashSet)19 Date (java.util.Date)14 HashMap (java.util.HashMap)11 LinkedHashSet (java.util.LinkedHashSet)10 JSONObject (org.json.simple.JSONObject)10 OAuthApplicationInfo (org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo)10 TreeMap (java.util.TreeMap)9