AliasByTagTransform implements Transform { @Override public List<Metric> transform(QueryContext context, List<Metric> metrics) { SystemAssert.requireArgument(metrics != null, "Metric list cannot be null."); for(Metric metric : metrics) { String displayName = ""; for(Map.Entry<String, String> entry : metric.getTags().entrySet()) { displayName += entry.getValue() + DELIMITER; } if(!displayName.isEmpty()) { displayName = displayName.substring(0, displayName.length() - 1); metric.setDisplayName(displayName); metric.setScope(DEFAULT_SCOPE_NAME); metric.setMetric(displayName); metric.setTags(null); } } return metrics; }  @Override List<Metric> transform(QueryContext context, List<Metric> metrics); @Override List<Metric> transform(QueryContext queryContext, List<Metric> metrics, List<String> constants); @Override List<Metric> transform(QueryContext queryContext, @SuppressWarnings("unchecked") List<Metric>... metrics); @Override String getResultScopeName(); static final String DELIMITER; static final String DEFAULT_SCOPE_NAME; }
@Test public void testAliasByTagTransformWithoutConstants() { Metric metric = new Metric("scope", "metric"); metric.setTag("device", "someDevice"); metric.setTag("target", "someTarget"); Metric actual = aliasByTagTransform.transform(null, Arrays.asList(metric)).get(0); assertEquals("someDevice" + AliasByTagTransform.DELIMITER + "someTarget", actual.getDisplayName()); }
@Test public void testAliasByTagTransformSingleTagKey() { Metric metric = new Metric("scope", "metric"); metric.setTag("device", "someDevice"); metric.setTag("target", "someTarget"); Metric actual = aliasByTagTransform.transform(null, Arrays.asList(metric), Arrays.asList("device")).get(0); assertEquals("someDevice", actual.getDisplayName()); }
@Test public void testAliasByTagTransformMultipleTagKeys() { Metric metric = new Metric("scope", "metric"); metric.setTag("device", "someDevice"); metric.setTag("target", "someTarget"); Metric actual = aliasByTagTransform.transform(null, Arrays.asList(metric), Arrays.asList("device", "target")).get(0); assertEquals("someDevice" + AliasByTagTransform.DELIMITER + "someTarget", actual.getDisplayName()); }
@Test public void testAliasByTagTransformInvalidTagKey() { Metric metric = new Metric("scope", "metric"); metric.setTag("device", "someDevice"); metric.setTag("target", "someTarget"); Metric actual = aliasByTagTransform.transform(null, Arrays.asList(metric), Arrays.asList("devicessss")).get(0); assertEquals(null, actual.getDisplayName()); }
@Test public void testAliasByTagTransformOneValidOneInvalidTagKey() { Metric metric = new Metric("scope", "metric"); metric.setTag("device", "someDevice"); metric.setTag("target", "someTarget"); Metric actual = aliasByTagTransform.transform(null, Arrays.asList(metric), Arrays.asList("devicessss", "target")).get(0); assertEquals("someTarget", actual.getDisplayName()); }
@Test public void testAliasByTagTransformEmptyTagKey() { Metric metric = new Metric("scope", "metric"); metric.setTag("device", "someDevice"); metric.setTag("target", "someTarget"); Metric actual = aliasByTagTransform.transform(null, Arrays.asList(metric), Arrays.asList("")).get(0); assertEquals(null, actual.getDisplayName()); }
@Test public void testAliasByTagScopeNMetricWithOneTag() { Metric metric = new Metric("scope", "metric"); metric.setTag("device", "someDevice"); metric.setTag("target", "someTarget"); Metric actual = aliasByTagTransform.transform(null, Arrays.asList(metric), Arrays.asList("device")).get(0); assertEquals(AliasByTagTransform.DEFAULT_SCOPE_NAME, actual.getScope()); assertEquals("someDevice", actual.getMetric()); }
@Test public void testAliasByTagScopeNMetricWithTwoTags() { Metric metric = new Metric("scope", "metric"); metric.setTag("device", "someDevice"); metric.setTag("target", "someTarget"); Metric actual = aliasByTagTransform.transform(null, Arrays.asList(metric), Arrays.asList("device","target")).get(0); assertEquals(AliasByTagTransform.DEFAULT_SCOPE_NAME, actual.getScope()); assertEquals("someDevice,someTarget", actual.getMetric()); }