Search in sources :

Example 31 with APIDefinition

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;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition)

Example 32 with APIDefinition

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));
}
Also used : JSONObject(org.json.JSONObject) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate)

Example 33 with APIDefinition

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));
}
Also used : JSONObject(org.json.JSONObject) Scope(org.wso2.carbon.apimgt.api.model.Scope)

Example 34 with APIDefinition

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));
}
Also used : JSONObject(org.json.JSONObject) Scope(org.wso2.carbon.apimgt.api.model.Scope) SwaggerData(org.wso2.carbon.apimgt.api.model.SwaggerData) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) Iterator(java.util.Iterator) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) API(org.wso2.carbon.apimgt.api.model.API) APIDefinitionValidationResponse(org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse)

Example 35 with APIDefinition

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;
}
Also used : AsyncAPISpecificationValidationResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.AsyncAPISpecificationValidationResponseDTO) AsyncAPISpecificationValidationResponseInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.AsyncAPISpecificationValidationResponseInfoDTO) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) Map(java.util.Map) HashMap(java.util.HashMap) APIDefinitionValidationResponse(org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)32 HashMap (java.util.HashMap)30 APIDefinition (org.wso2.carbon.apimgt.api.APIDefinition)30 API (org.wso2.carbon.apimgt.core.models.API)30 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)25 ArrayList (java.util.ArrayList)23 API (org.wso2.carbon.apimgt.api.model.API)20 Map (java.util.Map)19 IOException (java.io.IOException)18 Test (org.testng.annotations.Test)18 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)18 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)16 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)15 Scope (org.wso2.carbon.apimgt.api.model.Scope)15 HashSet (java.util.HashSet)13 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)13 APIDefinitionValidationResponse (org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse)12 SwaggerData (org.wso2.carbon.apimgt.api.model.SwaggerData)12 BusinessInformation (org.wso2.carbon.apimgt.core.models.BusinessInformation)12 CorsConfiguration (org.wso2.carbon.apimgt.core.models.CorsConfiguration)12