Search in sources :

Example 1 with ConditionDTO

use of org.wso2.carbon.apimgt.api.dto.ConditionDTO in project carbon-apimgt by wso2.

the class ThrottleConditionEvaluatorTest method testGetThrottledInConditionWithJWTCondition.

@Test
public void testGetThrottledInConditionWithJWTCondition() {
    ThrottleProperties throttleProperties = new ThrottleProperties();
    throttleProperties.setEnableJwtConditions(true);
    String jwt = "eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ3c28yLm9yZy9wcm9kdWN0cy9hbSIsImV4cCI6MTM0NTE4MzQ5MjE4MSwiaHR0cDov" + "L3dzbzIub3JnL2NsYWltcy9hYmMiOiJjZGUiLCJodHRwOi8vd3NvMi5vcmcvY2xhaW1zL2JjZCI6Inh5eiJ9.9zGU062DJ5mQ5hne" + "41h4IRpLbaY_b5thRxb3feebOcA";
    AuthenticationContext authenticationContext = new AuthenticationContext();
    authenticationContext.setCallerToken(jwt);
    ServiceReferenceHolder.getInstance().setThrottleProperties(throttleProperties);
    MessageContext messageContext = TestUtils.getMessageContext(apiContext, apiVersion);
    Map<String, List<ConditionDto>> conditionMap = new HashMap<>();
    conditionMap.put("condition1", Arrays.asList(new ConditionDto[] { getJWTCondition(false) }));
    conditionMap.put("default", Arrays.asList(new ConditionDto[] { getJWTCondition(false) }));
    String condition = throttleConditionEvaluator.getThrottledInCondition(messageContext, authenticationContext, conditionMap);
    Assert.assertEquals(condition, "condition1");
}
Also used : AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) 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 2 with ConditionDTO

use of org.wso2.carbon.apimgt.api.dto.ConditionDTO in project carbon-apimgt by wso2.

the class ThrottleConditionEvaluatorTest method getComplexCondition2.

private ConditionDto getComplexCondition2() {
    ConditionDto conditionDto = new ConditionDto();
    ConditionDto.IPCondition ipCondition = new ConditionDto.IPCondition(APIUtil.ipToBigInteger("127.0.0.1"), false);
    conditionDto.setIpCondition(ipCondition);
    ConditionDto.QueryParamConditions queryParamConditions = new ConditionDto.QueryParamConditions();
    Map map = new HashMap();
    map.put("abc", "cde");
    map.put("bcd", "xyz");
    queryParamConditions.setValues(map);
    conditionDto.setQueryParameterConditions(queryParamConditions);
    return conditionDto;
}
Also used : HashMap(java.util.HashMap) ConditionDto(org.wso2.carbon.apimgt.impl.dto.ConditionDto) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 3 with ConditionDTO

use of org.wso2.carbon.apimgt.api.dto.ConditionDTO in project carbon-apimgt by wso2.

the class ThrottleConditionEvaluatorTest method testGetThrottledInConditionWithIPConditionWithDefaultWithInvert.

@Test
public void testGetThrottledInConditionWithIPConditionWithDefaultWithInvert() {
    MessageContext messageContext = TestUtils.getMessageContext(apiContext, apiVersion);
    Map map = new TreeMap();
    map.put("X-Forwarded-For", "127.0.0.2");
    ((Axis2MessageContext) messageContext).getAxis2MessageContext().setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, map);
    Map<String, List<ConditionDto>> conditionMap = new HashMap<>();
    conditionMap.put("condition1", Arrays.asList(new ConditionDto[] { getIPCondition(true) }));
    conditionMap.put("default", Arrays.asList(new ConditionDto[] { getIPCondition(true) }));
    String condition = throttleConditionEvaluator.getThrottledInCondition(messageContext, null, conditionMap);
    Assert.assertEquals(condition, "condition1");
}
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) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with ConditionDTO

use of org.wso2.carbon.apimgt.api.dto.ConditionDTO in project carbon-apimgt by wso2.

the class ThrottleConditionEvaluatorTest method testGetThrottledInConditionWithHeaderCondition.

@Test
public void testGetThrottledInConditionWithHeaderCondition() {
    ThrottleProperties throttleProperties = new ThrottleProperties();
    throttleProperties.setEnableHeaderConditions(true);
    ServiceReferenceHolder.getInstance().setThrottleProperties(throttleProperties);
    MessageContext messageContext = TestUtils.getMessageContext(apiContext, apiVersion);
    Map map = new TreeMap();
    map.put("abc", "cde");
    map.put("bcd", "xyz");
    ((Axis2MessageContext) messageContext).getAxis2MessageContext().setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, map);
    Map<String, List<ConditionDto>> conditionMap = new HashMap<>();
    conditionMap.put("condition1", Arrays.asList(new ConditionDto[] { getHeaderCondition(false) }));
    conditionMap.put("default", Arrays.asList(new ConditionDto[] { getHeaderCondition(false) }));
    String condition = throttleConditionEvaluator.getThrottledInCondition(messageContext, null, conditionMap);
    Assert.assertEquals(condition, "condition1");
}
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) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties) Test(org.junit.Test)

Example 5 with ConditionDTO

use of org.wso2.carbon.apimgt.api.dto.ConditionDTO in project carbon-apimgt by wso2.

the class ThrottleConditionEvaluatorTest method getIpRangeCondition.

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

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