Search in sources :

Example 1 with ClientCache

use of org.apache.geode.cache.client.ClientCache in project geode by apache.

the class ClientSnapshotDUnitTest method loadCache.

@SuppressWarnings("serial")
public void loadCache() throws Exception {
    CacheFactory cf = new CacheFactory().setPdxSerializer(new MyPdxSerializer());
    Cache cache = getCache(cf);
    CacheServer server = cache.addCacheServer();
    final int port = AvailablePortHelper.getRandomAvailableTCPPort();
    server.setPort(port);
    server.start();
    region = cache.<Integer, MyObject>createRegionFactory(RegionShortcut.REPLICATE).create("clienttest");
    final Host host = Host.getHost(0);
    SerializableCallable client = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            ClientCacheFactory cf = new ClientCacheFactory().set(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel()).setPdxSerializer(new MyPdxSerializer()).addPoolServer(NetworkUtils.getServerHostName(host), port).setPoolSubscriptionEnabled(true).setPoolPRSingleHopEnabled(false);
            ClientCache cache = getClientCache(cf);
            Region r = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY_HEAP_LRU).setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(5)).create("clienttest");
            return null;
        }
    };
    SerializableCallable remote = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            CacheFactory cf = new CacheFactory().setPdxSerializer(new MyPdxSerializer());
            Cache cache = getCache(cf);
            cache.<Integer, MyObject>createRegionFactory(RegionShortcut.REPLICATE).create("clienttest");
            return null;
        }
    };
    host.getVM(3).invoke(client);
    host.getVM(2).invoke(remote);
}
Also used : MyPdxSerializer(com.examples.snapshot.MyPdxSerializer) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) CacheServer(org.apache.geode.cache.server.CacheServer) MyObject(com.examples.snapshot.MyObject) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) ClientCache(org.apache.geode.cache.client.ClientCache) CacheFactory(org.apache.geode.cache.CacheFactory) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory)

Example 2 with ClientCache

use of org.apache.geode.cache.client.ClientCache in project geode by apache.

the class CQClientAuthDUnitTest method testPostProcess.

@Test
public void testPostProcess() {
    String query = "select * from /" + REGION_NAME;
    client1.invoke(() -> {
        Properties props = new Properties();
        props.setProperty(LOCATORS, "");
        props.setProperty(MCAST_PORT, "0");
        props.setProperty(SECURITY_CLIENT_AUTH_INIT, UserPasswordAuthInit.class.getName() + ".create");
        ClientCacheFactory factory = new ClientCacheFactory(props);
        factory.addPoolServer("localhost", server.getPort());
        factory.setPoolThreadLocalConnections(false);
        factory.setPoolMinConnections(5);
        factory.setPoolSubscriptionEnabled(true);
        factory.setPoolMultiuserAuthentication(true);
        ClientCache clientCache = factory.create();
        Region region = clientCache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(REGION_NAME);
        Pool pool = PoolManager.find(region);
        Properties userProps = new Properties();
        userProps.setProperty("security-username", "super-user");
        userProps.setProperty("security-password", "1234567");
        ProxyCache cache = (ProxyCache) clientCache.createAuthenticatedView(userProps, pool.getName());
        QueryService qs = cache.getQueryService();
        CqAttributesFactory cqAttributesFactory = new CqAttributesFactory();
        CqAttributes cqa = cqAttributesFactory.create();
        // Create the CqQuery
        CqQuery cq = qs.newCq("CQ1", query, cqa, true);
        cq.execute();
    });
}
Also used : QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) ProxyCache(org.apache.geode.cache.client.internal.ProxyCache) 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) CqQuery(org.apache.geode.cache.query.CqQuery) Properties(java.util.Properties) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) SecurityTest(org.apache.geode.test.junit.categories.SecurityTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 3 with ClientCache

use of org.apache.geode.cache.client.ClientCache 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 4 with ClientCache

use of org.apache.geode.cache.client.ClientCache 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 5 with ClientCache

use of org.apache.geode.cache.client.ClientCache in project geode by apache.

the class ClientQueryAuthDUnitTest method testQuery.

@Test
public void testQuery() {
    client1.invoke(() -> {
        ClientCache cache = createClientCache("stranger", "1234567", server.getPort());
        final Region region = createProxyRegion(cache, REGION_NAME);
        String query = "select * from /AuthRegion";
        assertNotAuthorized(() -> region.query(query), "DATA:READ:AuthRegion");
        Pool pool = PoolManager.find(region);
        assertNotAuthorized(() -> pool.getQueryService().newQuery(query).execute(), "DATA:READ:AuthRegion");
    });
}
Also used : SecurityTestUtil.createProxyRegion(org.apache.geode.security.SecurityTestUtil.createProxyRegion) Region(org.apache.geode.cache.Region) Pool(org.apache.geode.cache.client.Pool) ClientCache(org.apache.geode.cache.client.ClientCache) SecurityTestUtil.createClientCache(org.apache.geode.security.SecurityTestUtil.createClientCache) SecurityTest(org.apache.geode.test.junit.categories.SecurityTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

ClientCache (org.apache.geode.cache.client.ClientCache)115 Test (org.junit.Test)74 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)73 ClientCacheFactory (org.apache.geode.cache.client.ClientCacheFactory)67 Region (org.apache.geode.cache.Region)65 VM (org.apache.geode.test.dunit.VM)43 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)42 Host (org.apache.geode.test.dunit.Host)40 SecurityTest (org.apache.geode.test.junit.categories.SecurityTest)31 QueryService (org.apache.geode.cache.query.QueryService)27 SelectResults (org.apache.geode.cache.query.SelectResults)26 SecurityTestUtil.createClientCache (org.apache.geode.security.SecurityTestUtil.createClientCache)25 SecurityTestUtil.createProxyRegion (org.apache.geode.security.SecurityTestUtil.createProxyRegion)23 CacheServer (org.apache.geode.cache.server.CacheServer)22 Cache (org.apache.geode.cache.Cache)20 CacheException (org.apache.geode.cache.CacheException)15 PortfolioPdx (org.apache.geode.cache.query.data.PortfolioPdx)13 IOException (java.io.IOException)12 Properties (java.util.Properties)12 IgnoredException (org.apache.geode.test.dunit.IgnoredException)12