diff --git a/apache-geode/src/main/java/com/baeldung/geode/functions/UpperCaseNames.java b/apache-geode/src/main/java/com/baeldung/geode/functions/UpperCaseNames.java new file mode 100644 index 0000000000..5ff8e53da8 --- /dev/null +++ b/apache-geode/src/main/java/com/baeldung/geode/functions/UpperCaseNames.java @@ -0,0 +1,34 @@ +package com.baeldung.geode.functions; + +import com.baeldung.geode.Customer; +import com.baeldung.geode.CustomerKey; +import org.apache.geode.cache.Region; +import org.apache.geode.cache.execute.Function; +import org.apache.geode.cache.execute.FunctionContext; +import org.apache.geode.cache.execute.RegionFunctionContext; + +import java.util.Map; + +public class UpperCaseNames implements Function { + private static final long serialVersionUID = -8946294032165677602L; + + @Override + public void execute(FunctionContext context) { + RegionFunctionContext regionContext = (RegionFunctionContext) context; + Region region = regionContext.getDataSet(); + + for (Map.Entry entry : region.entrySet()) { + Customer customer = entry.getValue(); + customer.setFirstName(customer.getFirstName() + .toUpperCase()); + } + + context.getResultSender() + .lastResult(true); + } + + @Override + public String getId() { + return getClass().getName(); + } +} diff --git a/apache-geode/src/test/java/com/baeldung/geode/GeodeSamplesIntegrationTest.java b/apache-geode/src/test/java/com/baeldung/geode/GeodeSamplesIntegrationTest.java index 9c47d099da..eb9affde8e 100644 --- a/apache-geode/src/test/java/com/baeldung/geode/GeodeSamplesIntegrationTest.java +++ b/apache-geode/src/test/java/com/baeldung/geode/GeodeSamplesIntegrationTest.java @@ -2,6 +2,7 @@ package com.baeldung.geode; import com.baeldung.geode.functions.CustomerWithMaxAge; import com.baeldung.geode.functions.PrimeNumber; +import com.baeldung.geode.functions.UpperCaseNames; import org.apache.geode.cache.Region; import org.apache.geode.cache.client.ClientCache; import org.apache.geode.cache.client.ClientCacheFactory; @@ -28,10 +29,8 @@ public class GeodeSamplesIntegrationTest { ClientCache cache = null; Region region = null; - Region partitionedRegion = null; Region queryRegion = null; - Region functionRegion = null; - Region customKeyRegion = null; + Region customerRegion = null; @Before public void connect() { @@ -39,14 +38,8 @@ public class GeodeSamplesIntegrationTest { .create(); this.region = this.cache. createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY) .create("baeldung"); - this.partitionedRegion = this.cache. createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY) - .create("baeldung-partition"); - this.queryRegion = this.cache. createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY) - .create("baeldung-oql"); - this.functionRegion = this.cache. createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY) - .create("baeldung-function"); - this.customKeyRegion = this.cache. createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY) - .create("baeldung-custom"); + this.customerRegion = this.cache. createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY) + .create("baeldung-customers"); } @After @@ -87,88 +80,35 @@ public class GeodeSamplesIntegrationTest { Map customerInfo = new HashMap<>(); customerInfo.put(key, customer); - this.customKeyRegion.putAll(customerInfo); + this.customerRegion.putAll(customerInfo); - Customer storedCustomer = this.customKeyRegion.get(key); + Customer storedCustomer = this.customerRegion.get(key); assertEquals("William", storedCustomer.getFirstName()); assertEquals("Russell", storedCustomer.getLastName()); } - @Test - public void whenSaveCustomerDataOnPartitionedRegion_thenDataSavedCorrectly() { - Customer customer1 = new Customer(new CustomerKey(1l), "Gheorge", "Manuc", 36); - Customer customer2 = new Customer(new CustomerKey(2l), "Allan", "McDowell", 43); - Customer customer3 = new Customer(new CustomerKey(3l), "Alan", "McClean", 23); - Customer customer4 = new Customer(new CustomerKey(4l), "Allan", "Donald", 46); - - Map customerData = new HashMap<>(); - customerData.put(1, customer1); - customerData.put(2, customer2); - customerData.put(3, customer3); - customerData.put(4, customer4); - - this.partitionedRegion.putAll(customerData); - // assert the size on the cache server. - assertEquals(4, this.partitionedRegion.sizeOnServer()); - } - @Test public void whenFindACustomerUsingOQL_thenCorrectCustomerObject() throws NameResolutionException, TypeMismatchException, QueryInvocationTargetException, FunctionDomainException { - Customer customer1 = new Customer("Gheorge", "Manuc", 36); - Customer customer2 = new Customer("Allan", "McDowell", 43); - Customer customer3 = new Customer("Alan", "McClean", 23); - Customer customer4 = new Customer("Allan", "Donald", 46); - - Map customerData = new HashMap<>(); - customerData.put(1, customer1); - customerData.put(2, customer2); - customerData.put(3, customer3); - customerData.put(4, customer4); - - this.queryRegion.putAll(customerData); - // assert the size on the cache server. - assertEquals(4, this.queryRegion.sizeOnServer()); + Map data = new HashMap<>(); + data.put(new CustomerKey(1), new Customer("Gheorge", "Manuc", 36)); + data.put(new CustomerKey(2), new Customer("Allan", "McDowell", 43)); + this.customerRegion.putAll(data); QueryService queryService = this.cache.getQueryService(); - String query = "select * from /baeldung-oql c where c.firstName = 'Allan'"; + String query = "select * from /baeldung-customers c where c.firstName = 'Allan'"; SelectResults queryResults = (SelectResults) queryService.newQuery(query) .execute(); - assertEquals(2, queryResults.size()); + assertEquals(1, queryResults.size()); } @Test - public void whenExecuteFindEldestCustomerFunction_thenReturnTheEldestCustomer() { - Execution execution = FunctionService.onRegion(this.queryRegion); - - ResultCollector result = execution.execute(CustomerWithMaxAge.ID); - List resultList = (List) result.getResult(); - assertNotNull(resultList); - assertEquals(1, resultList.size()); - - Customer customer = resultList.get(0); - assertEquals(Integer.valueOf(46), customer.getAge()); + public void whenExecuteUppercaseNames_thenCustomerNamesAreUppercased() { + Execution execution = FunctionService.onRegion(this.customerRegion); + execution.execute(UpperCaseNames.class.getName()); + Customer customer = this.customerRegion.get(new CustomerKey(1)); + assertEquals("GHEORGE", customer.getFirstName()); } - - @Test - public void whenExecutePrimeNumberFunction_thenReturnOnlyPrimeNumbers() { - - Execution execution = FunctionService.onRegion(this.functionRegion); - - IntStream.rangeClosed(1, 5) - .forEach(i -> this.functionRegion.put(i, String.valueOf(i))); - - ResultCollector results = execution.execute(PrimeNumber.ID); - Set primes = new HashSet<>(); - List resultList = results.getResult(); - assertNotNull(resultList); - assertEquals(1, resultList.size()); - - primes.addAll((List) resultList.iterator() - .next()); - assertEquals(4, primes.size()); - } - }