/** Computes the summary of this table, given the assertion */ public AnswerSummary computeSummary(Assertion assertion) { String notes = "Found " + _rows.size() + " results"; return computeSummary(assertion, notes); }
@Override protected Integer featureValueOf(Rows actual) { return actual.size(); } }
/** Computes the summary of this table, given the assertion and notes */ public AnswerSummary computeSummary(Assertion assertion, String notes) { int numPassed = 0; int numFailed = 0; if (assertion != null) { if (evaluateAssertion(assertion)) { numPassed = 1; } else { numFailed = 1; } } return new AnswerSummary(notes, numFailed, numPassed, _rows.size()); }
/** * Evaluates the assertion over the rows * * @return The results of the evaluation */ public boolean evaluateAssertion(Assertion assertion) { if (assertion == null) { throw new IllegalArgumentException("Provided assertion object cannot be null"); } switch (assertion.getType()) { case countequals: return _rows.size() == assertion.getExpect().asInt(); case countlessthan: return _rows.size() < assertion.getExpect().asInt(); case countmorethan: return _rows.size() > assertion.getExpect().asInt(); case equals: Rows expectedEntries; try { expectedEntries = BatfishObjectMapper.mapper().readValue(assertion.getExpect().toString(), Rows.class); } catch (IOException e) { throw new BatfishException("Could not recover Rows object from expect", e); } return _rows.equals(expectedEntries); default: throw new BatfishException("Unhandled assertion type: " + assertion.getType()); } }
/** Checks if we get back the expected number of rows based on ignoring missing keys. */ @Test public void diffTablesTestIncludeOneTableKeys() { List<ColumnMetadata> columns = ImmutableList.of( new ColumnMetadata("key", Schema.STRING, "key", true, false), new ColumnMetadata("value", Schema.STRING, "value", false, true)); TableMetadata metadata = new TableMetadata(columns, "desc"); Row row1 = Row.of("key", "sameKey", "value", "value1"); Row row2 = Row.of("key", "sameKey", "value", "value2"); Row row3 = Row.of("key", "diffKey", "value", "value3"); TableAnswerElement table12 = new TableAnswerElement(metadata).addRow(row1).addRow(row2); TableAnswerElement table3 = new TableAnswerElement(metadata).addRow(row3); TableAnswerElement table1 = new TableAnswerElement(metadata).addRow(row1); TableAnswerElement table23 = new TableAnswerElement(metadata).addRow(row2).addRow(row3); // includeOneTableKeys = false ==> 0 rows since the tables have no key in common assertThat(TableDiff.diffTables(table12, table3, false).getRows().size(), equalTo(0)); assertThat(TableDiff.diffTables(table3, table12, false).getRows().size(), equalTo(0)); // includeOneTableKeys = false ==> 1 row since the tables have a common key w/ different values assertThat(TableDiff.diffTables(table23, table1, false).getRows().size(), equalTo(1)); assertThat(TableDiff.diffTables(table1, table23, false).getRows().size(), equalTo(1)); // includeOneTableKsy = true ==> should get 3 rows assertThat(TableDiff.diffTables(table12, table3, true).getRows().size(), equalTo(3)); assertThat(TableDiff.diffTables(table3, table12, true).getRows().size(), equalTo(3)); }
/** * Checks if we get back the expected number of rows. Multiple rows with same key within a table * should be thrown out, and the same value across two tables should not be reported. */ @Test public void diffTablesTestRowCount() { List<ColumnMetadata> columns = ImmutableList.of( new ColumnMetadata("key", Schema.STRING, "key", true, false), new ColumnMetadata("value", Schema.STRING, "value", false, true)); TableMetadata metadata = new TableMetadata(columns, "desc"); Row row1 = Row.of("key", "sameKey", "value", "value1"); Row row2 = Row.of("key", "sameKey", "value", "value2"); Row row3 = Row.of("key", "diffKey", "value", "value3"); TableAnswerElement table0 = new TableAnswerElement(metadata); TableAnswerElement table1 = new TableAnswerElement(metadata).addRow(row1); TableAnswerElement table2 = new TableAnswerElement(metadata).addRow(row2); TableAnswerElement table13 = new TableAnswerElement(metadata).addRow(row1).addRow(row3); TableAnswerElement table123 = new TableAnswerElement(metadata).addRow(row1).addRow(row2).addRow(row3); // expected count is 3 -- all rows from table123 assertThat(TableDiff.diffTables(table123, table0, true).getRows().size(), equalTo(3)); assertThat(TableDiff.diffTables(table0, table123, true).getRows().size(), equalTo(3)); // expected count is 2: one for diffKey, one for sameKey assertThat(TableDiff.diffTables(table13, table2, true).getRows().size(), equalTo(2)); assertThat(TableDiff.diffTables(table2, table13, true).getRows().size(), equalTo(2)); // expected count is 1: one for diffKey; sameKey should be removed since values are same assertThat(TableDiff.diffTables(table1, table13, true).getRows().size(), equalTo(1)); assertThat(TableDiff.diffTables(table13, table1, true).getRows().size(), equalTo(1)); }
@Test public void testHeaderSpaceConstraint2() throws IOException { Question question = new DifferentialReachabilityQuestion( new DispositionSpecifier(ImmutableSet.of(FlowDisposition.ACCEPTED)), PacketHeaderConstraints.builder().setDstIp("5.5.5.5").build(), false, false, TracePruner.DEFAULT_MAX_TRACES, PathConstraintsInput.unconstrained()); Batfish batfish = initBatfish(); TableAnswerElement answer = new DifferentialReachabilityAnswerer(question, batfish).answer(); assertThat(answer.getRows().size(), equalTo(0)); } }
assertThat(answer.getRows().size(), equalTo(1)); assertThat(answer.getExcludedRows().size(), equalTo(1));