Search in sources :

Example 1 with CqEvent

use of org.apache.geode.cache.query.CqEvent in project geode by apache.

the class PRDeltaPropagationDUnitTest method createClientCache.

public static void createClientCache(Integer port1, Boolean subscriptionEnable, Boolean isEmpty, Boolean isCq) throws Exception {
    PRDeltaPropagationDUnitTest test = new PRDeltaPropagationDUnitTest();
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    test.createCache(props);
    lastKeyReceived = false;
    queryUpdateExecuted = false;
    queryDestroyExecuted = false;
    notADeltaInstanceObj = false;
    isFailed = false;
    procced = false;
    numValidCqEvents = 0;
    PoolImpl p = (PoolImpl) PoolManager.createFactory().addServer("localhost", port1).setSubscriptionEnabled(true).setSubscriptionRedundancy(0).setThreadLocalConnections(true).setMinConnections(6).setReadTimeout(20000).setPingInterval(10000).setRetryAttempts(5).create("PRDeltaPropagationDUnitTestPool");
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.LOCAL);
    factory.setConcurrencyChecksEnabled(true);
    if (isEmpty.booleanValue()) {
        factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
        factory.setDataPolicy(DataPolicy.EMPTY);
    }
    factory.setPoolName(p.getName());
    factory.setCloningEnabled(false);
    factory.addCacheListener(new CacheListenerAdapter() {

        @Override
        public void afterCreate(EntryEvent event) {
            if (LAST_KEY.equals(event.getKey())) {
                lastKeyReceived = true;
            }
        }
    });
    RegionAttributes attrs = factory.create();
    deltaPR = cache.createRegion(REGION_NAME, attrs);
    if (subscriptionEnable.booleanValue()) {
        deltaPR.registerInterest("ALL_KEYS");
    }
    pool = p;
    if (isCq.booleanValue()) {
        CqAttributesFactory cqf = new CqAttributesFactory();
        CqListenerAdapter cqlist = new CqListenerAdapter() {

            @Override
            @SuppressWarnings("synthetic-access")
            public void onEvent(CqEvent cqEvent) {
                if (LAST_KEY.equals(cqEvent.getKey().toString())) {
                    lastKeyReceived = true;
                } else if (!(cqEvent.getNewValue() instanceof Delta)) {
                    notADeltaInstanceObj = true;
                } else if (cqEvent.getQueryOperation().isUpdate() && cqEvent.getBaseOperation().isUpdate() && DELTA_KEY.equals(cqEvent.getKey().toString())) {
                    queryUpdateExecuted = true;
                } else if (cqEvent.getQueryOperation().isDestroy() && cqEvent.getBaseOperation().isUpdate() && DELTA_KEY.equals(cqEvent.getKey().toString())) {
                    queryDestroyExecuted = true;
                }
                if (forOldNewCQVarification) {
                    if (DELTA_KEY.equals(cqEvent.getKey().toString())) {
                        if (numValidCqEvents == 0 && ((DeltaTestImpl) cqEvent.getNewValue()).getIntVar() == 8) {
                            procced = true;
                        } else if (procced && numValidCqEvents == 1 && ((DeltaTestImpl) cqEvent.getNewValue()).getIntVar() == 10) {
                            // this tell us that every thing is fine
                            isFailed = true;
                        }
                    }
                }
                numValidCqEvents++;
            }
        };
        cqf.addCqListener(cqlist);
        CqAttributes cqa = cqf.create();
        CqQuery cq = cache.getQueryService().newCq("CQ_Delta", CQ, cqa);
        cq.execute();
    }
}
Also used : RegionAttributes(org.apache.geode.cache.RegionAttributes) DeltaTestImpl(org.apache.geode.DeltaTestImpl) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) PoolImpl(org.apache.geode.cache.client.internal.PoolImpl) CqListenerAdapter(org.apache.geode.cache.util.CqListenerAdapter) AttributesFactory(org.apache.geode.cache.AttributesFactory) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) CqEvent(org.apache.geode.cache.query.CqEvent) Delta(org.apache.geode.Delta) CqAttributes(org.apache.geode.cache.query.CqAttributes) EntryEvent(org.apache.geode.cache.EntryEvent) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes)

Example 2 with CqEvent

use of org.apache.geode.cache.query.CqEvent in project geode by apache.

the class CQPDXPostProcessorDUnitTest method testCQ.

