/** * Gets the full user agent in the form of * {@code "(UserAgent1,UserAgent2,UserAgent3,...)"} */ public String getUserAgent(String nonLibraryUserAgent) { StringBuilder agentStringBuilder = new StringBuilder(); agentStringBuilder.append(nonLibraryUserAgent); agentStringBuilder.append(" ("); Joiner.on(", ") .skipNulls() .appendTo( agentStringBuilder, Iterables.transform( userAgentProviders, input -> input == null ? null : input.getUserAgent())); agentStringBuilder.append(')'); return agentStringBuilder.toString(); }
/** * Creates a map with data from the user's service session which is needed to set Ad Manager SOAP * headers. * * @param adManagerSession the user's session object * @return a map of HTTP header names to values */ private Map<String, Object> readHeaderElements(AdManagerSession adManagerSession) { // The order here must match the order of the SoapRequestHeader elements in the WSDL. Map<String, Object> mapToFill = Maps.newLinkedHashMap(); mapToFill.put("networkCode", adManagerSession.getNetworkCode()); mapToFill.put( "applicationName", userAgentCombiner.getUserAgent(adManagerSession.getApplicationName())); return mapToFill; }
@Test public void testGenerateLibraryUserAgent() { String nonLibraryUserAgent = "myUserAgent"; when(userAgentProvider1.getUserAgent()).thenReturn("userAgent/1"); when(userAgentProvider2.getUserAgent()).thenReturn("userAgent/2"); assertEquals( "myUserAgent (userAgent/1, userAgent/2)", userAgentCombiner.getUserAgent(nonLibraryUserAgent)); assertEquals( "myUserAgent (userAgent/1, userAgent/2)", userAgentCombiner.getUserAgent(nonLibraryUserAgent)); } }
when(extensionUserAgentProvider.getUserAgent()).thenReturn(pluginUserAgentSnippet); String combinedUserAgent = userAgentCombiner.getUserAgent("foo"); assertThat( "Combined user agent does not contain the string from the plugin", userAgentCombiner = genericAdWordsServices.getBootstrapper().getInstanceOf(session, UserAgentCombiner.class); combinedUserAgent = userAgentCombiner.getUserAgent("foo"); assertThat( "Combined user agent contains string from the plugin, but plugin was not provided",
/** * Tests that if no utilities are registered, the provider will return null. */ @Test public void testGenerateUserAgent_noUtilitiesRegistered_includeUtilitiesTrue() { when(adsLibConfiguration.isIncludeAdsUtilitiesInUserAgent()).thenReturn(true); String actualUserAgent = userAgentProvider.getUserAgent(); assertNull("User agent should be null if no utilities are registered", actualUserAgent); assertTrue("User agent provider should clear utilities", adsUtilityRegistry.popRegisteredUtilities().isEmpty()); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); userAgentProvider = new AdsUtilitiesUserAgentProvider(adsUtilityRegistry, adsLibConfiguration); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); userAgentCombiner = new UserAgentCombiner(Lists.newArrayList(userAgentProvider1, userAgentProvider2)); }
/** * Creates a map with data from the user's service session which is needed to * set AdWords SOAP headers. * * @param adWordsSession the user's session object * @return a map of HTTP header names to values */ private Map<String, Object> readHeaderElements(AdWordsSession adWordsSession) { // The order here must match the order of the SoapHeader elements in the WSDL. Map<String, Object> mapToFill = Maps.newLinkedHashMap(); mapToFill.put("clientCustomerId", adWordsSession.getClientCustomerId()); mapToFill.put("developerToken", adWordsSession.getDeveloperToken()); mapToFill.put("userAgent", userAgentCombiner.getUserAgent(adWordsSession.getUserAgent())); mapToFill.put("validateOnly", adWordsSession.isValidateOnly()); mapToFill.put("partialFailure", adWordsSession.isPartialFailure()); return mapToFill; }
/** * Gets the full user agent in the form of * {@code "(UserAgent1,UserAgent2,UserAgent3,...)"} */ public String getUserAgent(String nonLibraryUserAgent) { StringBuilder agentStringBuilder = new StringBuilder(); agentStringBuilder.append(nonLibraryUserAgent); agentStringBuilder.append(" ("); Joiner.on(", ") .skipNulls() .appendTo( agentStringBuilder, Iterables.transform( userAgentProviders, input -> input == null ? null : input.getUserAgent())); agentStringBuilder.append(')'); return agentStringBuilder.toString(); }
/** * Tests that a single registered utility will result in the correct user agent if include * utilities is true. */ @Test public void testGenerateUserAgent_utilityRegistered_includeUtilitiesTrue() { when(adsLibConfiguration.isIncludeAdsUtilitiesInUserAgent()).thenReturn(true); adsUtilityRegistry.addUtility(AdsUtility.PRODUCT_PARTITION_TREE); String actualUserAgent = userAgentProvider.getUserAgent(); assertEquals("User agent should include registered utilities if include utilities is true", AdsUtility.PRODUCT_PARTITION_TREE.getUserAgentIdentifier(), actualUserAgent); }
/** * Test method for {@link AdWordsInternals#getInstance()}. */ @Test public void testGetInstance() { Internals internals = AdWordsInternals.getInstance(); String userAgent = internals.getUserAgentCombiner().getUserAgent("test"); assertTrue(userAgent.contains("AwApi")); assertFalse(userAgent.contains("DfaApi")); assertFalse(userAgent.contains("DfpApi")); }
/** * Tests that if include utilities is false, the provider will return null (even if utilities * are registered). */ @Test public void testGenerateUserAgent_utilitiesRegistered_includeUtilitiesFalse() { when(adsLibConfiguration.isIncludeAdsUtilitiesInUserAgent()).thenReturn(false); adsUtilityRegistry.addUtility(AdsUtility.PRODUCT_PARTITION_TREE); String actualUserAgent = userAgentProvider.getUserAgent(); assertNull("User agent should be null if include utilities is false", actualUserAgent); assertTrue("User agent provider should clear utilities", adsUtilityRegistry.popRegisteredUtilities().isEmpty()); } }
/** Test method for {@link AdManagerInternals#getInstance()}. */ @Test public void testGetInstance() { Internals internals = AdManagerInternals.getInstance(); String userAgent = internals.getUserAgentCombiner().getUserAgent("test"); assertTrue(userAgent.contains("DfpApi")); assertFalse(userAgent.contains("DfaApi")); assertFalse(userAgent.contains("AwApi")); } }
/** * Tests that the user agent provider will always return multiple user agent identifiers * in alphabetical order, regardless of the order of registration. */ @Test public void testGenerateUserAgent_multipleUtilitiesRegistered_includeUtilitiesTrue() { when(adsLibConfiguration.isIncludeAdsUtilitiesInUserAgent()).thenReturn(true); List<AdsUtility> adsUtilities = Arrays.asList( AdsUtility.PRODUCT_PARTITION_TREE, AdsUtility.SELECTOR_BUILDER, AdsUtility.REPORT_DOWNLOADER); for (int i = 0; i < adsUtilities.size(); i++) { Collections.shuffle(adsUtilities); for (AdsUtility adsUtility : adsUtilities) { adsUtilityRegistry.addUtility(adsUtility); } String expectedUserAgent = "ProductPartitionTree, ReportDownloader, SelectorBuilder"; String actualUserAgent = userAgentProvider.getUserAgent(); assertEquals( "User agent should include registered utilities in alphabetical order if include " + "utilities is true", expectedUserAgent, actualUserAgent); assertTrue("User agent provider should clear utilities", adsUtilityRegistry.popRegisteredUtilities().isEmpty()); } }
httpHeaders.setAuthorization( authorizationHeaderProvider.getAuthorizationHeader(session, reportUrl)); httpHeaders.setUserAgent(userAgentCombiner.getUserAgent(session.getUserAgent())); httpHeaders.set("developerToken", session.getDeveloperToken()); httpHeaders.set("clientCustomerId", session.getClientCustomerId());
httpHeaders.setAuthorization( authorizationHeaderProvider.getAuthorizationHeader(session, reportUrl)); httpHeaders.setUserAgent(userAgentCombiner.getUserAgent(session.getUserAgent())); httpHeaders.set("developerToken", session.getDeveloperToken()); httpHeaders.set("clientCustomerId", session.getClientCustomerId());
adWordsSession.getClientCustomerId()); soapClientHandler.setHeaderChild(stub, REQUEST_HEADER_LOCAL_PART, "userAgent", userAgentCombiner.getUserAgent(adWordsSession.getUserAgent())); soapClientHandler.setHeaderChild(stub, REQUEST_HEADER_LOCAL_PART, "validateOnly", adWordsSession.isValidateOnly());
adWordsSession.getClientCustomerId()); soapClientHandler.setHeaderChild(stub, REQUEST_HEADER_LOCAL_PART, "userAgent", userAgentCombiner.getUserAgent(adWordsSession.getUserAgent())); soapClientHandler.setHeaderChild(stub, REQUEST_HEADER_LOCAL_PART, "validateOnly", adWordsSession.isValidateOnly());
soapHeader, "applicationName", userAgentCombiner.getUserAgent(adManagerSession.getApplicationName())); if (adManagerSession.getNetworkCode() != null) { BeanUtils.setProperty(soapHeader, "networkCode", adManagerSession.getNetworkCode());
when(authorizationHeaderProvider.getAuthorizationHeader(session, ENDPOINT_URL.build())) .thenReturn("fooauthheader"); when(userAgentCombiner.getUserAgent(anyString())).thenReturn("foouseragent"); ReportRequestFactoryHelper helper = new ReportRequestFactoryHelper(