// Path: cm-schema/src/main/java/com/cloudera/csd/validation/monitoring/MonitoringValidationContext.java
// public class MonitoringValidationContext {
// 
//   public final ServiceMonitoringDefinitionsDescriptor serviceDescriptor;
//   public final ImmutableMap<String, MetricDescriptor> metricsDefined;
//   public final ImmutableMap<String, MetricEntityTypeDescriptor> entitiesDefined;
//   public final ImmutableMap<String, RoleMonitoringDefinitionsDescriptor> rolesDefined;
//   public final ImmutableMap<String, MetricEntityAttributeDescriptor> attributesDefined;
// 
//   public MonitoringValidationContext(
//       ServiceMonitoringDefinitionsDescriptor serviceDescriptor) {
//     Preconditions.checkNotNull(serviceDescriptor);
//     this.serviceDescriptor = serviceDescriptor;
//     ImmutableMap.Builder<String, RoleMonitoringDefinitionsDescriptor>
//       rolesDefinedBuilder = ImmutableMap.builder();
//     // We can't use an ImmutableMap.Builder since it will not allow multiple
//     // entries with the same key. Instead we build a local hash map and make
//     // it immutable below.
//     Map<String, MetricDescriptor> metricsDefinedBuilder = Maps.newHashMap();
//     metricsDefinedBuilder.putAll(
//         extractMetrics(serviceDescriptor.getMetricDefinitions()));
//     if (null != serviceDescriptor.getRoles()) {
//       for (RoleMonitoringDefinitionsDescriptor role :
//            serviceDescriptor.getRoles()) {
//         rolesDefinedBuilder.put(
//             MonitoringConventions.getRoleMetricEntityTypeName(
//                 serviceDescriptor.getName(),
//                 role.getName()),
//             role);
//         metricsDefinedBuilder.putAll(
//             extractMetrics(role.getMetricDefinitions()));
//       }
//     }
//     if (null != serviceDescriptor.getMetricEntityTypeDefinitions()) {
//       for (MetricEntityTypeDescriptor entity :
//           serviceDescriptor.getMetricEntityTypeDefinitions()) {
//         metricsDefinedBuilder.putAll(
//             extractMetrics(entity.getMetricDefinitions()));
//       }
//     }
//     ImmutableMap.Builder<String, MetricEntityTypeDescriptor>
//       entitiesDefinedBuilder = ImmutableMap.builder();
//     if (null != serviceDescriptor.getMetricEntityTypeDefinitions()) {
//       for (MetricEntityTypeDescriptor entity :
//            serviceDescriptor.getMetricEntityTypeDefinitions()) {
//         entitiesDefinedBuilder.put(entity.getName(), entity);
//       }
//     }
//     ImmutableMap.Builder<String, MetricEntityAttributeDescriptor>
//       attributesDefinedBuilder = ImmutableMap.builder();
//     if (null != serviceDescriptor.getMetricEntityAttributeDefinitions()) {
//       for (MetricEntityAttributeDescriptor attribute :
//            serviceDescriptor.getMetricEntityAttributeDefinitions()) {
//         attributesDefinedBuilder.put(attribute.getName(), attribute);
//       }
//     }
//     metricsDefined = ImmutableMap.copyOf(metricsDefinedBuilder);
//     rolesDefined = rolesDefinedBuilder.build();
//     entitiesDefined = entitiesDefinedBuilder.build();
//     attributesDefined = attributesDefinedBuilder.build();
//   }
// 
//   private Map<String, MetricDescriptor> extractMetrics(
//       @Nullable List<MetricDescriptor> metrics) {
//     if (null == metrics) {
//       return ImmutableMap.of();
//     }
//     Map<String, MetricDescriptor> ret = Maps.newHashMap();
//     for (MetricDescriptor metric : metrics) {
//       ret.put(metric.getName(), metric);
//     }
//     return ret;
//   }
// }


import com.cloudera.csd.validation.monitoring.MonitoringValidationContext;
import com.google.common.collect.Iterables;
import java.util.List;
import javax.validation.ConstraintViolation;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
// Licensed to Cloudera, Inc. under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  Cloudera, Inc. licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.cloudera.csd.validation.monitoring.constraints;





