Search in sources :

Example 36 with Mock

use of org.mockito.Mock in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class InstantAppButtonsPreferenceControllerTest method clickInstallButton_shouldOpenAppStore.

@Test
public void clickInstallButton_shouldOpenAppStore() {
    final ResolveInfo resolveInfo = mock(ResolveInfo.class);
    final ActivityInfo activityInfo = mock(ActivityInfo.class);
    resolveInfo.activityInfo = activityInfo;
    activityInfo.packageName = TEST_INSTALLER_PACKAGE_NAME;
    activityInfo.name = TEST_INSTALLER_ACTIVITY_NAME;
    when(mPackageManager.resolveActivity(any(), anyInt())).thenReturn(resolveInfo);
    mController.displayPreference(mScreen);
    final ComponentName componentName = new ComponentName(TEST_INSTALLER_PACKAGE_NAME, TEST_INSTALLER_ACTIVITY_NAME);
    mInstallButton.callOnClick();
    verify(mFragment).startActivity(argThat(intent -> intent != null && intent.getAction().equals(Intent.ACTION_SHOW_APP_INFO) && intent.getComponent().equals(componentName)));
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Context(android.content.Context) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) PackageManager(android.content.pm.PackageManager) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) LayoutPreference(com.android.settingslib.widget.LayoutPreference) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Intent(android.content.Intent) Mockito.spy(org.mockito.Mockito.spy) PackageInfo(android.content.pm.PackageInfo) MenuItem(android.view.MenuItem) MockitoAnnotations(org.mockito.MockitoAnnotations) ReflectionHelpers(org.robolectric.util.ReflectionHelpers) AppUtils(com.android.settingslib.applications.AppUtils) ActivityInfo(android.content.pm.ActivityInfo) Menu(android.view.Menu) BasePreferenceController(com.android.settings.core.BasePreferenceController) View(android.view.View) Button(android.widget.Button) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) R(com.android.settings.R) Before(org.junit.Before) PreferenceScreen(androidx.preference.PreferenceScreen) FragmentManager(androidx.fragment.app.FragmentManager) ComponentName(android.content.ComponentName) TextUtils(android.text.TextUtils) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) RuntimeEnvironment(org.robolectric.RuntimeEnvironment) Truth.assertThat(com.google.common.truth.Truth.assertThat) FragmentTransaction(androidx.fragment.app.FragmentTransaction) InstantAppDataProvider(com.android.settingslib.applications.instantapps.InstantAppDataProvider) Mockito.verify(org.mockito.Mockito.verify) ResolveInfo(android.content.pm.ResolveInfo) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) Mockito.never(org.mockito.Mockito.never) PreferenceManager(androidx.preference.PreferenceManager) ApplicationInfo(android.content.pm.ApplicationInfo) Mockito.mock(org.mockito.Mockito.mock) ActivityInfo(android.content.pm.ActivityInfo) ComponentName(android.content.ComponentName) Test(org.junit.Test)

Example 37 with Mock

use of org.mockito.Mock in project graylog2-server by Graylog2.

the class ElasticsearchBackendErrorHandlingTest method setUp.

@Before
public void setUp() throws Exception {
    final FieldTypesLookup fieldTypesLookup = mock(FieldTypesLookup.class);
    this.backend = new ElasticsearchBackend(ImmutableMap.of("dummy", () -> mock(DummyHandler.class)), jestClient, indexLookup, new QueryStringDecorators(Collections.emptySet()), (elasticsearchBackend, ssb, job, query) -> new ESGeneratedQueryContext(elasticsearchBackend, ssb, job, query, fieldTypesLookup), false, objectMapper);
    when(indexLookup.indexNamesForStreamsInTimeRange(any(), any())).thenReturn(Collections.emptySet());
    final SearchType searchType1 = mock(SearchType.class);
    when(searchType1.id()).thenReturn("deadbeef");
    when(searchType1.type()).thenReturn("dummy");
    final SearchType searchType2 = mock(SearchType.class);
    when(searchType2.id()).thenReturn("cafeaffe");
    when(searchType2.type()).thenReturn("dummy");
    final Set<SearchType> searchTypes = ImmutableSet.of(searchType1, searchType2);
    this.query = Query.builder().id("query1").timerange(RelativeRange.create(300)).query(ElasticsearchQueryString.of("*")).searchTypes(searchTypes).build();
    final Search search = Search.builder().id("search1").queries(ImmutableSet.of(query)).build();
    this.searchJob = new SearchJob("job1", search, "admin");
    this.queryContext = new ESGeneratedQueryContext(this.backend, new SearchSourceBuilder(), searchJob, query, mock(FieldTypesLookup.class));
    searchTypes.forEach(queryContext::searchSourceBuilder);
    when(jestClient.execute(any())).thenReturn(result);
}
Also used : ESSearchTypeHandler(org.graylog.storage.elasticsearch6.views.searchtypes.ESSearchTypeHandler) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Query(org.graylog.plugins.views.search.Query) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MultiSearchResult(io.searchbox.core.MultiSearchResult) ElasticsearchException(org.graylog2.indexer.ElasticsearchException) RelativeRange(org.graylog2.plugin.indexer.searches.timeranges.RelativeRange) JestClient(io.searchbox.client.JestClient) ElasticsearchQueryString(org.graylog.plugins.views.search.elasticsearch.ElasticsearchQueryString) SearchType(org.graylog.plugins.views.search.SearchType) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) FieldTypesLookup(org.graylog.plugins.views.search.elasticsearch.FieldTypesLookup) MockitoJUnit(org.mockito.junit.MockitoJUnit) JsonNode(com.fasterxml.jackson.databind.JsonNode) Search(org.graylog.plugins.views.search.Search) QueryResult(org.graylog.plugins.views.search.QueryResult) Before(org.junit.Before) SearchJob(org.graylog.plugins.views.search.SearchJob) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) QueryStringDecorators(org.graylog.plugins.views.search.elasticsearch.QueryStringDecorators) Rule(org.junit.Rule) SearchError(org.graylog.plugins.views.search.errors.SearchError) MockitoRule(org.mockito.junit.MockitoRule) IndexLookup(org.graylog.plugins.views.search.elasticsearch.IndexLookup) Collections(java.util.Collections) SearchSourceBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder) Mockito.mock(org.mockito.Mockito.mock) QueryStringDecorators(org.graylog.plugins.views.search.elasticsearch.QueryStringDecorators) Search(org.graylog.plugins.views.search.Search) SearchJob(org.graylog.plugins.views.search.SearchJob) FieldTypesLookup(org.graylog.plugins.views.search.elasticsearch.FieldTypesLookup) SearchType(org.graylog.plugins.views.search.SearchType) SearchSourceBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder) Before(org.junit.Before)

