Some cleanups to the MockCloudfoundry (but more needed)

This commit is contained in:
Kris De Volder
2017-01-27 14:41:38 -08:00
parent ae8413e900
commit 08a147c9a5
2 changed files with 39 additions and 24 deletions

View File

@@ -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" +

View File

@@ -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.
* <p>
* 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);
}
}