Search in sources :

Example 56 with FunctionContext

use of org.apache.geode.cache.execute.FunctionContext in project geode by apache.

the class ListIndexFunctionJUnitTest method testExecute.

@Test
@SuppressWarnings("unchecked")
public void testExecute() throws Throwable {
    final String memberId = "mockMemberId";
    final String memberName = "mockMemberName";
    final Cache mockCache = mockContext.mock(Cache.class, "Cache");
    final DistributedSystem mockDistributedSystem = mockContext.mock(DistributedSystem.class, "DistributedSystem");
    final DistributedMember mockDistributedMember = mockContext.mock(DistributedMember.class, "DistributedMember");
    final IndexDetails indexDetailsOne = createIndexDetails(memberId, "/Employees", "empIdIdx", IndexType.PRIMARY_KEY, "/Employees", "id", memberName, "id, firstName, lastName", "Employees");
    indexDetailsOne.setIndexStatisticsDetails(createIndexStatisticsDetails(10124l, 4096l, 10124l, 1284100l, 280120l));
    final IndexDetails indexDetailsTwo = createIndexDetails(memberId, "/Employees", "empGivenNameIdx", IndexType.FUNCTIONAL, "/Employees", "lastName", memberName, "id, firstName, lastName", "Employees");
    final IndexDetails indexDetailsThree = createIndexDetails(memberId, "/Contractors", "empIdIdx", IndexType.PRIMARY_KEY, "/Contrators", "id", memberName, "id, firstName, lastName", "Contractors");
    indexDetailsThree.setIndexStatisticsDetails(createIndexStatisticsDetails(1024l, 256l, 20248l, 768001l, 24480l));
    final IndexDetails indexDetailsFour = createIndexDetails(memberId, "/Employees", "empIdIdx", IndexType.FUNCTIONAL, "/Employees", "emp_id", memberName, "id, surname, givenname", "Employees");
    final Set<IndexDetails> expectedIndexDetailsSet = new HashSet<IndexDetails>(3);
    expectedIndexDetailsSet.add(indexDetailsOne);
    expectedIndexDetailsSet.add(indexDetailsTwo);
    expectedIndexDetailsSet.add(indexDetailsThree);
    final QueryService mockQueryService = mockContext.mock(QueryService.class, "QueryService");
    final FunctionContext mockFunctionContext = mockContext.mock(FunctionContext.class, "FunctionContext");
    final TestResultSender testResultSender = new TestResultSender();
    mockContext.checking(new Expectations() {

        {
            oneOf(mockCache).getDistributedSystem();
            will(returnValue(mockDistributedSystem));
            oneOf(mockCache).getQueryService();
            will(returnValue(mockQueryService));
            oneOf(mockDistributedSystem).getDistributedMember();
            will(returnValue(mockDistributedMember));
            exactly(4).of(mockDistributedMember).getId();
            will(returnValue(memberId));
            exactly(4).of(mockDistributedMember).getName();
            will(returnValue(memberName));
            oneOf(mockQueryService).getIndexes();
            will(returnValue(Arrays.asList(createMockIndex(indexDetailsOne), createMockIndex(indexDetailsTwo), createMockIndex(indexDetailsThree), createMockIndex(indexDetailsFour))));
            oneOf(mockFunctionContext).getResultSender();
            will(returnValue(testResultSender));
        }
    });
    final ListIndexFunction function = createListIndexFunction(mockCache);
    function.execute(mockFunctionContext);
    final List<?> results = testResultSender.getResults();
    assertNotNull(results);
    assertEquals(1, results.size());
    final Set<IndexDetails> actualIndexDetailsSet = (Set<IndexDetails>) results.get(0);
    assertNotNull(actualIndexDetailsSet);
    assertEquals(expectedIndexDetailsSet.size(), actualIndexDetailsSet.size());
    for (final IndexDetails expectedIndexDetails : expectedIndexDetailsSet) {
        final IndexDetails actualIndexDetails = CollectionUtils.findBy(actualIndexDetailsSet, new Filter<IndexDetails>() {

            @Override
            public boolean accept(final IndexDetails indexDetails) {
                return ObjectUtils.equals(expectedIndexDetails, indexDetails);
            }
        });
        assertNotNull(actualIndexDetails);
        assertIndexDetailsEquals(expectedIndexDetails, actualIndexDetails);
    }
}
Also used : Expectations(org.jmock.Expectations) HashSet(java.util.HashSet) Set(java.util.Set) IndexDetails(org.apache.geode.management.internal.cli.domain.IndexDetails) DistributedSystem(org.apache.geode.distributed.DistributedSystem) FunctionContext(org.apache.geode.cache.execute.FunctionContext) QueryService(org.apache.geode.cache.query.QueryService) DistributedMember(org.apache.geode.distributed.DistributedMember) Cache(org.apache.geode.cache.Cache) HashSet(java.util.HashSet) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 57 with FunctionContext

