MediaDaoImpl implements MediaDao { @Override public List<MediaNode> getChildNodes(final String parentNodeId) { List<MediaNode> childNodes; MediaIndexElement indexElement = mediaIndexDao.get(parentNodeId); if (indexElement != null) { MediaType mediaType = MediaType.getByValue(indexElement.getMediaType()); childNodes = getFolderChildNodes(parentNodeId, indexElement.getPath(), mediaType); } else { childNodes = new ArrayList<>(0); LOGGER.error("[getChildNodes] {} node not found in media index", parentNodeId); } return childNodes; } @Inject  MediaDaoImpl(final ConfigurationManager configurationManager, final MimeTypeManager mimeTypeManager, final MediaIndexDao mediaIndexDao); @Override Optional<MediaNode> getNode(final String nodeId); @Override List<MediaNode> getChildNodes(final String parentNodeId); @Override List<MediaNode> getRootNodeChildren(final RootNode rootNode); @Override void cleanUpCache();  }
@Test public void testGetChildNodesNotInIndex() { ConfigurationManager configurationManager = new ConfigurationManagerImpl(new TestConfigurationDao()); MimeTypeManager mimeTypeManager = createMock(MimeTypeManager.class); MediaIndexDao mediaIndexDao = createMock(MediaIndexDao.class); expect(mediaIndexDao.get(eq("nodeId"))).andReturn(null); replay(mimeTypeManager, mediaIndexDao); MediaDaoImpl mediaDao = new MediaDaoImpl(configurationManager, mimeTypeManager, mediaIndexDao); List<MediaNode> result = mediaDao.getChildNodes("nodeId"); assertTrue(result.isEmpty()); verify(mimeTypeManager, mediaIndexDao); }
@Test public void testGetChildNodesOfVideoFolder() { ConfigurationManager configurationManager = new ConfigurationManagerImpl(new TestConfigurationDao()); MimeTypeManager mimeTypeManager = createMock(MimeTypeManager.class); MediaIndexDao mediaIndexDao = createMock(MediaIndexDao.class); ConfigurationNode videoNode = configurationManager.getNodes(VIDEO).get(0); Path videoFolderPath = Paths.get(videoNode.getPath(), "subFolder"); MediaIndexElement videoElement = new MediaIndexElement(VIDEO.getId(), TYPE_VIDEO.getValue(), "video/avi", videoFolderPath.toFile().getPath(), videoFolderPath.toFile().getName(), VIDEO.isLocalPath(), true); expect(mediaIndexDao.get(eq("nodeId"))).andReturn(videoElement); expect(mediaIndexDao.add(isA(MediaIndexElement.class))).andReturn(UniqueIdGenerator.newUniqueId()).atLeastOnce(); expect(mimeTypeManager.getMimeType(eq("video.avi"))).andReturn(MimeType.valueOf("video/avi")).atLeastOnce(); expect(mimeTypeManager.getMimeType(eq("video.unknown"))).andReturn(null).atLeastOnce(); expect(mimeTypeManager.getMimeType(eq("video.srt"))).andReturn(MIME_TYPE_SUBTITLE).atLeastOnce(); replay(mimeTypeManager, mediaIndexDao); MediaDaoImpl mediaDao = new MediaDaoImpl(configurationManager, mimeTypeManager, mediaIndexDao); List<MediaNode> result = mediaDao.getChildNodes("nodeId"); assertFalse(result.isEmpty()); verify(mimeTypeManager, mediaIndexDao); }
@Test public void testGetChildNodesOfVideoFile() { ConfigurationManager configurationManager = new ConfigurationManagerImpl(new TestConfigurationDao()); MimeTypeManager mimeTypeManager = createMock(MimeTypeManager.class); MediaIndexDao mediaIndexDao = createMock(MediaIndexDao.class); ConfigurationNode videoNode = configurationManager.getNodes(VIDEO).get(0); Path videoFolderPath = Paths.get(videoNode.getPath(), "video.avi"); MediaIndexElement videoElement = new MediaIndexElement(VIDEO.getId(), TYPE_VIDEO.getValue(), "video/avi", videoFolderPath.toFile().getPath(), videoFolderPath.toFile().getName(), VIDEO.isLocalPath(), true); expect(mediaIndexDao.get(eq("nodeId"))).andReturn(videoElement); replay(mimeTypeManager, mediaIndexDao); MediaDaoImpl mediaDao = new MediaDaoImpl(configurationManager, mimeTypeManager, mediaIndexDao); List<MediaNode> result = mediaDao.getChildNodes("nodeId"); assertTrue(result.isEmpty()); verify(mimeTypeManager, mediaIndexDao); }
@Test public void testGetChildNodesOfVideoFolderBadMediaType() { ConfigurationManager configurationManager = new ConfigurationManagerImpl(new TestConfigurationDao()); MimeTypeManager mimeTypeManager = createMock(MimeTypeManager.class); MediaIndexDao mediaIndexDao = createMock(MediaIndexDao.class); ConfigurationNode videoNode = configurationManager.getNodes(VIDEO).get(0); Path videoFolderPath = Paths.get(videoNode.getPath(), "subFolder"); MediaIndexElement videoElement = new MediaIndexElement(VIDEO.getId(), TYPE_AUDIO.getValue(), "video/avi", videoFolderPath.toFile().getPath(), videoFolderPath.toFile().getName(), VIDEO.isLocalPath(), true); expect(mediaIndexDao.get(eq("nodeId"))).andReturn(videoElement); expect(mediaIndexDao.add(isA(MediaIndexElement.class))).andReturn(UniqueIdGenerator.newUniqueId()).atLeastOnce(); expect(mimeTypeManager.getMimeType(eq("video.avi"))).andReturn(MimeType.valueOf("video/avi")).atLeastOnce(); expect(mimeTypeManager.getMimeType(eq("video.unknown"))).andReturn(null).atLeastOnce(); expect(mimeTypeManager.getMimeType(eq("video.srt"))).andReturn(MIME_TYPE_SUBTITLE).atLeastOnce(); replay(mimeTypeManager, mediaIndexDao); MediaDaoImpl mediaDao = new MediaDaoImpl(configurationManager, mimeTypeManager, mediaIndexDao); List<MediaNode> result = mediaDao.getChildNodes("nodeId"); assertFalse(result.isEmpty()); verify(mimeTypeManager, mediaIndexDao); }