Search in sources :

Example 56 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class APIProviderImplTest method testCreateNewAPIVersion_ForDefaultVersion.

@Ignore
@Test
public void testCreateNewAPIVersion_ForDefaultVersion() throws Exception {
    // Create Original API
    APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
    API api = new API(apiId);
    api.setContext("/test");
    api.setVisibility("Public");
    api.setStatus(APIConstants.CREATED);
    api.setAsDefaultVersion(true);
    String newVersion = "1.0.1";
    // Create new API object
    APIIdentifier newApiId = new APIIdentifier("admin", "API1", "1.0.1");
    final API newApi = new API(newApiId);
    newApi.setStatus(APIConstants.CREATED);
    newApi.setContext("/test");
    // Mock API as a default version
    Mockito.when(apimgtDAO.getDefaultVersion(apiId)).thenReturn("1.0.0");
    final APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, new ArrayList<Documentation>(), null);
    Mockito.when(artifactManager.newGovernanceArtifact(any(QName.class))).thenReturn(artifact);
    Mockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
    GenericArtifact artifactNew = Mockito.mock(GenericArtifact.class);
    Mockito.when(APIUtil.createAPIArtifactContent(artifact, newApi)).thenReturn(artifactNew);
    RegistryService rs = Mockito.mock(RegistryService.class);
    UserRegistry ur = Mockito.mock(UserRegistry.class);
    ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
    RealmService realmService = Mockito.mock(RealmService.class);
    TenantManager tenantManager = Mockito.mock(TenantManager.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(rs);
    Mockito.when(rs.getConfigSystemRegistry(Mockito.anyInt())).thenReturn(ur);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    PublisherAPI publisherAPI = Mockito.mock(PublisherAPI.class);
    PowerMockito.when(apiPersistenceInstance.addAPI(any(Organization.class), any(PublisherAPI.class))).thenReturn(publisherAPI);
    apiProvider.addAPI(api);
    String targetPath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + api.getId().getProviderName() + RegistryConstants.PATH_SEPARATOR + api.getId().getApiName() + RegistryConstants.PATH_SEPARATOR + newVersion + APIConstants.API_RESOURCE_NAME;
    String apiSourcePath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + apiId.getProviderName() + RegistryConstants.PATH_SEPARATOR + apiId.getApiName() + RegistryConstants.PATH_SEPARATOR + apiId.getVersion() + APIConstants.API_RESOURCE_NAME;
    PowerMockito.when(APIUtil.getAPIPath(apiId)).thenReturn(apiSourcePath);
    String apiSourceUUID = "87ty543-899hyt";
    Mockito.when(apiProvider.registry.resourceExists(targetPath)).thenReturn(false);
    Resource apiSourceArtifact = Mockito.mock(Resource.class);
    Mockito.when(apiProvider.registry.get(apiSourcePath)).thenReturn(apiSourceArtifact);
    // Mock API as a default version
    Mockito.when(apimgtDAO.getDefaultVersion(apiId)).thenReturn("1.0.0");
    Mockito.when(apiSourceArtifact.getUUID()).thenReturn(apiSourceUUID);
    Mockito.doNothing().when(artifactManager).updateGenericArtifact(artifact);
    // Mocking Old API retrieval
    Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("PUBLISHED");
    Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT)).thenReturn("test");
    Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT_TEMPLATE)).thenReturn("test/{version}");
    Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_WEBSOCKET)).thenReturn("false");
    Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES)).thenReturn("admin, subscriber");
    Mockito.when(artifactManager.getGenericArtifact(apiSourceUUID)).thenReturn(artifact);
    // Mocking no thumbnail case
    String thumbUrl = APIConstants.API_IMAGE_LOCATION + RegistryConstants.PATH_SEPARATOR + api.getId().getProviderName() + RegistryConstants.PATH_SEPARATOR + api.getId().getApiName() + RegistryConstants.PATH_SEPARATOR + api.getId().getVersion() + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
    Mockito.when(apiProvider.registry.resourceExists(thumbUrl)).thenReturn(false);
    // Mocking In sequence retrieval
    String inSeqFilePath = "API1/1.0.0/in";
    PowerMockito.when(APIUtil.getSequencePath(api.getId(), "in")).thenReturn(inSeqFilePath);
    Mockito.when(apiProvider.registry.resourceExists(inSeqFilePath)).thenReturn(false);
    // Mocking Out sequence retrieval
    String outSeqFilePath = "API1/1.0.0/out";
    PowerMockito.when(APIUtil.getSequencePath(api.getId(), "out")).thenReturn(outSeqFilePath);
    Mockito.when(apiProvider.registry.resourceExists(outSeqFilePath)).thenReturn(false);
    // Mock Adding new API artifact with new version
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            apiProvider.createAPI(newApi);
            return null;
        }
    }).when(artifactManager).addGenericArtifact(artifact);
    Mockito.doNothing().when(artifact).attachLifecycle(APIConstants.API_LIFE_CYCLE);
    PowerMockito.when(APIUtil.getAPIProviderPath(api.getId())).thenReturn("/dummy/provider/path");
    Mockito.doNothing().when(apiProvider.registry).addAssociation("/dummy/provider/path", targetPath, APIConstants.PROVIDER_ASSOCIATION);
    PowerMockito.when(GovernanceUtils.getArtifactPath(apiProvider.registry, artifact.getId())).thenReturn(artifactPath);
    PowerMockito.doNothing().when(APIUtil.class);
    String[] roles = { "admin", "subscriber" };
    APIUtil.setResourcePermissions("admin", "Public", roles, artifactPath);
    // Mock no tags case
    Mockito.when(apiProvider.registry.getTags(apiSourcePath)).thenReturn(null);
    // Mock new API retrieval
    String newApiPath = "API1/1.0.1/";
    PowerMockito.when(APIUtil.getAPIPath(newApi.getId())).thenReturn(newApiPath);
    String newApiUUID = "87ty543-899hy23";
    GenericArtifact newArtifact = Mockito.mock(GenericArtifact.class);
    Resource newApiResource = Mockito.mock(Resource.class);
    Mockito.when(newApiResource.getUUID()).thenReturn(newApiUUID);
    Mockito.when(apiProvider.registry.get(newApiPath)).thenReturn(newApiResource);
    Mockito.when(artifactManager.getGenericArtifact(newApiUUID)).thenReturn(newArtifact);
    PowerMockito.when(APIUtil.getAPI(newArtifact, apiProvider.registry, api.getId(), "test")).thenReturn(newApi);
    String resourcePath = APIUtil.getOpenAPIDefinitionFilePath(api.getId().getApiName(), api.getId().getVersion(), api.getId().getProviderName());
    Mockito.when(apiProvider.registry.resourceExists(resourcePath + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME)).thenReturn(false);
    // Mock Config system registry
    ServiceReferenceHolder sh = TestUtils.getServiceReferenceHolder();
    RegistryService registryService = Mockito.mock(RegistryService.class);
    PowerMockito.when(sh.getRegistryService()).thenReturn(registryService);
    PowerMockito.when(apiPersistenceInstance.getPublisherAPI(any(Organization.class), any(String.class))).thenReturn(publisherAPI);
    // apiProvider.createNewAPIVersion(api, newVersion);
    apiProvider.createNewAPIVersion(apiSourceUUID, newVersion, true, superTenantDomain);
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) QName(javax.xml.namespace.QName) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RealmService(org.wso2.carbon.user.core.service.RealmService) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) 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) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) Ignore(org.junit.Ignore) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 57 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class APIProviderImplTest method testUpdateAPI_InCreatedState.

