Search in sources :

Example 1 with GraphQLPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method saveGraphQLSchemaDefinition.

@Override
public void saveGraphQLSchemaDefinition(Organization org, String apiId, String schemaDefinition) throws GraphQLPersistenceException {
    boolean tenantFlowStarted = false;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        Registry registry = holder.getRegistry();
        tenantFlowStarted = holder.isTenantFlowStarted();
        BasicAPI api = getbasicAPIInfo(apiId, registry);
        if (api == null) {
            throw new GraphQLPersistenceException("API not foud ", ExceptionCodes.API_NOT_FOUND);
        }
        String path = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + api.apiProvider + RegistryConstants.PATH_SEPARATOR + api.apiName + RegistryConstants.PATH_SEPARATOR + api.apiVersion + RegistryConstants.PATH_SEPARATOR;
        String saveResourcePath = path + api.apiProvider + APIConstants.GRAPHQL_SCHEMA_PROVIDER_SEPERATOR + api.apiName + api.apiVersion + APIConstants.GRAPHQL_SCHEMA_FILE_EXTENSION;
        Resource resource;
        if (!registry.resourceExists(saveResourcePath)) {
            resource = registry.newResource();
        } else {
            resource = registry.get(saveResourcePath);
        }
        resource.setContent(schemaDefinition);
        resource.setMediaType(String.valueOf(ContentType.TEXT_PLAIN));
        registry.put(saveResourcePath, resource);
        if (log.isDebugEnabled()) {
            log.debug("Successfully imported the schema: " + schemaDefinition);
        }
        // Need to set anonymous if the visibility is public
        RegistryPersistenceUtil.clearResourcePermissions(saveResourcePath, new APIIdentifier(api.apiProvider, api.apiName, api.apiVersion), ((UserRegistry) registry).getTenantId());
        RegistryPersistenceUtil.setResourcePermissions(api.apiProvider, api.visibility, api.visibleRoles, saveResourcePath);
    } catch (RegistryException | APIManagementException | APIPersistenceException e) {
        throw new GraphQLPersistenceException("Error while adding Graphql Definition for api " + apiId, e);
    } finally {
        if (tenantFlowStarted) {
            RegistryPersistenceUtil.endTenantFlow();
        }
    }
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) 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) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 2 with GraphQLPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method getGraphQLSchema.

@Override
public String getGraphQLSchema(Organization org, String apiId) throws GraphQLPersistenceException {
    boolean tenantFlowStarted = false;
    String schemaDoc = null;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        Registry registry = holder.getRegistry();
        tenantFlowStarted = holder.isTenantFlowStarted();
        BasicAPI api = getbasicAPIInfo(apiId, registry);
        if (api == null) {
            throw new GraphQLPersistenceException("API not foud ", ExceptionCodes.API_NOT_FOUND);
        }
        String apiPath = GovernanceUtils.getArtifactPath(registry, apiId);
        int prependIndex = apiPath.lastIndexOf("/api");
        String apiSourcePath = apiPath.substring(0, prependIndex);
        String schemaName = api.apiProvider + APIConstants.GRAPHQL_SCHEMA_PROVIDER_SEPERATOR + api.apiName + api.apiVersion + APIConstants.GRAPHQL_SCHEMA_FILE_EXTENSION;
        String schemaResourcePath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + schemaName;
        if (registry.resourceExists(schemaResourcePath)) {
            Resource schemaResource = registry.get(schemaResourcePath);
            schemaDoc = IOUtils.toString(schemaResource.getContentStream(), RegistryConstants.DEFAULT_CHARSET_ENCODING);
        }
    } catch (APIPersistenceException | RegistryException | IOException e) {
        throw new GraphQLPersistenceException("Error while accessing graphql schema definition ", e);
    } finally {
        if (tenantFlowStarted) {
            RegistryPersistenceUtil.endTenantFlow();
        }
    }
    return schemaDoc;
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) IOException(java.io.IOException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 3 with GraphQLPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException in project carbon-apimgt by wso2.

the class RegistryPersistenceImplTestCase method testGetGraphQLSchema.

@Test
public void testGetGraphQLSchema() throws GraphQLPersistenceException, RegistryException {
    Registry registry = Mockito.mock(Registry.class);
    GenericArtifact artifact = PersistenceHelper.getSampleAPIArtifact();
    String apiUUID = artifact.getId();
    String apiProviderName = artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
    apiProviderName = RegistryPersistenceUtil.replaceEmailDomain(apiProviderName);
    String apiName = artifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
    String apiVersion = artifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
    String path = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion + RegistryConstants.PATH_SEPARATOR;
    String schemaName = apiProviderName + APIConstants.GRAPHQL_SCHEMA_PROVIDER_SEPERATOR + apiName + apiVersion + APIConstants.GRAPHQL_SCHEMA_FILE_EXTENSION;
    String schemaResourePath = path + schemaName;
    String apiPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion + RegistryConstants.PATH_SEPARATOR + "api";
    PowerMockito.when(GovernanceUtils.getArtifactPath(registry, apiUUID)).thenReturn(apiPath);
    String schema = "{\n" + "  hero {\n" + "    name\n" + "    friends {\n" + "      name\n" + "    }\n" + "  }\n" + "}";
    Organization org = new Organization(SUPER_TENANT_DOMAIN);
    APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(registry, artifact);
    Mockito.when(registry.resourceExists(schemaResourePath)).thenReturn(true);
    Resource oasResource = new ResourceImpl();
    oasResource.setContent(schema.getBytes());
    Mockito.when(registry.get(schemaResourePath)).thenReturn(oasResource);
    String def = apiPersistenceInstance.getGraphQLSchema(org, apiUUID);
    Assert.assertEquals("API graphql schema does not match", schema, def);
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Registry (org.wso2.carbon.registry.core.Registry)3 Resource (org.wso2.carbon.registry.core.Resource)3 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)3 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)2 GraphQLPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException)2 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)2 IOException (java.io.IOException)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)1 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)1 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)1 ResourceImpl (org.wso2.carbon.registry.core.ResourceImpl)1