Search in sources :

Example 26 with ArgumentCaptor

use of org.mockito.ArgumentCaptor in project java-design-patterns by iluwatar.

the class DispatcherTest method testMenuItemSelected.

@Test
public void testMenuItemSelected() throws Exception {
    final Dispatcher dispatcher = Dispatcher.getInstance();
    final Store store = mock(Store.class);
    dispatcher.registerStore(store);
    dispatcher.menuItemSelected(MenuItem.HOME);
    dispatcher.menuItemSelected(MenuItem.COMPANY);
    // We expect 4 events, 2 menu selections and 2 content change actions
    final ArgumentCaptor<Action> actionCaptor = ArgumentCaptor.forClass(Action.class);
    verify(store, times(4)).onAction(actionCaptor.capture());
    verifyNoMoreInteractions(store);
    final List<Action> actions = actionCaptor.getAllValues();
    final List<MenuAction> menuActions = actions.stream().filter(a -> a.getType().equals(ActionType.MENU_ITEM_SELECTED)).map(a -> (MenuAction) a).collect(Collectors.toList());
    final List<ContentAction> contentActions = actions.stream().filter(a -> a.getType().equals(ActionType.CONTENT_CHANGED)).map(a -> (ContentAction) a).collect(Collectors.toList());
    assertEquals(2, menuActions.size());
    assertEquals(1, menuActions.stream().map(MenuAction::getMenuItem).filter(MenuItem.HOME::equals).count());
    assertEquals(1, menuActions.stream().map(MenuAction::getMenuItem).filter(MenuItem.COMPANY::equals).count());
    assertEquals(2, contentActions.size());
    assertEquals(1, contentActions.stream().map(ContentAction::getContent).filter(Content.PRODUCTS::equals).count());
    assertEquals(1, contentActions.stream().map(ContentAction::getContent).filter(Content.COMPANY::equals).count());
}
Also used : MenuAction(com.iluwatar.flux.action.MenuAction) MenuItem(com.iluwatar.flux.action.MenuItem) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Field(java.lang.reflect.Field) Constructor(java.lang.reflect.Constructor) Collectors(java.util.stream.Collectors) Content(com.iluwatar.flux.action.Content) Mockito.verify(org.mockito.Mockito.verify) Assert.assertSame(org.junit.Assert.assertSame) List(java.util.List) ArgumentCaptor(org.mockito.ArgumentCaptor) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) ActionType(com.iluwatar.flux.action.ActionType) Store(com.iluwatar.flux.store.Store) ContentAction(com.iluwatar.flux.action.ContentAction) Action(com.iluwatar.flux.action.Action) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) MenuAction(com.iluwatar.flux.action.MenuAction) ContentAction(com.iluwatar.flux.action.ContentAction) Action(com.iluwatar.flux.action.Action) Store(com.iluwatar.flux.store.Store) ContentAction(com.iluwatar.flux.action.ContentAction) MenuAction(com.iluwatar.flux.action.MenuAction) Test(org.junit.Test)

Example 27 with ArgumentCaptor

use of org.mockito.ArgumentCaptor in project sonarqube by SonarSource.

the class DeleteActionTest method request_also_deletes_components_of_specified_organization.

@Test
public void request_also_deletes_components_of_specified_organization() {
    OrganizationDto organization = dbTester.organizations().insert();
    ComponentDto project = dbTester.components().insertProject(organization);
    ComponentDto module = dbTester.components().insertComponent(ComponentTesting.newModuleDto(project));
    ComponentDto directory = dbTester.components().insertComponent(ComponentTesting.newDirectory(module, "a/b"));
    ComponentDto file = dbTester.components().insertComponent(ComponentTesting.newFileDto(module, directory));
    ComponentDto view = dbTester.components().insertView(organization, (dto) -> {
    });
    ComponentDto subview1 = dbTester.components().insertComponent(ComponentTesting.newSubView(view, "v1", "ksv1"));
    ComponentDto subview2 = dbTester.components().insertComponent(ComponentTesting.newSubView(subview1, "v2", "ksv2"));
    ComponentDto projectCopy = dbTester.components().insertComponent(ComponentTesting.newProjectCopy("pc1", project, subview1));
    logInAsAdministrator(organization);
    sendRequest(organization);
    verifyOrganizationDoesNotExist(organization);
    ArgumentCaptor<List<ComponentDto>> arg = (ArgumentCaptor<List<ComponentDto>>) ((ArgumentCaptor) ArgumentCaptor.forClass(List.class));
    verify(componentCleanerService).delete(any(DbSession.class), arg.capture());
    assertThat(arg.getValue()).containsOnly(project, view);
}
Also used : DbSession(org.sonar.db.DbSession) ArgumentCaptor(org.mockito.ArgumentCaptor) ComponentDto(org.sonar.db.component.ComponentDto) List(java.util.List) OrganizationDto(org.sonar.db.organization.OrganizationDto) Test(org.junit.Test)

