Search in sources :

Example 6 with Association

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

the class RegistryPersistenceUtilTestCase method testGetArtifactManager.

@Test
public void testGetArtifactManager() throws RegistryException, APIPersistenceException {
    Registry registry = Mockito.mock(UserRegistry.class);
    GovernanceArtifactConfiguration conf = Mockito.mock(GovernanceArtifactConfiguration.class);
    PowerMockito.when(GovernanceUtils.findGovernanceArtifactConfiguration(APIConstants.API_KEY, registry)).thenReturn(conf);
    Association[] assosiations = new Association[0];
    Mockito.when(conf.getRelationshipDefinitions()).thenReturn(assosiations);
    GenericArtifactManager manager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
    Assert.assertNotNull("Manager is null", manager);
}
Also used : Association(org.wso2.carbon.registry.core.Association) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) GovernanceArtifactConfiguration(org.wso2.carbon.governance.api.util.GovernanceArtifactConfiguration) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with Association

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

the class APIProviderImplTest method testRemoveDocumentation.

@Test
public void testRemoveDocumentation() throws APIManagementException, RegistryException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
    PowerMockito.when(APIUtil.getAPIDocPath(apiId) + "testDoc").thenReturn("testPath");
    Resource resource = Mockito.mock(Resource.class);
    Mockito.when(apiProvider.registry.get("testPathtestDoc")).thenReturn(resource);
    Mockito.when(resource.getUUID()).thenReturn("1111");
    PowerMockito.when(APIUtil.getArtifactManager(apiProvider.registry, APIConstants.DOCUMENTATION_KEY)).thenReturn(artifactManager);
    GenericArtifact genericArtifact = Mockito.mock(GenericArtifact.class);
    Mockito.when(artifactManager.getGenericArtifact("1111")).thenReturn(genericArtifact);
    Mockito.when(genericArtifact.getAttribute(APIConstants.DOC_FILE_PATH)).thenReturn("testDocPath");
    Association association = Mockito.mock(Association.class);
    Association[] associations = new Association[] { association };
    Mockito.when(apiProvider.registry.getAssociations("testPathtestDoc", APIConstants.DOCUMENTATION_KEY)).thenReturn(associations);
    apiProvider.removeDocumentation(apiId, "testDoc", "testType", "carbon.super");
    Mockito.verify(apiProvider.registry);
}
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) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 8 with Association

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

the class APIProviderImplTest method testRemoveDocumentation1.

@Test
public void testRemoveDocumentation1() throws APIManagementException, RegistryException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, null, null);
    APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
    PowerMockito.when(APIUtil.getArtifactManager(apiProvider.registry, APIConstants.DOCUMENTATION_KEY)).thenReturn(artifactManager);
    GenericArtifact artifact = Mockito.mock(GenericArtifact.class);
    Mockito.when(artifactManager.getGenericArtifact("testId")).thenReturn(artifact);
    Mockito.when(artifact.getPath()).thenReturn("docPath");
    Mockito.when(artifact.getAttribute(APIConstants.DOC_FILE_PATH)).thenReturn("docFilePath");
    Association association = Mockito.mock(Association.class);
    Association[] associations = new Association[] { association };
    Mockito.when(apiProvider.registry.getAssociations("docPath", APIConstants.DOCUMENTATION_KEY)).thenReturn(associations);
    apiProvider.removeDocumentation(apiId, "testId", "carbon.super");
    Mockito.verify(apiProvider.registry);
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) Association(org.wso2.carbon.registry.core.Association) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 9 with Association

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

the class APIProviderImpl method createDocumentation.

/**
 * Create a product documentation
 *
 * @param product           APIProduct
 * @param documentation Documentation
 * @throws APIManagementException if failed to add documentation
 */
