@Override public Row build() { // Fill in missing columns with null entries _columns.keySet().stream().filter(c -> !_data.has(c)).forEach(c -> super.put(c, null)); return super.build(); } }
@VisibleForTesting static void fillProperty( String columnName, Object propertyValue, RowBuilder row, PropertyDescriptor<?> propertyDescriptor) { row.put(columnName, propertyValue); // if this barfs, the value cannot be converted to expected Schema row.build().get(columnName, propertyDescriptor.getSchema()); }
@Test public void testGetTypeReference() { String column = "col"; List<String> stringList = ImmutableList.of("a", "b"); Row row = Row.builder().put(column, stringList).build(); List<String> extracted = row.get(column, new TypeReference<List<String>>() {}); assertThat(stringList, equalTo(extracted)); }
@Test public void putAllFail() { Row row = Row.builder().put("col1", 20).build(); // Specifying a non-existent column throws an exception _thrown.expect(NoSuchElementException.class); _thrown.expectMessage("is not present"); Row.builder().putAll(row, ImmutableSet.of("col2")); }
@Test public void putAllCorrect() { Row row = Row.builder().put("col1", 20).put("col2", 21).put("col3", 24).build(); // all columns should be copied over when we don't limit assertThat( Row.builder().putAll(row).build(), equalTo(Row.builder().put("col1", 20).put("col2", 21).put("col3", 24).build())); // only specified columns should be copied assertThat( Row.builder().putAll(row, ImmutableSet.of("col1", "col3")).build(), equalTo(Row.builder().put("col1", 20).put("col3", 24).build())); }
static Row getRipEdgeRow(String node, String iface, String remoteNode, String remoteIface) { RowBuilder row = Row.builder(); row.put(COL_INTERFACE, new NodeInterfacePair(node, iface)) .put(COL_REMOTE_INTERFACE, new NodeInterfacePair(remoteNode, remoteIface)); return row.build(); }
@Test public void fillPropertyMap() { Configuration configuration = new Configuration("hostname", ConfigurationFormat.CISCO_IOS); configuration.setInterfaces(ImmutableSortedMap.of("i1", new Interface("i1"))); String property = NodePropertySpecifier.INTERFACES; PropertyDescriptor<Configuration> propertyDescriptor = NodePropertySpecifier.JAVA_MAP.get(property); RowBuilder row = Row.builder(); PropertySpecifier.fillProperty(propertyDescriptor, configuration, property, row); // the row should be filled out with the right list and the schemas map should be List<String> assertThat(row.build(), equalTo(Row.builder().put(property, ImmutableList.of("i1")).build())); }
@Test public void testAddRowMissingColumn() { TableAnswerElement table = new TableAnswerElement(twoKeyTableMetadata); Row row = Row.builder().put("key1", null).build(); _thrown.expect(IllegalArgumentException.class); _thrown.expectMessage( String.format( "Row columns %s do not match metadata columns metadata %s", row.getColumnNames(), twoKeyTableMetadata.toColumnMap().keySet())); table.addRow(row); }
@Test public void testMatches() { String columnName = "column"; String filterText = "bLah"; ColumnFilter filter = new ColumnFilter(columnName, filterText); Row row = Row.builder().put(columnName, "BlaHah").build(); assertTrue(filter.matches(row)); } }
@Test public void ofTyped() { assertThat( Row.of(ImmutableMap.of("a", new ColumnMetadata("a", Schema.INTEGER, "desc")), "a", 5), equalTo(Row.builder().put("a", 5).build())); _thrown.expect(IllegalArgumentException.class); _thrown.expectMessage("Column 'b' is not present"); Row.of(ImmutableMap.of("a", new ColumnMetadata("a", Schema.INTEGER, "desc")), "b", 5); } }
@Test public void testGetTypeReferenceInvalid() { String column = "col"; Row row = Row.builder().put(column, "text").build(); _thrown.expect(ClassCastException.class); row.get(column, new TypeReference<Integer>() {}); }
@Test public void matchesOperatorArithmeticTest() { Filter filter = new Filter("col >= 42"); // integer compare match should be good and shouldn't match null assertThat(filter.matches(Row.builder().put("col", 42).build()), equalTo(true)); assertThat(filter.matches(Row.builder().put("col", 41).build()), equalTo(false)); assertThat(filter.matches(Row.builder().put("col", null).build()), equalTo(false)); } }
@Test public void fillPropertyListEmpty() { Configuration configuration = new Configuration("hostname", ConfigurationFormat.CISCO_IOS); String property = NodePropertySpecifier.NTP_SERVERS; PropertyDescriptor<Configuration> propertyDescriptor = NodePropertySpecifier.JAVA_MAP.get(property); RowBuilder row = Row.builder(); PropertySpecifier.fillProperty(propertyDescriptor, configuration, property, row); // the row should be filled out with an empty list assertThat(row.build(), equalTo(Row.builder().put(property, new LinkedList<String>()).build())); }
@Test public void matchesDifferentTypes() { // throw an exception when objects are different types _thrown.expect(ClassCastException.class); _thrown.expectMessage("cannot be cast"); new Filter("col != true").matches(Row.builder().put("col", 42).build()); }
@VisibleForTesting static Row getOspfEdgeRow(String node, String iface, String remoteNode, String remoteIface) { RowBuilder row = Row.builder(); row.put(COL_INTERFACE, new NodeInterfacePair(node, iface)) .put(COL_REMOTE_INTERFACE, new NodeInterfacePair(remoteNode, remoteIface)); return row.build(); }
@Test public void fillPropertyNull() { Configuration configuration = new Configuration("hostname", ConfigurationFormat.CISCO_IOS); String property = NodePropertySpecifier.NTP_SOURCE_INTERFACE; PropertyDescriptor<Configuration> propertyDescriptor = NodePropertySpecifier.JAVA_MAP.get(property); RowBuilder row = Row.builder(); PropertySpecifier.fillProperty(propertyDescriptor, configuration, property, row); // the row should be filled out with null and the schemas shouldn't be assertThat(row.build(), equalTo(Row.builder().put(property, null).build())); } }
@Test public void testAddRowExtraColumn() { TableAnswerElement table = new TableAnswerElement(twoKeyTableMetadata); Row row = Row.builder().put("key1", null).put("key2", null).put("key3", null).build(); _thrown.expect(IllegalArgumentException.class); _thrown.expectMessage( String.format( "Row columns %s do not match metadata columns metadata %s", row.getColumnNames(), twoKeyTableMetadata.toColumnMap().keySet())); table.addRow(row); } }
@Test public void testAddValidRow() { TableAnswerElement table = new TableAnswerElement(twoKeyTableMetadata); Row row = Row.builder().put("key1", null).put("key2", null).build(); table.addRow(row); assertThat(table.getRows().getData(), contains(row)); }
static Row isisEdgeToRow(IsisEdge isisEdge) { RowBuilder row = Row.builder(); row.put( COL_INTERFACE, new NodeInterfacePair( isisEdge.getNode1().getNode(), isisEdge.getNode1().getInterfaceName())) .put( COL_REMOTE_INTERFACE, new NodeInterfacePair( isisEdge.getNode2().getNode(), isisEdge.getNode2().getInterfaceName())); return row.build(); }