### Question:
WeightingMetricValidator extends AbstractMonitoringValidator<MetricDescriptor> { @Override public <T> List<ConstraintViolation<T>> validate( MonitoringValidationContext context, MetricDescriptor metricDescriptor, DescriptorPathImpl path) { Preconditions.checkNotNull(context); Preconditions.checkNotNull(metricDescriptor); Preconditions.checkNotNull(path); path = constructPathFromProperty(metricDescriptor, "name", path); String weightingMetric = metricDescriptor.getWeightingMetricName(); if (null == weightingMetric || weightingMetric.isEmpty()) { return noViolations(); } if (!context.metricsDefined.containsKey(weightingMetric)) { String msg = String.format( "Weighting metric '%s' for metric '%s' refers to unknown metric. ", metricDescriptor.getWeightingMetricName(), metricDescriptor.getName()); return forViolation(msg, metricDescriptor, weightingMetric, path); } if (weightingMetric.equals(metricDescriptor.getName())) { String msg = String.format( "Weighting metric for metric '%s' refers to itself. ", metricDescriptor.getName()); return forViolation(msg, metricDescriptor, weightingMetric, path); } return noViolations(); }  @Override String getDescription(); @Override List<ConstraintViolation<T>> validate(
      MonitoringValidationContext context,
      MetricDescriptor metricDescriptor,
      DescriptorPathImpl path);  }### Answer:
@Test public void testNoWeightingMetric() { setName("bytes_read"); setWeightingMetric(null); assertTrue(validator.validate(context, metric, root).isEmpty()); setWeightingMetric(""); assertTrue(validator.validate(context, metric, root).isEmpty()); }
@Test public void testWeightingMetricInDefinedMetrics() { setName("bytes_read"); setServiceMetrics(ImmutableList.of(newWeightingMetric("foobar"))); context = new MonitoringValidationContext(serviceDescriptor); setWeightingMetric("foobar"); assertTrue(validator.validate(context, metric, root).isEmpty()); }
@Test public void testNoWeightingMetricInDefinedMetrics() { setName("bytes_read"); setServiceMetrics(ImmutableList.of(newWeightingMetric("foobar_2"))); context = new MonitoringValidationContext(serviceDescriptor); setWeightingMetric("foobar"); assertFalse(validator.validate(context, metric, root).isEmpty()); }
@Test public void testWeightingMetricReferringToSelf() { setName("bytes_read"); setServiceMetrics(ImmutableList.of(metric)); context = new MonitoringValidationContext(serviceDescriptor); setWeightingMetric("bytes_read"); assertFalse(validator.validate(context, metric, root).isEmpty()); }