/*
 * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.wso2.carbon.apimgt.gateway.handlers.analytics;

import org.apache.synapse.MessageContext;
import org.apache.synapse.rest.RESTConstants;
import org.junit.Test;
import org.mockito.Mockito;
import org.wso2.carbon.apimgt.gateway.APIMgtGatewayConstants;
import org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext;
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.impl.APIManagerAnalyticsConfiguration;
import org.wso2.carbon.apimgt.usage.publisher.APIMgtUsageDataPublisher;


public class APIMgtThrottleUsageHandlerTest {
    @Test
    public void mediateWhileEventReceiverIsSkipped() throws Exception {
        APIManagerAnalyticsConfiguration apiManagerAnalyticsConfiguration = Mockito.mock
                (APIManagerAnalyticsConfiguration.class);
        APIMgtUsageDataPublisher apiMgtUsageDataPublisher = Mockito.mock(APIMgtUsageDataPublisher.class);
        APIMgtThrottleUsageHandler apiMgtThrottleUsageHandler = new APIMgtThrottleUsageHandlerWrapper
                (apiMgtUsageDataPublisher, true, false, apiManagerAnalyticsConfiguration);
        MessageContext messageContext = Mockito.mock(MessageContext.class);
        AuthenticationContext authContext = new AuthenticationContext();
        authContext.setApiKey("ac-def");
        authContext.setUsername("admin");
        Mockito.when(messageContext.getProperty("__API_AUTH_CONTEXT")).thenReturn(authContext);
        Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION)).thenReturn("1.0.0");
        Mockito.when(messageContext.getProperty(RESTConstants.REST_API_CONTEXT)).thenReturn("/api1");
        Mockito.when(messageContext.getProperty(RESTConstants.REST_API_CONTEXT)).thenReturn("/api1");
        Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API_PUBLISHER)).thenReturn("admin");
        Mockito.when(messageContext.getProperty(RESTConstants.REST_FULL_REQUEST_PATH)).thenReturn("/api1/aa?vc=1");
        Mockito.when(messageContext.getProperty(APIConstants.THROTTLE_OUT_REASON_KEY)).thenReturn("application level " +
                "throttled out");
        apiMgtThrottleUsageHandler.mediate(messageContext);
    }

}
@@ -691,16 +691,63 @@
 ytics;%0A%0A
+import org.apache.commons.codec.binary.Base64;%0A
 import o
@@ -847,40 +847,294 @@
 nit.
-Test;%0Aimport org.mockito.Mockito
+Before;%0Aimport org.junit.BeforeClass;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0Aimport org.mockito.Mockito;%0Aimport org.powermock.api.mockito.PowerMockito;%0Aimport org.powermock.core.classloader.annotations.PrepareForTest;%0Aimport org.powermock.modules.junit4.PowerMockRunner
 ;%0Aim
@@ -1264,32 +1264,90 @@
 icationContext;%0A
+import org.wso2.carbon.apimgt.gateway.utils.GatewayUtils;%0A
 import org.wso2.
@@ -1448,16 +1448,66 @@
 ration;%0A
+import org.wso2.carbon.apimgt.impl.utils.APIUtil;%0A
 import o
@@ -1574,137 +1574,862 @@
 er;%0A
-%0A%0Apublic class APIMgtThrottleUsageHandlerTest %7B%0A    @Test%0A    public void mediateWhileEventReceiverIsSkipped() throws Exception %7B
+import org.wso2.carbon.context.PrivilegedCarbonContext;%0A%0Aimport static org.wso2.carbon.h2.osgi.utils.CarbonConstants.CARBON_HOME;%0A@RunWith(PowerMockRunner.class)%0A@PrepareForTest(%7BPrivilegedCarbonContext.class%7D)%0Apublic class APIMgtThrottleUsageHandlerTest %7B%0A    @Test%0A    public void mediateWhileEventReceiverIsSkipped() throws Exception %7B%0A        System.setProperty(CARBON_HOME, %22%22);%0A        PrivilegedCarbonContext carbonContext = Mockito.mock(PrivilegedCarbonContext.class);%0A        PowerMockito.mockStatic(PrivilegedCarbonContext.class);%0A        PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(carbonContext);%0A        PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()).thenReturn(-1234);%0A        PowerMockito.doNothing().when(carbonContext).setTenantDomain(%22carbon.super%22, true);
 %0A   

fix APIMgtThrottleUsageHandlerTest