@Before public void setUp() { when(mockCloudSdkServiceManager.getCloudSdkService()).thenReturn(mockSdkService); // empty error messages by default when(mockStatusHandler.getErrorMessage(any())).thenReturn(""); }
@Test public void waitFor_installingSdk_then_readySdk_noErrors() throws InterruptedException { mockSdkStatusChange(SdkStatus.INSTALLING, SdkStatus.READY); cloudSdkServiceManager.blockUntilSdkReady(mockProject, "", mockStatusHandler); ApplicationManager.getApplication() .invokeAndWait(() -> verify(mockStatusHandler, never()).onError(any())); }
@Override public void run(@NotNull ProgressIndicator indicator) { try { while (installationCompletionLatch.getCount() > 0) { // wait interruptibility to check for user cancel each second. installationCompletionLatch.await(1, SECONDS); if (checkIfCancelled()) { sdkLogging.onUserCancel(); break; } } } catch (InterruptedException e) { /* valid cancellation exception, no handling needed. */ } finally { // process UI related activities ApplicationManager.getApplication() .invokeLater( () -> { // remove the notification listener regardless of waiting outcome. cloudSdkService.removeStatusUpdateListener(sdkStatusUpdateListener); // process logging and error notifications. handleErrors(sdkLogging); }); // run the activity after wait is over, regardless of outcome. afterWaitComplete.run(); } } });
sdkLogging.log(CloudSdkMessageBundle.getString("managedsdk.waiting.for.sdk.ready") + "\n");
/** Checks the current SDK status after waiting for readiness, notifies and logs about errors. */ private void handleErrors(CloudSdkStatusHandler sdkLogging) { // check the status of SDK after install. SdkStatus postInstallSdkStatus = CloudSdkService.getInstance().getStatus(); switch (postInstallSdkStatus) { case READY: // can continue without logging anything. break; case INSTALLING: // still installing, do nothing, up to caller to decide which message to show. break; case NOT_AVAILABLE: case INVALID: String message; NotificationType notificationType; boolean fatalSdkError = !CloudSdkService.getInstance().isInstallSupported(); if (fatalSdkError) { message = CloudSdkMessageBundle.message("managedsdk.not.available"); notificationType = NotificationType.ERROR; } else { message = sdkLogging.getErrorMessage(postInstallSdkStatus); notificationType = NotificationType.WARNING; } sdkLogging.onError(message); showCloudSdkNotification(message, notificationType); break; default: // do nothing, no error, not ready. } }
@Test public void waitFor_installingSdk_then_invalidSdk_showsErrorNotification() throws InterruptedException { mockSdkStatusChange(SdkStatus.INSTALLING, SdkStatus.INVALID); when(mockStatusHandler.getErrorMessage(SdkStatus.INVALID)) .thenReturn("invalid SDK after waiting"); cloudSdkServiceManager.blockUntilSdkReady(mockProject, "", mockStatusHandler); ApplicationManager.getApplication() .invokeAndWait( () -> verify(cloudSdkServiceManager) .showCloudSdkNotification( "invalid SDK after waiting", NotificationType.WARNING)); }
@Test public void installingSdk_then_invalidSdk_showsErrorNotification() { mockSdkStatusChange(SdkStatus.INSTALLING, SdkStatus.INVALID); String errorMessage = "Deployment failed: Google Cloud SDK is not ready."; when(mockStatusHandler.getErrorMessage(SdkStatus.INVALID)).thenReturn(errorMessage); cloudSdkServiceManager.runWhenSdkReady(mockProject, mockRunnable, "", mockStatusHandler); ApplicationManager.getApplication() .invokeAndWait( () -> verify(cloudSdkServiceManager) .showCloudSdkNotification(errorMessage, NotificationType.WARNING)); }
@Test public void waitFor_installingSdk_then_invalidSdk_reportsError() throws InterruptedException { mockSdkStatusChange(SdkStatus.INSTALLING, SdkStatus.INVALID); cloudSdkServiceManager.blockUntilSdkReady(mockProject, "", mockStatusHandler); ApplicationManager.getApplication() .invokeAndWait(() -> verify(mockStatusHandler).onError(any())); }