ConsoleListener consoleListener = new CloudSdkDownloaderConsoleListener(log); if (!managedCloudSdk.isInstalled()) { managedCloudSdk.newInstaller().install(progressListener, consoleListener); && !managedCloudSdk.hasComponent(SdkComponent.APP_ENGINE_JAVA)) { managedCloudSdk .newComponentInstaller() .installComponent(SdkComponent.APP_ENGINE_JAVA, progressListener, consoleListener); if (!managedCloudSdk.isUpToDate()) { managedCloudSdk.newUpdater().update(progressListener, consoleListener); return managedCloudSdk.getSdkHome(); } catch (IOException | SdkInstallerException
@VisibleForTesting ManagedCloudSdk createManagedSdk() throws UnsupportedOsException { return ManagedCloudSdk.newManagedSdk(); }
/** Mocks managed SDK as if installed and having App Engine Component. */ private void makeMockSdkInstalled(Path mockSdkPath) { try { when(mockManagedCloudSdk.isInstalled()).thenReturn(true); when(mockManagedCloudSdk.hasComponent(SdkComponent.APP_ENGINE_JAVA)).thenReturn(true); when(mockManagedCloudSdk.getSdkHome()).thenReturn(mockSdkPath); } catch (Exception ex) { // shouldn't happen in the tests. throw new AssertionError(ex); } }
/** Mocks out-of-date SDK and update process. */ private void emulateMockSdkUpdateProcess() { try { when(mockManagedCloudSdk.isUpToDate()).thenReturn(false); SdkUpdater mockUpdater = mock(SdkUpdater.class); when(mockManagedCloudSdk.newUpdater()).thenReturn(mockUpdater); } catch (Exception ex) { // shouldn't happen in the tests. throw new AssertionError(ex); } } }
/** Mocks successful installation process with all steps included (SDK, App Engine Java). */ private void emulateMockSdkInstallationProcess(Path mockSdkPath) { try { when(mockManagedCloudSdk.isInstalled()).thenReturn(false); SdkInstaller mockInstaller = mock(SdkInstaller.class); when(mockManagedCloudSdk.newInstaller()).thenReturn(mockInstaller); when(mockInstaller.install(any(), any())).thenReturn(mockSdkPath); when(mockManagedCloudSdk.hasComponent(SdkComponent.APP_ENGINE_JAVA)).thenReturn(false); SdkComponentInstaller mockComponentInstaller = mock(SdkComponentInstaller.class); when(mockManagedCloudSdk.newComponentInstaller()).thenReturn(mockComponentInstaller); when(mockManagedCloudSdk.getSdkHome()).thenReturn(mockSdkPath); } catch (Exception ex) { // shouldn't happen in the tests. throw new AssertionError(ex); } }
private ManagedSdkJobResult updateManagedSdk() throws Exception { if (safeCheckSdkStatus(() -> managedCloudSdk.isInstalled()) && !safeCheckSdkStatus(() -> managedCloudSdk.isUpToDate())) { ConsoleListener sdkUpdateListener = logger::debug; progressListener = ManagedCloudSdkServiceUiPresenter.getInstance().createProgressListener(this); notifySdkProcessingStarted(); executeWithSdkWriteLock( () -> managedCloudSdk.newUpdater().update(progressListener, sdkUpdateListener)); return ManagedSdkJobResult.PROCESSED; } else { return ManagedSdkJobResult.UP_TO_DATE; } }
/** Setup DownloadCloudSdkTaskTest. */ @Before public void setup() { Project tempProject = ProjectBuilder.builder().build(); downloadCloudSdkTask = tempProject.getTasks().create("tempDownloadTask", DownloadCloudSdkTask.class); when(managedCloudSdk.newInstaller()).thenReturn(installer); when(managedCloudSdk.newComponentInstaller()).thenReturn(componentInstaller); when(managedCloudSdk.newUpdater()).thenReturn(updater); }
@Test public void testDownloadCloudSdkAction_skipInstallComponent() throws ManagedSdkVerificationException, ManagedSdkVersionMismatchException, InterruptedException, CommandExecutionException, SdkInstallerException, IOException, CommandExitException { downloadCloudSdkTask.setManagedCloudSdk(managedCloudSdk); downloadCloudSdkTask.requiresAppEngineJava(false); when(managedCloudSdk.isInstalled()).thenReturn(true); downloadCloudSdkTask.downloadCloudSdkAction(); verify(managedCloudSdk, never()).newInstaller(); verify(managedCloudSdk, never()).newComponentInstaller(); }
/** Installs core managed SDK if needed and returns its path if successful. */ private ManagedSdkJobResult installSdk() throws Exception { if (!safeCheckSdkStatus(() -> managedCloudSdk.isInstalled())) { ConsoleListener sdkConsoleListener = logger::debug; progressListener = ManagedCloudSdkServiceUiPresenter.getInstance().createProgressListener(this); notifySdkProcessingStarted(); executeWithSdkWriteLock( () -> managedCloudSdk.newInstaller().install(progressListener, sdkConsoleListener)); return ManagedSdkJobResult.PROCESSED; } return ManagedSdkJobResult.UP_TO_DATE; }
@Test public void cancelledInstall_showsCancelNotification() throws Exception { emulateMockSdkInstallationProcess(MOCK_SDK_PATH); SdkInstaller sdkInstaller = mockManagedCloudSdk.newInstaller(); when(sdkInstaller.install(any(), any())).thenThrow(new CancellationException()); when(mockManagedCloudSdk.newInstaller()).thenReturn(sdkInstaller); sdkService.install(); verify(mockUiPresenter).notifyManagedSdkJobCancellation(ManagedSdkJobType.INSTALL); }
private ManagedSdkJobResult installAppEngineJavaComponent() throws Exception { if (!safeCheckSdkStatus(() -> managedCloudSdk.hasComponent(SdkComponent.APP_ENGINE_JAVA))) { ConsoleListener appEngineConsoleListener = logger::debug; progressListener = ManagedCloudSdkServiceUiPresenter.getInstance().createProgressListener(this); notifySdkProcessingStarted(); executeWithSdkWriteLock( () -> managedCloudSdk .newComponentInstaller() .installComponent( SdkComponent.APP_ENGINE_JAVA, progressListener, appEngineConsoleListener)); return ManagedSdkJobResult.PROCESSED; } else { return ManagedSdkJobResult.UP_TO_DATE; } }
@Test public void testNewManagedSdk_null() throws UnsupportedOsException, BadCloudSdkVersionException { // There's no way of testing for direct ManagedCloudSdk equality, so compare home paths ManagedCloudSdk sdk = new ManagedCloudSdkFactory(null).newManagedSdk(); Assert.assertEquals(ManagedCloudSdk.newManagedSdk().getSdkHome(), sdk.getSdkHome()); }
@Test public void failed_update_invalidSdk_makesSdkStatus_notAvailable() throws Exception { makeMockSdkInstalled(MOCK_SDK_PATH); emulateMockSdkUpdateProcess(); SdkUpdater mockUpdater = mockManagedCloudSdk.newUpdater(); doThrow(new CommandExitException(-1, "")).when(mockUpdater).update(any(), any()); // initially SDK is installed, then update breaks SDK when(mockManagedCloudSdk.isInstalled()).thenReturn(true).thenReturn(false); sdkService.addStatusUpdateListener(mockStatusUpdateListener); sdkService.update(); ArgumentCaptor<SdkStatus> statusCaptor = ArgumentCaptor.forClass(SdkStatus.class); verify(mockStatusUpdateListener, times(2)).onSdkStatusChange(any(), statusCaptor.capture()); assertThat(statusCaptor.getAllValues()) .isEqualTo(Arrays.asList(SdkStatus.INSTALLING, SdkStatus.NOT_AVAILABLE)); }
@Test public void cancelled_update_keepsSdkStatus_available() throws Exception { makeMockSdkInstalled(MOCK_SDK_PATH); emulateMockSdkUpdateProcess(); SdkUpdater mockUpdater = mockManagedCloudSdk.newUpdater(); doThrow(new CancellationException()).when(mockUpdater).update(any(), any()); sdkService.addStatusUpdateListener(mockStatusUpdateListener); sdkService.update(); ArgumentCaptor<SdkStatus> statusCaptor = ArgumentCaptor.forClass(SdkStatus.class); verify(mockStatusUpdateListener, times(2)).onSdkStatusChange(any(), statusCaptor.capture()); assertThat(statusCaptor.getAllValues()) .isEqualTo(Arrays.asList(SdkStatus.INSTALLING, SdkStatus.READY)); }
public boolean isUpToDate() { try { return managedCloudSdk.isUpToDate(); } catch (Exception e) { // we ignore the exception and just assume SDK is either not up-to-date or in invalid state. return false; } }
@Nullable @Override public Path getSdkHomePath() { switch (sdkStatus) { case READY: return managedCloudSdk.getSdkHome(); default: return null; } }
private void checkSdkStatusAfterFailedUpdate() { try { if (managedCloudSdk.isInstalled()) { updateStatus(SdkStatus.READY); } else { updateStatus(SdkStatus.NOT_AVAILABLE); } } catch (Exception ex) { updateStatus(SdkStatus.NOT_AVAILABLE); } }
@Test public void failed_install_appEngineException_changesSdkStatus_inProgress() throws Exception { sdkService.addStatusUpdateListener(mockStatusUpdateListener); emulateMockSdkInstallationProcess(MOCK_SDK_PATH); SdkComponentInstaller mockComponentInstaller = mockManagedCloudSdk.newComponentInstaller(); doThrow(new CommandExecutionException(new UnsupportedOperationException())) .when(mockComponentInstaller) .installComponent(any(), any(), any()); sdkService.install(); ArgumentCaptor<SdkStatus> statusCaptor = ArgumentCaptor.forClass(SdkStatus.class); verify(mockStatusUpdateListener, times(2)).onSdkStatusChange(any(), statusCaptor.capture()); assertThat(statusCaptor.getAllValues()) .isEqualTo(Arrays.asList(SdkStatus.INSTALLING, SdkStatus.NOT_AVAILABLE)); }
@Test public void testDownloadCloudSdkAction_installComponent() throws ManagedSdkVerificationException, ManagedSdkVersionMismatchException, InterruptedException, CommandExecutionException, SdkInstallerException, IOException, CommandExitException { downloadCloudSdkTask.setManagedCloudSdk(managedCloudSdk); downloadCloudSdkTask.requiresAppEngineJava(true); when(managedCloudSdk.isInstalled()).thenReturn(true); when(managedCloudSdk.hasComponent(SdkComponent.APP_ENGINE_JAVA)).thenReturn(false); downloadCloudSdkTask.downloadCloudSdkAction(); verify(managedCloudSdk, never()).newInstaller(); verify(managedCloudSdk).newComponentInstaller(); }
@Test public void testDownloadCloudSdkAction_install() throws ManagedSdkVerificationException, ManagedSdkVersionMismatchException, InterruptedException, CommandExecutionException, SdkInstallerException, IOException, CommandExitException { downloadCloudSdkTask.setManagedCloudSdk(managedCloudSdk); when(managedCloudSdk.isInstalled()).thenReturn(false); downloadCloudSdkTask.downloadCloudSdkAction(); verify(managedCloudSdk).newInstaller(); }