GetOAuthAccessTokenAction extends AbstractAction { public void actionPerformed(ActionEvent event) { try { getOAuthClientFacade().requestAccessToken(target); } catch (InvalidOAuthParametersException e) { UISupport.showErrorMessage(messages.get("GetOAuthAccessTokenAction.Error.InvalidParameters", e.getMessage())); } catch (Exception e) { SoapUI.logError(e, messages.get("GetOAuthAccessTokenAction.Error.RetrievingFailLog")); UISupport.showErrorMessage(messages.get("GetOAuthAccessTokenAction.Error.RetrievingFailMessage")); } } GetOAuthAccessTokenAction(OAuth2Profile target); void actionPerformed(ActionEvent event);  }
@Test public void savesAccessTokenInProfile() throws Exception { final OAuth2ClientFacade clientFacade = mock(OAuth2ClientFacade.class); GetOAuthAccessTokenAction action = new GetOAuthAccessTokenAction(profile) { @Override protected OAuth2ClientFacade getOAuthClientFacade() { return clientFacade; } }; final String accessToken = "4/98789adfc8234278243987"; doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { profile.setAccessToken(accessToken); return null; } }).when(clientFacade).requestAccessToken(profile); action.actionPerformed(DUMMY_ACTION_EVENT); assertThat(profile.getAccessToken(), is(accessToken)); }
@Test public void showsAnErrorMessageWhenGetAccessTokenFails() throws Exception { final OAuth2ClientFacade clientFacade = mock(OAuth2ClientFacade.class); GetOAuthAccessTokenAction action = new GetOAuthAccessTokenAction(profile) { @Override protected OAuth2ClientFacade getOAuthClientFacade() { return clientFacade; } }; Mockito.doThrow(new OAuth2Exception(new RuntimeException())).when(clientFacade).requestAccessToken(profile); action.actionPerformed(DUMMY_ACTION_EVENT); assertThat(stubbedDialogs.getErrorMessages(), is(aCollectionWithSize(1))); }
@Test public void displaysValidationErrorWhenValidationFails() throws Exception { final OAuth2ClientFacade clientFacade = mock(OAuth2ClientFacade.class); GetOAuthAccessTokenAction action = new GetOAuthAccessTokenAction(profile) { @Override protected OAuth2ClientFacade getOAuthClientFacade() { return clientFacade; } }; String theMessage = "Access token URI blabla is not a valid HTTP URL"; Mockito.doThrow(new InvalidOAuthParametersException(theMessage)).when(clientFacade).requestAccessToken(profile); action.actionPerformed(DUMMY_ACTION_EVENT); assertThat(stubbedDialogs.getErrorMessages(), is(aCollectionWithSize(1))); assertThat(stubbedDialogs.getErrorMessages().get(0), containsString(theMessage)); }