/** * Adds the reference books in {@code newBooks} to the ReferenceLibrary at {@code path}. Books * with the same name are overwritten */ public static void mergeReferenceBooks( @Nonnull Path path, @Nonnull SortedSet<ReferenceBook> newBooks) throws IOException { ReferenceLibrary originalLibrary = read(path); List<ReferenceBook> booksToKeep = originalLibrary._books.stream() .filter( originalBook -> !newBooks.stream() .anyMatch(newBook -> newBook.getName().equals(originalBook.getName()))) .collect(Collectors.toList()); booksToKeep.addAll(newBooks); ReferenceLibrary newLibrary = new ReferenceLibrary(booksToKeep); write(newLibrary, path); }
@DELETE public Response delReferenceBook() { _logger.infof("WMS2: delReferenceBook '%s' '%s'\n", _container, _bookName); try { ReferenceLibrary library = Main.getWorkMgr().getReferenceLibrary(_container); library.delAddressBook(_bookName); ReferenceLibrary.write(library, Main.getWorkMgr().getReferenceLibraryPath(_container)); return Response.ok().build(); } catch (IOException e) { throw new InternalServerErrorException("Reference library is corrupted"); } catch (NoSuchElementException e) { throw new NotFoundException("Book not found: " + _bookName); } }
@Test public void getAddressBook() throws JsonProcessingException { 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)); // we only check that the right type of object is returned at the expected URL target // we rely on ReferenceBookBean to have created the object with the right content Response response = getAddressBookTarget(container, "book1").get(); assertThat(response.getStatus(), equalTo(OK.getStatusCode())); assertThat( response.readEntity(ReferenceBook.class), equalTo(ReferenceBook.builder("book1").build())); // should get 404 for non-existent dimension Response response2 = getAddressBookTarget(container, "book2").get(); assertThat(response2.getStatus(), equalTo(NOT_FOUND.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())); }
ImmutableList.of( ReferenceBook.builder("book1").build(), ReferenceBook.builder("book2").build())); ReferenceLibrary.write(library, tempPath);