Search in sources :

Example 26 with GenericArtifactManager

use of org.wso2.carbon.governance.api.generic.GenericArtifactManager in project carbon-apimgt by wso2.

the class APIProviderImplTest method prepareForGetAPIsByProvider.

/**
 * This method can be used when invoking getAPIsByProvider()
 */
private void prepareForGetAPIsByProvider(GenericArtifactManager artifactManager, APIProviderImplWrapper apiProvider, String providerId, API api1, API api2) throws APIManagementException, RegistryException {
    String providerPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + providerId;
    // Mocking API1 association
    Association association1 = Mockito.mock(Association.class);
    String apiPath1 = "/API1/1.0.0";
    Resource resource1 = Mockito.mock(Resource.class);
    String apiArtifactId1 = "76897689";
    Mockito.when(apiProvider.registry.resourceExists(apiPath1)).thenReturn(true);
    Mockito.when(association1.getDestinationPath()).thenReturn(apiPath1);
    Mockito.when(apiProvider.registry.get(apiPath1)).thenReturn(resource1);
    Mockito.when(resource1.getUUID()).thenReturn(apiArtifactId1);
    GenericArtifact apiArtifact1 = Mockito.mock(GenericArtifact.class);
    Mockito.when(artifactManager.getGenericArtifact(apiArtifactId1)).thenReturn(apiArtifact1);
    Mockito.when(APIUtil.getAPI(apiArtifact1, apiProvider.registry)).thenReturn(api1);
    // Mocking API2 association
    Association association2 = Mockito.mock(Association.class);
    String apiPath2 = "/API2/1.0.0";
    Resource resource2 = Mockito.mock(Resource.class);
    String apiArtifactId2 = "76897622";
    Mockito.when(apiProvider.registry.resourceExists(apiPath2)).thenReturn(true);
    Mockito.when(association2.getDestinationPath()).thenReturn(apiPath2);
    Mockito.when(apiProvider.registry.get(apiPath2)).thenReturn(resource2);
    Mockito.when(resource2.getUUID()).thenReturn(apiArtifactId2);
    GenericArtifact apiArtifact2 = Mockito.mock(GenericArtifact.class);
    Mockito.when(artifactManager.getGenericArtifact(apiArtifactId2)).thenReturn(apiArtifact2);
    Mockito.when(APIUtil.getAPI(apiArtifact2, apiProvider.registry)).thenReturn(api2);
    Association[] associactions = { association1, association2 };
    Mockito.when(apiProvider.registry.getAssociations(providerPath, APIConstants.PROVIDER_ASSOCIATION)).thenReturn(associactions);
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) Association(org.wso2.carbon.registry.core.Association) Resource(org.wso2.carbon.registry.core.Resource)

Example 27 with GenericArtifactManager

use of org.wso2.carbon.governance.api.generic.GenericArtifactManager 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 28 with GenericArtifactManager

use of org.wso2.carbon.governance.api.generic.GenericArtifactManager in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testGetDocumentationContent.

