@Override @Nonnull public Path getPath() { return parent.getPath().resolve(id); }
/******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull public PathAwareFinder findPhotos (final @Nonnull MediaFolder parent) { return parent.finderOf(p -> Arrays.asList( new VirtualMediaFolder(p, PATH_PLACES, "Places", this::placesFactory), new VirtualMediaFolder(p, PATH_SUBJECTS, "Subjects", this::subjectsFactory))); }
/******************************************************************************************************************* * * Relativizes a path against the finder path, that is it removes the parent path. If the path can't be * relativized, that is it doesn't start with the finder path, returns null. * ******************************************************************************************************************/ @CheckForNull private Path relative (final @Nonnull Path path) { return !mediaFolder.getParent().isPresent() ? path : path.startsWith(mediaFolder.getPath()) ? path.subpath(mediaFolder.getPath().getNameCount(), path.getNameCount()) : null; }
/******************************************************************************************************************* * ******************************************************************************************************************/ @BeforeMethod public void setup() { // required for DCI stuff mediaFolder = mock(MediaFolder.class); when(mediaFolder.getPath()).thenReturn(Paths.get("/folder")); when(mediaFolder.toString()).thenReturn("Folder(\"/folder\"))"); // when(mediaFolder.toDumpString()).thenReturn("Folder(\"/folder\"))"); when(mediaFolder.finderOf(any(Finder8.class))).thenCallRealMethod(); when(mediaFolder.finderOf(any(Function.class))).thenCallRealMethod(); }
@Test public void must_return_correct_root_children() throws Exception { // when final List<? extends Entity> children = underTest.findRoot().findChildren().results(); // then assertThat(children.size(), is(4)); assertThat(children.get(0).as(Displayable).getDisplayName(), is("Music")); assertThat(children.get(1).as(Displayable).getDisplayName(), is("Photos")); assertThat(children.get(2).as(Displayable).getDisplayName(), is("Videos")); assertThat(children.get(3).as(Displayable).getDisplayName(), is("Services")); // TODO: mock music and services, assert contents } }
.findChildren() .withPath(path) .optionalResult()
@Override @Nonnull public String getDisplayName() { return folder.getPath().toFile().getName(); } }
@Override @Nonnull public DIDLObject toObject() { log.debug("toObject() - {}", datum); final Container container = setCommonFields(new Container()); container.setClazz(StorageFolder.CLASS); container.setId(pathToDidlId(datum.getPath())); container.setParentID(datum.getParent().map(parent -> pathToDidlId(parent.getPath())).orElse(ID_NONE)); if (!container.getId().equals("/music")) // FIXME workaround for DefaultMediaFileSystem { container.setTitle(container.getTitle().replaceAll("^Music$", "By file")); // FIXME: this corrupts some title } return container; } }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Override @Nonnull public PathAwareFinder findPhotos(final @Nonnull MediaFolder parent) { return parent.finderOf( p1 -> IntStream.range(1999, 2016 + 1) // FIXME: use current year .mapToObj(x -> x) .map(year -> new VirtualMediaFolder(p1, Paths.get("" + year), "" + year, (EntityCollectionFactory)(p2 -> entriesFactory(p2, year)))) .collect(toList())); }
@Override @Nonnull public String toDumpString() { return String.format("PhotoItem(parent=%s, id=%s, title=%s)", parent.getPath().toString(), id, title); } }
private VirtualMediaFolder (final @Nonnull Optional<? extends MediaFolder> optionalParent, final @Nonnull Path pathSegment, final @Nonnull String displayName, final @Nonnull Optional<EntityCollectionFactory> childrenSupplier, final @Nonnull Optional<EntityFinderFactory> finderFactory) { super((Identifiable)() -> new Id(absolutePath(optionalParent, pathSegment).toString()), new DefaultDisplayable(displayName)); this.path = absolutePath(optionalParent, pathSegment); this.optionalParent = optionalParent; this.finderFactory = finderFactory.orElse(mediaFolder -> mediaFolder.finderOf(childrenSupplier.get())); }
@Nonnull private static Path absolutePath (final @Nonnull Optional<? extends MediaFolder> optionalParent, final @Nonnull Path pathSegment) { return optionalParent.map(parent -> parent.getPath().resolve(pathSegment)).orElse(pathSegment); } }
/******************************************************************************************************************* * * * ******************************************************************************************************************/ @Nonnull private Optional<? extends PathAwareEntity> filteredByPath (final @Nonnull Path path) { log.debug("filteredByPath({})", path); return mediaFolder.getPath().equals(path) ? Optional.of(mediaFolder) : childMatchingPathHead(path).flatMap(entity -> path.equals(entity.getPath()) ? Optional.of(entity) : childMatchingPath(entity, path)); }
@Test public void must_properly_generate_DIDL_content() throws Exception { // given final MediaFolder mediaFolder = mock(MediaFolder.class); when(mediaFolder.getPath()).thenReturn(Paths.get("/folder")); final PhotoItem photoItem = new PhotoItem(mediaFolder, "20150524-0034", "description"); final PhotoItemDIDLAdapter underTest = new PhotoItemDIDLAdapter(photoItem); // when final DIDLContent content = new DIDLContent(); content.addObject(underTest.toObject()); // then final DIDLParser parser = new DIDLParser(); final String xml = xmlPrettyPrinted(parser.generate(content)); final Path actualResult = Paths.get("target", "test-results", "didl.xml"); final Path expectedResult = Paths.get("target", "test-classes", "expected-results", "didl.xml"); Files.createDirectories(actualResult.getParent()); Files.write(actualResult, xml.getBytes(StandardCharsets.UTF_8)); assertSameContents(expectedResult.toFile(), actualResult.toFile()); } }