Search in sources :

Example 56 with Condition

use of org.wso2.carbon.user.core.model.Condition 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 57 with Condition

use of org.wso2.carbon.user.core.model.Condition 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 58 with Condition

use of org.wso2.carbon.user.core.model.Condition in project carbon-apimgt by wso2.

the class ThrottleConditionEvaluatorTest method testRetrievingEmptyApplicableConditionsWhenDefaultAndConditionGroupsAreNotAvailable.

@Test
public void testRetrievingEmptyApplicableConditionsWhenDefaultAndConditionGroupsAreNotAvailable() {
    ConditionGroupDTO[] conditionGroupDTOS = new ConditionGroupDTO[0];
    List<ConditionGroupDTO> conditionGroupDTOList = throttleConditionEvaluator.getApplicableConditions(TestUtils.getMessageContext(apiContext, apiVersion), new AuthenticationContext(), conditionGroupDTOS);
    // Should return empty Condition group in the Condition group array
    Assert.assertNull(conditionGroupDTOList.get(0));
}
Also used : AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO) Test(org.junit.Test)

Example 59 with Condition

use of org.wso2.carbon.user.core.model.Condition 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 60 with Condition

use of org.wso2.carbon.user.core.model.Condition in project carbon-apimgt by wso2.

the class ThrottleConditionEvaluatorTest method testGetThrottledInConditionWithJWTConditionNegative.

@Test
public void testGetThrottledInConditionWithJWTConditionNegative() {
    ThrottleProperties throttleProperties = new ThrottleProperties();
    throttleProperties.setEnableJwtConditions(true);
    ServiceReferenceHolder.getInstance().setThrottleProperties(throttleProperties);
    MessageContext messageContext = TestUtils.getMessageContext(apiContext, apiVersion);
    String jwt = "eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ3c28yLm9yZy9wcm9kdWN0cy9hbSIsImV4cCI6MTM0NTE4MzQ5MjE4MSwiaHR0cD" + "ovL3dzbzIub3JnL2NsYWltcy9hYmMiOiJjZCIsImh0dHA6Ly93c28yLm9yZy9jbGFpbXMvYmNkIjoieHl6In0=.yg-FAt9-h7CR" + "p7DCM6m4x5xWGxwj4mwXHH4b4sUP9h0";
    AuthenticationContext authenticationContext = new AuthenticationContext();
    authenticationContext.setCallerToken(jwt);
    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, "default");
}
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)

Aggregations

ArrayList (java.util.ArrayList)43 HashMap (java.util.HashMap)40 Test (org.testng.annotations.Test)34 Test (org.junit.Test)32 PreparedStatement (java.sql.PreparedStatement)29 List (java.util.List)28 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)26 ConditionDto (org.wso2.carbon.apimgt.impl.dto.ConditionDto)26 MessageContext (org.apache.synapse.MessageContext)25 ResultSet (java.sql.ResultSet)24 Map (java.util.Map)24 SQLException (java.sql.SQLException)22 Connection (java.sql.Connection)21 BlockConditions (org.wso2.carbon.apimgt.core.models.BlockConditions)18 ThrottleProperties (org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)18 HeaderCondition (org.wso2.carbon.apimgt.api.model.policy.HeaderCondition)15 JWTClaimsCondition (org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition)15 QueryParameterCondition (org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition)15 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)15 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)15