diff --git a/vscode-extensions/commons/commons-cf/src/main/java/org/springframework/ide/vscode/commons/cloudfoundry/client/CloudFoundryClientFactory.java b/vscode-extensions/commons/commons-cf/src/main/java/org/springframework/ide/vscode/commons/cloudfoundry/client/CloudFoundryClientFactory.java index 22c96ec7e..c650c780f 100644 --- a/vscode-extensions/commons/commons-cf/src/main/java/org/springframework/ide/vscode/commons/cloudfoundry/client/CloudFoundryClientFactory.java +++ b/vscode-extensions/commons/commons-cf/src/main/java/org/springframework/ide/vscode/commons/cloudfoundry/client/CloudFoundryClientFactory.java @@ -13,7 +13,5 @@ package org.springframework.ide.vscode.commons.cloudfoundry.client; import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFClientParams; public interface CloudFoundryClientFactory { - ClientRequests getClient(CFClientParams params, ClientTimeouts timeouts) throws Exception; - } \ No newline at end of file diff --git a/vscode-extensions/vscode-manifest-yaml/pom.xml b/vscode-extensions/vscode-manifest-yaml/pom.xml index 454ff4e0e..01d5eb6e5 100644 --- a/vscode-extensions/vscode-manifest-yaml/pom.xml +++ b/vscode-extensions/vscode-manifest-yaml/pom.xml @@ -4,7 +4,7 @@ 4.0.0 vscode-manifest-yaml jar - + org.springframework.ide.vscode commons-parent @@ -12,11 +12,11 @@ ../commons/pom.xml 0.0.2-SNAPSHOT - + 0.0.1-SNAPSHOT - + @@ -25,7 +25,7 @@ file://${basedir}/repo - + distribution-repository @@ -54,6 +54,12 @@ ${dependencies.version} + + org.mockito + mockito-all + 1.10.19 + test + org.springframework.ide.vscode language-server-test-harness diff --git a/vscode-extensions/vscode-manifest-yaml/src/main/java/org/springframework/ide/vscode/manifest/yaml/ManifestYamlLanguageServer.java b/vscode-extensions/vscode-manifest-yaml/src/main/java/org/springframework/ide/vscode/manifest/yaml/ManifestYamlLanguageServer.java index 232499d01..a262ed989 100644 --- a/vscode-extensions/vscode-manifest-yaml/src/main/java/org/springframework/ide/vscode/manifest/yaml/ManifestYamlLanguageServer.java +++ b/vscode-extensions/vscode-manifest-yaml/src/main/java/org/springframework/ide/vscode/manifest/yaml/ManifestYamlLanguageServer.java @@ -45,16 +45,22 @@ import org.yaml.snakeyaml.Yaml; public class ManifestYamlLanguageServer extends SimpleLanguageServer { - + private Yaml yaml = new Yaml(); private YamlSchema schema; private CFTargetCache cfTargetCache; - + private final CloudFoundryClientFactory cfClientFactory; + public ManifestYamlLanguageServer() { + this(DefaultCloudFoundryClientFactoryV2.INSTANCE); + } + + public ManifestYamlLanguageServer(CloudFoundryClientFactory cfClientFactory) { + this.cfClientFactory = cfClientFactory; SimpleTextDocumentService documents = getTextDocumentService(); - + YamlASTProvider parser = new YamlParser(yaml); - + Callable> buildPacksProvider = getBuildpacksProvider(); Callable> servicesProvider = getServicesProvider(); @@ -73,7 +79,7 @@ public class ManifestYamlLanguageServer extends SimpleLanguageServer { TextDocument doc = params.getDocument(); validateWith(doc, engine); }); - + // workspace.onDidChangeConfiguraton(settings -> { // System.out.println("Config changed: "+params); // Integer val = settings.getInt("languageServerExample", "maxNumberOfProblems"); @@ -84,16 +90,16 @@ public class ManifestYamlLanguageServer extends SimpleLanguageServer { // } // } // }); - + documents.onCompletion(completionEngine::getCompletions); documents.onCompletionResolve(completionEngine::resolveCompletion); documents.onHover(hoverEngine ::getHover); } - + private CFTargetCache getCfTargetCache() { if (cfTargetCache == null) { ClientParamsProvider paramsProvider = new CfCliParamsProvider(); - CloudFoundryClientFactory clientFactory = DefaultCloudFoundryClientFactoryV2.INSTANCE; + CloudFoundryClientFactory clientFactory = cfClientFactory; cfTargetCache = new CFTargetCache(paramsProvider, clientFactory, new ClientTimeouts()); } return cfTargetCache; @@ -102,22 +108,22 @@ public class ManifestYamlLanguageServer extends SimpleLanguageServer { private Callable> getBuildpacksProvider() { return new ManifestYamlCFBuildpacksProvider(getCfTargetCache()); } - + private Callable> getServicesProvider() { return new ManifestYamlCFServicesProvider(getCfTargetCache()); } - + @Override protected ServerCapabilities getServerCapabilities() { ServerCapabilities c = new ServerCapabilities(); - + c.setTextDocumentSync(TextDocumentSyncKind.Incremental); c.setHoverProvider(true); - + CompletionOptions completionProvider = new CompletionOptions(); completionProvider.setResolveProvider(false); c.setCompletionProvider(completionProvider); - + return c; } } diff --git a/vscode-extensions/vscode-manifest-yaml/src/test/java/org/springframework/ide/vscode/manifest/yaml/ManifestYamlEditorTest.java b/vscode-extensions/vscode-manifest-yaml/src/test/java/org/springframework/ide/vscode/manifest/yaml/ManifestYamlEditorTest.java index dec041c77..4da171cda 100644 --- a/vscode-extensions/vscode-manifest-yaml/src/test/java/org/springframework/ide/vscode/manifest/yaml/ManifestYamlEditorTest.java +++ b/vscode-extensions/vscode-manifest-yaml/src/test/java/org/springframework/ide/vscode/manifest/yaml/ManifestYamlEditorTest.java @@ -10,15 +10,24 @@ *******************************************************************************/ package org.springframework.ide.vscode.manifest.yaml; +import static org.junit.Assert.*; + +import static org.mockito.Mockito.*; + +import java.io.IOException; + import org.junit.Before; import org.junit.Ignore; import org.junit.Test; +import org.springframework.ide.vscode.commons.cloudfoundry.client.ClientRequests; +import org.springframework.ide.vscode.commons.cloudfoundry.client.CloudFoundryClientFactory; import org.springframework.ide.vscode.languageserver.testharness.Editor; import org.springframework.ide.vscode.languageserver.testharness.LanguageServerHarness; public class ManifestYamlEditorTest { LanguageServerHarness harness; + MockCloudfoundry cfClientFactory = new MockCloudfoundry(); @Before public void setup() throws Exception { harness = new LanguageServerHarness(ManifestYamlLanguageServer::new); @@ -674,6 +683,38 @@ public class ManifestYamlEditorTest { } + @Test + @Ignore + public void noReconcileErrorsWhenCFFactoryThrows() throws Exception { + cfClientFactory.throwException(new IOException("Can't create a client!")); + Editor editor = harness.newEditor( + "applications:\n" + + "- name: foo\n" + + " buildpack: bad-buildpack\n" + + " services:\n" + + " - bad-service\n" + + " bogus: bad" //a token error to make sure reconciler is actually running! + ); + editor.assertProblems("bogus|Unknown property"); + } + + @Test + @Ignore + public void noReconcileErrorsWhenClientThrows() throws Exception { + ClientRequests cfClient = cfClientFactory.client; + when(cfClient.getBuildpacks()).thenThrow(new IOException("Can't get buildpacks")); + when(cfClient.getServices()).thenThrow(new IOException("Can't get services")); + Editor editor = harness.newEditor( + "applications:\n" + + "- name: foo\n" + + " buildpack: bad-buildpack\n" + + " services:\n" + + " - bad-service\n" + + " bogus: bad" //a token error to make sure reconciler is actually running! + ); + editor.assertProblems("bogus|Unknown property"); + } + ////////////////////////////////////////////////////////////////////////////// private void assertCompletions(String textBefore, String... textAfter) throws Exception { diff --git a/vscode-extensions/vscode-manifest-yaml/src/test/java/org/springframework/ide/vscode/manifest/yaml/MockCloudfoundry.java b/vscode-extensions/vscode-manifest-yaml/src/test/java/org/springframework/ide/vscode/manifest/yaml/MockCloudfoundry.java new file mode 100644 index 000000000..7ae0564c1 --- /dev/null +++ b/vscode-extensions/vscode-manifest-yaml/src/test/java/org/springframework/ide/vscode/manifest/yaml/MockCloudfoundry.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright (c) 2017 Pivotal, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Pivotal, Inc. - initial API and implementation + *******************************************************************************/ +package org.springframework.ide.vscode.manifest.yaml; + +import org.mockito.Mockito; +import org.springframework.ide.vscode.commons.cloudfoundry.client.ClientRequests; +import org.springframework.ide.vscode.commons.cloudfoundry.client.ClientTimeouts; +import org.springframework.ide.vscode.commons.cloudfoundry.client.CloudFoundryClientFactory; +import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFClientParams; + +public class MockCloudfoundry implements CloudFoundryClientFactory { + + public final ClientRequests client = Mockito.mock(ClientRequests.class); + private Exception toThrow = null; + + @Override + public ClientRequests getClient(CFClientParams params, ClientTimeouts timeouts) throws Exception { + if (toThrow==null) { + return client; + } + throw toThrow; + } + + /** + * Makes the factory throw the given exception when its 'getClient' method is called. + */ + public void throwException(Exception e) { + toThrow = e; + } + +}