BackendManagerImpl implements BackendManager { @Override public void addFolder(final ConfigurationFolder folder, final RootNode rootNode) { validateFolder(folder, rootNode, null); ConfigurationNode node = new ConfigurationNode(newUniqueId(), folder.getName(), folder.getPath()); try { if (configurationManager.addNode(rootNode, node)) { eventBus.post(new ConfigurationEvent(ADD_FOLDER, node, rootNode)); } } catch (IOException e) { throw new BackendException(e); } } @Inject  BackendManagerImpl(final ConfigurationManager configurationManager, final EventBus eventBus); @Override Collection<ConfigurationFolder> getFolders(final RootNode rootNode); @Override ConfigurationFolder getFolder(final String id, final RootNode rootNode); @Override void addFolder(final ConfigurationFolder folder, final RootNode rootNode); @Override void editFolder(final String id, final ConfigurationFolder folder, final RootNode rootNode); @Override void removeFolder(final String id, final RootNode rootNode); @Override Settings getSettings(); @Override void saveSettings(final Settings settings);  }
@Test public void testAddFolder() throws IOException { ConfigurationManager configurationManager = createMock(ConfigurationManager.class); EventBus eventBus = createMock(EventBus.class); expect(configurationManager.findNode(AUDIO, null, "newAudioFolder", System.getProperty("java.io.tmpdir"))).andReturn(Optional.<ConfigurationNode>empty()); expect(configurationManager.addNode(eq(AUDIO), isA(ConfigurationNode.class))).andReturn(true); eventBus.post(isA(ConfigurationEvent.class)); expectLastCall(); replay(configurationManager, eventBus); try { BackendManagerImpl backendManager = new BackendManagerImpl(configurationManager, eventBus); backendManager.addFolder(new ConfigurationFolder(null, "newAudioFolder", System.getProperty("java.io.tmpdir")), AUDIO); } finally { verify(configurationManager, eventBus); } }
@Test public void testAddExistingFolder() throws IOException { ConfigurationManager configurationManager = createMock(ConfigurationManager.class); EventBus eventBus = createMock(EventBus.class); expect(configurationManager.findNode(AUDIO, null, "newAudioFolder", System.getProperty("java.io.tmpdir"))).andReturn(Optional.<ConfigurationNode>empty()); expect(configurationManager.addNode(eq(AUDIO), isA(ConfigurationNode.class))).andReturn(false); replay(configurationManager, eventBus); try { BackendManagerImpl backendManager = new BackendManagerImpl(configurationManager, eventBus); backendManager.addFolder(new ConfigurationFolder(null, "newAudioFolder", System.getProperty("java.io.tmpdir")), AUDIO); } finally { verify(configurationManager, eventBus); } }
@Test(expected = BackendException.class) public void testAddFolderIOException() throws IOException { ConfigurationManager configurationManager = createMock(ConfigurationManager.class); EventBus eventBus = createMock(EventBus.class); expect(configurationManager.findNode(AUDIO, null, "newAudioFolder", System.getProperty("java.io.tmpdir"))).andReturn(Optional.<ConfigurationNode>empty()); configurationManager.addNode(eq(AUDIO), isA(ConfigurationNode.class)); expectLastCall().andThrow(new IOException()); replay(configurationManager, eventBus); try { BackendManagerImpl backendManager = new BackendManagerImpl(configurationManager, eventBus); backendManager.addFolder(new ConfigurationFolder(null, "newAudioFolder", System.getProperty("java.io.tmpdir")), AUDIO); } finally { verify(configurationManager, eventBus); } }
@Test(expected = BackendException.class) public void testAddFolderWithSameName() throws IOException { ConfigurationManager configurationManager = createMock(ConfigurationManager.class); EventBus eventBus = createMock(EventBus.class); expect(configurationManager.findNode(AUDIO, null, "name", System.getProperty("java.io.tmpdir"))).andReturn(Optional.of(new ConfigurationNode("id", "name", "path"))); replay(configurationManager, eventBus); try { BackendManagerImpl backendManager = new BackendManagerImpl(configurationManager, eventBus); backendManager.addFolder(new ConfigurationFolder(null, "name", System.getProperty("java.io.tmpdir")), AUDIO); } finally { verify(configurationManager, eventBus); } }
@Test(expected = BackendException.class) public void testAddFolderWithSamePath() throws IOException { ConfigurationManager configurationManager = createMock(ConfigurationManager.class); EventBus eventBus = createMock(EventBus.class); expect(configurationManager.findNode(AUDIO, null, "newAudioFolder", System.getProperty("java.io.tmpdir"))).andReturn(Optional.of(new ConfigurationNode("id", "name", System.getProperty("java.io.tmpdir")))); replay(configurationManager, eventBus); try { BackendManagerImpl backendManager = new BackendManagerImpl(configurationManager, eventBus); backendManager.addFolder(new ConfigurationFolder(null, "newAudioFolder", System.getProperty("java.io.tmpdir")), AUDIO); } finally { verify(configurationManager, eventBus); } }
@Test(expected = BackendException.class) public void testAddFolderWithoutName() throws IOException { ConfigurationManager configurationManager = createMock(ConfigurationManager.class); EventBus eventBus = createMock(EventBus.class); replay(configurationManager, eventBus); try { BackendManagerImpl backendManager = new BackendManagerImpl(configurationManager, eventBus); backendManager.addFolder(new ConfigurationFolder(null, null, System.getProperty("java.io.tmpdir")), AUDIO); } finally { verify(configurationManager, eventBus); } }
@Test(expected = BackendException.class) public void testAddFolderWithoutPath() { ConfigurationManager configurationManager = createMock(ConfigurationManager.class); EventBus eventBus = createMock(EventBus.class); replay(configurationManager, eventBus); try { BackendManagerImpl backendManager = new BackendManagerImpl(configurationManager, eventBus); backendManager.addFolder(new ConfigurationFolder(null, "newAudioFolder", null), AUDIO); } finally { verify(configurationManager, eventBus); } }
@Test(expected = BackendException.class) public void testAddFolderWithBadPath() { ConfigurationManager configurationManager = createMock(ConfigurationManager.class); EventBus eventBus = createMock(EventBus.class); replay(configurationManager, eventBus); try { BackendManagerImpl backendManager = new BackendManagerImpl(configurationManager, eventBus); backendManager.addFolder(new ConfigurationFolder(null, "newAudioFolder", "bad_path"), AUDIO); } finally { verify(configurationManager, eventBus); } }