@Test
public void testGetDocumentationContent() throws APIManagementException, org.wso2.carbon.user.api.UserStoreException, RegistryException {
    int tenantId = -1234;
    UserRegistry registry = Mockito.mock(UserRegistry.class);
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapperExtended(genericArtifactManager, registryService, registry, tenantManager);
    Mockito.when(tenantManager.getTenantId(Mockito.anyString())).thenThrow(UserStoreException.class).thenReturn(tenantId);
    APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
    String docName = "doc1";
    try {
        abstractAPIManager.getDocumentationContent(identifier, docName);
        Assert.fail("User store exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Failed to get document content found for documentation:"));
    }
    Mockito.when(registryService.getGovernanceSystemRegistry(Mockito.anyInt())).thenThrow(RegistryException.class).thenReturn(registry);
    try {
        abstractAPIManager.getDocumentationContent(identifier, docName);
        Assert.fail("Registry exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("No document content found for documentation:"));
    }
    Mockito.when(registry.resourceExists(Mockito.anyString())).thenReturn(false, true, true);
    String docContent = abstractAPIManager.getDocumentationContent(identifier, docName);
    Assert.assertNull(docContent);
    String docObject = "samlple doc content";
    Resource resource = new ResourceImpl();
    resource.setContent(docObject.getBytes(StandardCharsets.UTF_8));
    Mockito.when(registry.get(Mockito.anyString())).thenReturn(resource);
    docContent = abstractAPIManager.getDocumentationContent(identifier, docName);
    Assert.assertEquals(docContent, docObject);
    abstractAPIManager = new AbstractAPIManagerWrapper(genericArtifactManager, registryService, registry, tenantManager);
    docContent = abstractAPIManager.getDocumentationContent(identifier, docName);
    Assert.assertEquals(docContent, docObject);
    Mockito.when(registryService.getGovernanceUserRegistry(Mockito.anyString(), Mockito.anyInt())).thenReturn(registry);
    abstractAPIManager.tenantDomain = SAMPLE_TENANT_DOMAIN_1;
    docContent = abstractAPIManager.getDocumentationContent(identifier, docName);
    Assert.assertEquals(docContent, docObject);
}
Also used : ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserStoreException(org.wso2.carbon.user.core.UserStoreException) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 29 with GenericArtifactManager

use of org.wso2.carbon.governance.api.generic.GenericArtifactManager in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testGetAllDocumentationWithLoggedUser.

@Test
public void testGetAllDocumentationWithLoggedUser() throws APIManagementException, org.wso2.carbon.user.api.UserStoreException, RegistryException {
    int tenantId = -1234;
    UserRegistry registry = Mockito.mock(UserRegistry.class);
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(genericArtifactManager, registryService, registry, tenantManager);
    Mockito.when(tenantManager.getTenantId(SAMPLE_TENANT_DOMAIN)).thenThrow(UserStoreException.class).thenReturn(tenantId);
    APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
    String loggedInUser = "admin";
    abstractAPIManager.registry = registry;
    GenericArtifact genericArtifact = getGenericArtifact(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION, "sample");
    genericArtifact.setAttribute(APIConstants.DOC_TYPE, "Other");
    genericArtifact.setAttribute(APIConstants.DOC_SOURCE_TYPE, "URL");
    String apiDocPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + RegistryConstants.PATH_SEPARATOR + APIConstants.DOC_DIR + RegistryConstants.PATH_SEPARATOR;
    PowerMockito.mockStatic(APIUtil.class);
    PowerMockito.when(APIUtil.getAPIOrAPIProductDocPath(identifier)).thenReturn(apiDocPath);
    Resource resource1 = new ResourceImpl();
    resource1.setUUID(SAMPLE_RESOURCE_ID);
    Mockito.when(genericArtifact.getPath()).thenReturn("test");
    String docName = "sample";
    Documentation documentation = new Documentation(DocumentationType.HOWTO, docName);
    PowerMockito.when(APIUtil.getDocumentation(genericArtifact)).thenReturn(documentation);
    Mockito.when(registry.resourceExists(apiDocPath)).thenReturn(true);
    try {
        abstractAPIManager.getAllDocumentation(identifier, loggedInUser);
        Assert.fail("Registry exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Failed to get documentations for api"));
    }
    Resource resource2 = new ResourceImpl();
    resource2.setUUID(SAMPLE_RESOURCE_ID);
    Mockito.when(genericArtifactManager.getGenericArtifact(SAMPLE_RESOURCE_ID)).thenReturn(genericArtifact);
    String documentationName = "doc1";
    Collection documentCollection = new CollectionImpl();
    documentCollection.setChildren(new String[] { apiDocPath + documentationName, apiDocPath + APIConstants.INLINE_DOCUMENT_CONTENT_DIR });
    Mockito.when(registry.get(apiDocPath)).thenReturn(documentCollection);
    Mockito.when(registry.get(apiDocPath + documentationName)).thenReturn(resource2);
    PowerMockito.when(APIUtil.getDocumentation(genericArtifact, loggedInUser)).thenReturn(documentation);
    List<Documentation> documentationList = abstractAPIManager.getAllDocumentation(identifier, loggedInUser);
    Assert.assertNotNull(documentationList);
    Assert.assertEquals(documentationList.size(), 1);
    String contentPath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + RegistryConstants.PATH_SEPARATOR + APIConstants.DOC_DIR + RegistryConstants.PATH_SEPARATOR + RegistryConstants.PATH_SEPARATOR + documentationName;
    genericArtifact.setAttribute(APIConstants.DOC_SOURCE_TYPE, "In line");
    genericArtifact.setAttribute(APIConstants.DOC_NAME, documentationName);
    ResourceDO resourceDO = new ResourceDO();
    resourceDO.setLastUpdatedOn(12344567);
    Resource resource3 = new ResourceImpl(contentPath, resourceDO);
    Mockito.when(registry.get(contentPath)).thenReturn(resource3);
    documentationList = abstractAPIManager.getAllDocumentation(identifier, loggedInUser);
    Assert.assertNotNull(documentationList);
    Assert.assertEquals(documentationList.size(), 1);
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) ResourceDO(org.wso2.carbon.registry.core.jdbc.dataobjects.ResourceDO) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) CollectionImpl(org.wso2.carbon.registry.core.CollectionImpl) UserStoreException(org.wso2.carbon.user.core.UserStoreException) Collection(org.wso2.carbon.registry.core.Collection) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 30 with GenericArtifactManager

