Search in sources :

Example 51 with Names

use of org.wso2.ballerinalang.compiler.util.Names in project carbon-apimgt by wso2.

the class APIUtilTierTest method testGetAvailableTiersWithExistingTier.

@Test
public void testGetAvailableTiersWithExistingTier() throws Exception {
    Map<String, Tier> definedTiers = getDefinedTiers();
    // Select valid tier names to be assigned to the API
    Set<Tier> expectedTiers = getRoundRobinTierString(validTierNames);
    String apiName = "testApi";
    Set<Tier> availableTiers = APIUtil.getAvailableTiers(definedTiers, getTiersAsString(expectedTiers), apiName);
    Assert.assertEquals("Expected tiers do not match", expectedTiers, availableTiers);
}
Also used : Tier(org.wso2.carbon.apimgt.api.model.Tier) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 52 with Names

use of org.wso2.ballerinalang.compiler.util.Names in project carbon-apimgt by wso2.

the class ExportUtils method getEndpointURLs.

/**
 * Get endpoint url list from endpoint config.
 *
 * @param endpointConfig JSON converted endpoint config
 * @param type           End point type - production/sandbox
 * @return List of host names
 */
private static List<String> getEndpointURLs(JSONObject endpointConfig, String type, String apiName) throws APIImportExportException {
    List<String> urls = new ArrayList<>();
    if (endpointConfig != null) {
        try {
            if (endpointConfig.has(type)) {
                Object item = endpointConfig.get(type);
                if (item instanceof JSONArray) {
                    JSONArray endpointsJSON = new JSONArray(endpointConfig.getJSONArray(type).toString());
                    for (int i = 0; i < endpointsJSON.length(); i++) {
                        try {
                            String urlValue = endpointsJSON.getJSONObject(i).get(APIConstants.API_DATA_URL).toString();
                            urls.add(urlValue);
                        } catch (JSONException ex) {
                            log.error("Endpoint URL extraction from endpoints JSON object failed in API: " + apiName, ex);
                        }
                    }
                } else if (item instanceof JSONObject) {
                    JSONObject endpointJSON = new JSONObject(endpointConfig.getJSONObject(type).toString());
                    try {
                        String urlValue = endpointJSON.get(APIConstants.API_DATA_URL).toString();
                        urls.add(urlValue);
                    } catch (JSONException ex) {
                        log.error("Endpoint URL extraction from endpoint JSON object failed in API: " + apiName, ex);
                    }
                }
            }
        } catch (JSONException ex) {
            throw new APIImportExportException("Endpoint type: " + type + " not found in API: " + apiName);
        }
    }
    return urls;
}
Also used : JSONObject(org.json.JSONObject) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.JSONObject)

Example 53 with Names

use of org.wso2.ballerinalang.compiler.util.Names in project carbon-apimgt by wso2.

the class AbstractAPIManager method getDocPaths.

/**
 * Get API Documents within the provided registry collection
 * In case the document names contained '/' character, need to get only leaf node documents within them
 *
 * @param docCollection          registry collection
 * @param apiOrAPIProductDocPath base api/api product document path
 * @return
 * @throws APIManagementException
 */
private List<String> getDocPaths(org.wso2.carbon.registry.core.Collection docCollection, String apiOrAPIProductDocPath) throws APIManagementException {
    List<String> docPaths = new ArrayList<>();
    String pathToContent = apiOrAPIProductDocPath + APIConstants.INLINE_DOCUMENT_CONTENT_DIR;
    String pathToDocFile = apiOrAPIProductDocPath + APIConstants.DOCUMENT_FILE_DIR;
    try {
        String[] resourcePaths = docCollection.getChildren();
        for (String resourcePath : resourcePaths) {
            if (!(resourcePath.equals(pathToContent) || resourcePath.equals(pathToDocFile))) {
                Resource resource = registry.get(resourcePath);
                if (resource instanceof org.wso2.carbon.registry.core.Collection) {
                    docPaths.addAll(getDocPaths((org.wso2.carbon.registry.core.Collection) resource, apiOrAPIProductDocPath));
                } else {
                    docPaths.add(resourcePath);
                }
            }
        }
    } catch (RegistryException e) {
        String msg = "Failed to get documents for api/product";
        throw new APIManagementException(msg, e);
    }
    return docPaths;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) Collection(org.wso2.carbon.registry.core.Collection) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 54 with Names

