/** * From the {@link ReferenceLibrary} at {@code dataPath}, get the {@link ReferenceBook} with name * {@code bookName}. * * @throws IOException If the contents of the file could not be cast to {@link ReferenceLibrary} */ public static Optional<ReferenceBook> getReferenceBook( @Nonnull Path dataPath, @Nonnull String bookName) throws IOException { ReferenceLibrary data = read(dataPath); return data.getReferenceBook(bookName); }
@Override public Optional<ReferenceBook> getReferenceBook(String bookName) { return _batfish.getReferenceLibraryData().getReferenceBook(bookName); }
/** Deletes the book */ public void delAddressBook(String bookName) { ReferenceBook book = getReferenceBook(bookName) .orElseThrow(() -> new NoSuchElementException("Book not found: " + bookName)); _books = _books.stream() .filter(b -> !b.equals(book)) .collect(ImmutableSortedSet.toImmutableSortedSet(Comparator.naturalOrder())); }
@GET public ReferenceBookBean getReferenceBook() { _logger.infof("WMS2: getReferenceBook: '%s' '%s'\n", _container, _bookName); try { return new ReferenceBookBean( Main.getWorkMgr() .getReferenceLibrary(_container) .getReferenceBook(_bookName) .orElseThrow( () -> new NotFoundException( String.format( "Reference book '%s' not found in container '%s'", _bookName, _container)))); } catch (IOException e) { throw new InternalServerErrorException("Reference library data is corrupted"); } } }
/** Adds a new {@link ReferenceBook} to the network's {@link ReferenceLibrary} */ @POST public Response addReferenceBook(ReferenceBookBean referenceBookBean) { _logger.infof("WMS2: addReferenceBook '%s'\n", _network); if (referenceBookBean.name == null) { throw new BadRequestException("ReferenceBook must have a name"); } try { ReferenceLibrary library = Main.getWorkMgr().getReferenceLibrary(_network); if (library.getReferenceBook(referenceBookBean.name).isPresent()) { throw new BadRequestException("Duplicate bookname: " + referenceBookBean.name); } ReferenceLibrary.mergeReferenceBooks( Main.getWorkMgr().getReferenceLibraryPath(_network), ImmutableSortedSet.of(referenceBookBean.toAddressBook())); return Response.ok().build(); } catch (IOException e) { throw new InternalServerErrorException("ReferenceLibrary resource is corrupted"); } }
@Test public void addReferenceBook() throws IOException { String network = "someNetwork"; Main.getWorkMgr().initNetwork(network, null); // add book1 ReferenceBookBean book = new ReferenceBookBean(ReferenceBook.builder("book1").build()); Response response = getReferenceLibraryTarget(network).post(Entity.entity(book, MediaType.APPLICATION_JSON)); // test: book1 should have been added assertThat(response.getStatus(), equalTo(OK.getStatusCode())); ReferenceLibrary library = Main.getWorkMgr().getReferenceLibrary(network); assertThat(library.getReferenceBook("book1").isPresent(), equalTo(true)); // test: another addition of book1 should fail Response response2 = getReferenceLibraryTarget(network).post(Entity.entity(book, MediaType.APPLICATION_JSON)); assertThat(response2.getStatus(), equalTo(BAD_REQUEST.getStatusCode())); }
@Test public void delAddressBook() throws IOException { String container = "someContainer"; Main.getWorkMgr().initNetwork(container, null); // write a library to the right place ReferenceLibrary.write( new ReferenceLibrary(ImmutableList.of(ReferenceBook.builder("book1").build())), Main.getWorkMgr().getReferenceLibraryPath(container)); Response response = getAddressBookTarget(container, "book1").delete(); // response should be OK and book1 should have disappeared assertThat(response.getStatus(), equalTo(OK.getStatusCode())); assertThat( Main.getWorkMgr().getReferenceLibrary(container).getReferenceBook("book1").isPresent(), equalTo(false)); // deleting again should fail Response response2 = getAddressBookTarget(container, "book1").delete(); assertThat(response2.getStatus(), equalTo(NOT_FOUND.getStatusCode())); }