From f7bde8559b376927224f85ae09ab5af33593fa9f Mon Sep 17 00:00:00 2001 From: Kris De Volder Date: Thu, 27 Apr 2017 14:43:39 -0700 Subject: [PATCH] Improve reconciler errors around wrong domains --- .../manifest/yaml/ManifestYmlSchema.java | 15 +++----- .../yaml/ManifestYmlValueParsers.java | 17 +++++++++ .../manifest/yaml/RouteValueParser.java | 2 +- .../manifest/yaml/ManifestYamlEditorTest.java | 36 +++++++++++++++++-- 4 files changed, 57 insertions(+), 13 deletions(-) diff --git a/headless-services/manifest-yaml-language-server/src/main/java/org/springframework/ide/vscode/manifest/yaml/ManifestYmlSchema.java b/headless-services/manifest-yaml-language-server/src/main/java/org/springframework/ide/vscode/manifest/yaml/ManifestYmlSchema.java index 529509145..d65218a71 100644 --- a/headless-services/manifest-yaml-language-server/src/main/java/org/springframework/ide/vscode/manifest/yaml/ManifestYmlSchema.java +++ b/headless-services/manifest-yaml-language-server/src/main/java/org/springframework/ide/vscode/manifest/yaml/ManifestYmlSchema.java @@ -14,8 +14,6 @@ import java.util.Collection; import java.util.Set; import java.util.concurrent.Callable; -import org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileException; -import org.springframework.ide.vscode.commons.util.EnumValueParser; import org.springframework.ide.vscode.commons.util.IntegerRange; import org.springframework.ide.vscode.commons.util.Renderable; import org.springframework.ide.vscode.commons.util.Renderables; @@ -73,18 +71,15 @@ public class ManifestYmlSchema implements YamlSchema { } YAtomicType t_stack = f.yatomic("Stack"); - t_stack.addHintProvider(stacksProvider); - t_stack.parseWith(new EnumValueParser(t_stack.toString(), YTypeFactory.valuesFromHintProvider(stacksProvider)) { - @Override - protected Exception errorOnParse(String message) { - return new ReconcileException(message, ManifestYamlSchemaProblemsTypes.UNKNOWN_STACK_PROBLEM); - } - }); + if (stacksProvider!=null) { + t_stack.addHintProvider(stacksProvider); + t_stack.parseWith(ManifestYmlValueParsers.fromValueHints(stacksProvider, t_stack, ManifestYamlSchemaProblemsTypes.UNKNOWN_STACK_PROBLEM)); + } YAtomicType t_domain = f.yatomic("Domain"); - if (domainsProvider != null) { t_domain.addHintProvider(domainsProvider); + t_domain.parseWith(ManifestYmlValueParsers.fromValueHints(domainsProvider, t_domain, ManifestYamlSchemaProblemsTypes.UNKNOWN_DOMAIN_PROBLEM)); } YAtomicType t_service = f.yatomic("Service"); diff --git a/headless-services/manifest-yaml-language-server/src/main/java/org/springframework/ide/vscode/manifest/yaml/ManifestYmlValueParsers.java b/headless-services/manifest-yaml-language-server/src/main/java/org/springframework/ide/vscode/manifest/yaml/ManifestYmlValueParsers.java index b04a47124..6849f02df 100644 --- a/headless-services/manifest-yaml-language-server/src/main/java/org/springframework/ide/vscode/manifest/yaml/ManifestYmlValueParsers.java +++ b/headless-services/manifest-yaml-language-server/src/main/java/org/springframework/ide/vscode/manifest/yaml/ManifestYmlValueParsers.java @@ -10,10 +10,18 @@ *******************************************************************************/ package org.springframework.ide.vscode.manifest.yaml; +import java.util.Collection; import java.util.Set; +import java.util.concurrent.Callable; +import org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemType; +import org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileException; import org.springframework.ide.vscode.commons.util.Assert; +import org.springframework.ide.vscode.commons.util.EnumValueParser; import org.springframework.ide.vscode.commons.util.ValueParser; +import org.springframework.ide.vscode.commons.yaml.schema.YTypeFactory; +import org.springframework.ide.vscode.commons.yaml.schema.YTypeFactory.YAtomicType; +import org.springframework.ide.vscode.commons.yaml.schema.YValueHint; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; @@ -87,4 +95,13 @@ public class ManifestYmlValueParsers { }; } + public static EnumValueParser fromValueHints(Callable> hintProvider, YAtomicType type, ProblemType problemType) { + return new EnumValueParser(type.toString(), YTypeFactory.valuesFromHintProvider(hintProvider)) { + @Override + protected Exception errorOnParse(String message) { + return new ReconcileException(message, problemType); + } + }; + } + } diff --git a/headless-services/manifest-yaml-language-server/src/main/java/org/springframework/ide/vscode/manifest/yaml/RouteValueParser.java b/headless-services/manifest-yaml-language-server/src/main/java/org/springframework/ide/vscode/manifest/yaml/RouteValueParser.java index 67e329516..440957a57 100644 --- a/headless-services/manifest-yaml-language-server/src/main/java/org/springframework/ide/vscode/manifest/yaml/RouteValueParser.java +++ b/headless-services/manifest-yaml-language-server/src/main/java/org/springframework/ide/vscode/manifest/yaml/RouteValueParser.java @@ -63,7 +63,7 @@ public class RouteValueParser extends RegexpParser { } if (!cloudDomains.contains(route.getDomain())) { String hostDomain = matcher.group(1); - throw new ReconcileException("Unknown domain", ManifestYamlSchemaProblemsTypes.UNKNOWN_DOMAIN_PROBLEM, hostDomain.lastIndexOf(route.getDomain()), hostDomain.length()); + throw new ReconcileException("Unknown 'Domain'. Valid domains are: "+cloudDomains, ManifestYamlSchemaProblemsTypes.UNKNOWN_DOMAIN_PROBLEM, hostDomain.lastIndexOf(route.getDomain()), hostDomain.length()); } return route; } catch (ConnectionException | NoTargetsException e) { diff --git a/headless-services/manifest-yaml-language-server/src/test/java/org/springframework/ide/vscode/manifest/yaml/ManifestYamlEditorTest.java b/headless-services/manifest-yaml-language-server/src/test/java/org/springframework/ide/vscode/manifest/yaml/ManifestYamlEditorTest.java index b621f0e04..4327525c6 100644 --- a/headless-services/manifest-yaml-language-server/src/test/java/org/springframework/ide/vscode/manifest/yaml/ManifestYamlEditorTest.java +++ b/headless-services/manifest-yaml-language-server/src/test/java/org/springframework/ide/vscode/manifest/yaml/ManifestYamlEditorTest.java @@ -606,6 +606,28 @@ public class ManifestYamlEditorTest { assertEquals("an-org : a-space [test.io]", c.getDocumentation()); } + + @Test public void domainReconcile() throws Exception { + List domains = ImmutableList.of(mockDomain("one.com"), mockDomain("two.com")); + when(cloudfoundry.client.getDomains()).thenReturn(domains); + Editor editor; + Diagnostic p; + + editor = harness.newEditor( + "domain: bad.com" + ); + p = editor.assertProblems("bad.com|unknown 'Domain'. Valid values are: [one.com, two.com]").get(0); + assertEquals(DiagnosticSeverity.Warning, p.getSeverity()); + + editor= harness.newEditor( + "domains:\n" + + "- one.com\n" + + "- bad.com\n" + + "- two.com" + ); + editor.assertProblems("bad.com|unknown 'Domain'. Valid values are: [one.com, two.com]"); + } + @Test public void stacksReconcile() throws Exception { List stacks = ImmutableList.of( mockStack("linux"), mockStack("windows") @@ -637,6 +659,8 @@ public class ManifestYamlEditorTest { @Test public void reconcileDuplicateKeys() throws Exception { + ImmutableList domains = ImmutableList.of(mockDomain("pivotal.io"), mockDomain("otherdomain.org")); + when(cloudfoundry.client.getDomains()).thenReturn(domains); Editor editor = harness.newEditor( "#comment\n" + "applications:\n" + @@ -1153,6 +1177,8 @@ public class ManifestYamlEditorTest { @Test public void reconcileRoute_Advanced() throws Exception { + ImmutableList domains = ImmutableList.of(mockDomain("somedomain.com")); + when(cloudfoundry.client.getDomains()).thenReturn(domains); Editor editor = harness.newEditor( "applications:\n" + "- name: foo\n" + @@ -1176,11 +1202,17 @@ public class ManifestYamlEditorTest { "- name: foo\n" + " routes:\n" + " - route: host.springsource.org\n"); - editor.assertProblems("springsource.org|Unknown domain"); + editor.assertProblems("springsource.org|Unknown 'Domain'. Valid domains are: [somedomain.com]"); problem = editor.assertProblem("springsource.org"); assertEquals(DiagnosticSeverity.Warning, problem.getSeverity()); } + private CFDomain mockDomain(String name) { + CFDomain domain = mock(CFDomain.class); + when(domain.getName()).thenReturn(name); + return domain; + } + @Test public void reconcileRouteValidDomain() throws Exception { ClientRequests cfClient = cloudfoundry.client; @@ -1194,7 +1226,7 @@ public class ManifestYamlEditorTest { " - route: host.springsource.org\n"); editor.assertProblems(); } - + @Test public void dashedContentAssistForServices() throws Exception { Editor editor; CFServiceInstance service = mock(CFServiceInstance.class);