NomadConfigurationManager { public static Optional<String> findNodeName(Path configurationDirectory, IParameterSubstitutor parameterSubstitutor) { requireNonNull(configurationDirectory); NomadConfigurationManager nomadConfigurationManager = new NomadConfigurationManager(configurationDirectory, parameterSubstitutor); ConfigDirDepth depth = nomadConfigurationManager.getConfigurationDirectoryDepth(); if (depth == FULL) { try (Stream<Path> pathStream = Files.list(nomadConfigurationManager.getClusterPath())) { Set<String> nodeNames = pathStream .map(Path::getFileName) .map(Path::toString) .map(ClusterConfigFilename::from) .filter(Optional::isPresent) .map(Optional::get) .map(ClusterConfigFilename::getNodeName) .collect(toSet()); if (nodeNames.size() > 1) { throw new IllegalStateException( String.format("Found versioned cluster config files for the following different nodes: %s in: %s", String.join(", ", nodeNames), nomadConfigurationManager.getClusterPath() ) ); } else if (nodeNames.size() == 1) { return Optional.of(nodeNames.iterator().next()); } } catch (IOException e) { throw new UncheckedIOException(e); } } return Optional.empty(); } NomadConfigurationManager(Path configurationDirectory, IParameterSubstitutor parameterSubstitutor); Optional<String> getNodeName(); void createDirectories(); Path getConfigurationDirectory(); Path getClusterPath(); Path getLicensePath(); Path getChangesPath(); static Optional<String> findNodeName(Path configurationDirectory, IParameterSubstitutor parameterSubstitutor);  }
@Test public void testExtractNodeName() throws Exception { File tmp = folder.newFolder(); Path configDir = tmp.toPath(); Path cluster = configDir.resolve("cluster"); Files.createDirectory(cluster); assertThat( () -> findNodeName(configDir, identity()), is(throwing(instanceOf(IllegalStateException.class)).andMessage(is(containsString("Configuration directory is partially formed")))) ); Files.createDirectory(configDir.resolve("license")); Files.createDirectory(configDir.resolve("changes")); assertThat(findNodeName(configDir, identity()), is(Optional.empty())); Path configFilePath = cluster.resolve("3.node1.properties"); Files.createFile(configFilePath); assertThat(findNodeName(configDir, identity()), is(Optional.empty())); org.terracotta.utilities.io.Files.delete(configFilePath); configFilePath = cluster.resolve("node1.3.properties"); Files.createFile(configFilePath); String nodeName = findNodeName(configDir, identity()).get(); assertThat(nodeName, is("node1")); configFilePath = cluster.resolve("node1.4.properties"); Files.createFile(configFilePath); findNodeName(configDir, identity()); assertThat(nodeName, is("node1")); configFilePath = cluster.resolve("node2.4.properties"); Files.createFile(configFilePath); assertThat( () -> findNodeName(configDir, identity()), is(throwing(instanceOf(IllegalStateException.class)) .andMessage(is("Found versioned cluster config files for the following different nodes: node2, node1 in: " + cluster.toAbsolutePath()))) ); org.terracotta.utilities.io.Files.delete(configFilePath); configFilePath = cluster.resolve("3.node1.properties"); Files.createFile(configFilePath); findNodeName(configDir, identity()); assertThat(nodeName, is("node1")); }