SpecMonitoringInfoValidator { public Optional<String> validate(MonitoringInfo monitoringInfo) { MonitoringInfoSpec spec = null; if (monitoringInfo.getUrn().isEmpty() || monitoringInfo.getType().isEmpty()) { return Optional.of( String.format( "MonitoringInfo requires both urn %s and type %s to be specified.", monitoringInfo.getUrn(), monitoringInfo.getType())); } for (MonitoringInfoSpec specIterator : specs) { if (monitoringInfo.getUrn().equals(specIterator.getUrn()) && monitoringInfo.getType().equals(specIterator.getType())) { spec = specIterator; break; } } if (spec == null) { return Optional.empty(); } Set<String> requiredLabels = new HashSet<>(spec.getRequiredLabelsList()); if (!monitoringInfo.getLabelsMap().keySet().containsAll(requiredLabels)) { return Optional.of( String.format( "MonitoringInfo with urn: %s should have labels: %s, actual: %s", monitoringInfo.getUrn(), requiredLabels, monitoringInfo.getLabelsMap())); } return Optional.empty(); } SpecMonitoringInfoValidator(); Optional<String> validate(MonitoringInfo monitoringInfo);  }
@Test public void validateReturnsErrorOnMissingUrnOrType() { MonitoringInfo testInput = MonitoringInfo.newBuilder() .putLabels(MonitoringInfoConstants.Labels.NAME, "anyCounter") .putLabels(MonitoringInfoConstants.Labels.NAMESPACE, "") .putLabels(MonitoringInfoConstants.Labels.PTRANSFORM, "anyString") .setType(TypeUrns.SUM_INT64_TYPE) .build(); assertTrue(new SpecMonitoringInfoValidator().validate(testInput).isPresent()); testInput = MonitoringInfo.newBuilder() .setUrn(Urns.USER_SUM_INT64) .putLabels(MonitoringInfoConstants.Labels.NAME, "anyCounter") .putLabels(MonitoringInfoConstants.Labels.NAMESPACE, "") .putLabels(MonitoringInfoConstants.Labels.PTRANSFORM, "anyString") .build(); assertTrue(new SpecMonitoringInfoValidator().validate(testInput).isPresent()); }
@Test public void validateReturnsNoErrorOnValidMonitoringInfo() { MonitoringInfo testInput = MonitoringInfo.newBuilder() .setUrn(Urns.USER_SUM_INT64) .putLabels(MonitoringInfoConstants.Labels.NAME, "anyCounter") .putLabels(MonitoringInfoConstants.Labels.NAMESPACE, "") .putLabels(MonitoringInfoConstants.Labels.PTRANSFORM, "anyString") .setType(TypeUrns.SUM_INT64_TYPE) .build(); assertFalse(new SpecMonitoringInfoValidator().validate(testInput).isPresent()); testInput = MonitoringInfo.newBuilder() .setUrn(Urns.USER_DISTRIBUTION_INT64) .putLabels(MonitoringInfoConstants.Labels.NAME, "anyDistribution") .putLabels(MonitoringInfoConstants.Labels.NAMESPACE, "namespace") .putLabels(MonitoringInfoConstants.Labels.PTRANSFORM, "anyString") .setType(TypeUrns.DISTRIBUTION_INT64_TYPE) .putLabels("dummy", "value") .build(); assertFalse(new SpecMonitoringInfoValidator().validate(testInput).isPresent()); testInput = MonitoringInfo.newBuilder() .setUrn(MonitoringInfoConstants.Urns.ELEMENT_COUNT) .setType(TypeUrns.SUM_INT64_TYPE) .putLabels(MonitoringInfoConstants.Labels.PTRANSFORM, "value") .putLabels(MonitoringInfoConstants.Labels.PCOLLECTION, "anotherValue") .build(); assertFalse(new SpecMonitoringInfoValidator().validate(testInput).isPresent()); }
@Test public void validateReturnsErrorOnInvalidMonitoringInfoLabels() { MonitoringInfo testInput = MonitoringInfo.newBuilder() .setUrn(MonitoringInfoConstants.Urns.ELEMENT_COUNT) .setType(TypeUrns.SUM_INT64_TYPE) .putLabels(MonitoringInfoConstants.Labels.PTRANSFORM, "unexpectedLabel") .build(); assertTrue(new SpecMonitoringInfoValidator().validate(testInput).isPresent()); }