/** 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; }
CloudSdkServiceUserSettings.getInstance().setUserCancelledInstallation(true); ManagedCloudSdkServiceUiPresenter.getInstance().notifyManagedSdkJobCancellation(jobType); } else { logger.warn("Error while installing/updating managed Cloud SDK", throwable); ManagedCloudSdkServiceUiPresenter.getInstance() .notifyManagedSdkJobFailure(jobType, throwable.toString());
public void notifyManagedSdkJobFailure(ManagedSdkJobType jobType, String errorMessage) { String message = CloudSdkMessageBundle.message( "managedsdk.failure." + jobType.name().toLowerCase(), errorMessage); showNotification(message, NotificationType.ERROR); }
/** Creates managed SDK, installs if necessary, and checks for fatal errors. */ @VisibleForTesting void initManagedSdk() { try { managedCloudSdk = createManagedSdk(); // do not install SDK on activation if user cancelled installation once. if (!CloudSdkServiceUserSettings.getInstance().isUserCancelledInstallation()) { install(); } } catch (UnsupportedOsException ex) { logger.warn("Unsupported OS for Managed Cloud SDK", ex); updateStatus(SdkStatus.NOT_AVAILABLE); ManagedCloudSdkServiceUiPresenter.getInstance() .notifyManagedSdkJobFailure( ManagedSdkJobType.INSTALL, CloudSdkMessageBundle.message("managedsdk.unsupported.os")); } }
@Test public void upToDate_sdk_passes_valid_jobSuccessResult() throws ManagedSdkVerificationException { makeMockSdkInstalled(MOCK_SDK_PATH); when(mockManagedCloudSdk.isUpToDate()).thenReturn(true); sdkService.update(); verify(mockUiPresenter) .notifyManagedSdkJobSuccess(ManagedSdkJobType.UPDATE, ManagedSdkJobResult.UP_TO_DATE); }
}; Notification updateNotification = ManagedCloudSdkServiceUiPresenter.getInstance() .notifyManagedSdkUpdate(cancelListener, disableUpdateListener);
@Override public void onSuccess(ManagedSdkJobResult result) { logger.info( "Managed Google Cloud SDK successfully installed/updated at: " + getSdkHomePath()); updateStatus(SdkStatus.READY); if (result == ManagedSdkJobResult.PROCESSED) { ManagedCloudSdkUpdateService.getInstance().notifySdkUpdateCompleted(); String trackingEventAction; switch (jobType) { case UPDATE: trackingEventAction = GctTracking.MANAGED_SDK_SUCCESSFUL_UPDATE; break; default: trackingEventAction = GctTracking.MANAGED_SDK_SUCCESSFUL_INSTALL; break; } UsageTrackerService.getInstance().trackEvent(trackingEventAction).ping(); } ManagedCloudSdkServiceUiPresenter.getInstance().notifyManagedSdkJobSuccess(jobType, result); // no need to abstain from checking install status anymore after success. CloudSdkServiceUserSettings.getInstance().setUserCancelledInstallation(false); }
@Before public void setUp() { // add timer thread to one not to be checked for 'leaks' ThreadTracker.longRunningThreadCreated( ApplicationManager.getApplication(), ManagedCloudSdkUpdateService.SDK_UPDATER_THREAD_NAME); when(mockPluginInfoService.shouldEnable(GctFeature.MANAGED_SDK_UPDATE)).thenReturn(true); when(mockSdkServiceManager.getCloudSdkService()).thenReturn(mockSdkService); ManagedCloudSdkServiceUiPresenter.setInstance(mockUiPresenter); doReturn(mockClock).when(managedCloudSdkUpdateService).getClock(); doReturn(mockUiTimer).when(managedCloudSdkUpdateService).createUiTimer(anyInt()); when(mockUiPresenter.notifyManagedSdkUpdate(any(), any())).thenReturn(mockNotification); // directly execute scheduled tasks. doAnswer( invocationOnMock -> { ((TimerTask) invocationOnMock.getArgument(0)).run(); return null; }) .when(managedCloudSdkUpdateService) .schedule(any(), anyLong(), anyLong()); // directly call task assigned to UI timer. doAnswer( invocationOnMock -> { ((ActionListener) invocationOnMock.getArgument(0)) .actionPerformed(mock(ActionEvent.class)); return null; }) .when(mockUiTimer) .addActionListener(any()); CloudSdkServiceUserSettings.reset(); }
@Before public void setUp() throws UnsupportedOsException { // add timer thread to one not to be checked for 'leaks' ThreadTracker.longRunningThreadCreated( ApplicationManager.getApplication(), ManagedCloudSdkUpdateService.SDK_UPDATER_THREAD_NAME); doReturn(mockManagedCloudSdk).when(sdkService).createManagedSdk(); // TODO(ivanporty) remove once test logging system is done via CloudToolsRule sdkService.setLogger(new TestInMemoryLogger()); // make sure everything in test is done synchronously ExecutorService directExecutorService = MoreExecutors.newDirectExecutorService(); ThreadUtil.getInstance().setBackgroundExecutorService(directExecutorService); // run UI updates synchronously doAnswer( invocation -> { ((Runnable) invocation.getArgument(0)).run(); return null; }) .when(sdkService) .invokeOnApplicationUIThread(any()); // replace UI presenter for verifications ManagedCloudSdkServiceUiPresenter.setInstance(mockUiPresenter); when(mockUiPresenter.createProgressListener(any())).thenReturn(mockProgressListener); // init SDK, most tests require initialized state. sdkService.initManagedSdk(); }
@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 notification_shown_beforeUpdate() { when(mockSdkService.isUpToDate()).thenReturn(false); managedCloudSdkUpdateService.activate(); ApplicationManager.getApplication() .invokeAndWait(() -> verify(mockUiPresenter).notifyManagedSdkUpdate(any(), any())); }
@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 successful_install_showsNotification() { emulateMockSdkInstallationProcess(MOCK_SDK_PATH); sdkService.install(); verify(mockUiPresenter) .notifyManagedSdkJobSuccess(ManagedSdkJobType.INSTALL, ManagedSdkJobResult.PROCESSED); }
@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 notification_disableUpdates_updatesSettings() { when(mockSdkService.isUpToDate()).thenReturn(false); CloudSdkServiceUserSettings.getInstance().setEnableAutomaticUpdates(true); managedCloudSdkUpdateService.activate(); ApplicationManager.getApplication() .invokeAndWait( () -> { ArgumentCaptor<ActionListener> disableListener = ArgumentCaptor.forClass(ActionListener.class); verify(mockUiPresenter).notifyManagedSdkUpdate(any(), disableListener.capture()); disableListener.getValue().actionPerformed(mock(ActionEvent.class)); assertThat(CloudSdkServiceUserSettings.getInstance().isAutomaticUpdateEnabled()) .isFalse(); }); } }
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 sdkUpToDate_install_passes_valid_jobSuccessResult() { makeMockSdkInstalled(MOCK_SDK_PATH); sdkService.install(); verify(mockUiPresenter) .notifyManagedSdkJobSuccess(ManagedSdkJobType.INSTALL, ManagedSdkJobResult.UP_TO_DATE); }
@Test public void cancelled_update_showsNotification() throws Exception { makeMockSdkInstalled(MOCK_SDK_PATH); emulateMockSdkUpdateProcess(); SdkUpdater mockUpdater = mockManagedCloudSdk.newUpdater(); doThrow(new CancellationException()).when(mockUpdater).update(any(), any()); sdkService.update(); verify(mockUiPresenter).notifyManagedSdkJobCancellation(ManagedSdkJobType.UPDATE); }
public void notifyManagedSdkJobSuccess(ManagedSdkJobType jobType, ManagedSdkJobResult jobResult) { String message = CloudSdkMessageBundle.message("managedsdk.success." + jobType.name().toLowerCase()); switch (jobResult) { case PROCESSED: showNotification(message, NotificationType.INFORMATION); return; default: // do nothing, everything is up-to-date. } }
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; } }