MediaManagerImpl implements MediaManager { @Override public Collection<MediaNode> searchChildNodes(final MediaSearchRequest request) { List<MediaNode> childNodes; RootNode rootNode = getById(request.getParentNode().getId()); if (rootNode == ROOT) { RootNode[] rootNodes = RootNode.values(); childNodes = new ArrayList<>(rootNodes.length); for (RootNode subRootNode : rootNodes) { if (subRootNode.getParentId().equals(ROOT.getId()) && !mediaDao.getRootNodeChildren(subRootNode).isEmpty()) { childNodes.add(new FolderNode(subRootNode.getId(), ROOT.getId(), resourceBundle.getString(subRootNode.getBundleKey()))); } } } else if (rootNode.getParentId().equals(ROOT.getId())) { childNodes = mediaDao.getRootNodeChildren(rootNode); } else { childNodes = mediaDao.getChildNodes(request.getParentNode().getId()); } Predicate<MediaNode> p = node -> !(node instanceof MimeTypeNode) || mimeTypeManager.isMimeTypeCompliant(((MimeTypeNode) node).getMimeType(), request.getAvailableMimeTypes()); return childNodes.stream().filter(p).collect(toList()); } @Inject  MediaManagerImpl(final ConfigurationManager configurationManager, final ResourceBundle resourceBundle, final MediaDao mediaDao,
                            final MimeTypeManager mimeTypeManager, @Named("localAddress") final InetAddress localAddress); @Override Optional<MediaNode> getNode(final String nodeId); @Override String getNodeUrl(final MediaNode node); @Override Collection<MediaNode> searchChildNodes(final MediaSearchRequest request); @Override void cleanUpCache(); @Subscribe void handleMediaEvent(final MediaEvent mediaEvent);  }
@Test public void testSearchChildNodesOfRoot() { ConfigurationDao configurationDao = new TestConfigurationDao(); ConfigurationManager configurationManager = new ConfigurationManagerImpl(configurationDao); ResourceBundle resourceBundle = ResourceBundle.getBundle("message"); MediaDao mediaDao = createMock(MediaDao.class); MimeTypeManager mimeTypeManager = createMock(MimeTypeManager.class); InetAddress localAddress = createMock(InetAddress.class); expect(mediaDao.getRootNodeChildren(eq(VIDEO))).andReturn(getRootChildNodes(VIDEO, configurationDao)); expect(mediaDao.getRootNodeChildren(eq(PICTURE))).andReturn(getRootChildNodes(PICTURE, configurationDao)); expect(mediaDao.getRootNodeChildren(eq(AUDIO))).andReturn(getRootChildNodes(AUDIO, configurationDao)); replay(mediaDao, mimeTypeManager, localAddress); MediaManagerImpl mediaManager = new MediaManagerImpl(configurationManager, resourceBundle, mediaDao, mimeTypeManager, localAddress); MediaSearchRequest request = new MediaSearchRequest(new FolderNode(ROOT.getId(), ROOT.getParentId(), ROOT.getBundleKey()), null); Collection<MediaNode> result = mediaManager.searchChildNodes(request); assertNotNull(result); assertFalse(result.isEmpty()); verify(mediaDao, mimeTypeManager, localAddress); }
@Test public void testSearchChildNodesOfVideoRoot() { ConfigurationDao configurationDao = new TestConfigurationDao(); ConfigurationManager configurationManager = new ConfigurationManagerImpl(configurationDao); ResourceBundle resourceBundle = ResourceBundle.getBundle("message"); MediaDao mediaDao = createMock(MediaDao.class); MimeTypeManager mimeTypeManager = createMock(MimeTypeManager.class); InetAddress localAddress = createMock(InetAddress.class); expect(mediaDao.getRootNodeChildren(eq(VIDEO))).andReturn(getRootChildNodes(VIDEO, configurationDao)); replay(mediaDao, mimeTypeManager, localAddress); MediaManagerImpl mediaManager = new MediaManagerImpl(configurationManager, resourceBundle, mediaDao, mimeTypeManager, localAddress); MediaSearchRequest request = new MediaSearchRequest(new FolderNode(VIDEO.getId(), VIDEO.getParentId(), VIDEO.getBundleKey()), newArrayList("video/avi")); Collection<MediaNode> result = mediaManager.searchChildNodes(request); assertNotNull(result); assertFalse(result.isEmpty()); verify(mediaDao, mimeTypeManager, localAddress); }
@Test @SuppressWarnings("unchecked") public void testSearchChildNodesOfFolder() { ConfigurationDao configurationDao = new TestConfigurationDao(); ConfigurationManager configurationManager = new ConfigurationManagerImpl(configurationDao); ResourceBundle resourceBundle = ResourceBundle.getBundle("message"); MediaDao mediaDao = createMock(MediaDao.class); MimeTypeManager mimeTypeManager = createMock(MimeTypeManager.class); InetAddress localAddress = createMock(InetAddress.class); List<MediaNode> childNodes = new ArrayList<>(); childNodes.add(new FolderNode("id", "parentId", "name")); MimeType videoMimeType = MimeType.valueOf("video/avi"); MimeType audioMimeType = MimeType.valueOf("audio/mp3"); childNodes.add(new ContentNode("id1", "parentId", "name", new File(""), videoMimeType)); childNodes.add(new ContentNode("id2", "parentId", "name", new File(""), audioMimeType)); expect(mediaDao.getChildNodes(eq("folderId"))).andReturn(childNodes); expect(mimeTypeManager.isMimeTypeCompliant(eq(videoMimeType), isA(List.class))).andReturn(true); expect(mimeTypeManager.isMimeTypeCompliant(eq(audioMimeType), isA(List.class))).andReturn(false); replay(mediaDao, mimeTypeManager, localAddress); MediaManagerImpl mediaManager = new MediaManagerImpl(configurationManager, resourceBundle, mediaDao, mimeTypeManager, localAddress); MediaSearchRequest request = new MediaSearchRequest(new FolderNode("folderId", "folderParentId", "folderName"), newArrayList("video/avi")); Collection<MediaNode> result = mediaManager.searchChildNodes(request); assertNotNull(result); assertFalse(result.isEmpty()); assertEquals(2, result.size()); verify(mediaDao, mimeTypeManager, localAddress); }