Search in sources :

Example 6 with APICategory

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

the class RegistryPersistenceUtilTestCase method testcreateAPIProductArtifactContent.

@Test
public void testcreateAPIProductArtifactContent() throws APIPersistenceException, APIManagementException, RegistryException {
    APIProduct product = new APIProduct(new APIProductIdentifier("pubuser", "TestAPIProd", "1.0.0"));
    GenericArtifact genericArtifact = new GenericArtifactImpl(new QName("", "TestAPIProd", ""), "application/vnd.wso2-api+xml");
    List<APICategory> categories = new ArrayList<APICategory>();
    APICategory category = new APICategory();
    category.setName("testcategory");
    categories.add(category);
    product.setApiCategories(categories);
    Set<Tier> availableTiers = new HashSet<Tier>();
    availableTiers.add(new Tier("Unlimited"));
    availableTiers.add(new Tier("Gold"));
    product.setAvailableTiers(availableTiers);
    GenericArtifact retArtifact = RegistryPersistenceUtil.createAPIProductArtifactContent(genericArtifact, product);
    Assert.assertEquals("API name does not match", product.getId().getName(), retArtifact.getAttribute("overview_name"));
    Assert.assertEquals("API version does not match", product.getId().getVersion(), retArtifact.getAttribute("overview_version"));
    Assert.assertEquals("API provider does not match", product.getId().getProviderName(), retArtifact.getAttribute("overview_provider"));
}
Also used : APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) Tier(org.wso2.carbon.apimgt.api.model.Tier) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) APICategory(org.wso2.carbon.apimgt.api.model.APICategory) GenericArtifactImpl(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifactImpl) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with APICategory

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

the class APICategoryMappingUtil method fromCategoryListToCategoryDTOList.

/**
 * Converts api category List to CategoryDTO List.
 *
 * @param categories List of api categories
 * @return CategoryDTO list
 */
private static List<APICategoryDTO> fromCategoryListToCategoryDTOList(List<APICategory> categories) {
    List<APICategoryDTO> categoryDTOs = new ArrayList<>();
    for (APICategory category : categories) {
        APICategoryDTO categoryDTO = new APICategoryDTO();
        categoryDTO.setId(category.getId());
        categoryDTO.setName(category.getName());
        categoryDTO.setDescription(category.getDescription());
        categoryDTO.setNumberOfAPIs(category.getNumberOfAPIs());
        categoryDTOs.add(categoryDTO);
    }
    return categoryDTOs;
}
Also used : ArrayList(java.util.ArrayList) APICategoryDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.APICategoryDTO) APICategory(org.wso2.carbon.apimgt.api.model.APICategory)

Example 8 with APICategory

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

the class APICategoryMappingUtil method fromCategoryToCategoryDTO.

/**
 * Converts a Category to CategoryDTO
 *
 * @param category
 * @return categoryDTO
 */
public static APICategoryDTO fromCategoryToCategoryDTO(APICategory category) {
    APICategoryDTO categoryDTO = new APICategoryDTO();
    categoryDTO.setId(category.getId());
    categoryDTO.setName(category.getName());
    categoryDTO.setDescription(category.getDescription());
    return categoryDTO;
}
Also used : APICategoryDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.APICategoryDTO)

Example 9 with APICategory

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

the class APICategoryMappingUtil method fromCategoryDTOToCategory.

/**
 * Converts a CategoryDTO to APICategory
 *
 * @param categoryDTO
 * @return APICategoty
 */
public static APICategory fromCategoryDTOToCategory(APICategoryDTO categoryDTO) {
    APICategory category = new APICategory();
    category.setId(categoryDTO.getId());
    category.setName(categoryDTO.getName());
    category.setDescription(categoryDTO.getDescription());
    return category;
}
Also used : APICategory(org.wso2.carbon.apimgt.api.model.APICategory)

Example 10 with APICategory

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

the class ApiCategoriesApiServiceImpl method apiCategoriesPost.

@Override
public Response apiCategoriesPost(APICategoryDTO body, MessageContext messageContext) {
    APICategory apiCategory = null;
    try {
        APIAdmin apiAdmin = new APIAdminImpl();
        String userName = RestApiCommonUtil.getLoggedInUsername();
        apiCategory = APICategoryMappingUtil.fromCategoryDTOToCategory(body);
        if (!org.apache.commons.lang3.StringUtils.isEmpty(apiCategory.getName())) {
            String regExSpecialChars = "!@#$%^&*(),?\"{}[\\]|<>";
            String regExSpecialCharsReplaced = regExSpecialChars.replaceAll(".", "\\\\$0");
            // include \n,\t, space
            Pattern pattern = Pattern.compile("[" + regExSpecialCharsReplaced + "\\s" + "]");
            Matcher matcher = pattern.matcher(apiCategory.getName());
            if (matcher.find()) {
                RestApiUtil.handleBadRequest("Name field contains special characters.", log);
            }
            if (apiCategory.getName().length() > 255) {
                RestApiUtil.handleBadRequest("API Category name is too long.", log);
            }
        } else {
            RestApiUtil.handleBadRequest("API Category name is empty.", log);
        }
        String organization = RestApiUtil.getOrganization(messageContext);
        APICategoryDTO categoryDTO = APICategoryMappingUtil.fromCategoryToCategoryDTO(apiAdmin.addCategory(apiCategory, userName, organization));
        URI location = new URI(RestApiConstants.RESOURCE_PATH_CATEGORY + "/" + categoryDTO.getId());
        return Response.created(location).entity(categoryDTO).build();
    } catch (APIManagementException | URISyntaxException e) {
        String errorMessage = "Error while adding new API Category '" + body.getName() + "' - " + e.getMessage();
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Matcher(java.util.regex.Matcher) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) APICategoryDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.APICategoryDTO) URISyntaxException(java.net.URISyntaxException) APICategory(org.wso2.carbon.apimgt.api.model.APICategory) URI(java.net.URI)

Aggregations

APICategory (org.wso2.carbon.apimgt.api.model.APICategory)38 ArrayList (java.util.ArrayList)23 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)14 Tier (org.wso2.carbon.apimgt.api.model.Tier)13 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)8 JSONObject (org.json.simple.JSONObject)7 Test (org.junit.Test)6 API (org.wso2.carbon.apimgt.api.model.API)6 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)6 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 JSONParser (org.json.simple.parser.JSONParser)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)4 Scope (org.wso2.carbon.apimgt.api.model.Scope)4 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)4 LinkedHashMap (java.util.LinkedHashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)3 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)3