|
| 1 | +package com.baeldung.geode; |
| 2 | + |
| 3 | +import com.baeldung.geode.functions.UpperCaseNames; |
| 4 | +import org.apache.geode.cache.Region; |
| 5 | +import org.apache.geode.cache.client.ClientCache; |
| 6 | +import org.apache.geode.cache.client.ClientCacheFactory; |
| 7 | +import org.apache.geode.cache.client.ClientRegionShortcut; |
| 8 | +import org.apache.geode.cache.execute.Execution; |
| 9 | +import org.apache.geode.cache.execute.FunctionService; |
| 10 | +import org.apache.geode.cache.query.*; |
| 11 | +import org.junit.After; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.function.Function; |
| 18 | +import java.util.function.Supplier; |
| 19 | +import java.util.stream.Collectors; |
| 20 | +import java.util.stream.Stream; |
| 21 | + |
| 22 | +import static org.junit.Assert.assertEquals; |
| 23 | + |
| 24 | +public class GeodeSamplesIntegrationTest { |
| 25 | + |
| 26 | + ClientCache cache = null; |
| 27 | + Region<String, String> region = null; |
| 28 | + Region<Integer, Customer> queryRegion = null; |
| 29 | + Region<CustomerKey, Customer> customerRegion = null; |
| 30 | + |
| 31 | + @Before |
| 32 | + public void connect() { |
| 33 | + this.cache = new ClientCacheFactory().addPoolLocator("localhost", 10334) |
| 34 | + .create(); |
| 35 | + this.region = this.cache.<String, String> createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY) |
| 36 | + .create("baeldung"); |
| 37 | + this.customerRegion = this.cache.<CustomerKey, Customer> createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY) |
| 38 | + .create("baeldung-customers"); |
| 39 | + } |
| 40 | + |
| 41 | + @After |
| 42 | + public void cleanup() { |
| 43 | + this.cache.close(); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void whenSendMessageToRegion_thenMessageSavedSuccessfully() { |
| 48 | + |
| 49 | + this.region.put("1", "Hello"); |
| 50 | + this.region.put("2", "Baeldung"); |
| 51 | + |
| 52 | + assertEquals("Hello", region.get("1")); |
| 53 | + assertEquals("Baeldung", region.get("2")); |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void whenPutMultipleValuesAtOnce_thenValuesSavedSuccessfully() { |
| 59 | + |
| 60 | + Supplier<Stream<String>> keys = () -> Stream.of("A", "B", "C", "D", "E"); |
| 61 | + Map<String, String> values = keys.get() |
| 62 | + .collect(Collectors.toMap(Function.identity(), String::toLowerCase)); |
| 63 | + |
| 64 | + this.region.putAll(values); |
| 65 | + |
| 66 | + keys.get() |
| 67 | + .forEach(k -> assertEquals(k.toLowerCase(), this.region.get(k))); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void whenPutCustomKey_thenValuesSavedSuccessfully() { |
| 73 | + CustomerKey key = new CustomerKey(123); |
| 74 | + Customer customer = new Customer(key, "William", "Russell", 35); |
| 75 | + |
| 76 | + Map<CustomerKey, Customer> customerInfo = new HashMap<>(); |
| 77 | + customerInfo.put(key, customer); |
| 78 | + |
| 79 | + this.customerRegion.putAll(customerInfo); |
| 80 | + |
| 81 | + Customer storedCustomer = this.customerRegion.get(key); |
| 82 | + assertEquals("William", storedCustomer.getFirstName()); |
| 83 | + assertEquals("Russell", storedCustomer.getLastName()); |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void whenFindACustomerUsingOQL_thenCorrectCustomerObject() throws NameResolutionException, TypeMismatchException, QueryInvocationTargetException, FunctionDomainException { |
| 89 | + |
| 90 | + Map<CustomerKey, Customer> data = new HashMap<>(); |
| 91 | + data.put(new CustomerKey(1), new Customer("Gheorge", "Manuc", 36)); |
| 92 | + data.put(new CustomerKey(2), new Customer("Allan", "McDowell", 43)); |
| 93 | + this.customerRegion.putAll(data); |
| 94 | + |
| 95 | + QueryService queryService = this.cache.getQueryService(); |
| 96 | + String query = "select * from /baeldung-customers c where c.firstName = 'Allan'"; |
| 97 | + SelectResults<Customer> queryResults = (SelectResults<Customer>) queryService.newQuery(query) |
| 98 | + .execute(); |
| 99 | + assertEquals(1, queryResults.size()); |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void whenExecuteUppercaseNames_thenCustomerNamesAreUppercased() { |
| 105 | + Execution execution = FunctionService.onRegion(this.customerRegion); |
| 106 | + execution.execute(UpperCaseNames.class.getName()); |
| 107 | + Customer customer = this.customerRegion.get(new CustomerKey(1)); |
| 108 | + assertEquals("GHEORGE", customer.getFirstName()); |
| 109 | + } |
| 110 | +} |
0 commit comments