Search in sources :

Example 41 with ResourceImpl

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

the class AbstractAPIManagerTestCase method testGetDocumentation.

@Test
public void testGetDocumentation() throws APIManagementException, RegistryException {
    Registry registry = Mockito.mock(UserRegistry.class);
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(genericArtifactManager, registry, null);
    APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
    String documentationName = "doc1";
    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 + documentationName;
    GenericArtifact genericArtifact = getGenericArtifact(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION, "sample");
    String docType = "other";
    genericArtifact.setAttribute(APIConstants.DOC_TYPE, docType);
    genericArtifact.setAttribute(APIConstants.DOC_SOURCE_TYPE, "URL");
    Resource resource = new ResourceImpl();
    resource.setUUID(SAMPLE_RESOURCE_ID);
    Mockito.when(registry.get(contentPath)).thenThrow(RegistryException.class).thenReturn(resource);
    Mockito.when(genericArtifactManager.getGenericArtifact(SAMPLE_RESOURCE_ID)).thenReturn(genericArtifact);
    try {
        abstractAPIManager.getDocumentation(identifier, DocumentationType.OTHER, documentationName);
        Assert.fail("Registry exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Failed to get documentation details"));
    }
    Assert.assertTrue(abstractAPIManager.getDocumentation(identifier, DocumentationType.OTHER, documentationName).getId().equals(genericArtifact.getId()));
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 42 with ResourceImpl

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

the class AbstractAPIManagerTestCase method testUpdateWsdl.

@Test
public void testUpdateWsdl() throws APIManagementException, RegistryException {
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
    Resource resource = new ResourceImpl();
    String resourcePath = "/test/wsdl";
    String wsdlContent = "sample wsdl";
    Mockito.when(registry.get(resourcePath)).thenThrow(RegistryException.class).thenReturn(resource);
    try {
        abstractAPIManager.updateWsdl(resourcePath, wsdlContent);
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error while updating the existing wsdl"));
    }
    try {
        abstractAPIManager.updateWsdl(resourcePath, wsdlContent);
    } catch (APIManagementException e) {
        Assert.fail("Error while updating wsdl");
    }
}
Also used : ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 43 with ResourceImpl

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

the class SequenceUtils method updateResourcePolicyFromRegistryResourceId.

/**
 * Updates resource policy resource for the given resource id from the registry.
 *
 * @param identifier API identifier
 * @param resourceId Resource identifier
 * @param content    resource policy content
 * @throws APIManagementException
 */
public static void updateResourcePolicyFromRegistryResourceId(APIIdentifier identifier, String resourceId, String content) throws APIManagementException {
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            isTenantFlowStarted = true;
            PrivilegedCarbonContext.startTenantFlow();
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
        }
        RegistryService registryService = ServiceReferenceHolder.getInstance().getRegistryService();
        int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
        APIUtil.loadTenantRegistry(tenantId);
        UserRegistry registry = registryService.getGovernanceSystemRegistry(tenantId);
        String resourcePath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + RegistryConstants.PATH_SEPARATOR + SOAPToRESTConstants.SOAP_TO_REST_RESOURCE;
        Collection collection = (Collection) registry.get(resourcePath);
        String[] resources = collection.getChildren();
        if (resources == null) {
            handleException("Cannot find any resource policies at the path: " + resourcePath);
        }
        for (String path : resources) {
            Collection resourcePolicyCollection = (Collection) registry.get(path);
            String[] resourcePolicies = resourcePolicyCollection.getChildren();
            if (resourcePolicies == null) {
                if (log.isDebugEnabled()) {
                    log.debug("Cannot find resource policies under path: " + path);
                }
                continue;
            }
            for (String resourcePolicyPath : resourcePolicies) {
                Resource resource = registry.get(resourcePolicyPath);
                if (StringUtils.isNotEmpty(resourceId) && resourceId.equals(((ResourceImpl) resource).getUUID())) {
                    resource.setContent(content);
                    resource.setMediaType(SOAPToRESTConstants.TEXT_XML);
                    registry.put(resourcePolicyPath, resource);
                    break;
                }
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Number of REST resources for " + resourcePath + " is: " + resources.length);
        }
    } catch (UserStoreException e) {
        handleException("Error while reading tenant information", e);
    } catch (RegistryException e) {
        handleException("Error when create registry instance", e);
    } catch (org.wso2.carbon.registry.api.RegistryException e) {
        handleException("Error while setting the resource policy content for the registry resource", e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
}
Also used : Resource(org.wso2.carbon.registry.api.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) UserStoreException(org.wso2.carbon.user.api.UserStoreException) Collection(org.wso2.carbon.registry.core.Collection) RegistryService(org.wso2.carbon.registry.core.service.RegistryService)

Example 44 with ResourceImpl

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

the class APIProviderImplTest method testCopyAllDocumentation.

@Test
public void testCopyAllDocumentation() throws APIManagementException, RegistryException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
    PowerMockito.when(APIUtil.getAPIDocPath(apiId)).thenReturn("oldVersion");
    Resource resource = new ResourceImpl();
    Mockito.when(apiProvider.registry.get("oldVersion")).thenReturn(resource);
    apiProvider.copyAllDocumentation(apiId, "testVersion");
    Mockito.verify(apiProvider.registry);
}
Also used : ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) 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 45 with ResourceImpl

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

