Search in sources :

Example 21 with APIProductResource

use of org.wso2.carbon.apimgt.api.model.APIProductResource in project carbon-apimgt by wso2.

the class OASParserUtilTest method testSyncOpenAPIResourcePaths.

@Test
public void testSyncOpenAPIResourcePaths() throws Exception {
    String calculatorSwaggerString = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("definitions" + File.separator + "calculator_scopes_v3.json"), "UTF-8");
    String calcSmallSwaggerString = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("definitions" + File.separator + "calc_small_v3.json"), "UTF-8");
    final String verb = "POST";
    final String existingPathString = "/add";
    final String nonExistingPathString = "/divide";
    API api = Mockito.mock(API.class);
    Mockito.when(api.getSwaggerDefinition()).thenReturn(calculatorSwaggerString);
    APIProductResource apiProductResource = Mockito.mock(APIProductResource.class);
    URITemplate uriTemplate = Mockito.mock(URITemplate.class);
    Mockito.when(apiProductResource.getUriTemplate()).thenReturn(uriTemplate);
    Mockito.when(uriTemplate.getHTTPVerb()).thenReturn(verb);
    Mockito.when(uriTemplate.getUriTemplate()).thenReturn(existingPathString);
    Map<API, List<APIProductResource>> apiToProductResourceMapping = new HashMap<>();
    List<APIProductResource> resources = new ArrayList<>();
    resources.add(apiProductResource);
    apiToProductResourceMapping.put(api, resources);
    String updatedCalcSmallSwaggerString = OASParserUtil.updateAPIProductSwaggerOperations(apiToProductResourceMapping, calcSmallSwaggerString);
    JSONObject calculatorSwagger = new JSONObject(calculatorSwaggerString);
    JSONObject updatedCalcSmallSwagger = new JSONObject(updatedCalcSmallSwaggerString);
    JSONObject calculatorPaths = (JSONObject) calculatorSwagger.get(APIConstants.SWAGGER_PATHS);
    JSONObject smallCalcPaths = (JSONObject) updatedCalcSmallSwagger.get(APIConstants.SWAGGER_PATHS);
    JSONObject calculatorPath = (JSONObject) calculatorPaths.get(existingPathString);
    JSONObject smallCalculatorPath = (JSONObject) smallCalcPaths.get(existingPathString);
    // Check if both paths match
    Assert.assertEquals(calculatorPath.toString(), smallCalculatorPath.toString());
    // Ensure that an originally non existing path was not added
    Assert.assertFalse(smallCalcPaths.has(nonExistingPathString));
}
Also used : JSONObject(org.json.JSONObject) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) HashMap(java.util.HashMap) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ArrayList(java.util.ArrayList) API(org.wso2.carbon.apimgt.api.model.API) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 22 with APIProductResource

use of org.wso2.carbon.apimgt.api.model.APIProductResource in project carbon-apimgt by wso2.

the class OASParserUtil method extractRelevantSourceData.

private static void extractRelevantSourceData(Map<API, List<APIProductResource>> apiToProductResourceMapping, SwaggerUpdateContext context) throws APIManagementException {
    // Extract Paths that exist in the destination swagger from the source swagger
    for (Map.Entry<API, List<APIProductResource>> mappingEntry : apiToProductResourceMapping.entrySet()) {
        String sourceSwagger = mappingEntry.getKey().getSwaggerDefinition();
        SwaggerVersion sourceSwaggerVersion = getSwaggerVersion(sourceSwagger);
        if (sourceSwaggerVersion == SwaggerVersion.OPEN_API) {
            OpenAPI srcOpenAPI = ((OAS3Parser) oas3Parser).getOpenAPI(sourceSwagger);
            Set<Components> aggregatedComponents = context.getAggregatedComponents();
            Components components = srcOpenAPI.getComponents();
            if (components != null) {
                aggregatedComponents.add(components);
            }
            Set<Scope> allScopes = oas3Parser.getScopes(sourceSwagger);
            Paths srcPaths = srcOpenAPI.getPaths();
            List<APIProductResource> apiProductResources = mappingEntry.getValue();
            for (APIProductResource apiProductResource : apiProductResources) {
                URITemplate uriTemplate = apiProductResource.getUriTemplate();
                PathItem srcPathItem = srcPaths.get(uriTemplate.getUriTemplate());
                readPathsAndScopes(srcPathItem, uriTemplate, allScopes, context);
            }
        } else if (sourceSwaggerVersion == SwaggerVersion.SWAGGER) {
            Swagger srcSwagger = ((OAS2Parser) oas2Parser).getSwagger(sourceSwagger);
            Set<Components> aggregatedComponents = context.getAggregatedComponents();
            Components components = swaggerConverter.readContents(sourceSwagger, null, null).getOpenAPI().getComponents();
            if (components != null) {
                aggregatedComponents.add(components);
            }
            Set<Scope> allScopes = oas2Parser.getScopes(sourceSwagger);
            Map<String, Path> srcPaths = srcSwagger.getPaths();
            List<APIProductResource> apiProductResources = mappingEntry.getValue();
            for (APIProductResource apiProductResource : apiProductResources) {
                URITemplate uriTemplate = apiProductResource.getUriTemplate();
                Path srcPath = srcPaths.get(uriTemplate.getUriTemplate());
                readPathsAndScopes(swaggerConverter.convert(srcPath), uriTemplate, allScopes, context);
            }
        }
    }
}
Also used : RefPath(io.swagger.models.RefPath) Path(io.swagger.models.Path) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) Scope(org.wso2.carbon.apimgt.api.model.Scope) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) Swagger(io.swagger.models.Swagger) OpenAPI(io.swagger.v3.oas.models.OpenAPI) API(org.wso2.carbon.apimgt.api.model.API) List(java.util.List) ArrayList(java.util.ArrayList) Paths(io.swagger.v3.oas.models.Paths) Map(java.util.Map) HashMap(java.util.HashMap) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Example 23 with APIProductResource

