@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); }
@Test public void interruptedInstall_showsCancelNotification() throws Exception { emulateMockSdkInstallationProcess(MOCK_SDK_PATH); SdkInstaller sdkInstaller = mockManagedCloudSdk.newInstaller(); when(sdkInstaller.install(any(), any())).thenThrow(new InterruptedException()); when(mockManagedCloudSdk.newInstaller()).thenReturn(sdkInstaller); sdkService.install(); verify(mockUiPresenter).notifyManagedSdkJobCancellation(ManagedSdkJobType.INSTALL); }
@Test public void interruptedInstall_status_notAvailable() throws Exception { emulateMockSdkInstallationProcess(MOCK_SDK_PATH); SdkInstaller sdkInstaller = mockManagedCloudSdk.newInstaller(); when(sdkInstaller.install(any(), any())).thenThrow(new InterruptedException()); when(mockManagedCloudSdk.newInstaller()).thenReturn(sdkInstaller); sdkService.install(); assertThat(sdkService.getStatus()).isEqualTo(SdkStatus.NOT_AVAILABLE); }
@Test public void failed_install_changesSdkStatus_inProgress() throws Exception { sdkService.addStatusUpdateListener(mockStatusUpdateListener); emulateMockSdkInstallationProcess(MOCK_SDK_PATH); SdkInstaller mockInstaller = mockManagedCloudSdk.newInstaller(); when(mockInstaller.install(any(), any())).thenThrow(new IOException("IO Error")); 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)); }
/** 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); }
/** 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); } }
@Test public void failed_install_showsErrorNotification() throws Exception { emulateMockSdkInstallationProcess(MOCK_SDK_PATH); SdkInstaller mockInstaller = mockManagedCloudSdk.newInstaller(); IOException ioException = new IOException("IO Error"); when(mockInstaller.install(any(), any())).thenThrow(ioException); sdkService.install(); verify(mockUiPresenter) .notifyManagedSdkJobFailure(ManagedSdkJobType.INSTALL, ioException.toString()); }
@Test public void failed_install_removesProgressIndicator() throws Exception { emulateMockSdkInstallationProcess(MOCK_SDK_PATH); SdkInstaller mockInstaller = mockManagedCloudSdk.newInstaller(); IOException ioException = new IOException("IO Error"); when(mockInstaller.install(any(), any())).thenThrow(ioException); sdkService.install(); verify(mockProgressListener, atLeastOnce()).done(); }
@Test public void cancelledInstall_stops_installing_onActivation() 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(); // cancelled, now attempt to do clean install process and activation. emulateMockSdkInstallationProcess(MOCK_SDK_PATH); sdkService.activate(); // install is not supposed to run on activation anymore. assertThat(sdkService.getStatus()).isEqualTo(SdkStatus.NOT_AVAILABLE); }
@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(); }
@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(); }
@Test public void testDownloadCloudSdkAction_update() throws ManagedSdkVerificationException, ManagedSdkVersionMismatchException, InterruptedException, CommandExecutionException, SdkInstallerException, IOException, CommandExitException { downloadCloudSdkTask.setManagedCloudSdk(managedCloudSdk); when(managedCloudSdk.isInstalled()).thenReturn(true); when(managedCloudSdk.isUpToDate()).thenReturn(false); downloadCloudSdkTask.downloadCloudSdkAction(); verify(managedCloudSdk, never()).newInstaller(); verify(managedCloudSdk, never()).newComponentInstaller(); verify(managedCloudSdk).newUpdater(); } }
@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 install_blocks_whenSDKReadOperations_running() throws Exception { emulateMockSdkInstallationProcess(MOCK_SDK_PATH); SdkInstaller mockInstaller = mock(SdkInstaller.class); when(mockManagedCloudSdk.newInstaller()).thenReturn(mockInstaller); CloudSdkServiceManager.getInstance().getSdkReadLock().lock(); try { // signal when install is about to start write operation. CountDownLatch waitForInstallToStart = new CountDownLatch(1); doAnswer( invocationOnMock -> { waitForInstallToStart.countDown(); return false; }) .when(mockManagedCloudSdk) .isInstalled(); Runnable installProcess = () -> sdkService.install(); Thread installProcessThread = new Thread(installProcess, "test-install-blocking-thread"); installProcessThread.start(); // do timed wait in case of test issues not to cause it to hang. waitForInstallToStart.await(100, TimeUnit.MILLISECONDS); installProcessThread.interrupt(); // finalize install() installProcessThread.join(); // since the write block was not available at the install, install() should never be called. verifyNoMoreInteractions(mockInstaller); } finally { CloudSdkServiceManager.getInstance().getSdkReadLock().unlock(); } }
/** 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; }
SdkInstaller installer = managedCloudSdk.newInstaller(); installer.install(progressListener, consoleListener);
managedCloudSdk.newInstaller().install(progressListener, consoleListener);