Search in sources :

Example 1 with TierListDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.TierListDTO in project carbon-apimgt by wso2.

the class TagMappingUtil method fromTagListToDTO.

/**
 * Converts a List object of Tags into a DTO
 *
 * @param tags  a list of Tag objects
 * @param limit  max number of objects returned
 * @param offset starting index
 * @return TierListDTO object containing TierDTOs
 */
public static TagListDTO fromTagListToDTO(List<Tag> tags, int limit, int offset) {
    TagListDTO tagListDTO = new TagListDTO();
    List<TagDTO> tierDTOs = tagListDTO.getList();
    if (tierDTOs == null) {
        tierDTOs = new ArrayList<>();
        tagListDTO.setList(tierDTOs);
    }
    // identifying the proper start and end indexes
    int size = tags.size();
    int start = offset < size && offset >= 0 ? offset : Integer.MAX_VALUE;
    int end = offset + limit - 1 <= size - 1 ? offset + limit - 1 : size - 1;
    for (int i = start; i <= end; i++) {
        Tag tag = tags.get(i);
        tierDTOs.add(fromTagToDTO(tag));
    }
    tagListDTO.setCount(tierDTOs.size());
    return tagListDTO;
}
Also used : TagListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TagListDTO) TagDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TagDTO) Tag(org.wso2.carbon.apimgt.core.models.Tag)

Example 2 with TierListDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.TierListDTO in project carbon-apimgt by wso2.

the class TierMappingUtil method fromTierListToDTO.

/**
 * Converts a List object of Tiers into a DTO
 *
 * @param tiers  a list of Tier objects
 * @param limit  max number of objects returned
 * @param offset starting index
 * @return TierListDTO object containing TierDTOs
 */
public static TierListDTO fromTierListToDTO(List<Policy> tiers, String tierLevel, int limit, int offset) {
    TierListDTO tierListDTO = new TierListDTO();
    List<TierDTO> tierDTOs = tierListDTO.getList();
    if (tierDTOs == null) {
        tierDTOs = new ArrayList<>();
        tierListDTO.setList(tierDTOs);
    }
    // identifying the proper start and end indexes
    int size = tiers.size();
    int start = offset < size && offset >= 0 ? offset : Integer.MAX_VALUE;
    int end = offset + limit - 1 <= size - 1 ? offset + limit - 1 : size - 1;
    for (int i = start; i <= end; i++) {
        Policy tier = tiers.get(i);
        tierDTOs.add(fromTierToDTO(tier, tierLevel));
    }
    tierListDTO.setCount(tierDTOs.size());
    return tierListDTO;
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) TierDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TierDTO) TierListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TierListDTO)

Example 3 with TierListDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.TierListDTO in project carbon-apimgt by wso2.

the class TierMappingUtilTestCase method testFromTierListToDTO.

@Test
public void testFromTierListToDTO() {
    Policy policy1 = SampleTestObjectCreator.createSubscriptionPolicyWithRequestLimit("Gold");
    Policy policy2 = SampleTestObjectCreator.createSubscriptionPolicyWithBndwidthLimit("Silver");
    List<Policy> policyList = new ArrayList<>();
    policyList.add(policy1);
    policyList.add(policy2);
    TierListDTO tierListDTO = TierMappingUtil.fromTierListToDTO(policyList, "subscription", 10, 0);
    assertEquals(tierListDTO.getCount(), (Integer) policyList.size());
    assertEquals(tierListDTO.getList().get(0).getName(), policy1.getPolicyName());
    assertEquals(tierListDTO.getList().get(0).getDescription(), policy1.getDescription());
    assertEquals(tierListDTO.getList().get(0).getTierLevel().name(), "SUBSCRIPTION");
    assertEquals(tierListDTO.getList().get(0).getUnitTime().longValue(), policy1.getDefaultQuotaPolicy().getLimit().getUnitTime());
    assertEquals(tierListDTO.getList().get(0).getRequestCount().longValue(), ((RequestCountLimit) policy1.getDefaultQuotaPolicy().getLimit()).getRequestCount());
    assertEquals(tierListDTO.getList().get(1).getName(), policy2.getPolicyName());
    assertEquals(tierListDTO.getList().get(1).getDescription(), policy2.getDescription());
    assertEquals(tierListDTO.getList().get(1).getTierLevel().name(), "SUBSCRIPTION");
    assertEquals(tierListDTO.getList().get(1).getUnitTime().longValue(), policy2.getDefaultQuotaPolicy().getLimit().getUnitTime());
    assertEquals(tierListDTO.getList().get(1).getRequestCount().longValue(), ((BandwidthLimit) policy2.getDefaultQuotaPolicy().getLimit()).getDataAmount());
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) ArrayList(java.util.ArrayList) TierListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TierListDTO) Test(org.testng.annotations.Test)

Example 4 with TierListDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.TierListDTO in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesTierLevelGet.

/**
 * Retrieve a list of tiers for a particular tier level
 *
 * @param tierLevel   Tier level
 * @param limit       maximum number of tiers to return
 * @param offset      starting position of the pagination
 * @param ifNoneMatch If-Non-Match header value
 * @param request     msf4j request object
 * @return A list of qualifying tiers
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response policiesTierLevelGet(String tierLevel, Integer limit, Integer offset, String ifNoneMatch, Request request) throws NotFoundException {
    TierListDTO tierListDTO = null;
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        List<Policy> tierList = apiStore.getPolicies(RestApiUtil.mapRestApiPolicyLevelToPolicyLevelEnum(tierLevel));
        tierListDTO = TierMappingUtil.fromTierListToDTO(tierList, tierLevel, limit, offset);
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving tiers";
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.TIER_LEVEL, tierLevel);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.ok().entity(tierListDTO).build();
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) TierListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TierListDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Aggregations

Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)3 TierListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.TierListDTO)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Test (org.testng.annotations.Test)1 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 Tag (org.wso2.carbon.apimgt.core.models.Tag)1 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)1 TagDTO (org.wso2.carbon.apimgt.rest.api.store.dto.TagDTO)1 TagListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.TagListDTO)1 TierDTO (org.wso2.carbon.apimgt.rest.api.store.dto.TierDTO)1