Search in sources :

Example 1 with APIMgtGoogleAnalyticsUtils

use of org.wso2.carbon.apimgt.gateway.utils.APIMgtGoogleAnalyticsUtils in project carbon-apimgt by wso2.

the class APIMgtGoogleAnalyticsUtilsTestCase method testPublishGATrackingData.

@Test
public void testPublishGATrackingData() {
    APIMgtGoogleAnalyticsUtils apiMgtGoogleAnalyticsUtils = new APIMgtGoogleAnalyticsUtils();
    GoogleAnalyticsData.DataBuilder dataBuilder = Mockito.mock(GoogleAnalyticsData.DataBuilder.class);
    // test when gaConfig == null
    apiMgtGoogleAnalyticsUtils.publishGATrackingData(dataBuilder, "abc", "567r637r6");
    // test when gaConfig !=  null
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    RegistryService registryService = Mockito.mock(RegistryService.class);
    UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
    Resource resource = Mockito.mock(Resource.class);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    try {
        Mockito.when(resource.getContentStream()).thenReturn(new ByteArrayInputStream(Charset.forName("UTF-16").encode("<GoogleAnalyticsTracking>\n" + "\t<!--Enable/Disable Google Analytics Tracking -->\n" + "\t<Enabled>false</Enabled>\n" + "\n" + "\t<!-- Google Analytics Tracking ID -->\n" + "\t<TrackingID>UA-XXXXXXXX-X</TrackingID>\n" + "\n" + "</GoogleAnalyticsTracking>").array()));
        Mockito.when(userRegistry.get("/apimgt/statistics/ga-config.xml")).thenReturn(resource);
    } catch (RegistryException e) {
        fail("RegistryException is thrown when tesing .");
        e.printStackTrace();
    }
    try {
        Mockito.when(registryService.getGovernanceSystemRegistry()).thenReturn(userRegistry);
    } catch (RegistryException e) {
        fail("RegistryException is thrown.");
    }
    apiMgtGoogleAnalyticsUtils.init("abc.com");
    // test when gaconfig.enabled=false
    apiMgtGoogleAnalyticsUtils.publishGATrackingData(dataBuilder, "ishara", "jhgy");
    // test when gaconfig.enabled=true annonymous
    try {
        Mockito.when(resource.getContentStream()).thenReturn(new ByteArrayInputStream(Charset.forName("UTF-16").encode("<GoogleAnalyticsTracking>\n" + "\t<!--Enable/Disable Google Analytics Tracking -->\n" + "\t<Enabled>true</Enabled>\n" + "\n" + "\t<!-- Google Analytics Tracking ID -->\n" + "\t<TrackingID>UA-XXXXXXXX-X</TrackingID>\n" + "\n" + "</GoogleAnalyticsTracking>").array()));
        apiMgtGoogleAnalyticsUtils.init("abc.com");
        GoogleAnalyticsData.DataBuilder dataBuilder1 = Mockito.mock(GoogleAnalyticsData.DataBuilder.class);
        Mockito.when(dataBuilder.setProtocolVersion("1")).thenReturn(dataBuilder);
        Mockito.when(dataBuilder.setTrackingId("UA-XXXXXXXX-X")).thenReturn(dataBuilder);
        Mockito.when(dataBuilder.setClientId("0x05a823c101178dd5")).thenReturn(dataBuilder);
        Mockito.when(dataBuilder.setHitType("pageview")).thenReturn(dataBuilder);
        GoogleAnalyticsData data = Mockito.mock(GoogleAnalyticsData.class);
        Mockito.when(dataBuilder.build()).thenReturn(data);
        PowerMockito.mockStatic(GoogleAnalyticsDataPublisher.class);
        PowerMockito.when(GoogleAnalyticsDataPublisher.buildPayloadString(data)).thenReturn("payload");
    } catch (RegistryException e) {
        fail(e.getMessage());
    }
    apiMgtGoogleAnalyticsUtils.publishGATrackingData(dataBuilder, "ishara", "Autorization ishara");
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) ByteArrayInputStream(java.io.ByteArrayInputStream) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) GoogleAnalyticsData(org.wso2.carbon.ganalytics.publisher.GoogleAnalyticsData) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with APIMgtGoogleAnalyticsUtils

use of org.wso2.carbon.apimgt.gateway.utils.APIMgtGoogleAnalyticsUtils in project carbon-apimgt by wso2.

the class InboundWebsocketProcessorUtil method publishGoogleAnalyticsData.

/**
 * Publish Google Analytics data.
 *
 * @param inboundMessageContext InboundMessageContext
 * @param remoteAddress         Remote IP address
 * @throws WebSocketApiException if an  error occurs
 */