use of org.apache.geode.cache.execute.FunctionContext in project geode by apache.

the class ListIndexFunctionJUnitTest method testExecuteWithNoIndexes.

@Test
@SuppressWarnings("unchecked")
public void testExecuteWithNoIndexes() throws Throwable {
    final Cache mockCache = mockContext.mock(Cache.class, "Cache");
    final DistributedSystem mockDistributedSystem = mockContext.mock(DistributedSystem.class, "DistributedSystem");
    final DistributedMember mockDistributedMember = mockContext.mock(DistributedMember.class, "DistributedMember");
    final QueryService mockQueryService = mockContext.mock(QueryService.class, "QueryService");
    final FunctionContext mockFunctionContext = mockContext.mock(FunctionContext.class, "FunctionContext");
    final TestResultSender testResultSender = new TestResultSender();
    mockContext.checking(new Expectations() {

        {
            oneOf(mockCache).getDistributedSystem();
            will(returnValue(mockDistributedSystem));
            oneOf(mockCache).getQueryService();
            will(returnValue(mockQueryService));
            oneOf(mockDistributedSystem).getDistributedMember();
            will(returnValue(mockDistributedMember));
            oneOf(mockQueryService).getIndexes();
            will(returnValue(Collections.emptyList()));
            oneOf(mockFunctionContext).getResultSender();
            will(returnValue(testResultSender));
        }
    });
    final ListIndexFunction function = createListIndexFunction(mockCache);
    function.execute(mockFunctionContext);
    final List<?> results = testResultSender.getResults();
    assertNotNull(results);
    assertEquals(1, results.size());
    final Set<IndexDetails> actualIndexDetailsSet = (Set<IndexDetails>) results.get(0);
    assertNotNull(actualIndexDetailsSet);
    assertTrue(actualIndexDetailsSet.isEmpty());
}
Also used : Expectations(org.jmock.Expectations) HashSet(java.util.HashSet) Set(java.util.Set) QueryService(org.apache.geode.cache.query.QueryService) DistributedMember(org.apache.geode.distributed.DistributedMember) IndexDetails(org.apache.geode.management.internal.cli.domain.IndexDetails) DistributedSystem(org.apache.geode.distributed.DistributedSystem) FunctionContext(org.apache.geode.cache.execute.FunctionContext) Cache(org.apache.geode.cache.Cache) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 58 with FunctionContext

use of org.apache.geode.cache.execute.FunctionContext in project geode by apache.

the class DescribeDiskStoreFunctionJUnitTest method testExecuteThrowingDiskStoreNotFoundException.

@Test
public void testExecuteThrowingDiskStoreNotFoundException() throws Exception {
    final String diskStoreName = "testDiskStore";
    final String memberId = "mockMemberId";
    final String memberName = "mockMemberName";
    final InternalCache mockCache = mockContext.mock(InternalCache.class, "Cache");
    final InternalDistributedMember mockMember = mockContext.mock(InternalDistributedMember.class, "DistributedMember");
    final FunctionContext mockFunctionContext = mockContext.mock(FunctionContext.class, "FunctionContext");
    final TestResultSender testResultSender = new TestResultSender();
    mockContext.checking(new Expectations() {

        {
            oneOf(mockCache).getMyId();
            will(returnValue(mockMember));
            oneOf(mockCache).findDiskStore(diskStoreName);
            will(returnValue(null));
            oneOf(mockMember).getId();
            will(returnValue(memberId));
            oneOf(mockMember).getName();
            will(returnValue(memberName));
            oneOf(mockFunctionContext).getArguments();
            will(returnValue(diskStoreName));
            oneOf(mockFunctionContext).getResultSender();
            will(returnValue(testResultSender));
        }
    });
    final DescribeDiskStoreFunction function = createDescribeDiskStoreFunction(mockCache);
    function.execute(mockFunctionContext);
    String expected = String.format("A disk store with name (%1$s) was not found on member (%2$s).", diskStoreName, memberName);
    assertThatThrownBy(() -> testResultSender.getResults()).isInstanceOf(DiskStoreNotFoundException.class).hasMessage(expected);
}
Also used : Expectations(org.jmock.Expectations) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) InternalCache(org.apache.geode.internal.cache.InternalCache) DiskStoreNotFoundException(org.apache.geode.management.internal.cli.util.DiskStoreNotFoundException) FunctionContext(org.apache.geode.cache.execute.FunctionContext) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 59 with FunctionContext

use of org.apache.geode.cache.execute.FunctionContext in project geode by apache.

