diff --git a/src/main/java/com/conveyal/datatools/manager/DataManager.java b/src/main/java/com/conveyal/datatools/manager/DataManager.java index f437c258d..b1f4c2335 100644 --- a/src/main/java/com/conveyal/datatools/manager/DataManager.java +++ b/src/main/java/com/conveyal/datatools/manager/DataManager.java @@ -3,7 +3,7 @@ import com.conveyal.datatools.common.utils.CorsFilter; import com.conveyal.datatools.common.utils.RequestSummary; import com.conveyal.datatools.common.utils.Scheduler; -import com.conveyal.datatools.common.utils.aws.S3Utils; +import com.conveyal.datatools.common.utils.SparkUtils; import com.conveyal.datatools.editor.controllers.EditorLockController; import com.conveyal.datatools.editor.controllers.api.EditorControllerImpl; import com.conveyal.datatools.editor.controllers.api.SnapshotController; @@ -55,7 +55,6 @@ import static com.conveyal.datatools.common.utils.SparkUtils.logMessageAndHalt; import static com.conveyal.datatools.common.utils.SparkUtils.logRequest; -import static com.conveyal.datatools.common.utils.SparkUtils.logResponse; import static spark.Service.SPARK_DEFAULT_PORT; import static spark.Spark.after; import static spark.Spark.before; @@ -130,7 +129,7 @@ static void initializeApplication(String[] args, boolean initScheduledTasks) thr // Optionally set port for server. Otherwise, Spark defaults to 4567. if (hasConfigProperty("application.port")) { - PORT = Integer.parseInt(getConfigPropertyAsText("application.port")); + PORT = getConfigProperty("application.port").asInt(); port(PORT); } useS3 = "true".equals(getConfigPropertyAsText("application.data.use_s3_storage")); @@ -339,9 +338,7 @@ static void registerRoutes() throws IOException { }); // add logger - after((request, response) -> { - logResponse(request, response); - }); + after(SparkUtils::logResponse); } /** @@ -363,10 +360,10 @@ private static String resourceToString (String resourceName) { } private static boolean hasConfigProperty(JsonNode config, String name) { - String parts[] = name.split("\\."); + String[] parts = name.split("\\."); JsonNode node = config; for (int i = 0; i < parts.length; i++) { - if(node == null) return false; + if (node == null) return false; node = node.get(parts[i]); } return node != null; @@ -393,10 +390,10 @@ public static JsonNode getConfigProperty(String name) { } private static JsonNode getConfigProperty(JsonNode config, String name) { - String parts[] = name.split("\\."); + String[] parts = name.split("\\."); JsonNode node = config; - for(int i = 0; i < parts.length; i++) { - if(node == null) { + for (int i = 0; i < parts.length; i++) { + if (node == null) { LOG.warn("Config property {} not found", name); return null; } @@ -455,18 +452,25 @@ public static boolean isExtensionEnabled(String extensionName) { * In a test environment allows for overriding a specific config value on the server config object. */ public static void overrideConfigProperty(String name, String value) { - String parts[] = name.split("\\."); + String[] parts = name.split("\\."); ObjectNode node = (ObjectNode) serverConfig; - //Loop through the dot separated field names to obtain final node and override that node's value. + // Loop through the dot separated field names to obtain final node and override that node's value. for (int i = 0; i < parts.length; i++) { + String part = parts[i]; if (i < parts.length - 1) { - if (!node.has(parts[i])) { - node.set(parts[i], JsonUtil.objectMapper.createObjectNode()); + if (!node.has(part)) { + node.set(part, JsonUtil.objectMapper.createObjectNode()); } - node = (ObjectNode) node.get(parts[i]); + node = (ObjectNode) node.get(part); } else { - node.put(parts[i], value); + // If a value is null, delete the corresponding node instead of put-ting the node value + // (After node.put(part, null), calling getConfigPropertyAsText will return "null".) + if (value == null) { + node.remove(part); + } else { + node.put(part, value); + } } } } diff --git a/src/test/java/com/conveyal/datatools/manager/extensions/mtc/MtcFeedResourceTest.java b/src/test/java/com/conveyal/datatools/manager/extensions/mtc/MtcFeedResourceTest.java index edac92893..a45ef5c5e 100644 --- a/src/test/java/com/conveyal/datatools/manager/extensions/mtc/MtcFeedResourceTest.java +++ b/src/test/java/com/conveyal/datatools/manager/extensions/mtc/MtcFeedResourceTest.java @@ -109,11 +109,7 @@ static void tearDown() { @AfterEach void resetState() { DataManager.overrideConfigProperty(CONFIG_MTC_CREDENTIALS, defaultMtcAwsCredentials); - - String currentMtcRegion = getConfigPropertyAsText(CONFIG_MTC_REGION); - if (currentMtcRegion != null && !currentMtcRegion.equals(defaultMtcAwsRegion)) { - DataManager.overrideConfigProperty(CONFIG_MTC_REGION, defaultMtcAwsRegion); - } + DataManager.overrideConfigProperty(CONFIG_MTC_REGION, defaultMtcAwsRegion); } @Test @@ -262,7 +258,7 @@ void shouldUpdateRtdCarrierProperty(String address) throws JsonProcessingExcepti } @ParameterizedTest - @ValueSource(booleans = { false, true }) + @ValueSource(booleans = { true, false }) void canUseCorrectMtcCredentials(boolean overwriteDefaults) { if (overwriteDefaults) { DataManager.overrideConfigProperty(CONFIG_MTC_REGION, "us-west-2");