public class MetricNamePrefixedWithServiceNameValidatorTest
    extends AbstractMonitoringValidatorBaseTest {

  private MetricNamePrefixedWithServiceNameValidator validator;
  private MonitoringValidationContext context;

  @Before
  public void setupMetricNamePrefixedWithServiceNameValidatorTest() {
    // We should do all comparisons in a case insensitive way.
    validator = new MetricNamePrefixedWithServiceNameValidator();
    context = new MonitoringValidationContext(serviceDescriptor);
  }

  @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(
// Path: cm-schema/src/main/java/com/cloudera/csd/validation/monitoring/MonitoringValidationContext.java
// public class MonitoringValidationContext {
// 
//   public final ServiceMonitoringDefinitionsDescriptor serviceDescriptor;
//   public final ImmutableMap<String, MetricDescriptor> metricsDefined;
//   public final ImmutableMap<String, MetricEntityTypeDescriptor> entitiesDefined;
//   public final ImmutableMap<String, RoleMonitoringDefinitionsDescriptor> rolesDefined;
//   public final ImmutableMap<String, MetricEntityAttributeDescriptor> attributesDefined;
// 
//   public MonitoringValidationContext(
//       ServiceMonitoringDefinitionsDescriptor serviceDescriptor) {
//     Preconditions.checkNotNull(serviceDescriptor);
//     this.serviceDescriptor = serviceDescriptor;
//     ImmutableMap.Builder<String, RoleMonitoringDefinitionsDescriptor>
//       rolesDefinedBuilder = ImmutableMap.builder();
//     // We can't use an ImmutableMap.Builder since it will not allow multiple
//     // entries with the same key. Instead we build a local hash map and make
//     // it immutable below.
//     Map<String, MetricDescriptor> metricsDefinedBuilder = Maps.newHashMap();
//     metricsDefinedBuilder.putAll(
//         extractMetrics(serviceDescriptor.getMetricDefinitions()));
//     if (null != serviceDescriptor.getRoles()) {
//       for (RoleMonitoringDefinitionsDescriptor role :
//            serviceDescriptor.getRoles()) {
//         rolesDefinedBuilder.put(
//             MonitoringConventions.getRoleMetricEntityTypeName(
//                 serviceDescriptor.getName(),
//                 role.getName()),
//             role);
//         metricsDefinedBuilder.putAll(
//             extractMetrics(role.getMetricDefinitions()));
//       }
//     }
//     if (null != serviceDescriptor.getMetricEntityTypeDefinitions()) {
//       for (MetricEntityTypeDescriptor entity :
//           serviceDescriptor.getMetricEntityTypeDefinitions()) {
//         metricsDefinedBuilder.putAll(
//             extractMetrics(entity.getMetricDefinitions()));
//       }
//     }
//     ImmutableMap.Builder<String, MetricEntityTypeDescriptor>
//       entitiesDefinedBuilder = ImmutableMap.builder();
//     if (null != serviceDescriptor.getMetricEntityTypeDefinitions()) {
//       for (MetricEntityTypeDescriptor entity :
//            serviceDescriptor.getMetricEntityTypeDefinitions()) {
//         entitiesDefinedBuilder.put(entity.getName(), entity);
//       }
//     }
//     ImmutableMap.Builder<String, MetricEntityAttributeDescriptor>
//       attributesDefinedBuilder = ImmutableMap.builder();
//     if (null != serviceDescriptor.getMetricEntityAttributeDefinitions()) {
//       for (MetricEntityAttributeDescriptor attribute :
//            serviceDescriptor.getMetricEntityAttributeDefinitions()) {
//         attributesDefinedBuilder.put(attribute.getName(), attribute);
//       }
//     }
//     metricsDefined = ImmutableMap.copyOf(metricsDefinedBuilder);
//     rolesDefined = rolesDefinedBuilder.build();
//     entitiesDefined = entitiesDefinedBuilder.build();
//     attributesDefined = attributesDefinedBuilder.build();
//   }
// 
//   private Map<String, MetricDescriptor> extractMetrics(
//       @Nullable List<MetricDescriptor> metrics) {
//     if (null == metrics) {
//       return ImmutableMap.of();
//     }
//     Map<String, MetricDescriptor> ret = Maps.newHashMap();
//     for (MetricDescriptor metric : metrics) {
//       ret.put(metric.getName(), metric);
//     }
//     return ret;
//   }
// }


// Path: cm-schema/src/test/java/com/cloudera/csd/validation/monitoring/constraints/MetricNamePrefixedWithServiceNameValidatorTest.java
import com.cloudera.csd.validation.monitoring.MonitoringValidationContext;
import com.google.common.collect.Iterables;
import java.util.List;
import javax.validation.ConstraintViolation;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

// Licensed to Cloudera, Inc. under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  Cloudera, Inc. licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.cloudera.csd.validation.monitoring.constraints;





public class MetricNamePrefixedWithServiceNameValidatorTest
    extends AbstractMonitoringValidatorBaseTest {

  private MetricNamePrefixedWithServiceNameValidator validator;
  private MonitoringValidationContext context;

  @Before
  public void setupMetricNamePrefixedWithServiceNameValidatorTest() {
    // We should do all comparisons in a case insensitive way.
    validator = new MetricNamePrefixedWithServiceNameValidator();
    context = new MonitoringValidationContext(serviceDescriptor);
  }

  @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);