Search in sources :

Example 31 with ConditionDTO

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

the class ThrottleConditionEvaluatorTest method testApplicabilityOfMatchingHeaderCondition.

@Test
public void testApplicabilityOfMatchingHeaderCondition() {
    ConditionGroupDTO conditionGroupDTO = new ConditionGroupDTO();
    conditionGroupDTO.setConditionGroupId("HeaderConditionGroup");
    ConditionDTO matchingCondition = new ConditionDTO();
    matchingCondition.setConditionType("Header");
    matchingCondition.setConditionName("host");
    matchingCondition.setConditionValue("org.wso2.com");
    ConditionDTO[] conditionDTOS = { matchingCondition };
    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.assertEquals(matchingConditionGroups.get(0).getConditionGroupId(), "HeaderConditionGroup");
}
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 32 with ConditionDTO

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

the class ThrottleConditionEvaluatorTest method testGetThrottledInConditionWithIPConditionWithDefaultWithInvertNegative.

@Test
public void testGetThrottledInConditionWithIPConditionWithDefaultWithInvertNegative() {
    MessageContext messageContext = TestUtils.getMessageContext(apiContext, apiVersion);
    Map map = new TreeMap();
    map.put("X-Forwarded-For", "127.0.0.1");
    ((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, "default");
}
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 33 with ConditionDTO

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

the class ThrottleConditionEvaluatorTest method testApplicabilityOfInvertedIPRangeCondition.

@Test
public void testApplicabilityOfInvertedIPRangeCondition() {
    ConditionGroupDTO conditionGroupDTO = new ConditionGroupDTO();
    conditionGroupDTO.setConditionGroupId("IPRangeConditionGroup");
    // 127.0.0.1 is not in inversion of 127.0.0.0 - 127.0.0.3 IP range
    ConditionDTO invertedIPRangeCondition = new ConditionDTO();
    invertedIPRangeCondition.setConditionType("IPRange");
    invertedIPRangeCondition.setConditionName("127.0.0.0");
    invertedIPRangeCondition.setConditionValue("127.0.0.3");
    invertedIPRangeCondition.isInverted(true);
    ConditionDTO[] conditionDTOS = { invertedIPRangeCondition };
    conditionGroupDTO.setConditions(conditionDTOS);
    ConditionGroupDTO[] conditionGroupDTOS = { conditionGroupDTO };
    List<ConditionGroupDTO> matchingConditionGroups = throttleConditionEvaluator.getApplicableConditions(TestUtils.getMessageContext(apiContext, apiVersion), new 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) Test(org.junit.Test)

Example 34 with ConditionDTO

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

the class ThrottleConditionEvaluatorTest method getHeaderCondition.

private ConditionDto getHeaderCondition(boolean invert) {
    ConditionDto conditionDto = new ConditionDto();
    ConditionDto.HeaderConditions headerConditions = new ConditionDto.HeaderConditions();
    Map map = new HashMap();
    map.put("abc", "cde");
    map.put("bcd", "xyz");
    headerConditions.setValues(map);
    headerConditions.setInvert(invert);
    conditionDto.setHeaderConditions(headerConditions);
    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 35 with ConditionDTO

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

the class ThrottleConditionEvaluatorTest method testGetThrottledInConditionWithHeaderConditionNegativeInvert.

@Test
public void testGetThrottledInConditionWithHeaderConditionNegativeInvert() {
    ThrottleProperties throttleProperties = new ThrottleProperties();
    throttleProperties.setEnableHeaderConditions(true);
    ServiceReferenceHolder.getInstance().setThrottleProperties(throttleProperties);
    MessageContext messageContext = TestUtils.getMessageContext(apiContext, apiVersion);
    Map map = new TreeMap();
    map.put("abc", "cd1");
    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(true) }));
    conditionMap.put("default", Arrays.asList(new ConditionDto[] { getHeaderCondition(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) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties) 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