use of org.wso2.carbon.apimgt.api.APIDefinition in project carbon-apimgt by wso2.
the class APIConsumerImpl method getOpenAPIDefinitionForDeployment.
/**
* Get server URL updated Open API definition for given synapse gateway environment
* @param environmentName Name of the synapse gateway environment
* @return Updated Open API definition
* @throws APIManagementException
*/
private String getOpenAPIDefinitionForDeployment(API api, String environmentName) throws APIManagementException {
String apiTenantDomain;
String updatedDefinition = null;
Map<String, String> hostsWithSchemes;
String definition;
if (api.getSwaggerDefinition() != null) {
definition = api.getSwaggerDefinition();
} else {
throw new APIManagementException("Missing API definition in the api " + api.getUuid());
}
APIDefinition oasParser = OASParserUtil.getOASParser(definition);
api.setScopes(oasParser.getScopes(definition));
api.setUriTemplates(oasParser.getURITemplates(definition));
apiTenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(api.getId().getProviderName()));
hostsWithSchemes = getHostWithSchemeMappingForEnvironment(api, apiTenantDomain, environmentName);
api.setContext(getBasePath(apiTenantDomain, api.getContext()));
updatedDefinition = oasParser.getOASDefinitionForStore(api, definition, hostsWithSchemes);
return updatedDefinition;
}
use of org.wso2.carbon.apimgt.api.APIDefinition in project carbon-apimgt by wso2.
the class OASTestBase method testGetURITemplates.
public void testGetURITemplates(APIDefinition parser, String content) throws Exception {
JSONObject jsonObject = new JSONObject(content);
URITemplate exUriTemplate = new URITemplate();
exUriTemplate.setUriTemplate("/pets");
exUriTemplate.setAuthType("Application & Application User");
exUriTemplate.setAuthTypes("Application & Application User");
exUriTemplate.setHTTPVerb("GET");
exUriTemplate.setHttpVerbs("GET");
exUriTemplate.setThrottlingTier("Unlimited");
exUriTemplate.setThrottlingTiers("Unlimited");
exUriTemplate.setScope(extensionScope);
exUriTemplate.setScopes(extensionScope);
String scopesOnlyInSecurity = jsonObject.getJSONObject("scopesOnlyInSecurity").toString();
Set<URITemplate> uriTemplates = parser.getURITemplates(scopesOnlyInSecurity);
Assert.assertEquals(1, uriTemplates.size());
Assert.assertTrue(uriTemplates.contains(petGet));
String scopesOnlyInExtension = jsonObject.getJSONObject("scopesOnlyInExtension").toString();
uriTemplates = parser.getURITemplates(scopesOnlyInExtension);
Assert.assertEquals(1, uriTemplates.size());
Assert.assertTrue(uriTemplates.contains(exUriTemplate));
String scopesInExtensionAndSec = jsonObject.getJSONObject("scopesInExtensionAndSec").toString();
uriTemplates = parser.getURITemplates(scopesInExtensionAndSec);
Assert.assertEquals(1, uriTemplates.size());
Assert.assertTrue(uriTemplates.contains(petGet));
}
use of org.wso2.carbon.apimgt.api.APIDefinition in project carbon-apimgt by wso2.
the class OASTestBase method testGetScopes.
public void testGetScopes(APIDefinition parser, String content) throws Exception {
JSONObject jsonObject = new JSONObject(content);
String scopesOnlyInSecurity = jsonObject.getJSONObject("scopesOnlyInSecurity").toString();
Set<Scope> scopes = parser.getScopes(scopesOnlyInSecurity);
Assert.assertEquals(2, scopes.size());
scopes.toArray()[0].equals(sampleScope);
Assert.assertTrue(scopes.contains(sampleScope));
Assert.assertTrue(scopes.contains(extensionScope));
String scopesOnlyInExtension = jsonObject.getJSONObject("scopesOnlyInExtension").toString();
scopes = parser.getScopes(scopesOnlyInExtension);
Assert.assertEquals(2, scopes.size());
Assert.assertTrue(scopes.contains(sampleScope));
Assert.assertTrue(scopes.contains(extensionScope));
String scopesInExtensionAndSec = jsonObject.getJSONObject("scopesInExtensionAndSec").toString();
scopes = parser.getScopes(scopesInExtensionAndSec);
Assert.assertEquals(2, scopes.size());
Assert.assertTrue(scopes.contains(sampleScope));
Assert.assertTrue(scopes.contains(extensionScope));
}
use of org.wso2.carbon.apimgt.api.APIDefinition in project carbon-apimgt by wso2.
the class OASTestBase method testGenerateAPIDefinition2.
public void testGenerateAPIDefinition2(APIDefinition parser, String content, OASParserEvaluator evaluator) throws Exception {
JSONObject jsonObject = new JSONObject(content);
String equalNoOfResources = jsonObject.getJSONObject("equalNoOfResources").toString();
APIIdentifier identifier = new APIIdentifier("admin", "simple", "1.0.0");
API api = new API(identifier);
api.setScopes(new HashSet<>(Arrays.asList(sampleScope, extensionScope)));
api.setUriTemplates(new HashSet<>(Arrays.asList(petGet, petPost, itemGet, itemPost)));
String definition = parser.generateAPIDefinition(new SwaggerData(api), equalNoOfResources);
APIDefinitionValidationResponse response = parser.validateAPIDefinition(definition, false);
Assert.assertTrue(response.isValid());
Assert.assertTrue(response.getParser().getClass().equals(parser.getClass()));
Set<URITemplate> uriTemplates = parser.getURITemplates(definition);
Assert.assertEquals(4, uriTemplates.size());
Assert.assertTrue(uriTemplates.contains(petGet));
Assert.assertTrue(uriTemplates.contains(petPost));
Assert.assertTrue(uriTemplates.contains(itemGet));
Assert.assertTrue(uriTemplates.contains(itemPost));
Set<Scope> scopes = parser.getScopes(definition);
Assert.assertEquals(2, scopes.size());
Assert.assertTrue(scopes.contains(sampleScope));
Assert.assertTrue(scopes.contains(extensionScope));
// Remove operation and path from API object
String extraResourcesInDefinition = jsonObject.getJSONObject("extraResourcesInDefinition").toString();
api.setUriTemplates(new HashSet<>(Arrays.asList(itemGet, itemPost)));
definition = parser.generateAPIDefinition(new SwaggerData(api), extraResourcesInDefinition);
response = parser.validateAPIDefinition(definition, false);
Assert.assertTrue(response.isValid());
Assert.assertTrue(response.getParser().getClass().equals(parser.getClass()));
uriTemplates = parser.getURITemplates(definition);
Assert.assertEquals(2, uriTemplates.size());
// assert generated paths
if (evaluator != null) {
evaluator.eval(definition);
}
Iterator iterator = uriTemplates.iterator();
while (iterator.hasNext()) {
URITemplate element = (URITemplate) iterator.next();
if ("/pets".equalsIgnoreCase(element.getUriTemplate())) {
Assert.fail("Removed paths from API operation should not present.");
}
if ("/items".equalsIgnoreCase(element.getUriTemplate()) && "PUT".equalsIgnoreCase(element.getHTTPVerb())) {
Assert.fail("Removed item from API operation should not present.");
}
}
Assert.assertTrue(uriTemplates.contains(itemGet));
Assert.assertTrue(uriTemplates.contains(itemPost));
// Add operation and path to API object
String lessResourcesInDefinition = jsonObject.getJSONObject("lessResourcesInDefinition").toString();
api.setUriTemplates(new HashSet<>(Arrays.asList(petGet, petPost, itemGet, itemPost)));
definition = parser.generateAPIDefinition(new SwaggerData(api), lessResourcesInDefinition);
response = parser.validateAPIDefinition(definition, false);
Assert.assertTrue(response.isValid());
Assert.assertTrue(response.getParser().getClass().equals(parser.getClass()));
uriTemplates = parser.getURITemplates(definition);
Assert.assertEquals(4, uriTemplates.size());
Assert.assertTrue(uriTemplates.contains(petGet));
Assert.assertTrue(uriTemplates.contains(petPost));
Assert.assertTrue(uriTemplates.contains(itemGet));
Assert.assertTrue(uriTemplates.contains(itemPost));
}
use of org.wso2.carbon.apimgt.api.APIDefinition in project carbon-apimgt by wso2.
the class APIMappingUtil method getAsyncAPISpecificationValidationResponseFromModel.
public static AsyncAPISpecificationValidationResponseDTO getAsyncAPISpecificationValidationResponseFromModel(APIDefinitionValidationResponse model, boolean returnContent) throws APIManagementException {
AsyncAPISpecificationValidationResponseDTO responseDTO = new AsyncAPISpecificationValidationResponseDTO();
responseDTO.setIsValid(model.isValid());
if (model.isValid()) {
APIDefinitionValidationResponse.Info modelInfo = model.getInfo();
if (modelInfo != null) {
AsyncAPISpecificationValidationResponseInfoDTO infoDTO = new AsyncAPISpecificationValidationResponseInfoDTO();
infoDTO.setAsyncAPIVersion(modelInfo.getOpenAPIVersion());
infoDTO.setName(modelInfo.getName());
infoDTO.setVersion(modelInfo.getVersion());
infoDTO.setContext(modelInfo.getContext());
infoDTO.setDescription(modelInfo.getDescription());
infoDTO.setEndpoints(modelInfo.getEndpoints());
infoDTO.setProtocol(model.getProtocol());
Map<String, APIDefinition> apiDefinitionMap = APIUtil.getApiDefinitionParsersMap();
apiDefinitionMap.remove(APIConstants.WSO2_GATEWAY_ENVIRONMENT);
if (!apiDefinitionMap.isEmpty()) {
for (Map.Entry<String, APIDefinition> apiDefinitionEntry : apiDefinitionMap.entrySet()) {
APIDefinition apiParser = apiDefinitionEntry.getValue();
String gatewayVendor = apiParser.getVendorFromExtension(model.getContent());
if (gatewayVendor != null) {
infoDTO.setGatewayVendor(gatewayVendor);
break;
}
}
infoDTO.asyncTransportProtocols(AsyncApiParser.getTransportProtocolsForAsyncAPI(model.getContent()));
}
// Set default value
if (infoDTO.getGatewayVendor() == null) {
infoDTO.setGatewayVendor(APIConstants.WSO2_GATEWAY_ENVIRONMENT);
}
responseDTO.setInfo(infoDTO);
}
if (returnContent) {
responseDTO.setContent(model.getContent());
}
} else {
responseDTO.setErrors(getErrorListItemsDTOsFromErrorHandlers(model.getErrorItems()));
}
return responseDTO;
}
Aggregations