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");
}
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());
}
}
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);
}
Aggregations