@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 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 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 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 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);
  }