the class APIProviderImplTest method testSaveGraphqlSchemaDefinition.

@Test
public void testSaveGraphqlSchemaDefinition() throws Exception {
    Resource resource = new ResourceImpl();
    String resourcePath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + "admin" + RegistryConstants.PATH_SEPARATOR + "API1" + RegistryConstants.PATH_SEPARATOR + "1.0.0" + RegistryConstants.PATH_SEPARATOR;
    String schemaContent = "sample schema";
    APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
    API api = new API(apiId);
    Mockito.when(APIUtil.getGraphqlDefinitionFilePath("API1", "1.0.0", "admin")).thenReturn(resourcePath);
    Resource resourceMock = Mockito.mock(Resource.class);
    resourceMock.setContent(schemaContent);
    resourceMock.setMediaType(String.valueOf(ContentType.TEXT_PLAIN));
    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 userRegistry = Mockito.mock(UserRegistry.class);
    Mockito.when(userRegistry.newResource()).thenReturn(resource);
    PowerMockito.doNothing().when(APIUtil.class, "clearResourcePermissions", Mockito.any(), Mockito.any(), Mockito.anyInt());
    PowerMockito.doNothing().when(APIUtil.class, "setResourcePermissions", Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
    GraphQLSchemaDefinition graphQLSchemaDefinition = Mockito.mock(GraphQLSchemaDefinition.class);
    PowerMockito.doCallRealMethod().when(graphQLSchemaDefinition).saveGraphQLSchemaDefinition(api, schemaContent, userRegistry);
    // org.wso2.carbon.registry.api.RegistryException
    Mockito.doThrow(RegistryException.class).when(registry).put(Matchers.anyString(), any(Resource.class));
    try {
        graphQLSchemaDefinition.saveGraphQLSchemaDefinition(api, schemaContent, registry);
    } catch (APIManagementException e) {
        String msg = "Error while adding Graphql Definition for API1-1.0.0";
        Assert.assertEquals(msg, e.getMessage());
    }
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) 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) GraphQLSchemaDefinition(org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Resource (org.wso2.carbon.registry.core.Resource)43 ResourceImpl (org.wso2.carbon.registry.core.ResourceImpl)43 Test (org.junit.Test)37 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)36 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)20 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)20 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)20 Registry (org.wso2.carbon.registry.core.Registry)17 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)17 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)16 Matchers.anyString (org.mockito.Matchers.anyString)12 Collection (org.wso2.carbon.registry.core.Collection)12 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)10 API (org.wso2.carbon.apimgt.api.model.API)9 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)9 CollectionImpl (org.wso2.carbon.registry.core.CollectionImpl)9 Tag (org.wso2.carbon.registry.core.Tag)9 ResourceDO (org.wso2.carbon.registry.core.jdbc.dataobjects.ResourceDO)9 InputStream (java.io.InputStream)7 ArrayInputStream (org.apache.derby.iapi.services.io.ArrayInputStream)6