### Question:
EntityParentsReferToExistingEntitiesValidator 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); if (null == entity.getParentMetricEntityTypeNames() || entity.getParentMetricEntityTypeNames().isEmpty()) { return noViolations(); } Set<String> entitiesNames = Sets.newHashSet(context.entitiesDefined.keySet()); entitiesNames.add( context.serviceDescriptor.getName().toUpperCase()); entitiesNames.addAll(context.rolesDefined.keySet()); path = constructPathFromProperty(entity, "parentMetricEntityTypeNames", path); List<ConstraintViolation<T>> ret = Lists.newArrayList(); for (String parentName : entity.getParentMetricEntityTypeNames()) { if (!entitiesNames.contains(parentName) && !builtInRoleTypes.contains(parentName) && !builtInEntityTypes.contains(parentName)) { String msg = String.format( "Unknown parent '%s' for metric entity type '%s'.", parentName, entity.getName()); ret.addAll( AbstractMonitoringValidator.<T, MetricEntityTypeDescriptor> forViolation(msg, entity, parentName, path)); } } return ret; } EntityParentsReferToExistingEntitiesValidator(
      ImmutableSet<String> builtInRoleTypes,
      ImmutableSet<String> builtInEntityTypes); @Override String getDescription(); @Override List<ConstraintViolation<T>> validate(
      MonitoringValidationContext context,
      MetricEntityTypeDescriptor entity,
      DescriptorPathImpl path);  }### Answer:
@Test public void testNoEntities() { assertTrue(validator.validate(context, entity, root).isEmpty()); }
@Test public void testEntitiesNoParents() { MetricEntityTypeDescriptor entity2 = mockEntity("foobar_entity_two"); addEntity(entity); addEntity(entity2); assertTrue(validator.validate(context, entity, root).isEmpty()); }
@Test public void testUnknownParent() { MetricEntityTypeDescriptor entity2 = mockEntity("foobar_entity_two"); doReturn(ImmutableList.of("FOOBAR", "non_existing_parent")).when(entity) .getParentMetricEntityTypeNames(); addEntity(entity); addEntity(entity2); context = new MonitoringValidationContext(serviceDescriptor); 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 parent 'non_existing_parent' for metric entity " + "type 'foobar_entity_one'")); String path = validation.getPropertyPath().toString(); assertEquals(String.format("%s.parentMetricEntityTypeNames", SERVICE_NAME), path); }
@Test public void testServiceParent() { doReturn(ImmutableList.of("FOOBAR")).when(entity) .getParentMetricEntityTypeNames(); addEntity(entity); assertTrue(validator.validate(context, entity, root).isEmpty()); }
@Test public void testRoleParent() { doReturn(ImmutableList.of( "FOOBAR", MonitoringConventions.getRoleMetricEntityTypeName( serviceDescriptor.getName(), "ROLE1"))) .when(entity) .getParentMetricEntityTypeNames(); addEntity(entity); RoleMonitoringDefinitionsDescriptor role = mockRole("ROLE1"); addRole(role); context = new MonitoringValidationContext(serviceDescriptor); assertTrue(validator.validate(context, entity, root).isEmpty()); }
@Test public void testBuiltInRoleType() { doReturn(ImmutableList.of("BUILT_IN_ROLE_TYPE")).when(entity) .getParentMetricEntityTypeNames(); addEntity(entity); assertTrue(validator.validate(context, entity, root).isEmpty()); }
@Test public void testBuiltInEntityType() { doReturn(ImmutableList.of("BUILT_IN_ENTITY_TYPE")).when(entity) .getParentMetricEntityTypeNames(); addEntity(entity); assertTrue(validator.validate(context, entity, root).isEmpty()); }