Search in sources :

Example 6 with ConditionDto

use of org.wso2.carbon.apimgt.impl.dto.ConditionDto in project carbon-apimgt by wso2.

the class ThrottleConditionEvaluatorTest method getIPCondition.

private ConditionDto getIPCondition(boolean invert) {
    ConditionDto conditionDto = new ConditionDto();
    conditionDto.setIpCondition(new ConditionDto.IPCondition(APIUtil.ipToBigInteger("127.0.0.1"), invert));
    return conditionDto;
}
Also used : ConditionDto(org.wso2.carbon.apimgt.impl.dto.ConditionDto)

Example 7 with ConditionDto

use of org.wso2.carbon.apimgt.impl.dto.ConditionDto in project carbon-apimgt by wso2.

the class ThrottleConditionEvaluatorTest method testApplicabilityOfNotMatchingHeaderCondition.

@Test
public void testApplicabilityOfNotMatchingHeaderCondition() {
    ConditionGroupDTO conditionGroupDTO = new ConditionGroupDTO();
    conditionGroupDTO.setConditionGroupId("HeaderConditionGroup");
    ConditionDTO notMatchingCondition = new ConditionDTO();
    notMatchingCondition.setConditionType("Header");
    notMatchingCondition.setConditionName("host");
    notMatchingCondition.setConditionValue("org.ibm.com");
    ConditionDTO[] conditionDTOS = { notMatchingCondition };
    conditionGroupDTO.setConditions(conditionDTOS);
    ConditionGroupDTO[] conditionGroupDTOS = { conditionGroupDTO };
    AuthenticationContext authenticationContext = new AuthenticationContext();
    authenticationContext.setCallerToken(JWTToken);
    MessageContext messageContext = TestUtils.getMessageContext(apiContext, apiVersion);
    Map map = new TreeMap();
    map.put("host", "org.wso2.com");
    ((Axis2MessageContext) messageContext).getAxis2MessageContext().setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, map);
    List<ConditionGroupDTO> matchingConditionGroups = throttleConditionEvaluator.getApplicableConditions(messageContext, authenticationContext, conditionGroupDTOS);
    Assert.assertNull(matchingConditionGroups.get(0));
}
Also used : AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) ConditionDTO(org.wso2.carbon.apimgt.api.dto.ConditionDTO) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Test(org.junit.Test)

Example 8 with ConditionDto

use of org.wso2.carbon.apimgt.impl.dto.ConditionDto in project carbon-apimgt by wso2.

the class ThrottleConditionEvaluatorTest method testGetThrottledInConditionWithComplexConditionNegative.

@Test
public void testGetThrottledInConditionWithComplexConditionNegative() {
    ThrottleProperties throttleProperties = new ThrottleProperties();
    throttleProperties.setEnableHeaderConditions(true);
    ServiceReferenceHolder.getInstance().setThrottleProperties(throttleProperties);
    MessageContext messageContext = TestUtils.getMessageContext(apiContext, apiVersion);
    ((Axis2MessageContext) messageContext).getAxis2MessageContext().setProperty("REST_URL_POSTFIX", "/temperature?abc=cde&bcd=xyz");
    Map<String, List<ConditionDto>> conditionMap = new HashMap<>();
    conditionMap.put("condition1", Arrays.asList(new ConditionDto[] { getComplexCondition1() }));
    conditionMap.put("default", Arrays.asList(new ConditionDto[] { getComplexCondition1(), getComplexCondition2() }));
    String condition = throttleConditionEvaluator.getThrottledInCondition(messageContext, null, conditionMap);
    Assert.assertEquals(null, condition);
}
Also used : HashMap(java.util.HashMap) ConditionDto(org.wso2.carbon.apimgt.impl.dto.ConditionDto) List(java.util.List) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties) Test(org.junit.Test)

Example 9 with ConditionDto

use of org.wso2.carbon.apimgt.impl.dto.ConditionDto in project carbon-apimgt by wso2.

the class APIUtil method extractConditionDto.

