Search in sources :

Example 26 with ThrottleDataHolder

use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder 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));
}
Also used : ThrottleDataHolder(org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder) AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) ArrayList(java.util.ArrayList) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 27 with ThrottleDataHolder

use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder in project carbon-apimgt by wso2.

the class ThrottleHandlerTest method testHandleRequestForGraphQLSubscriptions.

/**
 * This method will test request flow when "isGraphqlSubscriptionRequest" property is set in axis2 message context
 * when incoming transport is websocket. This occurs during Graphql Subscription request flow.
 */
@Test
public void testHandleRequestForGraphQLSubscriptions() {
    ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, new ThrottleDataHolder(), throttleEvaluator, accessInformation);
    Axis2MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
    org.apache.axis2.context.MessageContext axis2MessageContext = Mockito.mock(org.apache.axis2.context.MessageContext.class);
    Mockito.when(messageContext.getAxis2MessageContext()).thenReturn(axis2MessageContext);
    Mockito.when(axis2MessageContext.getIncomingTransportName()).thenReturn("ws");
    Mockito.when(messageContext.getProperty(APIConstants.GRAPHQL_SUBSCRIPTION_REQUEST)).thenReturn(true);
    Assert.assertTrue(throttleHandler.handleRequest(messageContext));
    Mockito.when(axis2MessageContext.getIncomingTransportName()).thenReturn("wss");
    Assert.assertTrue(throttleHandler.handleRequest(messageContext));
    // clean up message context
    Mockito.when(messageContext.getProperty(APIConstants.GRAPHQL_SUBSCRIPTION_REQUEST)).thenReturn(false);
    Mockito.when(axis2MessageContext.getIncomingTransportName()).thenReturn("http");
}
Also used : ThrottleDataHolder(org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 28 with ThrottleDataHolder

use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder 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));
}
Also used : ThrottleDataHolder(org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder) AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) ArrayList(java.util.ArrayList) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 29 with ThrottleDataHolder

use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder in project carbon-apimgt by wso2.

the class ThrottleHandlerTest method testMsgThrottleContinueWhenAPITierIsNotAvailable.

@Test
public void testMsgThrottleContinueWhenAPITierIsNotAvailable() {
    ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
    ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
    MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
    ((Axis2MessageContext) messageContext).getAxis2MessageContext().getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    // Make sure that the tier info is not available in the message context
    Assert.assertNull((VerbInfoDTO) messageContext.getProperty(VERB_INFO_DTO));
    // Should continue the message flow if the message context does not have throttling tier information
    Assert.assertTrue(throttleHandler.handleRequest(messageContext));
}
Also used : ThrottleDataHolder(org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 30 with ThrottleDataHolder

use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder in project carbon-apimgt by wso2.

the class ApplicationThrottleControllerTest method testCreatingThrottleContextThrowsSynapseExceptionWhenThrottlingPolicyResourceIsACollection.

@Test(expected = SynapseException.class)
public void testCreatingThrottleContextThrowsSynapseExceptionWhenThrottlingPolicyResourceIsACollection() throws UserStoreException, RegistryException {
    Mockito.when(throttleDataHolder.getThrottleContext(applicationId)).thenReturn(null);
    PowerMockito.when(tenantManager.getTenantId(tenantDomain)).thenReturn(tenantID);
    PowerMockito.when(registryService.getGovernanceSystemRegistry(tenantID)).thenReturn(registry);
    PowerMockito.when(registry.resourceExists(RESOURCE_PATH)).thenReturn(true);
    Collection collection = Mockito.mock(Collection.class);
    PowerMockito.when(registry.get(RESOURCE_PATH)).thenReturn(collection);
    ApplicationThrottleController.getApplicationThrottleContext(messageContext, throttleDataHolder, applicationId, THROTTLE_POLICY_KEY);
}
Also used : Collection(org.wso2.carbon.registry.core.Collection) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)30 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 ThrottleDataHolder (org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder)27 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)21 MessageContext (org.apache.synapse.MessageContext)20 AuthenticationContext (org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext)17 ArrayList (java.util.ArrayList)12 ConditionGroupDTO (org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO)12 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)6 ThrottleDataPublisher (org.wso2.carbon.apimgt.gateway.throttling.publisher.ThrottleDataPublisher)3 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HttpResponse (org.apache.http.HttpResponse)2 StatusLine (org.apache.http.StatusLine)2 HttpClient (org.apache.http.client.HttpClient)2 HttpGet (org.apache.http.client.methods.HttpGet)2 BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)2 ThrottleContext (org.apache.synapse.commons.throttle.core.ThrottleContext)2 ThrottleException (org.apache.synapse.commons.throttle.core.ThrottleException)2 EventHubConfigurationDto (org.wso2.carbon.apimgt.impl.dto.EventHubConfigurationDto)2