@Test
public void testUpdateAPI_InCreatedState() throws Exception {
    APIIdentifier identifier = new APIIdentifier("admin-AT-carbon.super", "API1", "1.0.0");
    Set<String> environments = new HashSet<String>();
    Set<URITemplate> uriTemplates = new HashSet<URITemplate>();
    Set<URITemplate> newUriTemplates = new HashSet<URITemplate>();
    Tier tier = new Tier("Gold");
    Map<String, Tier> tiers = new TreeMap<>();
    tiers.put("Gold", tier);
    URITemplate uriTemplate1 = new URITemplate();
    uriTemplate1.setHTTPVerb("POST");
    uriTemplate1.setAuthType("Application");
    uriTemplate1.setUriTemplate("/add");
    uriTemplate1.setThrottlingTier("Gold");
    uriTemplates.add(uriTemplate1);
    URITemplate uriTemplate2 = new URITemplate();
    uriTemplate2.setHTTPVerb("PUT");
    uriTemplate2.setAuthType("Application");
    uriTemplate2.setUriTemplate("/update");
    uriTemplate2.setThrottlingTier("Gold");
    newUriTemplates.add(uriTemplate1);
    newUriTemplates.add(uriTemplate2);
    final API api = new API(identifier);
    api.setStatus(APIConstants.CREATED);
    api.setVisibility("public");
    api.setAccessControl("all");
    api.setTransports("http,https");
    api.setContext("/test");
    api.setEnvironments(environments);
    api.setUriTemplates(newUriTemplates);
    api.setOrganization("carbon.super");
    API oldApi = new API(identifier);
    oldApi.setStatus(APIConstants.CREATED);
    oldApi.setVisibility("public");
    oldApi.setAccessControl("all");
    oldApi.setContext("/test");
    oldApi.setEnvironments(environments);
    api.setUriTemplates(uriTemplates);
    oldApi.setOrganization("carbon.super");
    List<Documentation> documentationList = getDocumentationList();
    Documentation documentation = documentationList.get(1);
    Mockito.when(APIUtil.getAPIDocPath(api.getId())).thenReturn(documentation.getFilePath());
    APIProviderImplWrapper apiProviderImplWrapper = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO);
    Resource docResource = Mockito.mock(Resource.class);
    Mockito.when(docResource.getUUID()).thenReturn(documentation.getId());
    Mockito.when(apiProviderImplWrapper.registry.get(documentation.getFilePath())).thenReturn(docResource);
    GenericArtifact docArtifact = Mockito.mock(GenericArtifact.class);
    Mockito.when(artifactManager.getGenericArtifact(documentation.getId())).thenReturn(docArtifact);
    Mockito.when(APIUtil.getDocumentation(docArtifact)).thenReturn(documentation);
    Mockito.when(docArtifact.getPath()).thenReturn(artifactPath);
    PowerMockito.doNothing().when(APIUtil.class, "clearResourcePermissions", Mockito.any(), Mockito.any(), Mockito.anyInt());
    String[] roles = { "admin", "subscriber" };
    APIUtil.setResourcePermissions("admin", "Public", roles, artifactPath);
    Mockito.when(docArtifact.getAttribute(APIConstants.DOC_FILE_PATH)).thenReturn("docFilePath");
    final APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, documentationList, null);
    RegistryService registryService = Mockito.mock(RegistryService.class);
    UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
    ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
    RealmService realmService = Mockito.mock(RealmService.class);
    TenantManager tenantManager = Mockito.mock(TenantManager.class);
    Mockito.when(artifactManager.newGovernanceArtifact(any(QName.class))).thenReturn(artifact);
    Mockito.when(APIUtil.createAPIArtifactContent(artifact, oldApi)).thenReturn(artifact);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    Mockito.when(registryService.getConfigSystemRegistry(Mockito.anyInt())).thenReturn(userRegistry);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    PublisherAPI publisherAPI = Mockito.mock(PublisherAPI.class);
    PowerMockito.when(apiPersistenceInstance.addAPI(any(Organization.class), any(PublisherAPI.class))).thenReturn(publisherAPI);
    apiProvider.addAPI(oldApi);
    // mock has permission
    Resource apiSourceArtifact = Mockito.mock(Resource.class);
    Mockito.when(apiSourceArtifact.getUUID()).thenReturn("12640983654");
    String apiSourcePath = "path";
    PowerMockito.when(APIUtil.getAPIPath(api.getId())).thenReturn(apiSourcePath);
    PowerMockito.when(APIUtil.getAPIPath(oldApi.getId())).thenReturn(apiSourcePath);
    PowerMockito.when(apiProvider.registry.get(apiSourcePath)).thenReturn(apiSourceArtifact);
    // API Status is CREATED and user has permission
    Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("CREATED");
    Mockito.when(artifactManager.getGenericArtifact(apiSourceArtifact.getUUID())).thenReturn(artifact);
    PowerMockito.when(APIUtil.hasPermission(null, APIConstants.Permissions.API_PUBLISH)).thenReturn(true);
    PowerMockito.when(APIUtil.hasPermission(null, APIConstants.Permissions.API_CREATE)).thenReturn(true);
    Mockito.when(apimgtDAO.getDefaultVersion(identifier)).thenReturn("1.0.0");
    Mockito.when(apimgtDAO.getPublishedDefaultVersion(identifier)).thenReturn("1.0.0");
    // updateDefaultAPIInRegistry
    String defaultAPIPath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + APIConstants.API_RESOURCE_NAME;
    Resource defaultAPISourceArtifact = Mockito.mock(Resource.class);
    String defaultAPIUUID = "12640983600";
    Mockito.when(defaultAPISourceArtifact.getUUID()).thenReturn(defaultAPIUUID);
    Mockito.when(apiProvider.registry.get(defaultAPIPath)).thenReturn(defaultAPISourceArtifact);
    GenericArtifact defaultAPIArtifact = Mockito.mock(GenericArtifact.class);
    Mockito.when(artifactManager.getGenericArtifact(defaultAPIUUID)).thenReturn(defaultAPIArtifact);
    Mockito.doNothing().when(artifactManager).updateGenericArtifact(defaultAPIArtifact);
    TestUtils.mockAPIMConfiguration(APIConstants.API_GATEWAY_TYPE, APIConstants.API_GATEWAY_TYPE_SYNAPSE, -1234);
    // updateApiArtifact
    PowerMockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
    Mockito.when(artifact.getId()).thenReturn("12640983654");
    PowerMockito.when(GovernanceUtils.getArtifactPath(apiProvider.registry, "12640983654")).thenReturn(apiSourcePath);
    // Mock Updating API
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            apiProvider.createAPI(api);
            return null;
        }
    }).when(artifactManager).updateGenericArtifact(artifact);
    APIManagerConfiguration config = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
    GatewayArtifactSynchronizerProperties synchronizerProperties = new GatewayArtifactSynchronizerProperties();
    Mockito.when(config.getGatewayArtifactSynchronizerProperties()).thenReturn(synchronizerProperties);
    PowerMockito.when(apiPersistenceInstance.getPublisherAPI(any(Organization.class), any(String.class))).thenReturn(publisherAPI);
    Mockito.when(APIUtil.getTiers(APIConstants.TIER_RESOURCE_TYPE, "carbon.super")).thenReturn(tiers);
    apiProvider.updateAPI(api, oldApi);
    Assert.assertEquals(0, api.getEnvironments().size());
    tiers.remove("Gold", tier);
    tier = new Tier("Unlimited");
    tiers.put("Unlimited", tier);
    try {
        apiProvider.updateAPI(api, oldApi);
    } catch (APIManagementException ex) {
        Assert.assertTrue(ex.getMessage().contains("Invalid x-throttling tier Gold found in api definition for " + "resource POST /add"));
    }
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) GatewayArtifactSynchronizerProperties(org.wso2.carbon.apimgt.impl.dto.GatewayArtifactSynchronizerProperties) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) HashSet(java.util.HashSet) GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) Tier(org.wso2.carbon.apimgt.api.model.Tier) QName(javax.xml.namespace.QName) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) TreeMap(java.util.TreeMap) RealmService(org.wso2.carbon.user.core.service.RealmService) InvocationOnMock(org.mockito.invocation.InvocationOnMock) 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) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 58 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class APIProviderImplTest method testGetCustomFaultSequences1.