use of org.wso2.carbon.apimgt.api.model.APIProductResource in project carbon-apimgt by wso2.

the class OASParserUtil method updateAPIProductSwaggerOperations.

public static String updateAPIProductSwaggerOperations(Map<API, List<APIProductResource>> apiToProductResourceMapping, String destinationSwagger) throws APIManagementException {
    SwaggerVersion destinationSwaggerVersion = getSwaggerVersion(destinationSwagger);
    OpenAPI destOpenAPI;
    if (destinationSwaggerVersion == SwaggerVersion.OPEN_API) {
        destOpenAPI = ((OAS3Parser) oas3Parser).getOpenAPI(destinationSwagger);
    } else {
        throw new APIManagementException("Cannot update destination swagger because it is not in OpenAPI format");
    }
    SwaggerUpdateContext context = new SwaggerUpdateContext();
    extractRelevantSourceData(apiToProductResourceMapping, context);
    // Update paths
    destOpenAPI.setPaths(context.getPaths());
    // Update Scopes
    setScopes(destOpenAPI, context.getAggregatedScopes());
    // Update reference definitions
    setReferenceObjectDefinitions(destOpenAPI, context);
    return Json.pretty(destOpenAPI);
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Example 24 with APIProductResource

use of org.wso2.carbon.apimgt.api.model.APIProductResource in project carbon-apimgt by wso2.

the class ApiMgtDAO method getProductMappingsForAPI.

public List<APIProductResource> getProductMappingsForAPI(API api) throws APIManagementException {
    List<APIProductResource> productMappings = new ArrayList<>();
    Set<URITemplate> uriTemplatesOfAPI = getURITemplatesOfAPI(api.getUuid());
    for (URITemplate uriTemplate : uriTemplatesOfAPI) {
        Set<APIProductIdentifier> apiProductIdentifiers = uriTemplate.retrieveUsedByProducts();
        for (APIProductIdentifier apiProductIdentifier : apiProductIdentifiers) {
            APIProductResource productMapping = new APIProductResource();
            productMapping.setProductIdentifier(apiProductIdentifier);
            productMapping.setUriTemplate(uriTemplate);
            productMappings.add(productMapping);
        }
    }
    return productMappings;
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) ArrayList(java.util.ArrayList) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate)

Example 25 with APIProductResource

use of org.wso2.carbon.apimgt.api.model.APIProductResource in project carbon-apimgt by wso2.

the class ImportUtils method updateApiProductSwagger.

/**
 * This method updates the API Product and the swagger with the correct scopes.
 *
 * @param pathToArchive      Path to the extracted folder
 * @param importedApiProduct Imported API Product
 * @param apiProvider        API Provider
 * @throws APIManagementException If an error occurs when retrieving the parser and updating the API Product
 * @throws FaultGatewaysException If an error occurs when updating the API to overwrite
 * @throws IOException            If an error occurs when loading the swagger file
 */
private static APIProduct updateApiProductSwagger(String pathToArchive, String apiProductId, APIProduct importedApiProduct, APIProvider apiProvider, String orgId) throws APIManagementException, FaultGatewaysException, IOException {
    String swaggerContent = loadSwaggerFile(pathToArchive);
    // Load required properties from swagger to the API Product
    APIDefinition apiDefinition = OASParserUtil.getOASParser(swaggerContent);
    Set<Scope> scopes = apiDefinition.getScopes(swaggerContent);
    importedApiProduct.setScopes(scopes);
    importedApiProduct.setOrganization(orgId);
    // This is required to make scopes get effected
    Map<API, List<APIProductResource>> apiToProductResourceMapping = apiProvider.updateAPIProduct(importedApiProduct);
    apiProvider.updateAPIProductSwagger(apiProductId, apiToProductResourceMapping, importedApiProduct, orgId);
    return importedApiProduct;
}
Also used : Scope(org.wso2.carbon.apimgt.api.model.Scope) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) API(org.wso2.carbon.apimgt.api.model.API) ArrayList(java.util.ArrayList) List(java.util.List) NodeList(org.w3c.dom.NodeList)

Aggregations

APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)23 ArrayList (java.util.ArrayList)17 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)14 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)13 API (org.wso2.carbon.apimgt.api.model.API)12 HashMap (java.util.HashMap)11 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)11 List (java.util.List)9 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)9 Scope (org.wso2.carbon.apimgt.api.model.Scope)8 HashSet (java.util.HashSet)7 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)7 Tier (org.wso2.carbon.apimgt.api.model.Tier)7 LinkedHashSet (java.util.LinkedHashSet)6 Map (java.util.Map)6 JSONObject (org.json.simple.JSONObject)5 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)5 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)5 VelocityContext (org.apache.velocity.VelocityContext)4 ParseException (org.json.simple.parser.ParseException)4