Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 40bb2dc

Browse files
authored
Merge pull request #507 from BillFarber/task/improvePropertyExceptions
Better invalid numeric property error handling
2 parents 1bff224 + e10a6c6 commit 40bb2dc

File tree

7 files changed

+59
-16
lines changed

7 files changed

+59
-16
lines changed

src/main/java/com/marklogic/appdeployer/DefaultAppConfigFactory.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import com.marklogic.appdeployer.util.JavaClientUtil;
1919
import com.marklogic.client.DatabaseClient;
20-
import com.marklogic.client.DatabaseClientFactory;
2120
import com.marklogic.client.ext.SecurityContextType;
2221
import com.marklogic.mgmt.util.PropertySource;
2322
import com.marklogic.mgmt.util.PropertySourceFactory;
@@ -250,8 +249,8 @@ public void initialize() {
250249
* different port, in which case you can set this to that port.
251250
*/
252251
propertyConsumerMap.put("mlAppServicesPort", (config, prop) -> {
253-
logger.info("App services port: " + prop);
254-
config.setAppServicesPort(Integer.parseInt(prop));
252+
logger.info("App services port: {}", prop);
253+
config.setAppServicesPort(propertyToInteger("mlAppServicesPort", prop));
255254
});
256255
/**
257256
* The username and password for a ML user with the rest-admin role that is used for e.g. loading
@@ -376,7 +375,7 @@ public void initialize() {
376375
*/
377376
propertyConsumerMap.put("mlRestPort", (config, prop) -> {
378377
logger.info("App REST port: " + prop);
379-
config.setRestPort(Integer.parseInt(prop));
378+
config.setRestPort(propertyToInteger("mlRestPort", prop));
380379
});
381380
/**
382381
* The username and password for a ML user with the rest-admin role. This user is used for operations against the
@@ -559,7 +558,7 @@ public void initialize() {
559558
*/
560559
propertyConsumerMap.put("mlTestRestPort", (config, prop) -> {
561560
logger.info("Test REST port: " + prop);
562-
config.setTestRestPort(Integer.parseInt(prop));
561+
config.setTestRestPort(propertyToInteger("mlTestRestPort", prop));
563562
});
564563

565564
propertyConsumerMap.put("mlTestRestServerName", (config, prop) -> {
@@ -605,7 +604,7 @@ public void initialize() {
605604

606605
propertyConsumerMap.put("mlContentForestsPerHost", (config, prop) -> {
607606
logger.info("Content forests per host: " + prop);
608-
config.setContentForestsPerHost(Integer.parseInt(prop));
607+
config.setContentForestsPerHost(propertyToInteger("mlContentForestsPerHost", prop));
609608
});
610609

611610
propertyConsumerMap.put("mlCreateForests", (config, prop) -> {
@@ -620,7 +619,7 @@ public void initialize() {
620619
logger.info("Forests per host: " + prop);
621620
String[] tokens = prop.split(",");
622621
for (int i = 0; i < tokens.length; i += 2) {
623-
config.getForestCounts().put(tokens[i], Integer.parseInt(tokens[i + 1]));
622+
config.getForestCounts().put(tokens[i], propertyToInteger("mlForestsPerHost", tokens[i + 1]));
624623
}
625624
});
626625

@@ -633,7 +632,7 @@ public void initialize() {
633632
String[] tokens = prop.split(",");
634633
Map<String, Integer> map = new HashMap<>();
635634
for (int i = 0; i < tokens.length; i += 2) {
636-
map.put(tokens[i], Integer.parseInt(tokens[i + 1]));
635+
map.put(tokens[i], propertyToInteger("mlDatabaseNamesAndReplicaCounts", tokens[i + 1]));
637636
}
638637
config.setDatabaseNamesAndReplicaCounts(map);
639638
});
@@ -884,12 +883,12 @@ public void initialize() {
884883

885884
propertyConsumerMap.put("mlModulesLoaderThreadCount", (config, prop) -> {
886885
logger.info("Modules loader thread count: " + prop);
887-
config.setModulesLoaderThreadCount(Integer.parseInt(prop));
886+
config.setModulesLoaderThreadCount(propertyToInteger("mlModulesLoaderThreadCount", prop));
888887
});
889888

890889
propertyConsumerMap.put("mlModulesLoaderBatchSize", (config, prop) -> {
891890
logger.info("Modules loader batch size: " + prop);
892-
config.setModulesLoaderBatchSize(Integer.parseInt(prop));
891+
config.setModulesLoaderBatchSize(propertyToInteger("mlModulesLoaderBatchSize", prop));
893892
});
894893

895894
propertyConsumerMap.put("mlCascadeCollections", (config, prop) -> {
@@ -1000,7 +999,7 @@ public void initialize() {
1000999
protected void registerDataLoadingProperties() {
10011000
propertyConsumerMap.put("mlDataBatchSize", (config, prop) -> {
10021001
logger.info("Batch size for loading data: " + prop);
1003-
config.getDataConfig().setBatchSize(Integer.parseInt(prop));
1002+
config.getDataConfig().setBatchSize(propertyToInteger("mlDataBatchSize", prop));
10041003
});
10051004

10061005
propertyConsumerMap.put("mlDataCollections", (config, prop) -> {

src/main/java/com/marklogic/mgmt/DefaultManageConfigFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void initialize() {
5656

5757
propertyConsumerMap.put("mlManagePort", (config, prop) -> {
5858
logger.info("Manage port: " + prop);
59-
config.setPort(Integer.parseInt(prop));
59+
config.setPort(propertyToInteger("mlManagePort", prop));
6060
});
6161

6262
propertyConsumerMap.put("mlManageAuthentication", (config, prop) -> {

src/main/java/com/marklogic/mgmt/admin/DefaultAdminConfigFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void initialize() {
5656

5757
propertyConsumerMap.put("mlAdminPort", (config, prop) -> {
5858
logger.info("Admin interface port: " + prop);
59-
config.setPort(Integer.parseInt(prop));
59+
config.setPort(propertyToInteger("mlAdminPort", prop));
6060
});
6161

6262
propertyConsumerMap.put("mlAdminAuthentication", (config, prop) -> {

src/main/java/com/marklogic/mgmt/util/PropertySourceFactory.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,13 @@ public void setCheckWithMarklogicPrefix(boolean applyMarklogicPrefix) {
7373
public PropertySource getPropertySource() {
7474
return propertySource;
7575
}
76+
77+
protected final Integer propertyToInteger(String propertyName, String propertyValue) {
78+
try {
79+
return Integer.parseInt(propertyValue);
80+
} catch (NumberFormatException ex) {
81+
throw new IllegalArgumentException(format("The property %s requires a numeric value; invalid value: ‘%s'", propertyName, propertyValue));
82+
}
83+
}
84+
7685
}

src/test/java/com/marklogic/appdeployer/DefaultAppConfigFactoryTest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@
1919
import com.marklogic.client.DatabaseClientFactory;
2020
import com.marklogic.client.ext.SecurityContextType;
2121
import com.marklogic.client.ext.modulesloader.impl.PropertiesModuleManager;
22-
import com.marklogic.mgmt.DefaultManageConfigFactory;
23-
import com.marklogic.mgmt.ManageConfig;
2422
import com.marklogic.mgmt.util.SimplePropertySource;
2523
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.CsvSource;
2626

2727
import java.io.File;
2828
import java.util.List;
2929
import java.util.Map;
3030
import java.util.Properties;
3131
import java.util.Set;
3232

33+
import static java.lang.String.format;
3334
import static org.junit.jupiter.api.Assertions.assertEquals;
3435
import static org.junit.jupiter.api.Assertions.assertFalse;
3536
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -929,6 +930,24 @@ void globalKeyStoreAndTrustStore() {
929930
assertEquals("SunX5092", config.getAppServicesTrustStoreAlgorithm());
930931
}
931932

933+
@ParameterizedTest
934+
@CsvSource(delimiter = ':', value = {
935+
"mlAppServicesPort:NaN",
936+
"mlRestPort:NaN",
937+
"mlTestRestPort:NaN",
938+
"mlContentForestsPerHost:NaN",
939+
"mlForestsPerHost:1,NaN,3",
940+
"mlDatabaseNamesAndReplicaCounts:1,NaN,3",
941+
"mlModulesLoaderThreadCount:NaN",
942+
"mlDataBatchSize:NaN"
943+
})
944+
void numericConfigValues(String propertyName, String propertyValue) {
945+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
946+
configure(propertyName, propertyValue);
947+
});
948+
assertTrue(exception.getMessage().contains(format("The property %s requires a numeric value; invalid value: ‘NaN'", propertyName)));
949+
}
950+
932951
private AppConfig configure(String... properties) {
933952
return new DefaultAppConfigFactory(new SimplePropertySource(properties)).newAppConfig();
934953
}

src/test/java/com/marklogic/mgmt/DefaultManageConfigFactoryTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,14 @@ void globalKeyStoreAndTrustStore() {
327327
assertEquals("https", config.getScheme());
328328
}
329329

330+
@Test
331+
void mlManagePort() {
332+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
333+
configure("mlManagePort", "NaN");
334+
});
335+
assertEquals("The property mlManagePort requires a numeric value; invalid value: ‘NaN'", exception.getMessage());
336+
}
337+
330338
private ManageConfig configure(String... properties) {
331339
return new DefaultManageConfigFactory(new SimplePropertySource(properties)).newManageConfig();
332340
}

src/test/java/com/marklogic/mgmt/admin/DefaultAdminConfigFactoryTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static org.junit.jupiter.api.Assertions.assertThrows;
2020
import static org.junit.jupiter.api.Assertions.assertTrue;
2121

22-
import com.marklogic.mgmt.ManageConfig;
2322
import org.junit.jupiter.api.Test;
2423

2524
import com.marklogic.client.DatabaseClientFactory;
@@ -289,4 +288,13 @@ void globalKeyStoreAndTrustStore() {
289288
private AdminConfig configure(String... properties) {
290289
return new DefaultAdminConfigFactory(new SimplePropertySource(properties)).newAdminConfig();
291290
}
291+
292+
@Test
293+
void mlAdminPort() {
294+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
295+
configure("mlAdminPort", "NaN");
296+
});
297+
assertEquals("The property mlAdminPort requires a numeric value; invalid value: ‘NaN'", exception.getMessage());
298+
}
292299
}
300+

0 commit comments

Comments
 (0)