/** * Creates {@link ColumnMetadata}s that the answer should have based on the {@code * propertySpecifier}. * * @param propertySpecifier The {@link NodePropertySpecifier} that describes the set of properties * @return The {@link List} of {@link ColumnMetadata}s */ public static List<ColumnMetadata> createColumnMetadata(NodePropertySpecifier propertySpecifier) { return new ImmutableList.Builder<ColumnMetadata>() .add(new ColumnMetadata(COL_NODE, Schema.NODE, "Node", true, false)) .addAll( propertySpecifier.getMatchingProperties().stream() .map( prop -> new ColumnMetadata( getColumnName(prop), NodePropertySpecifier.JAVA_MAP.get(prop).getSchema(), "Property " + prop, false, true)) .collect(Collectors.toList())) .build(); }
@Test public void getMatchingProperties() { // match everything assertThat( new NodePropertySpecifier(".*").getMatchingProperties().size(), equalTo(NodePropertySpecifier.JAVA_MAP.size())); // match the ntp properties assertThat(new NodePropertySpecifier("ntp.*").getMatchingProperties().size(), equalTo(2)); // match nothing: ntp assertTrue(new NodePropertySpecifier("ntp").getMatchingProperties().isEmpty()); }
/** * Gets properties of nodes. * * @param propertySpecifier Specifies which properties to get * @param configurations configuration to use in extractions * @param nodes the set of nodes to focus on * @param columns a map from column name to {@link ColumnMetadata} * @return A multiset of {@link Row}s where each row corresponds to a node and columns correspond * to property values. */ public static Multiset<Row> getProperties( NodePropertySpecifier propertySpecifier, Map<String, Configuration> configurations, Set<String> nodes, Map<String, ColumnMetadata> columns) { Multiset<Row> rows = HashMultiset.create(); for (String nodeName : nodes) { RowBuilder row = Row.builder(columns).put(COL_NODE, new Node(nodeName)); for (String property : propertySpecifier.getMatchingProperties()) { PropertySpecifier.fillProperty( NodePropertySpecifier.JAVA_MAP.get(property), configurations.get(nodeName), property, row); } rows.add(row.build()); } return rows; }
@Test public void testMatchingPropertiesSet() { Iterator<String> i = NodePropertySpecifier.JAVA_MAP.keySet().iterator(); String prop1 = i.next(); String prop2 = i.next(); Collection<String> firstTwoProperties = ImmutableList.of(prop1, prop2); // should match the two properties passed to constructor assertThat(new NodePropertySpecifier(firstTwoProperties).getMatchingProperties(), hasSize(2)); Collection<String> longer = ImmutableList.of(prop1 + prop1); // should not match longer assertThat(new NodePropertySpecifier(longer).getMatchingProperties(), emptyIterable()); Collection<String> shorter = ImmutableList.of(prop1.substring(0, 1)); // should not match shorter assertThat(new NodePropertySpecifier(shorter).getMatchingProperties(), emptyIterable()); } }