the class DescribeDiskStoreFunctionJUnitTest method doTestExecuteWithDiskDirsAndDiskSizesMismatch.

private void doTestExecuteWithDiskDirsAndDiskSizesMismatch() throws Exception {
    final String diskStoreName = "mockDiskStore";
    final String memberId = "mockMemberId";
    final String memberName = "mockMemberName";
    final UUID diskStoreId = UUID.randomUUID();
    final InternalCache mockCache = mockContext.mock(InternalCache.class, "Cache");
    final InternalDistributedMember mockMember = mockContext.mock(InternalDistributedMember.class, "DistributedMember");
    final DiskStore mockDiskStore = createMockDiskStore(diskStoreId, diskStoreName, false, true, 70, 8192000l, 1000, 300l, 8192, createFileArray("/export/disk0/gemfire/backup"), new int[0], 50, 75);
    final FunctionContext mockFunctionContext = mockContext.mock(FunctionContext.class, "FunctionContext");
    final TestResultSender testResultSender = new TestResultSender();
    mockContext.checking(new Expectations() {

        {
            oneOf(mockCache).getMyId();
            will(returnValue(mockMember));
            oneOf(mockCache).findDiskStore(diskStoreName);
            will(returnValue(mockDiskStore));
            oneOf(mockMember).getId();
            will(returnValue(memberId));
            oneOf(mockMember).getName();
            will(returnValue(memberName));
            oneOf(mockFunctionContext).getArguments();
            will(returnValue(diskStoreName));
            oneOf(mockFunctionContext).getResultSender();
            will(returnValue(testResultSender));
        }
    });
    final DescribeDiskStoreFunction function = createDescribeDiskStoreFunction(mockCache);
    function.execute(mockFunctionContext);
    String expected = "The number of disk directories with a specified size (0) does not match the number of disk directories (1)!";
    assertThatThrownBy(() -> testResultSender.getResults()).hasMessage(expected);
}
Also used : DiskStore(org.apache.geode.cache.DiskStore) Expectations(org.jmock.Expectations) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) InternalCache(org.apache.geode.internal.cache.InternalCache) UUID(java.util.UUID) FunctionContext(org.apache.geode.cache.execute.FunctionContext)

Example 60 with FunctionContext

use of org.apache.geode.cache.execute.FunctionContext in project geode by apache.

the class ListDiskStoresFunctionJUnitTest method testExecuteOnMemberWithNoCache.

@Test(expected = CacheClosedException.class)
public void testExecuteOnMemberWithNoCache() throws Throwable {
    final FunctionContext mockFunctionContext = mockContext.mock(FunctionContext.class, "MockFunctionContext");
    final ListDiskStoresFunction testListDiskStoresFunction = new TestListDiskStoresFunction(mockContext.mock(Cache.class, "MockCache")) {

        @Override
        protected Cache getCache() {
            throw new CacheClosedException("Expected");
        }
    };
    final TestResultSender testResultSender = new TestResultSender();
    mockContext.checking(new Expectations() {

        {
            oneOf(mockFunctionContext).getResultSender();
            will(returnValue(testResultSender));
        }
    });
    testListDiskStoresFunction.execute(mockFunctionContext);
    try {
        testResultSender.getResults();
    } catch (CacheClosedException expected) {
        assertEquals("Expected", expected.getMessage());
        throw expected;
    }
}
Also used : Expectations(org.jmock.Expectations) CacheClosedException(org.apache.geode.cache.CacheClosedException) FunctionContext(org.apache.geode.cache.execute.FunctionContext) InternalCache(org.apache.geode.internal.cache.InternalCache) Cache(org.apache.geode.cache.Cache) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Aggregations

FunctionContext (org.apache.geode.cache.execute.FunctionContext)68 FunctionAdapter (org.apache.geode.cache.execute.FunctionAdapter)38 Test (org.junit.Test)35 HashSet (java.util.HashSet)30 RegionFunctionContext (org.apache.geode.cache.execute.RegionFunctionContext)30 ArrayList (java.util.ArrayList)29 FunctionException (org.apache.geode.cache.execute.FunctionException)28 ResultCollector (org.apache.geode.cache.execute.ResultCollector)28 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)28 Region (org.apache.geode.cache.Region)27 Execution (org.apache.geode.cache.execute.Execution)26 IgnoredException (org.apache.geode.test.dunit.IgnoredException)24 List (java.util.List)20 Set (java.util.Set)19 Iterator (java.util.Iterator)16 FunctionInvocationTargetException (org.apache.geode.cache.execute.FunctionInvocationTargetException)15 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)15 UnitTest (org.apache.geode.test.junit.categories.UnitTest)15 DistributedMember (org.apache.geode.distributed.DistributedMember)14 InternalCache (org.apache.geode.internal.cache.InternalCache)14