diff options
author | Arnstein Ressem <aressem@gmail.com> | 2022-09-02 16:12:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-02 16:12:23 +0200 |
commit | 3edb00aee148bed53e74fb324fe46960acdbd498 (patch) | |
tree | 79c1ce4524eacdf4117a0908f5f55d8358b836ae | |
parent | c23bba0b2cd6ab7699ee2b0d6be34c023159ffb8 (diff) |
Revert "Cleanup, more tests and separate resolved from not"
4 files changed, 32 insertions, 77 deletions
diff --git a/config-lib/src/main/java/com/yahoo/config/ModelNode.java b/config-lib/src/main/java/com/yahoo/config/ModelNode.java index 67ea751d40e..2748ef8c7e9 100644 --- a/config-lib/src/main/java/com/yahoo/config/ModelNode.java +++ b/config-lib/src/main/java/com/yahoo/config/ModelNode.java @@ -23,13 +23,13 @@ public class ModelNode extends LeafNode<Path> { public ModelNode(ModelReference modelReference) { super(true); - this.value = modelReference.value(); // The resolved value, or null if not resolved + this.value = modelReference.value(); this.reference = modelReference; } @Override public String getValue() { - return reference.toString(); + return value.toString(); } @Override diff --git a/config-lib/src/main/java/com/yahoo/config/ModelReference.java b/config-lib/src/main/java/com/yahoo/config/ModelReference.java index ecee42225db..ba35812db4d 100644 --- a/config-lib/src/main/java/com/yahoo/config/ModelReference.java +++ b/config-lib/src/main/java/com/yahoo/config/ModelReference.java @@ -1,6 +1,7 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.config; +import java.io.File; import java.nio.file.Path; import java.util.Objects; import java.util.Optional; @@ -58,7 +59,14 @@ public class ModelReference { /** Returns the path to the file containing this model, or null if not available. */ public Path value() { - return resolved; + if (resolved != null) + return resolved; + + if (url.isPresent() && new File(url.get().value()).exists()) + return Path.of(url.get().value()); + if (path.isPresent()) + return Path.of(path.get().value()); + return null; } @Override @@ -78,10 +86,9 @@ public class ModelReference { /** Returns this on the format accepted by valueOf */ @Override public String toString() { - if (resolved != null) return resolved.toString(); - return modelId.orElse("\"\"") + " " + - url.map(v -> v.value()).orElse("\"\"") + " " + - path.map(v -> v.value()).orElse("\"\""); + return modelId.orElse("") + " " + + url.map(v -> v.value()).orElse("") + " " + + path.map(v -> v.value()).orElse(""); } /** Creates a model reference having a model id only. */ @@ -114,9 +121,9 @@ public class ModelReference { if (parts.length == 1) return new ModelReference(Path.of(s)); else if (parts.length == 3) - return new ModelReference(parts[0].equals("\"\"") ? Optional.empty() : Optional.of(parts[0]), - parts[1].equals("\"\"") ? Optional.empty() : Optional.of(new UrlReference(parts[1])), - parts[2].equals("\"\"") ? Optional.empty() : Optional.of(new FileReference(parts[2]))); + return new ModelReference(parts[0].equals("") ? Optional.empty() : Optional.of(parts[0]), + parts[1].equals("") ? Optional.empty() : Optional.of(new UrlReference(parts[1])), + parts[2].equals("") ? Optional.empty() : Optional.of(new FileReference(parts[2]))); else throw new IllegalArgumentException("Unexpected model string '" + s + "'"); } diff --git a/config/src/main/java/com/yahoo/vespa/config/ConfigPayloadApplier.java b/config/src/main/java/com/yahoo/vespa/config/ConfigPayloadApplier.java index 4f078059f46..b0ce2291438 100644 --- a/config/src/main/java/com/yahoo/vespa/config/ConfigPayloadApplier.java +++ b/config/src/main/java/com/yahoo/vespa/config/ConfigPayloadApplier.java @@ -250,12 +250,10 @@ public class ConfigPayloadApplier<T extends ConfigInstance.Builder> { private ModelReference resolveModel(String modelStringValue) { var model = ModelReference.valueOf(modelStringValue); // Resolve any of url and path present, in priority order - if (model.url().isPresent() && canResolveUrls()) { - model = new ModelReference(Path.of(resolveUrl(model.url().get().value()).value())); - } - else if (model.path().isPresent()) { - model = new ModelReference(Path.of(resolvePath(model.path().get().value()).value())); - } + if (model.url().isPresent() && canResolveUrls()) + model = model.withUrl(Optional.of(resolveUrl(model.url().get().value()))); + else if (model.path().isPresent()) + model = model.withPath(Optional.of(resolvePath(model.path().get().value()))); return model; } diff --git a/config/src/test/java/com/yahoo/vespa/config/ConfigPayloadApplierTest.java b/config/src/test/java/com/yahoo/vespa/config/ConfigPayloadApplierTest.java index aa517c943de..4d3b6b93b3b 100644 --- a/config/src/test/java/com/yahoo/vespa/config/ConfigPayloadApplierTest.java +++ b/config/src/test/java/com/yahoo/vespa/config/ConfigPayloadApplierTest.java @@ -11,78 +11,28 @@ import java.io.File; import java.nio.file.Path; import java.util.Optional; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - /** * @author bratseth */ public class ConfigPayloadApplierTest { @Test - public void testAllConfigApplierReferenceTypes() { - var configBuilder = new ResolvedTypesConfig.Builder(); - var applier = new ConfigPayloadApplier<>(configBuilder, new MockAcquirer(), new MockDownloader()); - - var inputConfig = new ResolvedTypesConfig.Builder(); - inputConfig.myPath(new FileReference("myPath.txt")); - inputConfig.myUrl(new UrlReference("myUrl.txt")); - inputConfig.myModel(new ModelReference(Optional.empty(), - Optional.of(new UrlReference("myUrl.txt")), - Optional.of(new FileReference("myPath.txt")))); - applier.applyPayload(ConfigPayload.fromInstance(inputConfig.build())); - var config = configBuilder.build(); - - assertEndsWith("resolvedPath/myPath.txt", config.myPath().toString()); - assertEndsWith("resolvedUrl/myUrl.txt", config.myUrl().toString()); - assertEndsWith("resolvedUrl/myUrl.txt", config.myModel().toString()); - } - - @Test - public void testModelWithUrlOnly() { - var configBuilder = new ResolvedTypesConfig.Builder(); - var applier = new ConfigPayloadApplier<>(configBuilder, new MockAcquirer(), new MockDownloader()); - - var inputConfig = new ResolvedTypesConfig.Builder(); - inputConfig.myPath(new FileReference("myPath.txt")); - inputConfig.myUrl(new UrlReference("myUrl.txt")); - inputConfig.myModel(ModelReference.valueOf("my-id myUrl.txt \"\"")); - applier.applyPayload(ConfigPayload.fromInstance(inputConfig.build())); - var config = configBuilder.build(); - - assertEndsWith("resolvedUrl/myUrl.txt", config.myModel().toString()); - } - - @Test - public void testModelWithPathOnly() { - var configBuilder = new ResolvedTypesConfig.Builder(); - var applier = new ConfigPayloadApplier<>(configBuilder, new MockAcquirer(), new MockDownloader()); - - var inputConfig = new ResolvedTypesConfig.Builder(); - inputConfig.myPath(new FileReference("myPath.txt")); - inputConfig.myUrl(new UrlReference("myUrl.txt")); - inputConfig.myModel(ModelReference.valueOf("my-id \"\" myPath.txt")); - applier.applyPayload(ConfigPayload.fromInstance(inputConfig.build())); - var config = configBuilder.build(); - - assertEndsWith("resolvedPath/myPath.txt", config.myModel().toString()); - } - - private void assertEndsWith(String ending, String string) { - String assertingThat = "'" + string + "' ends with '" + ending + "'"; - try { - assertEquals(assertingThat, ending, string.substring(string.length() - ending.length())); - } - catch (StringIndexOutOfBoundsException e) { - fail(assertingThat); - } + public void testConfigApplier() { + var applier = new ConfigPayloadApplier<>(new ResolvedTypesConfig.Builder(), new MockAcquirer(), new MockDownloader()); + var config = new ResolvedTypesConfig.Builder(); + config.myPath(new FileReference("mock/myPath.txt")); + config.myUrl(new UrlReference("mock/myUrl.txt")); + config.myModel(new ModelReference(Optional.empty(), + Optional.of(new UrlReference("mockPath/myPath.txt")), + Optional.of(new FileReference("mockUrl/myUrl.txt")))); + applier.applyPayload(ConfigPayload.fromInstance(config.build())); } private static class MockAcquirer implements ConfigTransformer.PathAcquirer { @Override public Path getPath(FileReference fileReference) { - return Path.of("resolvedPath", fileReference.value()); + return Path.of("mockPath", fileReference.value()); } } @@ -91,7 +41,7 @@ public class ConfigPayloadApplierTest { @Override public File waitFor(UrlReference urlReference, long timeout) { - return new File("resolvedUrl", urlReference.value()); + return new File("mockUrl", urlReference.value()); } } |