InitWriterConfig { public static InitWriterConfig fromMap(Map<String, String> map) { String nodeName = map.get(InitWriterConfig.NODE_NAME); if (nodeName == null || nodeName.equals("")) { throw new IllegalArgumentException(InitWriterConfig.NODE_NAME + " cannot be null or empty"); } String rackTopologyKey = map.get(InitWriterConfig.RACK_TOPOLOGY_KEY); boolean externalAddress = map.containsKey(InitWriterConfig.EXTERNAL_ADDRESS); String initFolder = DEFAULT_INIT_FOLDER; String initFolderEnvVar = map.get(InitWriterConfig.INIT_FOLDER); if (initFolderEnvVar != null) { initFolder = initFolderEnvVar; } String externalAddressType = map.get(InitWriterConfig.EXTERNAL_ADDRESS_TYPE); return new InitWriterConfig(nodeName, rackTopologyKey, externalAddress, initFolder, externalAddressType); } InitWriterConfig(String nodeName, String rackTopologyKey, boolean externalAddress, String initFolder, String externalAddressType); static InitWriterConfig fromMap(Map<String, String> map); String getNodeName(); String getRackTopologyKey(); String getInitFolder(); boolean isExternalAddress(); String getAddressType(); @Override String toString(); static final String INIT_FOLDER; static final String RACK_TOPOLOGY_KEY; static final String NODE_NAME; static final String EXTERNAL_ADDRESS; static final String EXTERNAL_ADDRESS_TYPE; static final String DEFAULT_INIT_FOLDER; }
@Test public void testFromMap() { InitWriterConfig config = InitWriterConfig.fromMap(envVars); assertThat(config.getNodeName(), is("localhost")); assertThat(config.getRackTopologyKey(), is("failure-domain.beta.kubernetes.io/zone")); assertThat(config.isExternalAddress(), is(true)); assertThat(config.getAddressType(), is(nullValue())); }
@Test public void testFromMapEmptyEnvVarsThrows() { assertThrows(IllegalArgumentException.class, () -> InitWriterConfig.fromMap(Collections.emptyMap())); }
@Test public void testFromMapMissingNodeNameThrows() { Map<String, String> envVars = new HashMap<>(InitWriterConfigTest.envVars); envVars.remove(InitWriterConfig.NODE_NAME); assertThrows(IllegalArgumentException.class, () -> InitWriterConfig.fromMap(envVars)); }