Example 38 with Mock

use of org.mockito.Mock in project graylog2-server by Graylog2.

the class EntitySharesServiceTest method ignoreInvisibleOwners.

@DisplayName("The validation should ignore invisble owners")
@Test
void ignoreInvisibleOwners() {
    final GRN entity = grnRegistry.newGRN(GRNTypes.STREAM, "54e3deadbeefdeadbeefaffe");
    final EntityShareRequest shareRequest = EntityShareRequest.create(ImmutableMap.of());
    final Set<GRN> allGrantees = dbGrantService.getAll().stream().map(GrantDTO::grantee).collect(Collectors.toSet());
    lenient().when(granteeService.getAvailableGrantees(any())).thenReturn(allGrantees.stream().filter(g -> g.toString().equals("grn::::user:invisible")).map(g -> Grantee.createUser(g, g.entity())).collect(Collectors.toSet()));
    final User user = createMockUser("hans");
    final Subject subject = mock(Subject.class);
    final EntityShareResponse entityShareResponse = entitySharesService.prepareShare(entity, shareRequest, user, subject);
    assertThat(entityShareResponse.validationResult()).satisfies(validationResult -> {
        assertThat(validationResult.failed()).isFalse();
    });
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) EntityDependencyPermissionChecker(org.graylog.security.entities.EntityDependencyPermissionChecker) BeforeEach(org.junit.jupiter.api.BeforeEach) Mock(org.mockito.Mock) BuiltinCapabilities(org.graylog.security.BuiltinCapabilities) Capability(org.graylog.security.Capability) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mockito.lenient(org.mockito.Mockito.lenient) GRNRegistry(org.graylog.grn.GRNRegistry) EventBus(com.google.common.eventbus.EventBus) MongoDBTestService(org.graylog.testing.mongodb.MongoDBTestService) DBGrantService(org.graylog.security.DBGrantService) GrantDTO(org.graylog.security.GrantDTO) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Subject(org.apache.shiro.subject.Subject) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ImmutableSet(com.google.common.collect.ImmutableSet) EntityDependencyResolver(org.graylog.security.entities.EntityDependencyResolver) ImmutableMap(com.google.common.collect.ImmutableMap) GRNTypes(org.graylog.grn.GRNTypes) MongoJackObjectMapperProvider(org.graylog2.bindings.providers.MongoJackObjectMapperProvider) Set(java.util.Set) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) MongoJackExtension(org.graylog.testing.mongodb.MongoJackExtension) GRN(org.graylog.grn.GRN) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) MongoDBExtension(org.graylog.testing.mongodb.MongoDBExtension) GRNExtension(org.graylog.testing.GRNExtension) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) User(org.graylog2.plugin.database.users.User) Mockito.mock(org.mockito.Mockito.mock) GRN(org.graylog.grn.GRN) User(org.graylog2.plugin.database.users.User) Subject(org.apache.shiro.subject.Subject) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 39 with Mock

use of org.mockito.Mock in project graylog2-server by Graylog2.

the class QualifyingViewsServiceTest method returnViewWhenBothSearchesWithAndWithoutParametersIsPresent.

