From 08a147c9a5ca317ca37bb0ef21eead2b199becaa Mon Sep 17 00:00:00 2001 From: Kris De Volder Date: Fri, 27 Jan 2017 14:41:38 -0800 Subject: [PATCH] Some cleanups to the MockCloudfoundry (but more needed) --- .../manifest/yaml/ManifestYamlEditorTest.java | 21 ++++------ .../manifest/yaml/MockCloudfoundry.java | 42 +++++++++++++------ 2 files changed, 39 insertions(+), 24 deletions(-) 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 afe8fb502..6aba1dc30 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 @@ -12,7 +12,7 @@ package org.springframework.ide.vscode.manifest.yaml; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import java.io.IOException; @@ -34,15 +34,11 @@ import com.google.common.collect.ImmutableList; public class ManifestYamlEditorTest { - CFClientParams DEFAULT_PARAMS = new CFClientParams("test.io", "testuser", - CFCredentials.fromRefreshToken("refreshtoken"), false); - ClientParamsProvider cfParamsProvider = () -> ImmutableList.of(DEFAULT_PARAMS); - LanguageServerHarness harness; - MockCloudfoundry cfClientFactory = new MockCloudfoundry(); + MockCloudfoundry cloudfoundry = new MockCloudfoundry(); @Before public void setup() throws Exception { - harness = new LanguageServerHarness(()-> new ManifestYamlLanguageServer(cfClientFactory, cfParamsProvider)); + harness = new LanguageServerHarness(()-> new ManifestYamlLanguageServer(cloudfoundry.factory, cloudfoundry.paramsProvider)); harness.intialize(null); } @@ -766,7 +762,8 @@ public class ManifestYamlEditorTest { @Test public void noReconcileErrorsWhenCFFactoryThrows() throws Exception { - cfClientFactory.throwException(new IOException("Can't create a client!")); + reset(cloudfoundry.factory); + when(cloudfoundry.factory.getClient(any(), any())).thenThrow(new IOException("Can't create a client!")); Editor editor = harness.newEditor( "applications:\n" + "- name: foo\n" + @@ -780,7 +777,7 @@ public class ManifestYamlEditorTest { @Test public void noReconcileErrorsWhenClientThrows() throws Exception { - ClientRequests cfClient = cfClientFactory.client; + ClientRequests cfClient = cloudfoundry.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( @@ -801,7 +798,7 @@ public class ManifestYamlEditorTest { // These tests pass when running on an Oracle JVM. @Test public void reconcileCFService() throws Exception { - ClientRequests cfClient = cfClientFactory.client; + ClientRequests cfClient = cloudfoundry.client; CFServiceInstance service = Mockito.mock(CFServiceInstance.class); when(service.getName()).thenReturn("myservice"); when(cfClient.getServices()).thenReturn(ImmutableList.of(service)); @@ -818,7 +815,7 @@ public class ManifestYamlEditorTest { @Test public void reconcileShowsWarningOnUnknownService() throws Exception { - ClientRequests cfClient = cfClientFactory.client; + ClientRequests cfClient = cloudfoundry.client; CFServiceInstance service = Mockito.mock(CFServiceInstance.class); when(service.getName()).thenReturn("myservice"); when(cfClient.getServices()).thenReturn(ImmutableList.of(service)); @@ -839,7 +836,7 @@ public class ManifestYamlEditorTest { @Ignore @Test public void reconcileShowsWarningOnNoService() throws Exception { - ClientRequests cfClient = cfClientFactory.client; + ClientRequests cfClient = cloudfoundry.client; when(cfClient.getServices()).thenReturn(ImmutableList.of()); Editor editor = harness.newEditor( "applications:\n" + 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 index 7ae0564c1..cc8f42ff8 100644 --- 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 @@ -10,30 +10,48 @@ *******************************************************************************/ package org.springframework.ide.vscode.manifest.yaml; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + 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; +import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFCredentials; +import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.ClientParamsProvider; +import org.springframework.ide.vscode.commons.util.ExceptionUtil; -public class MockCloudfoundry implements CloudFoundryClientFactory { +import com.google.common.collect.ImmutableList; - public final ClientRequests client = Mockito.mock(ClientRequests.class); - private Exception toThrow = null; +public class MockCloudfoundry { - @Override - public ClientRequests getClient(CFClientParams params, ClientTimeouts timeouts) throws Exception { - if (toThrow==null) { - return client; + public final CFClientParams DEFAULT_PARAMS = new CFClientParams("test.io", "testuser", + CFCredentials.fromRefreshToken("refreshtoken"), false); + + public final CloudFoundryClientFactory factory = mock(CloudFoundryClientFactory.class); + public final ClientRequests client = mock(ClientRequests.class); + public final ClientParamsProvider paramsProvider = mock(ClientParamsProvider.class); + + public MockCloudfoundry() { + try { + //program some default behavior into mocks... most tests will use this. + //other tests should 'reset' the mocks and reprogram them as needed. + when(factory.getClient(any(), any())).thenReturn(client); + when(paramsProvider.getParams()).thenReturn(ImmutableList.of(DEFAULT_PARAMS)); + } catch (Exception e) { + throw ExceptionUtil.unchecked(e); } - throw toThrow; } /** - * Makes the factory throw the given exception when its 'getClient' method is called. + * Reset the mocks. Use this if the default's programmed into the mocks don't suite your test case. + *

+ * Note: you may also choose to call {@link Mockito}.mock directly if you do not want to + * reset all of the mocks. */ - public void throwException(Exception e) { - toThrow = e; + public void reset() throws Exception { + Mockito.reset(factory, client, paramsProvider); } }