### Question:
DirectoryConfigSource extends AbstractConfigSource implements PollableSource<Instant>,
                   WatchableSource<Path>,
                   NodeConfigSource { @Override public Optional<NodeContent> load() throws ConfigException { if (!Files.exists(directoryPath)) { return Optional.empty(); } try { ConfigNode.ObjectNode.Builder objectNodeRoot = ConfigNode.ObjectNode.builder(); Files.walk(directoryPath, 1, FOLLOW_LINKS) .filter(Files::isRegularFile) .forEach(path -> { String content = FileSourceHelper.safeReadContent(path); objectNodeRoot.addValue(path.getFileName().toString(), content); }); NodeContent.Builder builder = NodeContent.builder() .node(objectNodeRoot.build()); lastModifiedTime(directoryPath).ifPresent(builder::stamp); return Optional.of(builder.build()); } catch (ConfigException ex) { throw ex; } catch (Exception ex) { throw new ConfigException("Configuration at directory '" + directoryPath + "' is not accessible.", ex); } } DirectoryConfigSource(Builder builder); static DirectoryConfigSource create(Config metaConfig); static Builder builder(); @Override boolean isModified(Instant stamp); @Override Optional<PollingStrategy> pollingStrategy(); @Override Optional<ChangeWatcher<Object>> changeWatcher(); @Override Path target(); @Override boolean exists(); @Override Class<Path> targetType(); @Override Optional<NodeContent> load();  }### Answer:
@Test public void testLoadNoDirectory() { DirectoryConfigSource configSource = ConfigSources.directory("unknown") .build(); assertThat(configSource.load(), is(Optional.empty())); }
@Test public void testLoadEmptyDirectory() throws IOException { DirectoryConfigSource configSource = ConfigSources.directory(folder.newFolder().getAbsolutePath()) .build(); Optional<ConfigContent.NodeContent> maybeContent = configSource.load(); assertThat(maybeContent, not(Optional.empty())); ConfigContent.NodeContent nodeContent = maybeContent.get(); Optional<Object> maybeStamp = nodeContent.stamp(); assertThat(maybeStamp, not(Optional.empty())); Instant stamp = (Instant) maybeStamp.get(); assertThat(stamp, both(greaterThan(Instant.now().minusSeconds(3))) .and(lessThan(Instant.now().plusSeconds(3)))); ObjectNode objectNode = nodeContent.data(); assertThat(objectNode.entrySet(), hasSize(0)); }
@Test public void testLoadDirectory() throws IOException { File folder = DirectoryConfigSourceTest.folder.newFolder(); Files.write(Files.createFile(new File(folder, "username").toPath()), "libor".getBytes()); Files.write(Files.createFile(new File(folder, "password").toPath()), "^ery$ecretP&ssword".getBytes()); DirectoryConfigSource configSource = ConfigSources.directory(folder.getAbsolutePath()) .build(); Optional<ConfigContent.NodeContent> maybeContent = configSource.load(); ConfigContent.NodeContent nodeContent = maybeContent.get(); Optional<Object> maybeStamp = nodeContent.stamp(); assertThat(maybeStamp, not(Optional.empty())); Instant stamp = (Instant) maybeStamp.get(); assertThat(stamp, both(greaterThan(Instant.now().minusSeconds(3))) .and(lessThan(Instant.now().plusSeconds(3)))); ObjectNode objectNode = nodeContent.data(); assertThat(objectNode.entrySet(), hasSize(2)); assertThat(objectNode.get("username"), valueNode("libor")); assertThat(objectNode.get("password"), valueNode("^ery$ecretP&ssword")); }