Search in sources :

Example 1 with PaginationContext

use of org.wso2.carbon.registry.core.pagination.PaginationContext in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method init.

@Before
public void init() {
    System.setProperty(CARBON_HOME, "");
    privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    PowerMockito.mockStatic(GovernanceUtils.class);
    paginationContext = Mockito.mock(PaginationContext.class);
    PowerMockito.mockStatic(PaginationContext.class);
    PowerMockito.when(PaginationContext.getInstance()).thenReturn(paginationContext);
    apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
    scopesDAO = Mockito.mock(ScopesDAO.class);
    registry = Mockito.mock(Registry.class);
    genericArtifactManager = Mockito.mock(GenericArtifactManager.class);
    registryService = Mockito.mock(RegistryService.class);
    tenantManager = Mockito.mock(TenantManager.class);
    graphQLSchemaDefinition = Mockito.mock(GraphQLSchemaDefinition.class);
    keyManager = Mockito.mock(KeyManager.class);
    apiPersistenceInstance = Mockito.mock(APIPersistence.class);
    PowerMockito.mockStatic(KeyManagerHolder.class);
    KeyManagerDto keyManagerDto = new KeyManagerDto();
    keyManagerDto.setName("default");
    keyManagerDto.setKeyManager(keyManager);
    keyManagerDto.setIssuer("https://localhost");
    Map<String, KeyManagerDto> tenantKeyManagerDtoMap = new HashMap<>();
    tenantKeyManagerDtoMap.put("default", keyManagerDto);
    PowerMockito.when(KeyManagerHolder.getTenantKeyManagers("carbon.super")).thenReturn(tenantKeyManagerDtoMap);
}
Also used : GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) HashMap(java.util.HashMap) PaginationContext(org.wso2.carbon.registry.core.pagination.PaginationContext) ScopesDAO(org.wso2.carbon.apimgt.impl.dao.ScopesDAO) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) KeyManagerDto(org.wso2.carbon.apimgt.impl.dto.KeyManagerDto) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) GraphQLSchemaDefinition(org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager) APIPersistence(org.wso2.carbon.apimgt.persistence.APIPersistence) Before(org.junit.Before)

Example 2 with PaginationContext

use of org.wso2.carbon.registry.core.pagination.PaginationContext in project carbon-apimgt by wso2.

the class APIProviderImplTest method testGetAllPaginatedAPIs.

@Test
public void testGetAllPaginatedAPIs() throws RegistryException, UserStoreException, APIManagementException, XMLStreamException {
    APIIdentifier apiId1 = new APIIdentifier("admin", "API1", "1.0.0");
    API api1 = new API(apiId1);
    api1.setContext("/test");
    APIIdentifier apiId2 = new APIIdentifier("admin", "API2", "1.0.0");
    API api2 = new API(apiId2);
    api2.setContext("/test1");
    PaginationContext paginationCtx = Mockito.mock(PaginationContext.class);
    PowerMockito.when(PaginationContext.getInstance()).thenReturn(paginationCtx);
    Mockito.when(paginationCtx.getLength()).thenReturn(2);
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    ServiceReferenceHolder sh = TestUtils.getServiceReferenceHolder();
    RegistryService registryService = Mockito.mock(RegistryService.class);
    PowerMockito.when(sh.getRegistryService()).thenReturn(registryService);
    UserRegistry userReg = Mockito.mock(UserRegistry.class);
    PowerMockito.when(registryService.getGovernanceUserRegistry(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME, -1234)).thenReturn(userReg);
    PowerMockito.when(APIUtil.getArtifactManager(userReg, APIConstants.API_KEY)).thenReturn(artifactManager);
    APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
    APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
    PowerMockito.when(sh.getAPIManagerConfigurationService()).thenReturn(amConfigService);
    PowerMockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
    PowerMockito.when(amConfig.getFirstProperty(APIConstants.API_PUBLISHER_APIS_PER_PAGE)).thenReturn("2");
    RealmService realmService = Mockito.mock(RealmService.class);
    TenantManager tm = Mockito.mock(TenantManager.class);
    PowerMockito.when(sh.getRealmService()).thenReturn(realmService);
    PowerMockito.when(realmService.getTenantManager()).thenReturn(tm);
    PowerMockito.when(tm.getTenantId("carbon.super")).thenReturn(-1234);
    GenericArtifact genericArtifact1 = Mockito.mock(GenericArtifact.class);
    GenericArtifact genericArtifact2 = Mockito.mock(GenericArtifact.class);
    Mockito.when(APIUtil.getAPI(genericArtifact1)).thenReturn(api1);
    Mockito.when(APIUtil.getAPI(genericArtifact2)).thenReturn(api2);
    List<GovernanceArtifact> governanceArtifacts = new ArrayList<GovernanceArtifact>();
    governanceArtifacts.add(genericArtifact1);
    governanceArtifacts.add(genericArtifact2);
    List<GovernanceArtifact> governanceArtifacts1 = new ArrayList<GovernanceArtifact>();
    PowerMockito.when(GovernanceUtils.findGovernanceArtifacts(Mockito.anyMap(), any(Registry.class), Mockito.anyString())).thenReturn(governanceArtifacts, governanceArtifacts1);
    Map<String, Object> result = apiProvider.getAllPaginatedAPIs("carbon.super", 0, 10);
    List<API> apiList = (List<API>) result.get("apis");
    Assert.assertEquals(2, apiList.size());
    Assert.assertEquals("API1", apiList.get(0).getId().getApiName());
    Assert.assertEquals("API2", apiList.get(1).getId().getApiName());
    Assert.assertEquals(2, result.get("totalLength"));
    // No APIs available
    Map<String, Object> result1 = apiProvider.getAllPaginatedAPIs("carbon.super", 0, 10);
    List<API> apiList1 = (List<API>) result1.get("apis");
    Assert.assertEquals(0, apiList1.size());
    // Registry Exception while retrieving artifacts
    Mockito.when(artifactManager.findGenericArtifacts(Matchers.anyMap())).thenThrow(RegistryException.class);
    try {
        apiProvider.getAllPaginatedAPIs("carbon.super", 0, 10);
    } catch (APIManagementException e) {
        Assert.assertEquals("Failed to get all APIs", e.getMessage());
    }
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) PaginationContext(org.wso2.carbon.registry.core.pagination.PaginationContext) GovernanceArtifact(org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact) ArrayList(java.util.ArrayList) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) RealmService(org.wso2.carbon.user.core.service.RealmService) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) JSONObject(org.json.simple.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Registry (org.wso2.carbon.registry.core.Registry)2 PaginationContext (org.wso2.carbon.registry.core.pagination.PaginationContext)2 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)2 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)2 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 JSONObject (org.json.simple.JSONObject)1 Before (org.junit.Before)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 API (org.wso2.carbon.apimgt.api.model.API)1 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)1 KeyManager (org.wso2.carbon.apimgt.api.model.KeyManager)1 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)1 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)1 ScopesDAO (org.wso2.carbon.apimgt.impl.dao.ScopesDAO)1 GraphQLSchemaDefinition (org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition)1