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));
}
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);
}
}
}
}
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);
}
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;
}
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;
}
Aggregations