Search in sources :

Example 6 with Timer

use of org.wso2.carbon.metrics.manager.Timer in project carbon-apimgt by wso2.

the class APIKeyValidationService method getAllURITemplates.

/**
 * Return the URI Templates for an API
 *
 * @param context Requested context
 * @param version API Version
 * @return APIKeyValidationInfoDTO with authorization info and tier info if authorized. If it is not
 * authorized, tier information will be <pre>null</pre>
 */
public ArrayList<URITemplate> getAllURITemplates(String context, String version) throws APIManagementException {
    Timer timer6 = MetricManager.timer(org.wso2.carbon.metrics.manager.Level.INFO, MetricManager.name(APIConstants.METRICS_PREFIX, this.getClass().getSimpleName(), "GET_URI_TEMPLATE"));
    Timer.Context timerContext6 = timer6.start();
    if (log.isDebugEnabled()) {
        log.debug("getAllURITemplates request from gateway to keymanager: requestTime=" + new SimpleDateFormat("[yyyy.MM.dd HH:mm:ss,SSS zzz]").format(new Date()) + " ,for:" + context);
    }
    ArrayList<URITemplate> templates = getTemplates(context, version);
    if (log.isDebugEnabled()) {
        log.debug("getAllURITemplates response from keyManager to gateway for:" + context + " at " + new SimpleDateFormat("[yyyy.MM.dd HH:mm:ss,SSS zzz]").format(new Date()));
    }
    timerContext6.stop();
    return templates;
}
Also used : Timer(org.wso2.carbon.metrics.manager.Timer) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 7 with Timer

use of org.wso2.carbon.metrics.manager.Timer in project carbon-apimgt by wso2.

the class APIKeyValidationServiceTest method Init.

@Before
public void Init() throws Exception {
    System.setProperty(CARBON_HOME, "");
    privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
    apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
    apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    metricService = Mockito.mock(MetricService.class);
    org.wso2.carbon.metrics.manager.ServiceReferenceHolder serviceReferenceHolder1 = Mockito.mock(org.wso2.carbon.metrics.manager.ServiceReferenceHolder.class);
    Timer timer = Mockito.mock(Timer.class);
    Timer.Context timerContext = Mockito.mock(Timer.Context.class);
    MessageContext messageContext = Mockito.mock(MessageContext.class);
    OperationContext operationContext = Mockito.mock(OperationContext.class);
    MessageContext responseMessageContext = Mockito.mock(MessageContext.class);
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.mockStatic(ApiMgtDAO.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    PowerMockito.mockStatic(org.wso2.carbon.metrics.manager.ServiceReferenceHolder.class);
    PowerMockito.mockStatic(APIKeyMgtUtil.class);
    PowerMockito.mockStatic(MessageContext.class);
    PowerMockito.mockStatic(APIKeyMgtDataHolder.class);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    PowerMockito.when(privilegedCarbonContext.getUsername()).thenReturn(USER_NAME);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    PowerMockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
    PowerMockito.when(MessageContext.getCurrentMessageContext()).thenReturn(messageContext);
    PowerMockito.when(APIKeyMgtDataHolder.isJwtGenerationEnabled()).thenReturn(true);
    Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
    Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_KEY_MANGER_VALIDATIONHANDLER_CLASS_NAME)).thenReturn(API_KEY_MANGER_VALIDATION_HANDLER_CLASS_NAME);
    Mockito.when(org.wso2.carbon.metrics.manager.ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder1);
    Mockito.when(serviceReferenceHolder1.getMetricService()).thenReturn(metricService);
    Mockito.when(timer.start()).thenReturn(timerContext);
    Mockito.when(metricService.timer(Mockito.anyString(), Mockito.any(org.wso2.carbon.metrics.manager.Level.class))).thenReturn(timer);
    Mockito.when(messageContext.getOperationContext()).thenReturn(operationContext);
    Mockito.when(operationContext.getMessageContext(WSDLConstants.MESSAGE_LABEL_OUT_VALUE)).thenReturn(responseMessageContext);
    Map headers = new HashMap();
    headers.put("activityID", "1s2f2g4g5");
    Mockito.when(messageContext.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn(headers);
    String cacheKey = APIUtil.getAccessTokenCacheKey(ACCESS_TOKEN, API_CONTEXT, API_VERSION, "/*", "GET", REQUIRED_AUTHENTICATION_LEVEL);
    APIKeyValidationInfoDTO infoDTO = new APIKeyValidationInfoDTO();
    infoDTO.setApiPublisher(USER_NAME);
    infoDTO.setEndUserName(USER_NAME);
    PowerMockito.when(APIKeyMgtUtil.getFromKeyManagerCache(cacheKey)).thenReturn(infoDTO);
}
Also used : OperationContext(org.apache.axis2.context.OperationContext) ServiceReferenceHolder(org.wso2.carbon.apimgt.keymgt.internal.ServiceReferenceHolder) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) APIManagerConfigurationService(org.wso2.carbon.apimgt.impl.APIManagerConfigurationService) HashMap(java.util.HashMap) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) MetricService(org.wso2.carbon.metrics.manager.MetricService) Timer(org.wso2.carbon.metrics.manager.Timer) MessageContext(org.apache.axis2.context.MessageContext) HashMap(java.util.HashMap) Map(java.util.Map) APIKeyValidationInfoDTO(org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO) Before(org.junit.Before)

Example 8 with Timer

use of org.wso2.carbon.metrics.manager.Timer in project carbon-apimgt by wso2.

the class ThrottleHandlerTest method testHandleResponse.

@Test
public void testHandleResponse() {
    ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
    ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
    MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
    Assert.assertTrue(throttleHandler.handleResponse(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 9 with Timer

use of org.wso2.carbon.metrics.manager.Timer in project carbon-apimgt by wso2.

the class ThrottleHandlerTest method testSubscriptionLevelThrottlingInitialization.

@Test
public void testSubscriptionLevelThrottlingInitialization() {
    ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
    ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
    MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
    messageContext.setProperty(RESPONSE, "true");
    // Test subscription level throttle context initialisation when throttle holder is null
    Assert.assertTrue(throttleHandler.handleRequest(messageContext));
    // Test subscription level throttle context initialisation when throttle holder is already initialized by first
    // request
    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 10 with Timer

use of org.wso2.carbon.metrics.manager.Timer in project carbon-apimgt by wso2.

the class ThrottleHandlerTest method testMsgDoThrottleWhenUserLevelThrottlingIsTriggerred.

@Test
public void testMsgDoThrottleWhenUserLevelThrottlingIsTriggerred() {
    ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
    ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
    MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
    verbInfo.setApplicableLevel("userLevel");
    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, when user level throttling is triggered and not exceeded
    Assert.assertTrue(throttleHandler.handleRequest(messageContext));
}
Also used : ThrottleDataHolder(org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder) AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)25 MessageContext (org.apache.synapse.MessageContext)22 Test (org.junit.Test)22 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)22 ThrottleDataHolder (org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder)21 AuthenticationContext (org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext)20 ArrayList (java.util.ArrayList)16 ConditionGroupDTO (org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO)14 Timer (org.wso2.carbon.metrics.manager.Timer)12 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)6 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)5 GroupedComplexEvent (org.wso2.siddhi.core.event.GroupedComplexEvent)5 Map (java.util.Map)4 AttributeProcessor (org.wso2.siddhi.core.query.selector.attribute.processor.AttributeProcessor)4 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 Before (org.junit.Before)3 APIKeyValidationInfoDTO (org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO)3 VerbInfoDTO (org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO)3 TracingSpan (org.wso2.carbon.apimgt.tracing.TracingSpan)3