Search in sources :

Example 11 with CARBON_HOME

use of org.wso2.carbon.utils.ServerConstants.CARBON_HOME in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testGetRecentlyAddedAPIs.

@Test
public void testGetRecentlyAddedAPIs() throws Exception {
    Registry userRegistry = Mockito.mock(Registry.class);
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(userRegistry, apiMgtDAO);
    UserRegistry userRegistry1 = 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");
    API api = Mockito.mock(API.class);
    Set<API> recentlyAddedAPI = new HashSet<API>();
    recentlyAddedAPI.add(api);
    PowerMockito.mockStatic(Caching.class);
    CacheManager cacheManager = Mockito.mock(CacheManager.class);
    Cache<Object, Object> cache = Mockito.mock(Cache.class);
    Mockito.when(Caching.getCacheManager(Mockito.anyString())).thenReturn(cacheManager);
    Mockito.when(cacheManager.getCache(Mockito.anyString())).thenReturn(cache);
    Mockito.when(cache.get(Mockito.anyObject())).thenReturn(recentlyAddedAPI);
    Resource resource = new ResourceImpl();
    resource.setProperty("overview_status", "overview_status");
    resource.setProperty("store_view_roles", "store_view_roles");
    String path = "testPath";
    Mockito.when(APIUtil.getAPIPath((APIIdentifier) Mockito.anyObject())).thenReturn(path);
    Mockito.when(userRegistry1.get(Mockito.anyString())).thenReturn(resource);
    PowerMockito.mockStatic(GovernanceUtils.class);
    PowerMockito.doNothing().when(APIUtil.class, "loadTenantRegistry", Mockito.anyInt());
    PowerMockito.when(APIUtil.isAllowDisplayMultipleVersions()).thenReturn(true, false);
    System.setProperty(CARBON_HOME, "");
    Mockito.when(registryService.getGovernanceUserRegistry(Mockito.anyString(), Mockito.anyInt())).thenReturn(userRegistry1);
    Mockito.when(registryService.getGovernanceSystemRegistry(Mockito.anyInt())).thenReturn(userRegistry1);
    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.findGovernanceArtifacts(Mockito.anyString())).thenReturn(genericArtifacts);
    APIIdentifier apiId1 = new APIIdentifier("admin", "API1", "1.0.0");
    API api1 = new API(apiId1);
    Mockito.when(APIUtil.getAPI(artifact)).thenReturn(api1);
    // set isAllowDisplayMultipleVersions true
    assertNotNull(apiConsumer.getRecentlyAddedAPIs(10, "testDomain"));
    // set isAllowDisplayMultipleVersions false
    assertNotNull(apiConsumer.getRecentlyAddedAPIs(10, "testDomain"));
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) Matchers.anyString(org.mockito.Matchers.anyString) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) CacheManager(javax.cache.CacheManager) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) API(org.wso2.carbon.apimgt.api.model.API) JSONObject(org.json.simple.JSONObject) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 12 with CARBON_HOME

use of org.wso2.carbon.utils.ServerConstants.CARBON_HOME in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testGetAllPaginatedAPIsByStatus.

@Test
public void testGetAllPaginatedAPIsByStatus() throws Exception {
    Registry userRegistry = Mockito.mock(Registry.class);
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(userRegistry, apiMgtDAO);
    System.setProperty(CARBON_HOME, "");
    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("10", "20");
    PowerMockito.mockStatic(GovernanceUtils.class);
    PowerMockito.mockStatic(GovernanceUtils.class);
    PowerMockito.doNothing().when(APIUtil.class, "loadTenantRegistry", Mockito.anyInt());
    GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
    PowerMockito.when(APIUtil.isAllowDisplayMultipleVersions()).thenReturn(false, true);
    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.findGenericArtifacts(Mockito.anyMap())).thenReturn(genericArtifacts);
    APIIdentifier apiId1 = new APIIdentifier("admin", "API1", "1.0.0");
    API api = new API(apiId1);
    Mockito.when(APIUtil.getAPI(artifact)).thenReturn(api);
    String artifactPath = "artifact/path";
    PowerMockito.when(GovernanceUtils.getArtifactPath(userRegistry, artifact.getId())).thenReturn(artifactPath);
    Tag tag = new Tag();
    Tag[] tags = new Tag[] { tag };
    Mockito.when(userRegistry.getTags(artifactPath)).thenReturn(tags);
    assertNotNull(apiConsumer.getAllPaginatedAPIsByStatus(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10, "testStatus", false));
    assertNotNull(apiConsumer.getAllPaginatedAPIsByStatus(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10, "testStatus", true));
    // artifact manager null path
    PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(null);
    assertNotNull(apiConsumer.getAllPaginatedAPIsByStatus(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10, "testStatus", true));
    // generic artifact null path
    PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(artifactManager);
    Mockito.when(artifactManager.findGenericArtifacts(Mockito.anyMap())).thenReturn(null);
    assertNotNull(apiConsumer.getAllPaginatedAPIsByStatus(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10, "testStatus", true));
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) Matchers.anyString(org.mockito.Matchers.anyString) 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) Tag(org.wso2.carbon.registry.core.Tag) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 13 with CARBON_HOME

use of org.wso2.carbon.utils.ServerConstants.CARBON_HOME in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testGetAllPaginatedPublishedAPIs.

