2828
2929import com .google .api .client .auth .oauth2 .Credential ;
3030import com .google .cloud .tools .eclipse .appengine .deploy .standard .StandardDeployPreferences ;
31+ import com .google .cloud .tools .eclipse .appengine .deploy .ui .StandardDeployPreferencesPanel .ProjectSelectionValidator ;
3132import com .google .cloud .tools .eclipse .login .IGoogleLoginService ;
33+ import com .google .cloud .tools .eclipse .login .ui .AccountSelector ;
3234import com .google .cloud .tools .eclipse .login .ui .AccountSelectorObservableValue ;
3335import com .google .cloud .tools .eclipse .projectselector .ProjectRepository ;
3436import com .google .cloud .tools .eclipse .projectselector .ProjectRepositoryException ;
@@ -65,6 +67,7 @@ public class StandardDeployPreferencesPanelTest {
6567 private static final String EMAIL_1 = "some-email-1@example.com" ;
6668
6769 private Composite parent ;
70+ private StandardDeployPreferencesPanel deployPanel ;
6871 @ Mock private IProject project ;
6972 @ Mock private IGoogleLoginService loginService ;
7073 @ Mock private Runnable layoutChangedHandler ;
@@ -87,14 +90,13 @@ public void setUp() throws Exception {
8790 @ Test
8891 public void testSelectSingleAccount () {
8992 when (loginService .getAccounts ()).thenReturn (new HashSet <>(Arrays .asList (account1 )));
90- StandardDeployPreferencesPanel deployPanel = new StandardDeployPreferencesPanel (
91- parent , project , loginService , layoutChangedHandler , true , projectRepository );
93+ deployPanel = createPanel (true /* requireValues */ );
9294 assertThat (deployPanel .getSelectedCredential (), is (credential ));
9395
9496 // todo? assertTrue(deployPanel.getAccountSelector().isAutoSelectAccountIfNone()
9597
9698 // verify not in error
97- IStatus status = getAccountSelectorValidationStatus (deployPanel );
99+ IStatus status = getAccountSelectorValidationStatus ();
98100 assertTrue ("account selector is in error: " + status .getMessage (), status .isOK ());
99101
100102 assertThat ("auto-selected value should be propagated back to model" ,
@@ -103,10 +105,8 @@ public void testSelectSingleAccount() {
103105
104106 @ Test
105107 public void testValidationMessageWhenNotSignedIn () {
106- StandardDeployPreferencesPanel deployPanel =
107- new StandardDeployPreferencesPanel (parent , project , loginService , layoutChangedHandler ,
108- true , projectRepository );
109- IStatus status = getAccountSelectorValidationStatus (deployPanel );
108+ deployPanel = createPanel (true /* requireValues */ );
109+ IStatus status = getAccountSelectorValidationStatus ();
110110 assertThat (status .getMessage (), is ("Sign in to Google." ));
111111 }
112112
@@ -115,34 +115,19 @@ public void testValidationMessageWhenSignedIn() {
115115 // Return two accounts because the account selector will auto-select if there exists only one.
116116 when (loginService .getAccounts ()).thenReturn (new HashSet <>(Arrays .asList (account1 , account2 )));
117117
118- StandardDeployPreferencesPanel deployPanel =
119- new StandardDeployPreferencesPanel (parent , project , loginService , layoutChangedHandler ,
120- true , projectRepository );
121- IStatus status = getAccountSelectorValidationStatus (deployPanel );
118+ deployPanel = createPanel (true /* requireValues */ );
119+ IStatus status = getAccountSelectorValidationStatus ();
122120 assertThat (status .getMessage (), is ("Select an account." ));
123121 }
124122
125- private static Button getButtonWithText (Composite parent , String text ) {
126- for (Control control : parent .getChildren ()) {
127- if (control instanceof Button ) {
128- Button button = (Button ) control ;
129- if (button .getText ().equals (text )) {
130- return button ;
131- }
132- }
133- }
134- return null ;
135- }
136-
137123 // https://github.com/GoogleCloudPlatform/google-cloud-eclipse/issues/1229
138124 @ Test
139125 public void testUncheckStopPreviousVersionButtonWhenDisabled () {
140- StandardDeployPreferencesPanel panel = new StandardDeployPreferencesPanel (
141- parent , project , loginService , layoutChangedHandler , true , projectRepository );
126+ deployPanel = createPanel (true /* requireValues */ );
142127
143128 Button promoteButton =
144- getButtonWithText (panel , "Promote the deployed version to receive all traffic" );
145- Button stopButton = getButtonWithText (panel , "Stop previous version" );
129+ getButtonWithText ("Promote the deployed version to receive all traffic" );
130+ Button stopButton = getButtonWithText ("Stop previous version" );
146131 SWTBotCheckBox promote = new SWTBotCheckBox (promoteButton );
147132 SWTBotCheckBox stop = new SWTBotCheckBox (stopButton );
148133
@@ -183,11 +168,9 @@ public void testProjectSavedInPreferencesSelected() throws ProjectRepositoryExce
183168 new ProjectScope (project ).getNode (StandardDeployPreferences .PREFERENCE_STORE_QUALIFIER );
184169 node .put ("project.id" , "projectId1" );
185170 node .put ("account.email" , EMAIL_1 );
186- initializeProjectRepository (projectRepository );
171+ initializeProjectRepository ();
187172 when (loginService .getAccounts ()).thenReturn (new HashSet <>(Arrays .asList (account1 , account2 )));
188- StandardDeployPreferencesPanel deployPanel =
189- new StandardDeployPreferencesPanel (parent , project , loginService , layoutChangedHandler ,
190- true , projectRepository );
173+ deployPanel = createPanel (true /* requireValues */ );
191174 Queue <Control > children = new ArrayDeque <>(Arrays .asList (deployPanel .getChildren ()));
192175 while (!children .isEmpty ()) {
193176 Control control = children .poll ();
@@ -204,7 +187,65 @@ public void testProjectSavedInPreferencesSelected() throws ProjectRepositoryExce
204187 fail ("Did not find ProjectSelector widget" );
205188 }
206189
207- private void initializeProjectRepository (ProjectRepository projectRepository )
190+ @ Test
191+ public void testProjectNotSelectedIsAnErrorWhenRequireValuesIsTrue () {
192+ deployPanel = createPanel (true /* requireValues */ );
193+ assertThat (getProjectSelectionValidator ().getSeverity (), is (IStatus .ERROR ));
194+ }
195+
196+ @ Test
197+ public void testProjectNotSelectedIsNotAnErrorWhenRequireValuesIsFalse () {
198+ deployPanel = createPanel (false /* requireValues */ );
199+ assertThat (getProjectSelectionValidator ().getSeverity (), is (IStatus .INFO ));
200+ }
201+
202+ @ Test
203+ public void testProjectsExistThenNoProjectNotFoundError () throws ProjectRepositoryException {
204+ when (loginService .getAccounts ()).thenReturn (new HashSet <>(Arrays .asList (account1 )));
205+ initializeProjectRepository ();
206+ deployPanel = createPanel (false /* requireValues */ );
207+ selectAccount (account1 );
208+ assertThat (getProjectSelectionValidator ().getSeverity (), is (IStatus .OK ));
209+ }
210+
211+ private Button getButtonWithText (String text ) {
212+ for (Control control : deployPanel .getChildren ()) {
213+ if (control instanceof Button ) {
214+ Button button = (Button ) control ;
215+ if (button .getText ().equals (text )) {
216+ return button ;
217+ }
218+ }
219+ }
220+ return null ;
221+ }
222+
223+ private void selectAccount (Account account ) {
224+ for (Control control : deployPanel .getChildren ()) {
225+ if (control instanceof AccountSelector ) {
226+ AccountSelector accountSelector = (AccountSelector ) control ;
227+ accountSelector .selectAccount (account .getEmail ());
228+ }
229+ }
230+ }
231+
232+ private StandardDeployPreferencesPanel createPanel (boolean requireValues ) {
233+ return new StandardDeployPreferencesPanel (parent , project , loginService , layoutChangedHandler ,
234+ requireValues , projectRepository );
235+ }
236+
237+ private IStatus getProjectSelectionValidator () {
238+ for (Object object : deployPanel .getDataBindingContext ().getValidationStatusProviders ()) {
239+ if (object instanceof ProjectSelectionValidator ) {
240+ ProjectSelectionValidator projectSelectionValidator = (ProjectSelectionValidator ) object ;
241+ return (IStatus ) projectSelectionValidator .getValidationStatus ().getValue ();
242+ }
243+ }
244+ fail ("Could not find ProjectSelectionValidator." );
245+ return null ;
246+ }
247+
248+ private void initializeProjectRepository ()
208249 throws ProjectRepositoryException {
209250 GcpProject project1 = new GcpProject ("Project1" , "projectId1" );
210251 GcpProject project2 = new GcpProject ("Project2" , "projectId2" );
@@ -216,7 +257,7 @@ private void initializeProjectRepository(ProjectRepository projectRepository)
216257 .thenReturn (project2 );
217258 }
218259
219- private IStatus getAccountSelectorValidationStatus (StandardDeployPreferencesPanel deployPanel ) {
260+ private IStatus getAccountSelectorValidationStatus () {
220261 IStatus status = null ;
221262 for (Object object : deployPanel .getDataBindingContext ().getValidationStatusProviders ()) {
222263 ValidationStatusProvider statusProvider = (ValidationStatusProvider ) object ;
0 commit comments