use of org.wso2.ballerinalang.compiler.util.Names in project carbon-apimgt by wso2.

the class AbstractAPIManager method getTiers.

/**
 * Returns a list of pre-defined # {@link org.wso2.carbon.apimgt.api.model.Tier} in the system.
 *
 * @param tierType type of the tiers (api,resource ot application)
 * @param username current logged user
 * @return Set<Tier> return list of tier names
 * @throws APIManagementException APIManagementException if failed to get the predefined tiers
 */
public Set<Tier> getTiers(int tierType, String username) throws APIManagementException {
    Set<Tier> tiers = new TreeSet<Tier>(new TierNameComparator());
    String tenantDomain = getTenantDomain(username);
    Map<String, Tier> tierMap;
    int tenantIdFromUsername = APIUtil.getTenantId(username);
    if (tierType == APIConstants.TIER_API_TYPE) {
        tierMap = APIUtil.getTiersFromPolicies(PolicyConstants.POLICY_LEVEL_SUB, tenantIdFromUsername);
    } else if (tierType == APIConstants.TIER_RESOURCE_TYPE) {
        tierMap = APIUtil.getTiersFromPolicies(PolicyConstants.POLICY_LEVEL_API, tenantIdFromUsername);
    } else if (tierType == APIConstants.TIER_APPLICATION_TYPE) {
        tierMap = APIUtil.getTiersFromPolicies(PolicyConstants.POLICY_LEVEL_APP, tenantIdFromUsername);
    } else {
        throw new APIManagementException("No such a tier type : " + tierType);
    }
    tiers.addAll(tierMap.values());
    return tiers;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Tier(org.wso2.carbon.apimgt.api.model.Tier) TreeSet(java.util.TreeSet) TierNameComparator(org.wso2.carbon.apimgt.impl.utils.TierNameComparator)

Example 55 with Names

use of org.wso2.ballerinalang.compiler.util.Names in project carbon-apimgt by wso2.

the class PublisherCommonUtils method validateAdditionalProperties.

/**
 * To validate the additional properties.
 * Validation will be done for the keys of additional properties. Property keys should not contain spaces in it
 * and property keys should not conflict with reserved key words.
 *
 * @param additionalProperties Map<String, String>  properties to validate
 * @return error message if there is an validation error with additional properties.
 */
public static String validateAdditionalProperties(List<APIInfoAdditionalPropertiesDTO> additionalProperties) {
    if (additionalProperties != null) {
        for (APIInfoAdditionalPropertiesDTO property : additionalProperties) {
            String propertyKey = property.getName();
            String propertyValue = property.getValue();
            if (propertyKey.contains(" ")) {
                return "Property names should not contain space character. Property '" + propertyKey + "' " + "contains space in it.";
            }
            if (Arrays.asList(APIConstants.API_SEARCH_PREFIXES).contains(propertyKey.toLowerCase())) {
                return "Property '" + propertyKey + "' conflicts with the reserved keywords. Reserved keywords " + "are [" + Arrays.toString(APIConstants.API_SEARCH_PREFIXES) + "]";
            }
            // restricting them to be within 80 and 900.
            if (propertyKey.length() > 80) {
                return "Property name can have maximum of 80 characters. Property '" + propertyKey + "' + contains " + propertyKey.length() + "characters";
            }
            if (propertyValue.length() > 900) {
                return "Property value can have maximum of 900 characters. Property '" + propertyKey + "' + " + "contains a value with " + propertyValue.length() + "characters";
            }
        }
    }
    return "";
}
Also used : APIInfoAdditionalPropertiesDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIInfoAdditionalPropertiesDTO)

Aggregations

ArrayList (java.util.ArrayList)14 Query (javax.persistence.Query)8 TaskStatus (org.wso2.carbon.humantask.core.dao.TaskStatus)8 HashMap (java.util.HashMap)7 Test (org.testng.annotations.Test)6 Name (org.wso2.ballerinalang.compiler.util.Name)5 IOException (java.io.IOException)4 Map (java.util.Map)4 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)4 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)4 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)4 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 Iterator (java.util.Iterator)3 OMElement (org.apache.axiom.om.OMElement)3 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)3 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)3 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)3 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)3 Tier (org.wso2.carbon.apimgt.api.model.Tier)3 JsonObject (com.google.gson.JsonObject)2