Example 28 with ArgumentCaptor

use of org.mockito.ArgumentCaptor in project Shuttle by timusus.

the class ShuttleUtilsTest method testIncrementPlayCount.

@Test
public void testIncrementPlayCount() throws Exception {
    Context mockContext = mock(Context.class);
    ContentResolver mockContentResolver = mock(ContentResolver.class);
    Song fakeSong = new Song(mock(Cursor.class));
    fakeSong.id = 100L;
    fakeSong.playCount = 50;
    // getPlayCount has extra logic to conduct SQL Queries, which would be a pain to mock in a test
    // so, we can use a Spy here in order to use both fake data (above) plus control return behaviors (below)
    Song spySong = spy(fakeSong);
    doReturn(50).when(spySong).getPlayCount(any(Context.class));
    when(mockContext.getContentResolver()).thenReturn(mockContentResolver);
    // Setup to perform the method call on a song with no play counts
    when(mockContentResolver.update(any(Uri.class), any(ContentValues.class), anyString(), any(String[].class))).thenReturn(0);
    // Call the method and capture the ContentValues object
    ArgumentCaptor<ContentValues> contentValuesCaptor = new ArgumentCaptor<>();
    ShuttleUtils.incrementPlayCount(mockContext, spySong);
    verify(mockContentResolver).update(any(Uri.class), contentValuesCaptor.capture(), anyString(), any(String[].class));
    // Check that the values being updated or inserted match the actual song
    ContentValues values = contentValuesCaptor.getValue();
    assertThat(values.get("_id")).isEqualTo(100L);
    assertThat(values.get("play_count")).isEqualTo(51);
    verify(mockContentResolver).insert(any(Uri.class), eq(values));
    // Next, test what happens with a song that already had play counts (should not cause an insert operation)
    reset(mockContentResolver);
    when(mockContentResolver.update(any(Uri.class), contentValuesCaptor.capture(), anyString(), any(String[].class))).thenReturn(1);
    ShuttleUtils.incrementPlayCount(mockContext, spySong);
    verify(mockContentResolver, never()).insert(any(Uri.class), any(ContentValues.class));
}
Also used : Context(android.content.Context) ContentValues(android.content.ContentValues) Song(com.simplecity.amp_library.model.Song) ArgumentCaptor(org.mockito.ArgumentCaptor) Cursor(android.database.Cursor) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) Test(org.junit.Test)

Example 29 with ArgumentCaptor

use of org.mockito.ArgumentCaptor in project Shuttle by timusus.

the class ShuttleUtilsTest method testOpenShuttleLinkWebLink.

@Test
public void testOpenShuttleLinkWebLink() throws Exception {
    Activity mockActivity = mock(Activity.class);
    String fakePackage = "fake.package.name";
    // Setup the mock to throw an ActivityNotFoundException only the first time startActivity is called
    doThrow(new ActivityNotFoundException()).doNothing().when(mockActivity).startActivity(any(Intent.class));
    // Call the method and capture the "fired" intent
    ShuttleUtils.openShuttleLink(mockActivity, fakePackage);
    ArgumentCaptor<Intent> intentCaptor = new ArgumentCaptor<>();
    verify(mockActivity, times(2)).startActivity(intentCaptor.capture());
    Intent intent = intentCaptor.getValue();
    assertThat(intent.getAction()).isEqualTo(Intent.ACTION_VIEW);
    // Also, I guess ShuttleUtils.isAmazonBuild() needs to be tested or we can't trust this test
    if (ShuttleUtils.isAmazonBuild()) {
        assertThat(intent.getData()).isEqualTo(Uri.parse("http://www.amazon.com/gp/mas/dl/android?p=" + fakePackage));
    } else {
        assertThat(intent.getData()).isEqualTo(Uri.parse("https://play.google.com/store/apps/details?id=" + fakePackage));
    }
}
Also used : ArgumentCaptor(org.mockito.ArgumentCaptor) ActivityNotFoundException(android.content.ActivityNotFoundException) Activity(android.app.Activity) Intent(android.content.Intent) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 30 with ArgumentCaptor

