/** ********************************************************************************* */
  private SourceMetricsImpl configureSourceMetrics(String sourceId) throws Exception {

    catalogProvider = mock(CatalogProvider.class);
    when(catalogProvider.getId()).thenReturn(sourceId);

    fedSource = mock(FederatedSource.class);
    when(fedSource.getId()).thenReturn("fs-1");

    sourceMetrics = new SourceMetricsImpl();
    sourceMetrics.setCatalogProviders(Collections.singletonList(catalogProvider));
    sourceMetrics.setFederatedSources(Collections.singletonList(fedSource));

    assertThat(sourceMetrics, not(nullValue()));

    return sourceMetrics;
  }
  private void addSource() throws Exception {
    // Do not call addingSource() because it starts createSourceMetrics()
    // in a separate thread hence predictable results are not assured
    // sourceMetrics.addingSource(catalogProvider, null);

    sourceMetrics.createSourceMetrics(catalogProvider);
  }
  @Test
  public void testUpdateNonExistentMetricForNewSourceId() throws Exception {
    String sourceId = "new-source";
    String metricName = "invalid-metric";

    SourceMetricsImpl sourceMetrics = configureSourceMetrics(sourceId);

    sourceMetrics.updateMetric(sourceId, metricName, 1);

    // Verify none of the valid metrics were updated (but they were
    // created since this is first time this sourceId was detected by
    // the SourceMetricsImpl)
    assertMetricCount(sourceId, SourceMetrics.QUERIES_TOTAL_RESULTS_SCOPE, 0);
    assertMetricCount(sourceId, SourceMetrics.QUERIES_SCOPE, 0);
    assertMetricCount(sourceId, SourceMetrics.EXCEPTIONS_SCOPE, 0);
  }
  @Test
  public void testNewSource() throws Exception {
    String sourceId = "cp-1";
    String metricName = SourceMetrics.QUERIES_SCOPE;

    sourceMetrics = configureSourceMetrics(sourceId);
    sourceMetrics.updateMetric(sourceId, metricName, 1);

    assertMetricCount(sourceId, metricName, 1);
  }
  @Test
  public void testUpdateEmptyMetricForSourceId() throws Exception {
    String sourceId = "existing-source";

    sourceMetrics = configureSourceMetrics(sourceId);

    sourceMetrics.updateMetric(sourceId, "", 1);

    assertThat(sourceMetrics.metrics.size(), is(0));
  }
  @Test
  public void testGetRrdFilenameNonAlphanumerics() throws Exception {
    String sourceId = "dib30rh%^&*()$e#@!l-58";
    String collectorName = SourceMetrics.QUERIES_TOTAL_RESULTS_SCOPE;

    sourceMetrics = configureSourceMetrics(sourceId);

    String rrdFilename = sourceMetrics.getRrdFilename(sourceId, collectorName);

    assertThat(rrdFilename, equalTo("sourceDib30rhEL58QueriesTotalResults"));
  }
  @Test
  public void testGetRrdFilename() throws Exception {
    String sourceId = "cp1";
    String collectorName = SourceMetrics.QUERIES_TOTAL_RESULTS_SCOPE;

    sourceMetrics = configureSourceMetrics(sourceId);

    String rrdFilename = sourceMetrics.getRrdFilename(sourceId, collectorName);

    assertThat(rrdFilename, equalTo("sourceCp1QueriesTotalResults"));
  }
  @Test
  public void testUpdateMetricForEmptySourceId() throws Exception {
    String sourceId = "existing-source";
    String metricName = SourceMetrics.QUERIES_SCOPE;

    sourceMetrics = configureSourceMetrics(sourceId);

    sourceMetrics.updateMetric("", metricName, 1);

    assertThat(sourceMetrics.metrics.size(), is(0));
  }
  @Test
  public void testDeleteSourceBlankSourceId() throws Exception {
    String sourceId = "cp-1";
    String metricName = SourceMetrics.QUERIES_SCOPE;

    sourceMetrics = configureSourceMetrics(sourceId);
    addSource();

    // Simulate Source returning empty sourceId
    when(catalogProvider.getId()).thenReturn("");

    sourceMetrics.deletingSource(catalogProvider, null);

    String key = sourceId + "." + metricName;
    SourceMetric sourceMetric = sourceMetrics.metrics.get(key);
    assertThat(sourceMetric, not(nullValue()));

    sourceMetrics.deletingSource(null, null);

    key = sourceId + "." + metricName;
    sourceMetric = sourceMetrics.metrics.get(key);
    assertThat(sourceMetric, not(nullValue()));
  }
  @Test
  public void testUpdateNonExistingSourceMetric() throws Exception {
    String sourceId = "existing-source";
    String metricName = SourceMetrics.QUERIES_SCOPE;

    sourceMetrics = configureSourceMetrics(sourceId);

    String nonExistingSourceId = "non-existing-source-id";
    sourceMetrics.updateMetric(nonExistingSourceId, metricName, 1);

    String key = nonExistingSourceId + "." + metricName;
    SourceMetric sourceMetric = sourceMetrics.metrics.get(key);
    assertThat(sourceMetric, is(nullValue()));
  }
  @Test
  public void testAddDeleteSource() throws Exception {
    String sourceId = "cp-1";
    String metricName = SourceMetrics.QUERIES_SCOPE;

    sourceMetrics = configureSourceMetrics(sourceId);
    addSource();

    String key = sourceId + "." + metricName;
    SourceMetric sourceMetric = sourceMetrics.metrics.get(key);
    assertThat(sourceMetric, not(nullValue()));

    sourceMetrics.deletingSource(catalogProvider, null);
    sourceMetric = sourceMetrics.metrics.get(key);
    assertThat(sourceMetric, is(nullValue()));
  }
  @Test
  public void testSourceCreatedWithNullId() throws Exception {
    String sourceId = null;
    String metricName = SourceMetrics.QUERIES_TOTAL_RESULTS_SCOPE;

    sourceMetrics = configureSourceMetrics(sourceId);

    // Simulate initial creation of Source
    addSource();

    // Simulate changing Source's name
    String newSourceId = "cp-1";
    when(catalogProvider.getId()).thenReturn(newSourceId);

    sourceMetrics.updateMetric(newSourceId, metricName, 1);

    assertMetricCount(newSourceId, metricName, 1);
  }