private void createDocumentation(APIProduct product, Documentation documentation) throws APIManagementException {
    try {
        APIProductIdentifier productId = product.getId();
        GenericArtifactManager artifactManager = new GenericArtifactManager(registry, APIConstants.DOCUMENTATION_KEY);
        GenericArtifact artifact = artifactManager.newGovernanceArtifact(new QName(documentation.getName()));
        artifactManager.addGenericArtifact(APIUtil.createDocArtifactContent(artifact, productId, documentation));
        String productPath = APIUtil.getAPIProductPath(productId);
        // Adding association from api to documentation . (API Product -----> doc)
        registry.addAssociation(productPath, artifact.getPath(), APIConstants.DOCUMENTATION_ASSOCIATION);
        String docVisibility = documentation.getVisibility().name();
        String[] authorizedRoles = getAuthorizedRoles(productPath);
        String visibility = product.getVisibility();
        if (docVisibility != null) {
            if (APIConstants.DOC_SHARED_VISIBILITY.equalsIgnoreCase(docVisibility)) {
                authorizedRoles = null;
                visibility = APIConstants.DOC_SHARED_VISIBILITY;
            } else if (APIConstants.DOC_OWNER_VISIBILITY.equalsIgnoreCase(docVisibility)) {
                authorizedRoles = null;
                visibility = APIConstants.DOC_OWNER_VISIBILITY;
            }
        }
        APIUtil.setResourcePermissions(product.getId().getProviderName(), visibility, authorizedRoles, artifact.getPath(), registry);
        String docFilePath = artifact.getAttribute(APIConstants.DOC_FILE_PATH);
        if (docFilePath != null && !StringUtils.EMPTY.equals(docFilePath)) {
            // The docFilePatch comes as /t/tenanatdoman/registry/resource/_system/governance/apimgt/applicationdata..
            // We need to remove the /t/tenanatdoman/registry/resource/_system/governance section to set permissions.
            int startIndex = docFilePath.indexOf(APIConstants.GOVERNANCE) + (APIConstants.GOVERNANCE).length();
            String filePath = docFilePath.substring(startIndex, docFilePath.length());
            APIUtil.setResourcePermissions(product.getId().getProviderName(), visibility, authorizedRoles, filePath, registry);
            registry.addAssociation(artifact.getPath(), filePath, APIConstants.DOCUMENTATION_FILE_ASSOCIATION);
        }
        documentation.setId(artifact.getId());
    } catch (RegistryException e) {
        handleException("Failed to add documentation", e);
    } catch (UserStoreException e) {
        handleException("Failed to add documentation", e);
    }
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) QName(javax.xml.namespace.QName) UserStoreException(org.wso2.carbon.user.api.UserStoreException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 10 with Association

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

the class APIProviderImpl method getAPIsByProvider.

/**
 * Get a list of APIs published by the given provider. If a given API has multiple APIs,
 * only the latest version will
 * be included in this list.
 *
 * @param providerId , provider id
 * @return set of API
 * @throws org.wso2.carbon.apimgt.api.APIManagementException if failed to get set of API
 */
@Override
public List<API> getAPIsByProvider(String providerId) throws APIManagementException {
    List<API> apiSortedList = new ArrayList<API>();
    try {
        providerId = APIUtil.replaceEmailDomain(providerId);
        String providerPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + providerId;
        GenericArtifactManager artifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
        Association[] associations = registry.getAssociations(providerPath, APIConstants.PROVIDER_ASSOCIATION);
        for (Association association : associations) {
            String apiPath = association.getDestinationPath();
            if (registry.resourceExists(apiPath)) {
                Resource resource = registry.get(apiPath);
                String apiArtifactId = resource.getUUID();
                if (apiArtifactId != null) {
                    GenericArtifact apiArtifact = artifactManager.getGenericArtifact(apiArtifactId);
                    if (apiArtifact != null) {
                        String type = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_TYPE);
                        if (!APIConstants.API_PRODUCT.equals(type)) {
                            apiSortedList.add(getAPI(apiArtifact));
                        }
                    }
                } else {
                    throw new GovernanceException("artifact id is null of " + apiPath);
                }
            }
        }
    } catch (RegistryException e) {
        handleException("Failed to get APIs for provider : " + providerId, e);
    }
    Collections.sort(apiSortedList, new APINameComparator());
    return apiSortedList;
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) Association(org.wso2.carbon.registry.core.Association) ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) APINameComparator(org.wso2.carbon.apimgt.impl.utils.APINameComparator) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Aggregations

Association (org.wso2.carbon.registry.core.Association)10 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)8 Resource (org.wso2.carbon.registry.core.Resource)7 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)5 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 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)4 UserStoreException (org.wso2.carbon.user.api.UserStoreException)4 ArrayList (java.util.ArrayList)3 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)3 Registry (org.wso2.carbon.registry.core.Registry)3 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)3 List (java.util.List)2 QName (javax.xml.namespace.QName)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 APIResource (org.wso2.carbon.apimgt.api.doc.model.APIResource)2 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)2