@Test
public void testGetAllPaginatedPublishedAPIs() throws Exception {
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper();
    PowerMockito.doNothing().when(APIUtil.class, "loadTenantRegistry", Mockito.anyInt());
    PowerMockito.when(APIUtil.isAllowDisplayMultipleVersions()).thenReturn(false, true);
    System.setProperty(CARBON_HOME, "");
    GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
    PowerMockito.when(APIUtil.getArtifactManager(apiConsumer.registry, APIConstants.API_KEY)).thenReturn(artifactManager);
    GenericArtifact artifact = Mockito.mock(GenericArtifact.class);
    GenericArtifact[] genericArtifacts = new GenericArtifact[] { artifact };
    Mockito.when(artifactManager.findGenericArtifacts(Mockito.anyMap())).thenReturn(genericArtifacts);
    APIIdentifier apiId1 = new APIIdentifier("admin", "API1", "1.0.0");
    API api = new API(apiId1);
    Mockito.when(APIUtil.getAPI(artifact)).thenReturn(api);
    assertNotNull(apiConsumer.getAllPaginatedPublishedAPIs(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10));
    assertNotNull(apiConsumer.getAllPaginatedPublishedAPIs(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10));
    // artifact manager null path
    PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(null);
    assertNotNull(apiConsumer.getAllPaginatedPublishedAPIs(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10));
    // generic artifact null path
    PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(artifactManager);
    Mockito.when(artifactManager.findGenericArtifacts(Mockito.anyMap())).thenReturn(null);
    assertNotNull(apiConsumer.getAllPaginatedPublishedAPIs(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 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) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 14 with CARBON_HOME

use of org.wso2.carbon.utils.ServerConstants.CARBON_HOME in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testUpdateAuthClient.

@Test
public void testUpdateAuthClient() throws APIManagementException {
    String consumerKey = "aNTf-EFga";
    OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
    OAuthAppRequest oAuthAppRequest = new OAuthAppRequest();
    oAuthAppRequest.setOAuthApplicationInfo(oAuthApplicationInfo);
    BDDMockito.when(ApplicationUtils.createOauthAppRequest(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(oAuthAppRequest);
    Mockito.when(apiMgtDAO.getConsumerKeyByApplicationIdKeyTypeKeyManager(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString())).thenReturn(consumerKey);
    OAuthApplicationInfo updatedAppInfo = new OAuthApplicationInfo();
    String clientName = "sample client";
    updatedAppInfo.setClientName(clientName);
    Mockito.when(keyManager.updateApplication((OAuthAppRequest) Mockito.any())).thenReturn(updatedAppInfo);
    KeyManagerConfigurationDTO keyManagerConfiguration = new KeyManagerConfigurationDTO();
    keyManagerConfiguration.setEnabled(true);
    Mockito.when(apiMgtDAO.getKeyManagerConfigurationByName(Mockito.anyString(), Mockito.anyString())).thenReturn(keyManagerConfiguration);
    System.setProperty(CARBON_HOME, "");
    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(APIConstants.KEYMANAGER_SERVERURL)).thenReturn("http://localhost");
    Application application = Mockito.mock(Application.class);
    application.setUUID(UUID.nameUUIDFromBytes("app1".getBytes()).toString());
    Subscriber subscriber = Mockito.mock(Subscriber.class);
    Mockito.when(ApplicationUtils.retrieveApplication("app1", "1", null)).thenReturn(application);
    Mockito.when(application.getSubscriber()).thenReturn(subscriber);
    Mockito.when(subscriber.getName()).thenReturn("1");
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
    apiConsumer.tenantDomain = SAMPLE_TENANT_DOMAIN_1;
    Assert.assertEquals(apiConsumer.updateAuthClient("1", application, "access", "www.host.com", new String[0], null, null, null, null, "default").getClientName(), clientName);
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) OAuthAppRequest(org.wso2.carbon.apimgt.api.model.OAuthAppRequest) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) 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 15 with CARBON_HOME

use of org.wso2.carbon.utils.ServerConstants.CARBON_HOME in project carbon-apimgt by wso2.

the class APIUtilTest method testGetTiersWhenTierTypeIsApplication.

@Test
public void testGetTiersWhenTierTypeIsApplication() throws Exception {
    System.setProperty(CARBON_HOME, "");
    int tierType = APIConstants.TIER_APPLICATION_TYPE;
    int tenantID = 1;
    Tier tier1 = Mockito.mock(Tier.class);
    Map<String, Tier> tierMap = new TreeMap<String, Tier>();
    tierMap.put("UNLIMITED", tier1);
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.doNothing().when(PrivilegedCarbonContext.class, "startTenantFlow");
    PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    Mockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    doNothing().when(privilegedCarbonContext).setTenantDomain(tenantDomain, true);
    Mockito.when(privilegedCarbonContext.getTenantId()).thenReturn(tenantID);
    PowerMockito.spy(APIUtil.class);
    PowerMockito.doReturn(tenantID).when(APIUtil.class, "getInternalOrganizationId", tenantDomain);
    PowerMockito.doReturn(tierMap).when(APIUtil.class, "getTiersFromPolicies", PolicyConstants.POLICY_LEVEL_APP, tenantID);
    Map<String, Tier> appTierMap = APIUtil.getTiers(tierType, tenantDomain);
    Assert.assertEquals(tierMap, appTierMap);
}
Also used : Tier(org.wso2.carbon.apimgt.api.model.Tier) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) TreeMap(java.util.TreeMap) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)16 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)13 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)5 Before (org.junit.Before)4 API (org.wso2.carbon.apimgt.api.model.API)4 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)4 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)4 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)4 APIManagerConfigurationService (org.wso2.carbon.apimgt.impl.APIManagerConfigurationService)4 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)4 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)4 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)4 Registry (org.wso2.carbon.registry.core.Registry)4 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)4 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)4 TreeMap (java.util.TreeMap)3 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)3 Matchers.anyString (org.mockito.Matchers.anyString)3 Tier (org.wso2.carbon.apimgt.api.model.Tier)3