@JsonCreator private static @Nonnull CompletionMetadata create( @Nullable @JsonProperty(PROP_ADDRESS_BOOKS) Set<String> addressBooks, @Nullable @JsonProperty(PROP_ADDRESS_GROUPS) Set<String> addressGroups, @Nullable @JsonProperty(PROP_FILTER_NAMES) Set<String> filterNames, @Nullable @JsonProperty(PROP_INTERFACES) Set<NodeInterfacePair> interfaces, @Nullable @JsonProperty(PROP_IPS) Set<String> ips, @Nullable @JsonProperty(PROP_PREFIXES) Set<String> prefixes, @Nullable @JsonProperty(PROP_STRUCTURE_NAMES) Set<String> structureNames, @Nullable @JsonProperty(PROP_VRFS) Set<String> vrfs, @Nullable @JsonProperty(PROP_ZONES) Set<String> zones) { return new CompletionMetadata( firstNonNull(addressBooks, ImmutableSet.of()), firstNonNull(addressGroups, ImmutableSet.of()), firstNonNull(filterNames, ImmutableSet.of()), firstNonNull(interfaces, ImmutableSet.of()), firstNonNull(ips, ImmutableSet.of()), firstNonNull(prefixes, ImmutableSet.of()), firstNonNull(structureNames, ImmutableSet.of()), firstNonNull(vrfs, ImmutableSet.of()), firstNonNull(zones, ImmutableSet.of())); }
@Test public void testPrefixAutocomplete() throws IOException { String network = "network"; String snapshot = "snapshot"; String suggested = "1.2.3.4/24"; String notSuggested = "1.3.2.4/30"; CompletionMetadata completionMetadata = CompletionMetadata.builder().setPrefixes(ImmutableSet.of(suggested, notSuggested)).build(); storeCompletionMetadata(completionMetadata, network, snapshot); assertThat( _manager.autoComplete(network, snapshot, Type.PREFIX, "1.2", 5).stream() .map(AutocompleteSuggestion::getText) .collect(Collectors.toSet()), equalTo(ImmutableSet.of(suggested))); }
PropertySpecifier.baseAutoComplete(query, completionMetadata.getAddressBooks()); PropertySpecifier.baseAutoComplete(query, completionMetadata.getAddressGroups()); break; PropertySpecifier.baseAutoComplete(query, completionMetadata.getFilterNames()); break; PropertySpecifier.baseAutoComplete( query, completionMetadata.getInterfaces().stream() .map(NodeInterfacePair::toString) .collect(Collectors.toSet())); suggestions = PropertySpecifier.baseAutoComplete(query, completionMetadata.getIps()); break; suggestions = PropertySpecifier.baseAutoComplete(query, completionMetadata.getPrefixes()); break; PropertySpecifier.baseAutoComplete(query, completionMetadata.getStructureNames()); break; suggestions = PropertySpecifier.baseAutoComplete(query, completionMetadata.getVrfs()); break; suggestions = PropertySpecifier.baseAutoComplete(query, completionMetadata.getZones()); break;
@Test public void testZoneAutocomplete() throws IOException { String network = "network"; String snapshot = "snapshot"; String suggested = "someZone"; String notSuggested = "blah"; CompletionMetadata completionMetadata = CompletionMetadata.builder().setZones(ImmutableSet.of(suggested, notSuggested)).build(); storeCompletionMetadata(completionMetadata, network, snapshot); assertThat( _manager.autoComplete(network, snapshot, Type.ZONE, "z", 5).stream() .map(AutocompleteSuggestion::getText) .collect(Collectors.toSet()), equalTo(ImmutableSet.of(suggested))); }
@Test public void testCompletionMetadataRoundtrip() throws IOException { NetworkId networkId = new NetworkId("network"); SnapshotId snapshotId = new SnapshotId("snapshot"); CompletionMetadata completionMetadata = new CompletionMetadata( ImmutableSet.of("addressBook1"), ImmutableSet.of("addressGroup1"), ImmutableSet.of("filter1"), ImmutableSet.of(new NodeInterfacePair("node", "iface")), ImmutableSet.of("1.1.1.1"), ImmutableSet.of("1.1.1.1/30"), ImmutableSet.of("structure1"), ImmutableSet.of("vrf1"), ImmutableSet.of("zone1")); _storage.storeCompletionMetadata(completionMetadata, networkId, snapshotId); assertThat(_storage.loadCompletionMetadata(networkId, snapshotId), equalTo(completionMetadata)); }
@Test public void testIpAutocomplete() throws IOException { String network = "network"; String snapshot = "snapshot"; String suggested = "1.2.3.4"; String notSuggested = "1.3.2.4"; CompletionMetadata completionMetadata = CompletionMetadata.builder().setIps(ImmutableSet.of(suggested, notSuggested)).build(); storeCompletionMetadata(completionMetadata, network, snapshot); assertThat( _manager.autoComplete(network, snapshot, Type.IP, "1.2", 5).stream() .map(AutocompleteSuggestion::getText) .collect(Collectors.toSet()), equalTo(ImmutableSet.of(suggested))); }
private CompletionMetadata computeCompletionMetadata(Map<String, Configuration> configurations) { ReferenceLibrary referenceLibrary = getReferenceLibraryData(); return new CompletionMetadata( getAddressBooks(referenceLibrary), getAddressGroups(referenceLibrary), getFilterNames(configurations), getInterfaces(configurations), getIps(configurations), getPrefixes(configurations), getStructureNames(configurations), getVrfs(configurations), getZones(configurations)); }
@Test public void testVrfAutocomplete() throws IOException { String network = "network"; String snapshot = "snapshot"; String suggested = "someVrf"; String notSuggested = "blah"; CompletionMetadata completionMetadata = CompletionMetadata.builder().setVrfs(ImmutableSet.of(suggested, notSuggested)).build(); storeCompletionMetadata(completionMetadata, network, snapshot); assertThat( _manager.autoComplete(network, snapshot, Type.VRF, "v", 5).stream() .map(AutocompleteSuggestion::getText) .collect(Collectors.toSet()), equalTo(ImmutableSet.of(suggested))); }
@Test public void testAddressGroupAutocomplete() throws IOException { String network = "network"; String snapshot = "snapshot"; String suggestion = "group"; String notSuggestion = "addresses"; CompletionMetadata completionMetadata = CompletionMetadata.builder() .setAddressGroups(ImmutableSet.of(suggestion, notSuggestion)) .build(); storeCompletionMetadata(completionMetadata, network, snapshot); assertThat( _manager.autoComplete(network, snapshot, Type.ADDRESS_GROUP, "gr", 5).stream() .map(AutocompleteSuggestion::getText) .collect(Collectors.toSet()), equalTo(ImmutableSet.of(suggestion))); }
@Test public void testAddressBookAutocomplete() throws IOException { String network = "network"; String snapshot = "snapshot"; String suggestion = "book"; String notSuggestion = "addresses"; CompletionMetadata completionMetadata = CompletionMetadata.builder() .setAddressBooks(ImmutableSet.of(notSuggestion, suggestion)) .build(); storeCompletionMetadata(completionMetadata, network, snapshot); assertThat( _manager.autoComplete(network, snapshot, Type.ADDRESS_BOOK, "bo", 5).stream() .map(AutocompleteSuggestion::getText) .collect(Collectors.toSet()), equalTo(ImmutableSet.of(suggestion))); }
@Test public void testFilterAutocomplete() throws IOException { String network = "network"; String snapshot = "snapshot"; String suggestion = "someFilter"; String notSuggestion = "blah"; CompletionMetadata completionMetadata = CompletionMetadata.builder() .setFilterNames(ImmutableSet.of(suggestion, notSuggestion)) .build(); storeCompletionMetadata(completionMetadata, network, snapshot); assertThat( _manager.autoComplete(network, snapshot, Type.FILTER, "fil", 5).stream() .map(AutocompleteSuggestion::getText) .collect(Collectors.toSet()), equalTo(ImmutableSet.of(suggestion))); }
@Test public void testStructureNameAutocomplete() throws IOException { String network = "network"; String snapshot = "snapshot"; String suggested = "someStructure"; String notSuggested = "blah"; CompletionMetadata completionMetadata = CompletionMetadata.builder() .setStructureNames(ImmutableSet.of(suggested, notSuggested)) .build(); storeCompletionMetadata(completionMetadata, network, snapshot); assertThat( _manager.autoComplete(network, snapshot, Type.STRUCTURE_NAME, "str", 5).stream() .map(AutocompleteSuggestion::getText) .collect(Collectors.toSet()), equalTo(ImmutableSet.of(suggested))); }
@Test public void testInterfaceAutocomplete() throws IOException { String network = "network"; String snapshot = "snapshot"; NodeInterfacePair suggested = new NodeInterfacePair("hostname", "interface"); NodeInterfacePair notSuggested = new NodeInterfacePair("blah", "blahhh"); CompletionMetadata completionMetadata = CompletionMetadata.builder() .setInterfaces(ImmutableSet.of(suggested, notSuggested)) .build(); storeCompletionMetadata(completionMetadata, network, snapshot); assertThat( _manager.autoComplete(network, snapshot, Type.INTERFACE, "int", 5).stream() .map(AutocompleteSuggestion::getText) .collect(Collectors.toSet()), equalTo(ImmutableSet.of(suggested.toString()))); }
@Test public void testJsonSerialization() throws IOException { CompletionMetadata completionMetadata = CompletionMetadata.builder() .setAddressBooks(ImmutableSet.of("addressBook")) .setAddressGroups(ImmutableSet.of("addressGroup")) .setFilterNames(ImmutableSet.of("filter")) .setInterfaces(ImmutableSet.of(new NodeInterfacePair("interface", "node"))) .setIps(ImmutableSet.of("ip")) .setPrefixes(ImmutableSet.of("prefix")) .setStructureNames(ImmutableSet.of("structure")) .setVrfs(ImmutableSet.of("vrf")) .setZones(ImmutableSet.of("zone")) .build(); assertThat( BatfishObjectMapper.clone(completionMetadata, CompletionMetadata.class), equalTo(completionMetadata)); }
@Test public void testJavaSerialization() throws IOException { CompletionMetadata completionMetadata = CompletionMetadata.builder() .setAddressBooks(ImmutableSet.of("addressBook")) .setAddressGroups(ImmutableSet.of("addressGroup")) .setFilterNames(ImmutableSet.of("filter")) .setInterfaces(ImmutableSet.of(new NodeInterfacePair("interface", "node"))) .setIps(ImmutableSet.of("ip")) .setPrefixes(ImmutableSet.of("prefix")) .setStructureNames(ImmutableSet.of("structure")) .setVrfs(ImmutableSet.of("vrf")) .setZones(ImmutableSet.of("zone")) .build(); assertThat(SerializationUtils.clone(completionMetadata), equalTo(completionMetadata)); }
@Test public void testEquals() { CompletionMetadata.Builder builder = CompletionMetadata.builder(); CompletionMetadata initial = builder.build(); new EqualsTester() .addEqualityGroup(new Object()) .addEqualityGroup(initial, initial, builder.build(), CompletionMetadata.EMPTY) .addEqualityGroup(builder.setAddressBooks(ImmutableSet.of("addressBook")).build()) .addEqualityGroup(builder.setAddressGroups(ImmutableSet.of("addressGroup")).build()) .addEqualityGroup(builder.setFilterNames(ImmutableSet.of("filter")).build()) .addEqualityGroup( builder .setInterfaces(ImmutableSet.of(new NodeInterfacePair("interface", "node"))) .build()) .addEqualityGroup(builder.setIps(ImmutableSet.of("ip")).build()) .addEqualityGroup(builder.setPrefixes(ImmutableSet.of("prefix")).build()) .addEqualityGroup(builder.setStructureNames(ImmutableSet.of("structure")).build()) .addEqualityGroup(builder.setVrfs(ImmutableSet.of("vrf")).build()) .addEqualityGroup(builder.setZones(ImmutableSet.of("zone")).build()) .testEquals(); } }