use of org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO in project carbon-apimgt by wso2.
the class ThrottleHandlerTest method testMsgDoContinueWhenAllThrottlingLevelsAreNotThrolled.
@Test
public void testMsgDoContinueWhenAllThrottlingLevelsAreNotThrolled() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ServiceReferenceHolder.getInstance().setThrottleDataPublisher(new ThrottleDataPublisher());
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);
messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
// Should continue the message flow if API level, application level, resource level, subscription level,
// subscription spike level and hard throttling limit levels are not throttled
Assert.assertTrue(throttleHandler.handleRequest(messageContext));
}
use of org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO in project carbon-apimgt by wso2.
the class ThrottleHandlerTest method testMsgThrottleOutWhenHittingSubscriptionLevelSpike.
@Test
public void testMsgThrottleOutWhenHittingSubscriptionLevelSpike() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator, accessInformation);
throttleHandler.setSandboxMaxCount("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.setKeyType("SANDBOX");
authenticationContext.setSpikeArrestLimit(100);
authenticationContext.setStopOnQuotaReach(true);
messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
verbInfo.setConditionGroups(conditionGroupDTOs);
ArrayList<ConditionGroupDTO> matchingConditions = new ArrayList<>();
matchingConditions.add(conditionGroupDTO);
throttleDataHolder.addKeyTemplate("$user", "$user");
Mockito.when(accessInformation.isAccessAllowed()).thenReturn(false);
Assert.assertFalse(throttleHandler.handleRequest(messageContext));
}
use of org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO in project carbon-apimgt by wso2.
the class ThrottleHandlerTest method init.
@Before
public void init() {
timer = Mockito.mock(Timer.class);
timer = Mockito.mock(Timer.class);
context = Mockito.mock(Timer.Context.class);
throttleEvaluator = Mockito.mock(ThrottleConditionEvaluator.class);
accessInformation = Mockito.mock(AccessInformation.class);
Mockito.when(timer.start()).thenReturn(context);
verbInfoDTO = new ArrayList<>();
verbInfo = new VerbInfoDTO();
verbInfo.setHttpVerb(httpVerb);
verbInfo.setRequestKey(apiContext + "/" + apiVersion + resourceUri + ":" + httpVerb);
verbInfo.setThrottling(throttlingTier);
verbInfoDTO.add(verbInfo);
conditionGroupDTO = new ConditionGroupDTO();
conditionGroupDTO.setConditionGroupId("_default");
conditionGroupDTOs = new ConditionGroupDTO[1];
conditionGroupDTOs[0] = conditionGroupDTO;
apiLevelThrottleKey = apiContext + ":" + apiVersion;
resourceLevelThrottleKey = apiContext + "/" + apiVersion + resourceUri + ":" + httpVerb;
org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
PowerMockito.mockStatic(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
Mockito.when(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
Mockito.when(apiManagerConfiguration.getExtensionListenerMap()).thenReturn(extensionListenerMap);
}
use of org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO in project carbon-apimgt by wso2.
the class ThrottleHandlerTest method testMsgThrottleOutWhenCustomThrottlingLimitExceeded.
@Test
public void testMsgThrottleOutWhenCustomThrottlingLimitExceeded() {
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.setSpikeArrestLimit(0);
messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
verbInfo.setConditionGroups(conditionGroupDTOs);
ArrayList<ConditionGroupDTO> matchingConditions = new ArrayList<>();
Mockito.when(accessInformation.isAccessAllowed()).thenReturn(false);
matchingConditions.add(conditionGroupDTO);
throttleDataHolder.addKeyTemplate("$user", "$user");
throttleDataHolder.addKeyTemplate("testKeyTemplate", "testKeyTemplateValue");
throttleDataHolder.addThrottleData("testKeyTemplate", System.currentTimeMillis() + 10000);
Assert.assertFalse(throttleHandler.handleRequest(messageContext));
throttleDataHolder.removeKeyTemplate("testKeyTemplate");
Assert.assertTrue(throttleHandler.handleRequest(messageContext));
}
use of org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO in project carbon-apimgt by wso2.
the class ThrottleHandlerTest method testCheckForStaledThrottleData.
@Test
public void testCheckForStaledThrottleData() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ServiceReferenceHolder.getInstance().setThrottleDataPublisher(new ThrottleDataPublisher());
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.setSpikeArrestLimit(0);
messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
verbInfo.setConditionGroups(conditionGroupDTOs);
ArrayList<ConditionGroupDTO> matchingConditions = new ArrayList<>();
Mockito.when(accessInformation.isAccessAllowed()).thenReturn(false);
matchingConditions.add(conditionGroupDTO);
throttleDataHolder.addKeyTemplate("testKeyTemplate", "testKeyTemplateValue");
throttleDataHolder.addThrottleData("testKeyTemplate", System.currentTimeMillis() - 10000);
Assert.assertTrue(throttleHandler.handleRequest(messageContext));
}
Aggregations