@Test
public void testCQ() {
    String query = "select * from /" + REGION_NAME;
    client1.invoke(() -> {
        ClientCache cache = createClientCache("super-user", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        Pool pool = PoolManager.find(region);
        QueryService qs = pool.getQueryService();
        CqAttributesFactory factory = new CqAttributesFactory();
        factory.addCqListener(new CqListenerImpl() {

            @Override
            public void onEvent(final CqEvent aCqEvent) {
                Object key = aCqEvent.getKey();
                Object value = aCqEvent.getNewValue();
                if (key.equals("key1")) {
                    assertTrue(value instanceof SimpleClass);
                } else if (key.equals("key2")) {
                    assertTrue(Arrays.equals(BYTES, (byte[]) value));
                }
            }
        });
        CqAttributes cqa = factory.create();
        // Create the CqQuery
        CqQuery cq = qs.newCq("CQ1", query, cqa);
        CqResults results = cq.executeWithInitialResults();
    });
    client2.invoke(() -> {
        ClientCache cache = createClientCache("authRegionUser", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        region.put("key1", new SimpleClass(1, (byte) 1));
        region.put("key2", BYTES);
    });
    // wait for events to fire
    Awaitility.await().atMost(1, TimeUnit.SECONDS);
    PDXPostProcessor pp = (PDXPostProcessor) SecurityService.getSecurityService().getPostProcessor();
    assertEquals(pp.getCount(), 2);
}
Also used : ClientCache(org.apache.geode.cache.client.ClientCache) SecurityTestUtil.createClientCache(org.apache.geode.security.SecurityTestUtil.createClientCache) SimpleClass(org.apache.geode.pdx.SimpleClass) CqListenerImpl(org.apache.geode.cache.query.internal.cq.CqListenerImpl) CqEvent(org.apache.geode.cache.query.CqEvent) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) Region(org.apache.geode.cache.Region) SecurityTestUtil.createProxyRegion(org.apache.geode.security.SecurityTestUtil.createProxyRegion) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqResults(org.apache.geode.cache.query.CqResults) Pool(org.apache.geode.cache.client.Pool) CqQuery(org.apache.geode.cache.query.CqQuery) SecurityTest(org.apache.geode.test.junit.categories.SecurityTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 3 with CqEvent

use of org.apache.geode.cache.query.CqEvent in project geode by apache.

the class CQPostProcessorDunitTest method testPostProcess.

@Test
public void testPostProcess() {
    String query = "select * from /AuthRegion";
    client1.invoke(() -> {
        ClientCache cache = createClientCache("super-user", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        Pool pool = PoolManager.find(region);
        QueryService qs = pool.getQueryService();
        CqAttributesFactory factory = new CqAttributesFactory();
        factory.addCqListener(new CqListenerImpl() {

            @Override
            public void onEvent(final CqEvent aCqEvent) {
                assertEquals("key6", aCqEvent.getKey());
                assertEquals("super-user/AuthRegion/key6/value6", aCqEvent.getNewValue());
            }
        });
        CqAttributes cqa = factory.create();
        // Create the CqQuery
        CqQuery cq = qs.newCq("CQ1", query, cqa);
        CqResults results = cq.executeWithInitialResults();
        assertEquals(5, results.size());
        String resultString = results.toString();
        assertTrue(resultString, resultString.contains("key:key0,value:super-user/null/key0/value0"));
        assertTrue(resultString.contains("key:key1,value:super-user/null/key1/value1"));
        assertTrue(resultString.contains("key:key2,value:super-user/null/key2/value2"));
        assertTrue(resultString.contains("key:key3,value:super-user/null/key3/value3"));
        assertTrue(resultString.contains("key:key4,value:super-user/null/key4/value4"));
    });
    client2.invoke(() -> {
        ClientCache cache = createClientCache("authRegionUser", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        region.put("key6", "value6");
    });
}
Also used : CqListenerImpl(org.apache.geode.cache.query.internal.cq.CqListenerImpl) CqEvent(org.apache.geode.cache.query.CqEvent) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) Region(org.apache.geode.cache.Region) SecurityTestUtil.createProxyRegion(org.apache.geode.security.SecurityTestUtil.createProxyRegion) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqResults(org.apache.geode.cache.query.CqResults) Pool(org.apache.geode.cache.client.Pool) ClientCache(org.apache.geode.cache.client.ClientCache) SecurityTestUtil.createClientCache(org.apache.geode.security.SecurityTestUtil.createClientCache) CqQuery(org.apache.geode.cache.query.CqQuery) SecurityTest(org.apache.geode.test.junit.categories.SecurityTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 4 with CqEvent

use of org.apache.geode.cache.query.CqEvent in project geode by apache.

the class ClientQueryAuthDUnitTest method testCQ.

@Test
public void testCQ() {
    String query = "select * from /AuthRegion";
    client1.invoke(() -> {
        ClientCache cache = createClientCache("stranger", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        Pool pool = PoolManager.find(region);
        QueryService qs = pool.getQueryService();
        CqAttributes cqa = new CqAttributesFactory().create();
        // Create the CqQuery
        CqQuery cq = qs.newCq("CQ1", query, cqa);
        assertNotAuthorized(() -> cq.executeWithInitialResults(), "DATA:READ:AuthRegion");
        assertNotAuthorized(() -> cq.execute(), "DATA:READ:AuthRegion");
        assertNotAuthorized(() -> cq.close(), "DATA:MANAGE");
    });
    client2.invoke(() -> {
        ClientCache cache = createClientCache("authRegionReader", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        Pool pool = PoolManager.find(region);
        QueryService qs = pool.getQueryService();
        CqAttributes cqa = new CqAttributesFactory().create();
        // Create the CqQuery
        CqQuery cq = qs.newCq("CQ1", query, cqa);
        cq.execute();
        assertNotAuthorized(() -> cq.stop(), "DATA:MANAGE");
        assertNotAuthorized(() -> qs.getAllDurableCqsFromServer(), "CLUSTER:READ");
    });
    client3.invoke(() -> {
        ClientCache cache = createClientCache("super-user", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        Pool pool = PoolManager.find(region);
        QueryService qs = pool.getQueryService();
        CqAttributesFactory factory = new CqAttributesFactory();
        factory.addCqListener(new CqListener() {

            @Override
            public void onEvent(final CqEvent aCqEvent) {
                System.out.println(aCqEvent);
            }

            @Override
            public void onError(final CqEvent aCqEvent) {
            }

            @Override
            public void close() {
            }
        });
        CqAttributes cqa = factory.create();
        // Create the CqQuery
        CqQuery cq = qs.newCq("CQ1", query, cqa);
        System.out.println("query result: " + cq.executeWithInitialResults());
        cq.stop();
    });
}
Also used : CqEvent(org.apache.geode.cache.query.CqEvent) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) CqListener(org.apache.geode.cache.query.CqListener) SecurityTestUtil.createProxyRegion(org.apache.geode.security.SecurityTestUtil.createProxyRegion) Region(org.apache.geode.cache.Region) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) Pool(org.apache.geode.cache.client.Pool) ClientCache(org.apache.geode.cache.client.ClientCache) SecurityTestUtil.createClientCache(org.apache.geode.security.SecurityTestUtil.createClientCache) CqQuery(org.apache.geode.cache.query.CqQuery) SecurityTest(org.apache.geode.test.junit.categories.SecurityTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 5 with CqEvent

use of org.apache.geode.cache.query.CqEvent in project geode by apache.

the class CqDataUsingPoolDUnitTest method createCq.

private CqQuery createCq(String regionName, String cqName) {
    // Create CQ Attributes.
    CqAttributesFactory cqAf = new CqAttributesFactory();
    // Initialize and set CqListener.
    CqListener[] cqListeners = { new CqListener() {

        @Override
        public void close() {
        }

        @Override
        public void onEvent(CqEvent aCqEvent) {
        }

        @Override
        public void onError(CqEvent aCqEvent) {
        }
    } };
    cqAf.initCqListeners(cqListeners);
    CqAttributes cqa = cqAf.create();
    // Create cq's
    // Get the query service for the Pool
    QueryService queryService = CacheServerTestUtil.getCache().getQueryService();
    CqQuery query = null;
    try {
        query = queryService.newCq(cqName, "Select * from /" + regionName, cqa);
        query.execute();
    } catch (CqExistsException e) {
        fail("Could not find specified region:" + regionName + ":", e);
    } catch (CqException e) {
        fail("Could not find specified region:" + regionName + ":", e);
    } catch (RegionNotFoundException e) {
        fail("Could not find specified region:" + regionName + ":", e);
    }
    return query;
}
Also used : CqEvent(org.apache.geode.cache.query.CqEvent) CqAttributes(org.apache.geode.cache.query.CqAttributes) QueryService(org.apache.geode.cache.query.QueryService) CqListener(org.apache.geode.cache.query.CqListener) CqException(org.apache.geode.cache.query.CqException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CqExistsException(org.apache.geode.cache.query.CqExistsException) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery)

Aggregations

CqEvent (org.apache.geode.cache.query.CqEvent)15 CqAttributesFactory (org.apache.geode.cache.query.CqAttributesFactory)11 CqQuery (org.apache.geode.cache.query.CqQuery)10 Test (org.junit.Test)10 CqAttributes (org.apache.geode.cache.query.CqAttributes)8 QueryService (org.apache.geode.cache.query.QueryService)6 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)6 CqListenerAdapter (org.apache.geode.cache.util.CqListenerAdapter)5 Region (org.apache.geode.cache.Region)4 CqListener (org.apache.geode.cache.query.CqListener)4 ClientCache (org.apache.geode.cache.client.ClientCache)3 Pool (org.apache.geode.cache.client.Pool)3 SecurityTestUtil.createClientCache (org.apache.geode.security.SecurityTestUtil.createClientCache)3 SecurityTestUtil.createProxyRegion (org.apache.geode.security.SecurityTestUtil.createProxyRegion)3 SecurityTest (org.apache.geode.test.junit.categories.SecurityTest)3 MyObject (com.examples.snapshot.MyObject)2 File (java.io.File)2 Properties (java.util.Properties)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AttributesFactory (org.apache.geode.cache.AttributesFactory)2