From b6816bdcdd66540f483403a273f2862e1820fb57 Mon Sep 17 00:00:00 2001 From: Sandip Singh Date: Fri, 24 Aug 2018 23:50:40 +0530 Subject: [PATCH] BAEL-1466 Updated the Integration Tests. --- .../geode/GeodeSamplesIntegrationTest.java | 112 ++++++++++-------- 1 file changed, 64 insertions(+), 48 deletions(-) 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 5445772259..1eb71671a1 100644 --- a/apache-geode/src/test/java/com/baeldung/geode/GeodeSamplesIntegrationTest.java +++ b/apache-geode/src/test/java/com/baeldung/geode/GeodeSamplesIntegrationTest.java @@ -9,62 +9,93 @@ import org.apache.geode.cache.execute.Execution; import org.apache.geode.cache.execute.FunctionService; import org.apache.geode.cache.execute.ResultCollector; import org.apache.geode.cache.query.*; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import java.util.*; import java.util.function.Function; +import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.IntStream; +import java.util.stream.Stream; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; public class GeodeSamplesIntegrationTest { + ClientCache cache = null; + Region region = null; + Region partitionedRegion = null; + Region queryRegion = null; + Region functionRegion = null; + Region customKeyRegion = null; + + @Before + public void connect() { + this.cache = new ClientCacheFactory().addPoolLocator("localhost", 10334) + .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"); + } + + @After + public void cleanup() { + this.cache.close(); + } + @Test public void whenSendMessageToRegion_thenMessageSavedSuccessfully() { - ClientCache cache = new ClientCacheFactory().addPoolLocator("localhost", 10334) - .create(); - Region region = cache. createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY) - .create("baeldung"); - region.put("1", "Hello"); - region.put("2", "Baeldung"); + this.region.put("1", "Hello"); + this.region.put("2", "Baeldung"); assertEquals("Hello", region.get("1")); assertEquals("Baeldung", region.get("2")); - cache.close(); } @Test public void whenPutMultipleValuesAtOnce_thenValuesSavedSuccessfully() { - ClientCache cache = new ClientCacheFactory().addPoolLocator("localhost", 10334) - .create(); - Region region = cache. createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY) - .create("baeldung"); - Map values = IntStream.rangeClosed(1, 5) - .mapToObj(String::valueOf) - .collect(Collectors.toMap(Function.identity(), i -> "value" + i)); + Supplier> keys = () -> Stream.of("A", "B", "C", "D", "E"); + Map values = keys.get() + .collect(Collectors.toMap(Function.identity(), String::toLowerCase)); - region.putAll(values); + this.region.putAll(values); + + keys.get() + .forEach(k -> assertEquals(k.toLowerCase(), this.region.get(k))); + + } + + @Test + public void whenPutCustomKey_thenValuesSavedSuccessfully() { + CustomerKey key = new CustomerKey(123); + Customer customer = new Customer(key, "William", "Russell", 35); + + Map customerInfo = new HashMap<>(); + customerInfo.put(key, customer); + + this.customKeyRegion.putAll(customerInfo); + + Customer storedCustomer = this.customKeyRegion.get(key); + assertEquals("William", storedCustomer.getFirstName()); + assertEquals("Russell", storedCustomer.getLastName()); - IntStream.rangeClosed(1, 5) - .mapToObj(String::valueOf) - .forEach(e -> { - assertEquals("value".concat(e), region.get(e)); - }); - cache.close(); } @Test public void whenSaveCustomerDataOnPartitionedRegion_thenDataSavedCorrectly() { - ClientCache cache = new ClientCacheFactory().addPoolLocator("localhost", 10334) - .create(); - Region region = cache. createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY) - .create("baeldung-partition"); - 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); @@ -76,18 +107,13 @@ public class GeodeSamplesIntegrationTest { customerData.put(3, customer3); customerData.put(4, customer4); - region.putAll(customerData); + this.partitionedRegion.putAll(customerData); // assert the size on the cache server. - assertEquals(4, region.sizeOnServer()); - cache.close(); + assertEquals(4, this.partitionedRegion.sizeOnServer()); } @Test public void whenFindACustomerUsingOQL_thenCorrectCustomerObject() throws NameResolutionException, TypeMismatchException, QueryInvocationTargetException, FunctionDomainException { - ClientCache cache = new ClientCacheFactory().addPoolLocator("localhost", 10334) - .create(); - Region region = cache. createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY) - .create("baeldung-oql"); Customer customer1 = new Customer("Gheorge", "Manuc", 36); Customer customer2 = new Customer("Allan", "McDowell", 43); @@ -100,34 +126,25 @@ public class GeodeSamplesIntegrationTest { customerData.put(3, customer3); customerData.put(4, customer4); - region.putAll(customerData); + this.queryRegion.putAll(customerData); // assert the size on the cache server. - assertEquals(4, region.sizeOnServer()); + assertEquals(4, this.queryRegion.sizeOnServer()); - QueryService queryService = cache.getQueryService(); + QueryService queryService = this.cache.getQueryService(); String query = "select * from /baeldung-oql c where c.firstName = 'Allan'"; SelectResults queryResults = (SelectResults) queryService.newQuery(query) .execute(); assertEquals(2, queryResults.size()); - cache.close(); - } @Test public void whenExecutePrimeNumberFunction_thenReturnOnlyPrimeNumbers() { - // connect to the locator using default port 10334 - ClientCache cache = new ClientCacheFactory().addPoolLocator("localhost", 10334) - .create(); - // create a local region that matches the server region - Region region = cache. createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY) - .create("baeldung-function"); - - Execution execution = FunctionService.onRegion(region); + Execution execution = FunctionService.onRegion(this.functionRegion); IntStream.rangeClosed(1, 5) - .forEach(i -> region.put(i, String.valueOf(i))); + .forEach(i -> this.functionRegion.put(i, String.valueOf(i))); ResultCollector results = execution.execute(PrimeNumber.ID); Set primes = new HashSet<>(); @@ -138,7 +155,6 @@ public class GeodeSamplesIntegrationTest { primes.addAll((List) resultList.iterator() .next()); assertEquals(4, primes.size()); - cache.close(); } }