private MockTelemetryModule generateTelemetryModules(boolean addParameter) {
    AppInsightsConfigurationBuilder mockParser = createMockParser(true, true, false);
    ApplicationInsightsXmlConfiguration appConf = mockParser.build(null);
    appConf.setInstrumentationKey(MOCK_IKEY);

    TelemetryModulesXmlElement modulesXmlElement = new TelemetryModulesXmlElement();
    ArrayList<AddTypeXmlElement> modules = new ArrayList<AddTypeXmlElement>();
    AddTypeXmlElement addXmlElement = new AddTypeXmlElement();
    addXmlElement.setType("com.microsoft.applicationinsights.internal.config.MockTelemetryModule");

    if (addParameter) {
      final ParamXmlElement param1 = new ParamXmlElement();
      param1.setName("param1");
      param1.setValue("value1");

      ArrayList<ParamXmlElement> list = new ArrayList<ParamXmlElement>();
      list.add(param1);

      addXmlElement.setParameters(list);
    }

    modules.add(addXmlElement);
    modulesXmlElement.setAdds(modules);
    appConf.setModules(modulesXmlElement);

    TelemetryConfiguration mockConfiguration = new TelemetryConfiguration();

    initializeWithFactory(mockParser, mockConfiguration);

    MockTelemetryModule module =
        (MockTelemetryModule) mockConfiguration.getTelemetryModules().get(0);

    return module;
  }
  @Test
  public void testInitializeWithFailingParse() throws Exception {
    AppInsightsConfigurationBuilder mockParser = createMockParserThatFailsToParse();

    TelemetryConfiguration mockConfiguration = new TelemetryConfiguration();

    initializeWithFactory(mockParser, mockConfiguration);

    assertEquals(mockConfiguration.isTrackingDisabled(), false);
    assertTrue(mockConfiguration.getChannel() instanceof InProcessTelemetryChannel);
  }
  @Test
  public void testDefaultChannelWithData() {
    AppInsightsConfigurationBuilder mockParser = createMockParserWithDefaultChannel(true);
    ApplicationInsightsXmlConfiguration appConf = mockParser.build(null);
    appConf.setInstrumentationKey(MOCK_IKEY);
    appConf.getChannel().setDeveloperMode(true);

    TelemetryConfiguration mockConfiguration = new TelemetryConfiguration();

    initializeWithFactory(mockParser, mockConfiguration);

    assertEquals(mockConfiguration.getChannel().isDeveloperMode(), true);
  }
  @Test
  public void testInitializeWithEmptyGetInstrumentationKey() throws Exception {
    AppInsightsConfigurationBuilder mockParser = createMockParser(false, true, false);
    ApplicationInsightsXmlConfiguration appConf = mockParser.build(null);
    appConf.setInstrumentationKey("");

    TelemetryConfiguration mockConfiguration = new TelemetryConfiguration();

    initializeWithFactory(mockParser, mockConfiguration);

    assertEquals(mockConfiguration.isTrackingDisabled(), false);
    assertTrue(mockConfiguration.getChannel() instanceof InProcessTelemetryChannel);
  }
  private void ikeyTest(String configurationIkey, String expectedIkey) {
    // Make sure that there is no exception when fetching the i-key by having both
    // the i-key and channel in the configuration, otherwise the channel won't be instantiated
    AppInsightsConfigurationBuilder mockParser = createMockParser(true, false, false);

    ApplicationInsightsXmlConfiguration appConf = new ApplicationInsightsXmlConfiguration();
    appConf.setInstrumentationKey(configurationIkey);
    Mockito.doReturn(appConf).when(mockParser).build(any(InputStream.class));

    TelemetryConfiguration mockConfiguration = new TelemetryConfiguration();

    initializeWithFactory(mockParser, mockConfiguration);
    assertEquals(mockConfiguration.getInstrumentationKey(), expectedIkey);
    assertTrue(mockConfiguration.getChannel() instanceof InProcessTelemetryChannel);
  }
  @Test
  public void testContextInitializers() {
    AppInsightsConfigurationBuilder mockParser = createMockParser(true, true, false);
    ApplicationInsightsXmlConfiguration appConf = mockParser.build(null);
    appConf.setInstrumentationKey(MOCK_IKEY);

    ContextInitializersXmlElement contextInitializersXmlElement =
        new ContextInitializersXmlElement();
    ArrayList<AddTypeXmlElement> contexts = new ArrayList<AddTypeXmlElement>();
    AddTypeXmlElement addXmlElement = new AddTypeXmlElement();
    addXmlElement.setType(
        "com.microsoft.applicationinsights.extensibility.initializer.DeviceInfoContextInitializer");
    contexts.add(addXmlElement);
    contextInitializersXmlElement.setAdds(contexts);
    appConf.setContextInitializers(contextInitializersXmlElement);

    TelemetryConfiguration mockConfiguration = new TelemetryConfiguration();

    initializeWithFactory(mockParser, mockConfiguration);

    assertEquals(mockConfiguration.isTrackingDisabled(), false);
    assertEquals(mockConfiguration.getInstrumentationKey(), MOCK_IKEY);
    assertEquals(mockConfiguration.getContextInitializers().size(), 3);
    assertTrue(mockConfiguration.getTelemetryInitializers().isEmpty());
    assertTrue(mockConfiguration.getChannel() instanceof StdOutChannel);
  }
  @Test
  public void testPerformanceModules() {
    AppInsightsConfigurationBuilder mockParser = createMockParser(true, true, true);
    ApplicationInsightsXmlConfiguration appConf = mockParser.build(null);
    appConf.setInstrumentationKey(MOCK_IKEY);
    appConf.getChannel().setDeveloperMode(true);

    TelemetryConfigurationFactory.INSTANCE.setPerformanceCountersSection(
        "com.microsoft.applicationinsights.internal.config");
    TelemetryConfiguration mockConfiguration = new TelemetryConfiguration();

    initializeWithFactory(mockParser, mockConfiguration);

    assertEquals(mockConfiguration.getTelemetryModules().size(), 1);
    assertTrue(mockConfiguration.getTelemetryModules().get(0) instanceof MockPerformanceModule);
    assertTrue(
        ((MockPerformanceModule) mockConfiguration.getTelemetryModules().get(0))
            .initializeWasCalled);
    assertTrue(
        ((MockPerformanceModule) mockConfiguration.getTelemetryModules().get(0))
            .addConfigurationDataWasCalled);
  }
  @Test
  public void testWithEmptySections() {
    AppInsightsConfigurationBuilder mockParser =
        Mockito.mock(AppInsightsConfigurationBuilder.class);

    ApplicationInsightsXmlConfiguration appConf = new ApplicationInsightsXmlConfiguration();
    appConf.setInstrumentationKey(MOCK_IKEY);
    appConf.setTelemetryInitializers(null);
    appConf.setContextInitializers(null);
    appConf.setModules(null);
    appConf.setSdkLogger(null);
    Mockito.doReturn(appConf).when(mockParser).build(any(InputStream.class));

    TelemetryConfiguration mockConfiguration = new TelemetryConfiguration();

    initializeWithFactory(mockParser, mockConfiguration);

    assertEquals(mockConfiguration.isTrackingDisabled(), false);
    assertEquals(mockConfiguration.getInstrumentationKey(), MOCK_IKEY);
    assertEquals(mockConfiguration.getContextInitializers().size(), 2);
    assertTrue(mockConfiguration.getChannel() instanceof InProcessTelemetryChannel);
  }
  @Test
  public void testInitializeAllDefaults() throws Exception {
    AppInsightsConfigurationBuilder mockParser = createMockParser(true, true, false);
    ApplicationInsightsXmlConfiguration appConf = mockParser.build(null);
    appConf.setInstrumentationKey(MOCK_IKEY);

    TelemetryConfiguration mockConfiguration = new TelemetryConfiguration();

    initializeWithFactory(mockParser, mockConfiguration);

    assertEquals(mockConfiguration.isTrackingDisabled(), false);
    assertEquals(mockConfiguration.getInstrumentationKey(), MOCK_IKEY);
    assertEquals(mockConfiguration.getContextInitializers().size(), 2);
    assertTrue(mockConfiguration.getTelemetryInitializers().isEmpty());
    assertTrue(mockConfiguration.getChannel() instanceof StdOutChannel);
  }