public static void publishGoogleAnalyticsData(InboundMessageContext inboundMessageContext, String remoteAddress) throws WebSocketApiException {
    // publish Google Analytics data
    GoogleAnalyticsData.DataBuilder gaData;
    try {
        gaData = new GoogleAnalyticsData.DataBuilder(null, null, null, null).setDocumentPath(inboundMessageContext.getFullRequestPath()).setDocumentHostName(DataPublisherUtil.getHostAddress()).setSessionControl("end").setCacheBuster(APIMgtGoogleAnalyticsUtils.getCacheBusterId()).setIPOverride(remoteAddress);
        APIMgtGoogleAnalyticsUtils gaUtils = new APIMgtGoogleAnalyticsUtils();
        gaUtils.init(inboundMessageContext.getTenantDomain());
        gaUtils.publishGATrackingData(gaData, inboundMessageContext.getRequestHeaders().get(HttpHeaders.USER_AGENT), inboundMessageContext.getRequestHeaders().get(HttpHeaders.AUTHORIZATION));
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
        throw new WebSocketApiException("Error while publishing google analytics data for API " + inboundMessageContext.getApiContext());
    }
}
Also used : WebSocketApiException(org.wso2.carbon.apimgt.gateway.handlers.streaming.websocket.WebSocketApiException) APIMgtGoogleAnalyticsUtils(org.wso2.carbon.apimgt.gateway.utils.APIMgtGoogleAnalyticsUtils) UnsupportedEncodingException(java.io.UnsupportedEncodingException) GoogleAnalyticsData(org.wso2.carbon.ganalytics.publisher.GoogleAnalyticsData) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 3 with APIMgtGoogleAnalyticsUtils

use of org.wso2.carbon.apimgt.gateway.utils.APIMgtGoogleAnalyticsUtils in project carbon-apimgt by wso2.

the class WebsocketInboundHandlerTestCase method setup.

@Before
public void setup() throws Exception {
    inboundWebSocketProcessor = Mockito.mock(InboundWebSocketProcessor.class);
    channelHandlerContext = Mockito.mock(ChannelHandlerContext.class);
    Channel channel = Mockito.mock(Channel.class);
    ChannelId channelId = Mockito.mock(ChannelId.class);
    Mockito.when(channelHandlerContext.channel()).thenReturn(channel);
    Mockito.when(channel.id()).thenReturn(channelId);
    Mockito.when(channelId.asLongText()).thenReturn(channelIdString);
    Mockito.when(channel.attr(WebSocketUtils.WSO2_PROPERTIES)).thenReturn(getChannelAttributeMap());
    PowerMockito.mockStatic(WebSocketUtils.class);
    SocketAddress socketAddress = Mockito.mock(SocketAddress.class);
    Mockito.when(channel.remoteAddress()).thenReturn(socketAddress);
    PowerMockito.when(WebSocketUtils.getApiProperties(channelHandlerContext)).thenReturn(new HashMap<>());
    APIMgtGoogleAnalyticsUtils apiMgtGoogleAnalyticsUtils = Mockito.mock(APIMgtGoogleAnalyticsUtils.class);
    PowerMockito.whenNew(APIMgtGoogleAnalyticsUtils.class).withAnyArguments().thenReturn(apiMgtGoogleAnalyticsUtils);
    Mockito.doNothing().when(apiMgtGoogleAnalyticsUtils).init("carbon.super");
    Mockito.doNothing().when(apiMgtGoogleAnalyticsUtils).publishGATrackingData(Mockito.anyObject(), Mockito.anyObject(), Mockito.anyObject());
    websocketInboundHandler = new WebsocketInboundHandler() {

        @Override
        protected String getRemoteIP(ChannelHandlerContext ctx) {
            return remoteIP;
        }

        @Override
        public InboundWebSocketProcessor initializeWebSocketProcessor() {
            return inboundWebSocketProcessor;
        }
    };
    websocketAPI = new API(UUID.randomUUID().toString(), 1, "admin", "WSAPI", "1.0.0", "/wscontext", "Unlimited", APIConstants.API_TYPE_WS, APIConstants.PUBLISHED_STATUS, false);
}
Also used : InboundWebSocketProcessor(org.wso2.carbon.apimgt.gateway.inbound.websocket.InboundWebSocketProcessor) Channel(io.netty.channel.Channel) APIMgtGoogleAnalyticsUtils(org.wso2.carbon.apimgt.gateway.utils.APIMgtGoogleAnalyticsUtils) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) API(org.wso2.carbon.apimgt.keymgt.model.entity.API) ChannelId(io.netty.channel.ChannelId) SocketAddress(java.net.SocketAddress) Before(org.junit.Before)

Aggregations

APIMgtGoogleAnalyticsUtils (org.wso2.carbon.apimgt.gateway.utils.APIMgtGoogleAnalyticsUtils)2 GoogleAnalyticsData (org.wso2.carbon.ganalytics.publisher.GoogleAnalyticsData)2 Channel (io.netty.channel.Channel)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelId (io.netty.channel.ChannelId)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SocketAddress (java.net.SocketAddress)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Before (org.junit.Before)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 WebSocketApiException (org.wso2.carbon.apimgt.gateway.handlers.streaming.websocket.WebSocketApiException)1 InboundWebSocketProcessor (org.wso2.carbon.apimgt.gateway.inbound.websocket.InboundWebSocketProcessor)1 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)1 API (org.wso2.carbon.apimgt.keymgt.model.entity.API)1 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)1 Resource (org.wso2.carbon.registry.core.Resource)1 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)1 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)1