@Test
public void returnViewWhenBothSearchesWithAndWithoutParametersIsPresent() {
    final ViewDTO view1 = mock(ViewDTO.class);
    final ViewDTO view2 = mock(ViewDTO.class);
    final String viewId = "viewWithParameter";
    final Search search1 = mock(Search.class);
    final Search search2 = mock(Search.class);
    final String search1Id = "streamWithParameter";
    when(view1.id()).thenReturn(viewId);
    when(view1.type()).thenReturn(ViewDTO.Type.SEARCH);
    when(view1.searchId()).thenReturn(search1Id);
    when(view1.title()).thenReturn("My View");
    when(view1.summary()).thenReturn("My Summary");
    when(view1.description()).thenReturn("My Description");
    when(view2.type()).thenReturn(ViewDTO.Type.SEARCH);
    when(search1.id()).thenReturn(search1Id);
    when(search1.parameters()).thenReturn(ImmutableSet.of(ValueParameter.any("foobar")));
    when(search2.parameters()).thenReturn(ImmutableSet.of());
    when(viewService.streamAll()).then(invocation -> Stream.of(view1, view2));
    when(searchDbService.findByIds(any())).thenReturn(ImmutableList.of(search1, search2));
    final Collection<ViewParameterSummaryDTO> result = this.qualifyingViewsService.forValue();
    assertThat(result).hasSize(1).allMatch(summary -> summary.id().equals(viewId)).allMatch(summary -> summary.title().equals("My View")).allMatch(summary -> summary.summary().equals("My Summary")).allMatch(summary -> summary.description().equals("My Description"));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ImmutableSet(com.google.common.collect.ImmutableSet) Mock(org.mockito.Mock) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ValueParameter(org.graylog.plugins.views.search.ValueParameter) Rule(org.junit.Rule) Stream(java.util.stream.Stream) ImmutableList(com.google.common.collect.ImmutableList) MockitoJUnit(org.mockito.junit.MockitoJUnit) MockitoRule(org.mockito.junit.MockitoRule) Search(org.graylog.plugins.views.search.Search) SearchDbService(org.graylog.plugins.views.search.db.SearchDbService) Collections(java.util.Collections) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) Search(org.graylog.plugins.views.search.Search) Test(org.junit.Test)

Example 40 with Mock

use of org.mockito.Mock in project graylog2-server by Graylog2.

the class QualifyingViewsServiceTest method returnViewWhenSearchWithParametersIsPresent.

@Test
public void returnViewWhenSearchWithParametersIsPresent() {
    final ViewDTO view1 = mock(ViewDTO.class);
    final String viewId = "viewWithParameter";
    final Search search = mock(Search.class);
    final String searchId = "streamWithParameter";
    when(view1.id()).thenReturn(viewId);
    when(view1.type()).thenReturn(ViewDTO.Type.SEARCH);
    when(view1.searchId()).thenReturn(searchId);
    when(view1.title()).thenReturn("My View");
    when(view1.summary()).thenReturn("My Summary");
    when(view1.description()).thenReturn("My Description");
    when(search.id()).thenReturn(searchId);
    when(search.parameters()).thenReturn(ImmutableSet.of(ValueParameter.any("foobar")));
    when(viewService.streamAll()).then(invocation -> Stream.of(view1));
    when(searchDbService.findByIds(any())).thenReturn(ImmutableList.of(search));
    final Collection<ViewParameterSummaryDTO> result = this.qualifyingViewsService.forValue();
    assertThat(result).hasSize(1).allMatch(summary -> summary.id().equals(viewId)).allMatch(summary -> summary.title().equals("My View")).allMatch(summary -> summary.summary().equals("My Summary")).allMatch(summary -> summary.description().equals("My Description"));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ImmutableSet(com.google.common.collect.ImmutableSet) Mock(org.mockito.Mock) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ValueParameter(org.graylog.plugins.views.search.ValueParameter) Rule(org.junit.Rule) Stream(java.util.stream.Stream) ImmutableList(com.google.common.collect.ImmutableList) MockitoJUnit(org.mockito.junit.MockitoJUnit) MockitoRule(org.mockito.junit.MockitoRule) Search(org.graylog.plugins.views.search.Search) SearchDbService(org.graylog.plugins.views.search.db.SearchDbService) Collections(java.util.Collections) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) Search(org.graylog.plugins.views.search.Search) Test(org.junit.Test)

Aggregations

Mock (org.mockito.Mock)128 Mockito.mock (org.mockito.Mockito.mock)103 Test (org.junit.Test)98 Before (org.junit.Before)90 Mockito.verify (org.mockito.Mockito.verify)84 List (java.util.List)80 Mockito.when (org.mockito.Mockito.when)76 RunWith (org.junit.runner.RunWith)72 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)60 Collections (java.util.Collections)55 ArrayList (java.util.ArrayList)54 ArgumentCaptor (org.mockito.ArgumentCaptor)51 Mockito.spy (org.mockito.Mockito.spy)45 Arrays (java.util.Arrays)42 Assert.assertEquals (org.junit.Assert.assertEquals)40 Mockito.doReturn (org.mockito.Mockito.doReturn)40 Map (java.util.Map)38 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)38 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)37 Mockito.times (org.mockito.Mockito.times)36