use of org.mockito.ArgumentCaptor in project OpenAM by OpenRock.

the class RestAuthenticationHandlerTest method shouldInitiateAuthenticationViaGET1.

@Test
public void shouldInitiateAuthenticationViaGET1() throws AuthLoginException, L10NMessageImpl, JSONException, IOException, RestAuthException, RestAuthResponseException {
    //Given
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse httpResponse = mock(HttpServletResponse.class);
    String authIndexType = AuthIndexType.MODULE.toString();
    String indexValue = "INDEX_VALUE";
    String sessionUpgradeSSOTokenId = null;
    AuthContextLocalWrapper authContextLocalWrapper = mock(AuthContextLocalWrapper.class);
    given(authContextLocalWrapper.getErrorCode()).willReturn("ERROR_CODE");
    given(authContextLocalWrapper.getErrorMessage()).willReturn("ERROR_MESSAGE");
    LoginProcess loginProcess = mock(LoginProcess.class);
    given(loginProcess.getLoginStage()).willReturn(LoginStage.COMPLETE);
    given(loginProcess.isSuccessful()).willReturn(false);
    given(loginProcess.getAuthContext()).willReturn(authContextLocalWrapper);
    given(loginAuthenticator.getLoginProcess(Matchers.<LoginConfiguration>anyObject())).willReturn(loginProcess);
    //When
    try {
        restAuthenticationHandler.initiateAuthentication(request, httpResponse, authIndexType, indexValue, sessionUpgradeSSOTokenId);
    } catch (RestAuthErrorCodeException e) {
        assertEquals(e.getStatusCode(), 401);
        ArgumentCaptor<LoginConfiguration> argumentCaptor = ArgumentCaptor.forClass(LoginConfiguration.class);
        verify(loginAuthenticator).getLoginProcess(argumentCaptor.capture());
        LoginConfiguration loginConfiguration = argumentCaptor.getValue();
        assertEquals(loginConfiguration.getHttpRequest(), request);
        assertEquals(loginConfiguration.getIndexType(), AuthIndexType.MODULE);
        assertEquals(loginConfiguration.getIndexValue(), "INDEX_VALUE");
        assertEquals(loginConfiguration.getSessionId(), "");
        assertEquals(loginConfiguration.getSSOTokenId(), "");
        return;
    }
    //Then
    fail();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RestAuthErrorCodeException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthErrorCodeException) ArgumentCaptor(org.mockito.ArgumentCaptor) HttpServletResponse(javax.servlet.http.HttpServletResponse) LoginConfiguration(org.forgerock.openam.core.rest.authn.core.LoginConfiguration) AuthContextLocalWrapper(org.forgerock.openam.core.rest.authn.core.wrappers.AuthContextLocalWrapper) LoginProcess(org.forgerock.openam.core.rest.authn.core.LoginProcess) Test(org.testng.annotations.Test)

Aggregations

ArgumentCaptor (org.mockito.ArgumentCaptor)33 List (java.util.List)16 Matchers.anyString (org.mockito.Matchers.anyString)15 Test (org.testng.annotations.Test)15 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)13 HashSet (java.util.HashSet)13 Map (java.util.Map)13 Set (java.util.Set)13 Collectors (java.util.stream.Collectors)13 Test (org.junit.Test)13 Mockito.verify (org.mockito.Mockito.verify)13 Collections (java.util.Collections)12 Arrays.asList (java.util.Arrays.asList)11 IOException (java.io.IOException)10 Arrays (java.util.Arrays)10 Collections.emptySet (java.util.Collections.emptySet)10 Collections.singleton (java.util.Collections.singleton)10 Collections.singletonMap (java.util.Collections.singletonMap)10 Function (java.util.function.Function)10