Search in sources :

Example 26 with APIConsumer

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

the class APIConsumerImplTest method testRateAPI.

@Test
public void testRateAPI() throws APIManagementException {
    String uuid = UUID.randomUUID().toString();
    APIRating apiRating = APIRating.RATING_FOUR;
    String user = "Tom";
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
    Mockito.doNothing().when(apiMgtDAO).addRating(uuid, apiRating.getRating(), user);
    apiConsumer.rateAPI(uuid, apiRating, user);
    Mockito.verify(apiMgtDAO, Mockito.times(1)).addRating(uuid, apiRating.getRating(), user);
}
Also used : APIRating(org.wso2.carbon.apimgt.api.model.APIRating) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 27 with APIConsumer

use of org.wso2.carbon.apimgt.api.APIConsumer 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 28 with APIConsumer

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

the class APIConsumerImplTest method testRenewConsumerSecret.

@Test
public void testRenewConsumerSecret() throws APIManagementException {
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper();
    String clientId = UUID.randomUUID().toString();
    apiConsumer.apiMgtDAO = apiMgtDAO;
    KeyManagerConfigurationDTO keyManagerConfiguration = new KeyManagerConfigurationDTO();
    keyManagerConfiguration.setEnabled(true);
    Mockito.when(apiMgtDAO.getKeyManagerConfigurationByName(Mockito.anyString(), Mockito.anyString())).thenReturn(keyManagerConfiguration);
    Mockito.when(keyManager.getNewApplicationConsumerSecret(Mockito.anyObject())).thenReturn("updatedClientSecret");
    assertNotNull(apiConsumer.renewConsumerSecret(clientId, APIConstants.KeyManager.DEFAULT_KEY_MANAGER));
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 29 with APIConsumer

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

the class APIConsumerImplTest method testGetDeniedTiers.

@Test
public void testGetDeniedTiers() throws APIManagementException, org.wso2.carbon.user.core.UserStoreException {
    UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
    APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
    Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
    Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
    Mockito.when(apiManagerConfiguration.getFirstProperty(Mockito.anyString())).thenReturn("true", "false");
    APIConsumerImpl apiConsumer = new UserAwareAPIConsumerWrapper(userRegistry, apiMgtDAO);
    Mockito.when(userRegistry.getUserRealm()).thenReturn(userRealm);
    Mockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
    String[] currentUserRoles = { "role1", "role2" };
    PowerMockito.when(APIUtil.getListOfRoles(Mockito.anyString())).thenReturn(currentUserRoles);
    Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 0);
    TierPermissionDTO tierPermissionDTO = new TierPermissionDTO();
    TierPermissionDTO tierPermissionDTO1 = new TierPermissionDTO();
    tierPermissionDTO.setRoles(new String[] { "role1" });
    Set<TierPermissionDTO> tierPermissionDTOs = new HashSet<TierPermissionDTO>();
    tierPermissionDTOs.add(tierPermissionDTO);
    Mockito.when(apiMgtDAO.getThrottleTierPermissions(Mockito.anyInt())).thenReturn(tierPermissionDTOs);
    Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 1);
    tierPermissionDTO.setRoles(new String[] { "role3" });
    Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 0);
    Mockito.when(apiMgtDAO.getTierPermissions(Mockito.anyInt())).thenReturn(tierPermissionDTOs);
    Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 0);
    tierPermissionDTO.setPermissionType(APIConstants.TIER_PERMISSION_ALLOW);
    Mockito.when(userStoreManager.getRoleListOfUser(Mockito.anyString())).thenReturn(new String[0]);
    tierPermissionDTOs.add(tierPermissionDTO1);
    tierPermissionDTO1.setRoles(new String[] { "role4" });
    Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 1);
    Mockito.when(userStoreManager.getRoleListOfUser(Mockito.anyString())).thenReturn(new String[] { "role1", "role2" });
    tierPermissionDTO1.setRoles(new String[] { "role2" });
    tierPermissionDTO1.setTierName("Silver");
    Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 2);
}
Also used : TierPermissionDTO(org.wso2.carbon.apimgt.impl.dto.TierPermissionDTO) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Matchers.anyString(org.mockito.Matchers.anyString) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 30 with APIConsumer

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

the class APIManagerFactory method getAPIConsumerFromCache.

public APIConsumer getAPIConsumerFromCache(String key, ConsumerCreator consumerCreator) throws APIManagementException {
    APIConsumer consumer = consumers.get(key);
    if (consumer == null) {
        synchronized (key.intern()) {
            consumer = consumers.get(key);
            if (consumer != null) {
                return consumer;
            }
            consumer = consumerCreator.create();
            consumers.put(key, consumer);
        }
    }
    return consumer;
}
Also used : APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Aggregations

APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)91 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)79 Application (org.wso2.carbon.apimgt.api.model.Application)50 Test (org.junit.Test)46 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)46 HashMap (java.util.HashMap)32 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)29 ArrayList (java.util.ArrayList)28 API (org.wso2.carbon.apimgt.api.model.API)28 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)28 JSONObject (org.json.simple.JSONObject)23 ExportedApplication (org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication)23 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)20 Map (java.util.Map)19 Matchers.anyString (org.mockito.Matchers.anyString)19 ApiTypeWrapper (org.wso2.carbon.apimgt.api.model.ApiTypeWrapper)18 Tier (org.wso2.carbon.apimgt.api.model.Tier)18 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)15 URI (java.net.URI)13 URISyntaxException (java.net.URISyntaxException)13