Search in sources :

Example 16 with APIConsumer

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

the class APIConsumerImplTest method testGetTopRatedAPIs.

@Test
public void testGetTopRatedAPIs() throws APIManagementException, RegistryException {
    Registry userRegistry = Mockito.mock(Registry.class);
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(userRegistry, apiMgtDAO);
    GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
    PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(artifactManager);
    GenericArtifact artifact = Mockito.mock(GenericArtifact.class);
    GenericArtifact[] genericArtifacts = new GenericArtifact[] { artifact };
    Mockito.when(artifactManager.getAllGenericArtifacts()).thenReturn(genericArtifacts);
    Mockito.when(artifact.getAttribute(Mockito.anyString())).thenReturn("PUBLISHED");
    Mockito.when(artifact.getPath()).thenReturn("testPath");
    Mockito.when(userRegistry.getAverageRating("testPath")).thenReturn((float) 20.0);
    APIIdentifier apiId1 = new APIIdentifier("admin", "API1", "1.0.0");
    API api = new API(apiId1);
    Mockito.when(APIUtil.getAPI(artifact, userRegistry)).thenReturn(api);
    assertNotNull(apiConsumer.getTopRatedAPIs(10));
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) API(org.wso2.carbon.apimgt.api.model.API) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 17 with APIConsumer

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

the class APIConsumerImplTest method testAddApplicationInvalidTier.

@Test
public void testAddApplicationInvalidTier() throws APIManagementException {
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
    Application application = Mockito.mock(Application.class);
    Mockito.when(application.getName()).thenReturn("app");
    Mockito.when(application.getTier()).thenReturn("tier1");
    PowerMockito.when(application.getSubscriber()).thenReturn(new Subscriber("User1"));
    PowerMockito.when(MultitenantUtils.getTenantDomain("userID")).thenReturn("carbon.super");
    PowerMockito.when(APIUtil.isApplicationExist("userID", "app", "1", "testorg")).thenReturn(false);
    Map<String, Tier> tierMap = new HashMap<>();
    tierMap.put("tier2", new Tier("tier2"));
    PowerMockito.when(APIUtil.getTiers(APIConstants.TIER_APPLICATION_TYPE, "testorg")).thenReturn(tierMap);
    PowerMockito.when(APIUtil.findTier(tierMap.values(), "tier1")).thenReturn(null);
    Mockito.when(apiMgtDAO.addApplication(application, "userID", "testorg")).thenReturn(1);
    try {
        apiConsumer.addApplication(application, "userID", "testorg");
        Assert.fail();
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getErrorHandler(), ExceptionCodes.TIER_NAME_INVALID);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) Tier(org.wso2.carbon.apimgt.api.model.Tier) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) Application(org.wso2.carbon.apimgt.api.model.Application) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 18 with APIConsumer

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

the class APIConsumerImplTest method testAddSubscription.

@Test
public void testAddSubscription() 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("tier1");
    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);
    Assert.assertEquals(apiConsumer.addSubscription(apiTypeWrapper, "user1", application).getSubscriptionUUID(), subscribedAPI.getUUID());
    try {
        api.setStatus(APIConstants.CREATED);
        apiConsumer.addSubscription(apiTypeWrapper, "sub1", application);
        Assert.fail("Resource not found exception not thrown for wrong api state");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Subscriptions not allowed on APIs/API Products in the state"));
    }
}
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 19 with APIConsumer

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

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

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