### Question:
AttributesReferToExistingAttributesValidator extends AbstractMonitoringValidator<MetricEntityTypeDescriptor> { @Override public <T> List<ConstraintViolation<T>> validate( MonitoringValidationContext context, MetricEntityTypeDescriptor entity, DescriptorPathImpl path) { Preconditions.checkNotNull(context); Preconditions.checkNotNull(entity); Preconditions.checkNotNull(path); List<ConstraintViolation<T>> ret = Lists.newArrayList(); ret.addAll(this.<T>checkAttributes(context, entity.getImmutableAttributeNames(), "immutableAttributeNames", entity, path)); ret.addAll(this.<T>checkAttributes(context, entity.getMutableAttributeNames(), "mutableAttributeNames", entity, path)); return ret; } AttributesReferToExistingAttributesValidator(
      Set<String> builtInAttributes); @Override String getDescription(); @Override List<ConstraintViolation<T>> validate(
      MonitoringValidationContext context,
      MetricEntityTypeDescriptor entity,
      DescriptorPathImpl path);  }### Answer:
@Test public void testNoAttributes() { assertTrue(validator.validate(context, entity, root).isEmpty()); }
@Test public void testImmutableBuiltInAttributes() { doReturn(ImmutableList.of("builtIn1", "builtIn2")).when(entity) .getImmutableAttributeNames(); addEntity(entity); assertTrue(validator.validate(context, entity, root).isEmpty()); }
@Test public void testImmutableDefinedAttributes() { doReturn(ImmutableList.of("attr1", "attr2", "builtIn1", "builtIn2")) .when(entity).getImmutableAttributeNames(); addEntity(entity); assertTrue(validator.validate(context, entity, root).isEmpty()); }
@Test public void testMutableBuiltInAttributes() { doReturn(ImmutableList.of("builtIn1", "builtIn2")).when(entity) .getMutableAttributeNames(); addEntity(entity); assertTrue(validator.validate(context, entity, root).isEmpty()); }
@Test public void testMutableDefinedAttributes() { doReturn(ImmutableList.of("attr1", "attr2", "builtIn1", "builtIn2")) .when(entity).getMutableAttributeNames(); addEntity(entity); assertTrue(validator.validate(context, entity, root).isEmpty()); }
@Test public void testNonExistingMutableAttributes() { doReturn(ImmutableList.of("nonExistingAttribute", "attr2" )) .when(entity).getMutableAttributeNames(); doReturn(ImmutableList.of("builtIn1", "builtIn2", "attr1")).when(entity) .getImmutableAttributeNames(); addEntity(entity); List<ConstraintViolation<Object>> validations = validator.validate(context, entity, root); assertFalse(validations.isEmpty()); ConstraintViolation<Object> validation = Iterables.getOnlyElement( validations); assertTrue(validation.toString(), validation.getMessage().contains( "Unknown attribute 'nonExistingAttribute' for metric " + "entity type 'FOOBAR_ENTITY'.")); String path = validation.getPropertyPath().toString(); assertEquals(String.format("%s.mutableAttributeNames", SERVICE_NAME), path); }
@Test public void testNonExistingImmutableAttributes() { doReturn(ImmutableList.of("nonExistingAttribute", "attr2" )) .when(entity).getImmutableAttributeNames(); doReturn(ImmutableList.of("builtIn1", "builtIn2", "attr1")).when(entity) .getMutableAttributeNames(); addEntity(entity); List<ConstraintViolation<Object>> validations = validator.validate( context, entity, root); assertFalse(validations.isEmpty()); ConstraintViolation<Object> validation = Iterables.getOnlyElement( validations); assertTrue(validation.toString(), validation.getMessage().contains( "Unknown attribute 'nonExistingAttribute' for metric " + "entity type 'FOOBAR_ENTITY'.")); String path = validation.getPropertyPath().toString(); assertEquals(String.format("%s.immutableAttributeNames", SERVICE_NAME), path); }