use of org.wso2.carbon.governance.api.generic.GenericArtifactManager in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testGetIcon.

@Test
public void testGetIcon() throws APIManagementException, org.wso2.carbon.user.api.UserStoreException, RegistryException {
    APIIdentifier identifier = new APIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
    int tenantId = -1234;
    UserRegistry registry = Mockito.mock(UserRegistry.class);
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapperExtended(genericArtifactManager, registryService, registry, tenantManager);
    Mockito.when(tenantManager.getTenantId(Mockito.anyString())).thenThrow(UserStoreException.class).thenReturn(tenantId);
    try {
        abstractAPIManager.getIcon(identifier);
        Assert.fail("User store exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error while loading API icon of API"));
    }
    Mockito.when(registryService.getGovernanceSystemRegistry(Mockito.anyInt())).thenThrow(RegistryException.class).thenReturn(registry);
    try {
        abstractAPIManager.getIcon(identifier);
        Assert.fail("User store exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error while loading API icon of API"));
    }
    Assert.assertNull(abstractAPIManager.getIcon(identifier));
    AbstractAPIManager abstractAPIManager1 = new AbstractAPIManagerWrapper(genericArtifactManager, registryService, registry, tenantManager);
    Mockito.when(registryService.getGovernanceUserRegistry(Mockito.anyString(), Mockito.anyInt())).thenReturn(registry);
    Assert.assertNull(abstractAPIManager1.getIcon(identifier));
    abstractAPIManager1.tenantDomain = SAMPLE_TENANT_DOMAIN_1;
    Mockito.when(registry.resourceExists(Mockito.anyString())).thenReturn(true);
    Resource resource = new ResourceImpl();
    resource.setContent("sample conetent");
    resource.setMediaType("api");
    Mockito.when(registry.get(Mockito.anyString())).thenReturn(resource);
    Assert.assertTrue(abstractAPIManager1.getIcon(identifier).getContentType().equals("api"));
}
Also used : ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserStoreException(org.wso2.carbon.user.core.UserStoreException) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)95 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)90 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)70 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)63 Registry (org.wso2.carbon.registry.core.Registry)57 Resource (org.wso2.carbon.registry.core.Resource)54 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)51 API (org.wso2.carbon.apimgt.api.model.API)37 Test (org.junit.Test)29 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)29 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)29 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)28 UserStoreException (org.wso2.carbon.user.api.UserStoreException)27 ArrayList (java.util.ArrayList)26 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)26 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)20 HashMap (java.util.HashMap)18 DocumentationPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException)17 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)16