@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 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 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 testSerialization() throws JsonParseException, JsonMappingException, IOException { Row row1 = Row.builder().put("key1", "v1").build(); Row row2 = Row.builder().put("key1", "v2").build(); TableView tableView = new TableView( OPTIONS, ImmutableList.of(new TableViewRow(0, row1), new TableViewRow(1, row2)), METADATA); TableView cycledTableView = BatfishObjectMapper.clone(tableView, TableView.class); assertThat(tableView.getOptions(), equalTo(cycledTableView.getOptions())); assertThat(tableView.getRows(), equalTo(cycledTableView.getRows())); assertThat(tableView.getTableMetadata(), equalTo(cycledTableView.getTableMetadata())); } }
private Row initRowThree() { return Row.builder().put("col1", "value1").put("col2", "value2").put("col3", "value3").build(); }
@Test public void ofUntyped() { assertThat(Row.of("a", 5), equalTo(Row.builder().put("a", 5).build())); }
@Test public void fillPropertyForcedString() { Configuration configuration = new Configuration("hostname", ConfigurationFormat.CISCO_IOS); configuration.setDefaultInboundAction(LineAction.PERMIT); String property = NodePropertySpecifier.DEFAULT_INBOUND_ACTION; 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 String value assertThat( row.build(), equalTo(Row.builder().put(property, LineAction.PERMIT.toString()).build())); }
@Test public void fillPropertyListNonEmpty() { Configuration configuration = new Configuration("hostname", ConfigurationFormat.CISCO_IOS); configuration.setNtpServers(ImmutableSortedSet.of("sa", "sb")); 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 the right list and the schemas map should be List<String> assertThat( row.build(), equalTo(Row.builder().put(property, ImmutableList.of("sa", "sb")).build())); }
@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())); }
@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 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 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 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()); }
@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)); }