public static List<ConditionDto> extractConditionDto(String base64EncodedString) throws ParseException {
    List<ConditionDto> conditionDtoList = new ArrayList<>();
    String base64Decoded = new String(Base64.decodeBase64(base64EncodedString));
    JSONArray conditionJsonArray = (JSONArray) new JSONParser().parse(base64Decoded);
    for (Object conditionJson : conditionJsonArray) {
        ConditionDto conditionDto = new ConditionDto();
        JSONObject conditionJsonObject = (JSONObject) conditionJson;
        if (conditionJsonObject.containsKey(PolicyConstants.IP_SPECIFIC_TYPE.toLowerCase())) {
            JSONObject ipSpecificCondition = (JSONObject) conditionJsonObject.get(PolicyConstants.IP_SPECIFIC_TYPE.toLowerCase());
            ConditionDto.IPCondition ipCondition = new Gson().fromJson(ipSpecificCondition.toJSONString(), ConditionDto.IPCondition.class);
            conditionDto.setIpCondition(ipCondition);
        } else if (conditionJsonObject.containsKey(PolicyConstants.IP_RANGE_TYPE.toLowerCase())) {
            JSONObject ipRangeCondition = (JSONObject) conditionJsonObject.get(PolicyConstants.IP_RANGE_TYPE.toLowerCase());
            ConditionDto.IPCondition ipCondition = new Gson().fromJson(ipRangeCondition.toJSONString(), ConditionDto.IPCondition.class);
            conditionDto.setIpRangeCondition(ipCondition);
        }
        if (conditionJsonObject.containsKey(PolicyConstants.JWT_CLAIMS_TYPE.toLowerCase())) {
            JSONObject jwtClaimConditions = (JSONObject) conditionJsonObject.get(PolicyConstants.JWT_CLAIMS_TYPE.toLowerCase());
            ConditionDto.JWTClaimConditions jwtClaimCondition = new Gson().fromJson(jwtClaimConditions.toJSONString(), ConditionDto.JWTClaimConditions.class);
            conditionDto.setJwtClaimConditions(jwtClaimCondition);
        }
        if (conditionJsonObject.containsKey(PolicyConstants.HEADER_TYPE.toLowerCase())) {
            JSONObject headerConditionJson = (JSONObject) conditionJsonObject.get(PolicyConstants.HEADER_TYPE.toLowerCase());
            ConditionDto.HeaderConditions headerConditions = new Gson().fromJson(headerConditionJson.toJSONString(), ConditionDto.HeaderConditions.class);
            conditionDto.setHeaderConditions(headerConditions);
        }
        if (conditionJsonObject.containsKey(PolicyConstants.QUERY_PARAMETER_TYPE.toLowerCase())) {
            JSONObject queryParamConditionJson = (JSONObject) conditionJsonObject.get(PolicyConstants.QUERY_PARAMETER_TYPE.toLowerCase());
            ConditionDto.QueryParamConditions queryParamCondition = new Gson().fromJson(queryParamConditionJson.toJSONString(), ConditionDto.QueryParamConditions.class);
            conditionDto.setQueryParameterConditions(queryParamCondition);
        }
        conditionDtoList.add(conditionDto);
    }
    conditionDtoList.sort(new Comparator<ConditionDto>() {

        @Override
        public int compare(ConditionDto o1, ConditionDto o2) {
            if (o1.getIpCondition() != null && o2.getIpCondition() == null) {
                return -1;
            } else if (o1.getIpCondition() == null && o2.getIpCondition() != null) {
                return 1;
            } else {
                if (o1.getIpRangeCondition() != null && o2.getIpRangeCondition() == null) {
                    return -1;
                } else if (o1.getIpRangeCondition() == null && o2.getIpRangeCondition() != null) {
                    return 1;
                } else {
                    if (o1.getHeaderConditions() != null && o2.getHeaderConditions() == null) {
                        return -1;
                    } else if (o1.getHeaderConditions() == null && o2.getHeaderConditions() != null) {
                        return 1;
                    } else {
                        if (o1.getQueryParameterConditions() != null && o2.getQueryParameterConditions() == null) {
                            return -1;
                        } else if (o1.getQueryParameterConditions() == null && o2.getQueryParameterConditions() != null) {
                            return 1;
                        } else {
                            if (o1.getJwtClaimConditions() != null && o2.getJwtClaimConditions() == null) {
                                return -1;
                            } else if (o1.getJwtClaimConditions() == null && o2.getJwtClaimConditions() != null) {
                                return 1;
                            }
                        }
                    }
                }
            }
            return 0;
        }
    });
    return conditionDtoList;
}
Also used : ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) Gson(com.google.gson.Gson) JSONObject(org.json.simple.JSONObject) ConditionDto(org.wso2.carbon.apimgt.impl.dto.ConditionDto) JSONParser(org.json.simple.parser.JSONParser) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject)

Example 10 with ConditionDto

use of org.wso2.carbon.apimgt.impl.dto.ConditionDto in project carbon-apimgt by wso2.

the class APIUtilTest method testGetConditionDtoListWithHavingQueryParamConditionOnly.

@Test
public void testGetConditionDtoListWithHavingQueryParamConditionOnly() throws ParseException {
    String base64EncodedString = "W3sicXVlcnlwYXJhbWV0ZXJ0eXBlIjp7ImludmVydCI6ZmFsc2UsInZhbHVlcyI6eyJhYmMiOiJkZWYifX19XQo=";
    List<ConditionDto> conditionDtoList = APIUtil.extractConditionDto(base64EncodedString);
    Assert.assertEquals(conditionDtoList.size(), 1);
    ConditionDto conditionDto = conditionDtoList.get(0);
    Assert.assertNotNull(conditionDto.getQueryParameterConditions());
    Assert.assertEquals(conditionDto.getQueryParameterConditions().getValues().size(), 1);
}
Also used : ConditionDto(org.wso2.carbon.apimgt.impl.dto.ConditionDto) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)45 ConditionDto (org.wso2.carbon.apimgt.impl.dto.ConditionDto)40 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)33 MessageContext (org.apache.synapse.MessageContext)32 HashMap (java.util.HashMap)31 List (java.util.List)26 Map (java.util.Map)23 TreeMap (java.util.TreeMap)23 ConditionDTO (org.wso2.carbon.apimgt.api.dto.ConditionDTO)20 AuthenticationContext (org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext)20 ConditionGroupDTO (org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO)19 ThrottleProperties (org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)17 ArrayList (java.util.ArrayList)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 HeaderCondition (org.wso2.carbon.apimgt.api.model.policy.HeaderCondition)2 IPCondition (org.wso2.carbon.apimgt.api.model.policy.IPCondition)2 JWTClaimsCondition (org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition)2 QueryParameterCondition (org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition)2 APIPolicy (org.wso2.carbon.apimgt.api.model.subscription.APIPolicy)2