use of org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO in project carbon-apimgt by wso2.
the class ThrottleHandlerTest method testMsgThrottleOutWhenSubscriptionLevelIsThrottledAndStopOnQuotaReachIsEnabled.
@Test
public void testMsgThrottleOutWhenSubscriptionLevelIsThrottledAndStopOnQuotaReachIsEnabled() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
messageContext.setProperty(VERB_INFO_DTO, verbInfoDTO);
((Axis2MessageContext) messageContext).getAxis2MessageContext().getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
AuthenticationContext authenticationContext = (AuthenticationContext) messageContext.getProperty(API_AUTH_CONTEXT);
authenticationContext.setApiTier(throttlingTier);
authenticationContext.setStopOnQuotaReach(true);
messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
verbInfo.setConditionGroups(conditionGroupDTOs);
ArrayList<ConditionGroupDTO> matchingConditions = new ArrayList<>();
matchingConditions.add(conditionGroupDTO);
String subscriptionLevelThrottleKey = authenticationContext.getApplicationId() + ":" + apiContext + ":" + apiVersion + ":" + authenticationContext.getTier();
throttleDataHolder.addThrottleData(subscriptionLevelThrottleKey, System.currentTimeMillis() + 10000);
// Should throttle out and discontinue message flow, when subscription level is throttled out
// and stop on quota reach is enabled
Assert.assertFalse(throttleHandler.handleRequest(messageContext));
}
use of org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO in project carbon-apimgt by wso2.
the class ThrottleHandlerTest method testMsgThrottleOutWhenProductionHardThrottlingLimitsThrottled.
@Test
public void testMsgThrottleOutWhenProductionHardThrottlingLimitsThrottled() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator, accessInformation);
throttleHandler.setProductionMaxCount("100");
SynapseEnvironment synapseEnvironment = Mockito.mock(SynapseEnvironment.class);
throttleHandler.init(synapseEnvironment);
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
messageContext.setProperty(VERB_INFO_DTO, verbInfoDTO);
((Axis2MessageContext) messageContext).getAxis2MessageContext().getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
AuthenticationContext authenticationContext = (AuthenticationContext) messageContext.getProperty(API_AUTH_CONTEXT);
authenticationContext.setApiTier(throttlingTier);
authenticationContext.setStopOnQuotaReach(false);
authenticationContext.setKeyType("PRODUCTION");
authenticationContext.setSpikeArrestLimit(0);
messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
verbInfo.setConditionGroups(conditionGroupDTOs);
ArrayList<ConditionGroupDTO> matchingConditions = new ArrayList<>();
matchingConditions.add(conditionGroupDTO);
Mockito.when(accessInformation.isAccessAllowed()).thenReturn(false);
// Should discontinue message flow if PRODUCTION hard throttling limits are exceeded
Assert.assertFalse(throttleHandler.handleRequest(messageContext));
}
use of org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO in project carbon-apimgt by wso2.
the class ThrottleConditionEvaluatorTest method testApplicabilityOfMatchingIPSpecificCondition.
@Test
public void testApplicabilityOfMatchingIPSpecificCondition() {
ConditionGroupDTO conditionGroupDTO = new ConditionGroupDTO();
conditionGroupDTO.setConditionGroupId("IPSpecificConditionGroup");
ConditionDTO matchingCondition = new ConditionDTO();
matchingCondition.setConditionType("IPSpecific");
matchingCondition.setConditionValue("127.0.0.1");
ConditionDTO[] conditionDTOS = { matchingCondition };
conditionGroupDTO.setConditions(conditionDTOS);
ConditionGroupDTO[] conditionGroupDTOS = { conditionGroupDTO };
List<ConditionGroupDTO> matchingConditionGroups = throttleConditionEvaluator.getApplicableConditions(TestUtils.getMessageContext(apiContext, apiVersion), new AuthenticationContext(), conditionGroupDTOS);
Assert.assertEquals(matchingConditionGroups.get(0).getConditionGroupId(), "IPSpecificConditionGroup");
}
use of org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO in project carbon-apimgt by wso2.
the class ThrottleConditionEvaluatorTest method testApplicabilityOfNonMatchingQueryParameterTypeCondition.
@Test
public void testApplicabilityOfNonMatchingQueryParameterTypeCondition() {
ConditionGroupDTO conditionGroupDTO = new ConditionGroupDTO();
conditionGroupDTO.setConditionGroupId("QueryParameterTypeConditionGroup");
ConditionDTO invertedCondition = new ConditionDTO();
invertedCondition.setConditionType("QueryParameterType");
invertedCondition.setConditionName("city");
invertedCondition.setConditionValue("mountain-view");
ConditionDTO[] conditionDTOS = { invertedCondition };
conditionGroupDTO.setConditions(conditionDTOS);
ConditionGroupDTO[] conditionGroupDTOS = { conditionGroupDTO };
MessageContext messageContext = TestUtils.getMessageContext(apiContext, apiVersion);
((Axis2MessageContext) messageContext).getAxis2MessageContext().setProperty("REST_URL_POSTFIX", "/temperature?city=colombo");
List<ConditionGroupDTO> matchingConditionGroups = throttleConditionEvaluator.getApplicableConditions(messageContext, new AuthenticationContext(), conditionGroupDTOS);
Assert.assertNull(matchingConditionGroups.get(0));
}
use of org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO in project carbon-apimgt by wso2.
the class ThrottleConditionEvaluatorTest method testApplicabilityOfNonMatchingJWTClaimsCondition.
@Test
public void testApplicabilityOfNonMatchingJWTClaimsCondition() {
ConditionGroupDTO conditionGroupDTO = new ConditionGroupDTO();
conditionGroupDTO.setConditionGroupId("JWTClaimsConditionGroup");
ConditionDTO nonMatchingCondition = new ConditionDTO();
nonMatchingCondition.setConditionType("JWTClaims");
nonMatchingCondition.setConditionName("http://wso2.org/claims/subscriber");
nonMatchingCondition.setConditionValue("testUser");
ConditionDTO[] conditionDTOS = { nonMatchingCondition };
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));
}
Aggregations