Search in sources :

Example 11 with ConditionDto

use of org.wso2.carbon.apimgt.impl.dto.ConditionDto 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)

Example 12 with ConditionDto

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

the class ThrottleConditionEvaluatorTest method testApplicabilityOfMatchingJWTClaimsCondition.

@Test
public void testApplicabilityOfMatchingJWTClaimsCondition() {
    ConditionGroupDTO conditionGroupDTO = new ConditionGroupDTO();
    conditionGroupDTO.setConditionGroupId("JWTClaimsConditionGroup");
    ConditionDTO matchingCondition = new ConditionDTO();
    matchingCondition.setConditionType("JWTClaims");
    matchingCondition.setConditionName("http://wso2.org/claims/subscriber");
    matchingCondition.setConditionValue("admin");
    ConditionDTO[] conditionDTOS = { matchingCondition };
    conditionGroupDTO.setConditions(conditionDTOS);
    ConditionGroupDTO[] conditionGroupDTOS = { conditionGroupDTO };
    AuthenticationContext authenticationContext = new AuthenticationContext();
    authenticationContext.setCallerToken(JWTToken);
    MessageContext messageContext = TestUtils.getMessageContext(apiContext, apiVersion);
    List<ConditionGroupDTO> matchingConditionGroups = throttleConditionEvaluator.getApplicableConditions(messageContext, authenticationContext, conditionGroupDTOS);
    Assert.assertEquals(matchingConditionGroups.get(0).getConditionGroupId(), "JWTClaimsConditionGroup");
}
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) Test(org.junit.Test)

Example 13 with ConditionDto

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

the class ThrottleConditionEvaluatorTest method testApplicabilityOfInvertedJWTClaimsCondition.

@Test
public void testApplicabilityOfInvertedJWTClaimsCondition() {
    ConditionGroupDTO conditionGroupDTO = new ConditionGroupDTO();
    conditionGroupDTO.setConditionGroupId("JWTClaimsConditionGroup");
    ConditionDTO invertedCondition = new ConditionDTO();
    invertedCondition.setConditionType("JWTClaims");
    invertedCondition.setConditionName("http://wso2.org/claims/subscriber");
    invertedCondition.setConditionValue("admin");
    invertedCondition.isInverted(true);
    ConditionDTO[] conditionDTOS = { invertedCondition };
    conditionGroupDTO.setConditions(conditionDTOS);
    ConditionGroupDTO[] conditionGroupDTOS = { conditionGroupDTO };
    AuthenticationContext authenticationContext = new AuthenticationContext();
    authenticationContext.setCallerToken(JWTToken);
    MessageContext messageContext = TestUtils.getMessageContext(apiContext, apiVersion);
    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) Test(org.junit.Test)

Example 14 with ConditionDto

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

the class ThrottleConditionEvaluatorTest method getComplexCondition1.

private ConditionDto getComplexCondition1() {
    ConditionDto conditionDto = new ConditionDto();
    ConditionDto.IPCondition ipCondition = new ConditionDto.IPCondition(APIUtil.ipToBigInteger("127.0.0.1"), false);
    conditionDto.setIpCondition(ipCondition);
    ConditionDto.HeaderConditions headerConditions = new ConditionDto.HeaderConditions();
    Map map = new HashMap();
    map.put("abc", "cde");
    map.put("bcd", "xyz");
    headerConditions.setValues(map);
    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 15 with ConditionDto

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

the class ThrottleConditionEvaluatorTest method testGetThrottledInConditionWithQueryConditionInvert.

@Test
public void testGetThrottledInConditionWithQueryConditionInvert() {
    ThrottleProperties throttleProperties = new ThrottleProperties();
    throttleProperties.setEnableQueryParamConditions(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[] { getQueryParamCondition(true) }));
    conditionMap.put("default", Arrays.asList(new ConditionDto[] { getQueryParamCondition(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) 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