@Test
public void testGetCustomFaultSequences1() throws Exception {
    APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    mockSequences(APIConstants.API_CUSTOM_FAULTSEQUENCE_LOCATION, APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT, apiId);
    List<String> sequenceList = apiProvider.getCustomFaultSequences();
    Assert.assertNotNull(sequenceList);
    Assert.assertEquals(1, sequenceList.size());
    // OMException when building OMElement
    PowerMockito.when(APIUtil.buildOMElement(any(InputStream.class))).thenThrow(new OMException());
    apiProvider.getCustomFaultSequences();
    // org.wso2.carbon.registry.api.RegistryException
    ServiceReferenceHolder sh = PowerMockito.mock(ServiceReferenceHolder.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(sh);
    RegistryService registryService = Mockito.mock(RegistryService.class);
    PowerMockito.when(sh.getRegistryService()).thenReturn(registryService);
    UserRegistry registry = Mockito.mock(UserRegistry.class);
    PowerMockito.when(registryService.getGovernanceSystemRegistry(Matchers.anyInt())).thenReturn(registry);
    Mockito.when(registry.resourceExists(APIConstants.API_CUSTOM_FAULTSEQUENCE_LOCATION)).thenThrow(org.wso2.carbon.registry.api.RegistryException.class);
    String msg = "Error while processing the fault in the registry";
    try {
        apiProvider.getCustomFaultSequences();
    } catch (APIManagementException e) {
        Assert.assertEquals(msg, e.getMessage());
    }
    // Registry Exception
    PowerMockito.when(registryService.getGovernanceSystemRegistry(Matchers.anyInt())).thenThrow(RegistryException.class);
    String msg1 = "Error while retrieving registry for tenant -1";
    try {
        apiProvider.getCustomFaultSequences();
    } catch (APIManagementException e) {
        Assert.assertEquals(msg1, e.getMessage());
    }
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) InputStream(java.io.InputStream) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) OMException(org.apache.axiom.om.OMException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 59 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry 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 60 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry 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

UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)147 Resource (org.wso2.carbon.registry.core.Resource)96 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)86 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)84 Test (org.junit.Test)75 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)75 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)66 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)65 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)53 API (org.wso2.carbon.apimgt.api.model.API)47 UserStoreException (org.wso2.carbon.user.api.UserStoreException)44 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)43 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)41 Registry (org.wso2.carbon.registry.core.Registry)39 IOException (java.io.IOException)38 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)37 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)34 ArrayList (java.util.ArrayList)33 QName (javax.xml.namespace.QName)32 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)32