### Question:
MetricNamePrefixedWithServiceNameValidator 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 metricName = metricDescriptor.getName(); String serviceName = context.serviceDescriptor.getName().toLowerCase() + "_"; if (!metricName.startsWith(serviceName)) { String msg = String.format( "Metric '%s' does not start with the service name", metricDescriptor.getName()); return forViolation(msg, metricDescriptor, metricName, path); } return noViolations(); }  @Override String getDescription(); @Override List<ConstraintViolation<T>> validate(
      MonitoringValidationContext context,
      MetricDescriptor metricDescriptor,
      DescriptorPathImpl path);  }### Answer:
@Test public void testValidMetric() { setName(SERVICE_NAME.toLowerCase() + "_a"); Assert.assertTrue(validator.validate(context, metric, root).isEmpty()); }
@Test public void testEmptyName() { setName(""); List<ConstraintViolation<Object>> validations = validator.validate(context, metric, root); Assert.assertFalse(validations.isEmpty()); ConstraintViolation<Object> validation = Iterables.getOnlyElement( validations); Assert.assertTrue(validation.toString(), validation.getMessage().contains( "does not start with the service name")); String path = validation.getPropertyPath().toString(); Assert.assertEquals(String.format("%s.name", SERVICE_NAME), path); }
@Test public void testInvaidMetricNotPrefixed() { setName("foo"); List<ConstraintViolation<Object>> validations = validator.validate(context, metric, root); Assert.assertFalse(validations.isEmpty()); ConstraintViolation<Object> validation = Iterables.getOnlyElement( validations); Assert.assertTrue(validation.toString(), validation.getMessage().contains( "Metric 'foo' does not start with the service name")); String path = validation.getPropertyPath().toString(); Assert.assertEquals(String.format("%s.name", SERVICE_NAME), path); }