use of org.infinispan.query.test.Person in project infinispan by infinispan.
the class ClusteredCacheTest method testMerge.
public void testMerge() throws Exception {
prepareTestData();
TransactionManager transactionManager = cache2.getAdvancedCache().getTransactionManager();
assertEquals(3, createSelectAllQuery(cache2).execute().list().size());
String key = "newGoat";
person4 = new Person(key, "eats something", 42);
// merge a new key
if (transactionsEnabled())
transactionManager.begin();
cache2.merge(key, person4, (k1, v3) -> new Person(key, "eats something", 42));
if (transactionsEnabled())
transactionManager.commit();
StaticTestingErrorHandler.assertAllGood(cache1, cache2);
List<Person> found = createSelectAllQuery(cache2).execute().list();
assertEquals(4, found.size());
assertTrue(found.contains(person4));
// merge and replace existing key
if (transactionsEnabled())
transactionManager.begin();
cache2.merge(key, new Person(key, "hola", 42), (v1, v2) -> new Person(v1.getName() + "_" + v2.getName(), v1.getBlurb() + "_" + v2.getBlurb(), v1.getAge() + v2.getAge()));
if (transactionsEnabled())
transactionManager.commit();
StaticTestingErrorHandler.assertAllGood(cache1, cache2);
found = createSelectAllQuery(cache2).execute().list();
assertEquals(4, found.size());
assertFalse(found.contains(person4));
assertTrue(found.contains(new Person("newGoat_newGoat", "eats something_hola", 84)));
// remove if merge result is null
if (transactionsEnabled())
transactionManager.begin();
cache2.merge(key, person4, (k, v) -> null);
if (transactionsEnabled())
transactionManager.commit();
StaticTestingErrorHandler.assertAllGood(cache1, cache2);
found = createSelectAllQuery(cache2).execute().list();
assertEquals(3, found.size());
assertFalse(found.contains(person4));
assertFalse(found.contains(new Person("newGoat_newGoat", "eats something_hola", 84)));
}
use of org.infinispan.query.test.Person in project infinispan by infinispan.
the class ClusteredCacheTest method testComputeIfAbsent.
public void testComputeIfAbsent() throws Exception {
prepareTestData();
TransactionManager transactionManager = cache2.getAdvancedCache().getTransactionManager();
assertEquals(3, createSelectAllQuery(cache2).execute().list().size());
String key = "newGoat";
person4 = new Person(key, "eats something", 42);
// compute a new key
Function<Object, Person> mappingFunction = (Function<Object, Person> & Serializable) k -> new Person(k.toString(), "eats something", 42);
if (transactionsEnabled())
transactionManager.begin();
cache2.computeIfAbsent(key, mappingFunction);
if (transactionsEnabled())
transactionManager.commit();
StaticTestingErrorHandler.assertAllGood(cache1, cache2);
List<Person> found = createSelectAllQuery(cache2).execute().list();
assertEquals(4, found.size());
assertTrue(found.contains(person4));
// do nothing for existing keys
if (transactionsEnabled())
transactionManager.begin();
cache2.computeIfAbsent(key1, mappingFunction);
if (transactionsEnabled())
transactionManager.commit();
StaticTestingErrorHandler.assertAllGood(cache1, cache2);
found = createSelectAllQuery(cache2).execute().list();
assertEquals(4, found.size());
assertFalse(found.contains(new Person(key1, "eats something", 42)));
assertTrue(found.contains(person1));
// do nothing if null
Function<Object, Person> mappingToNull = (Function<Object, Person> & Serializable) k -> null;
if (transactionsEnabled())
transactionManager.begin();
cache2.computeIfAbsent("anotherKey", mappingToNull);
if (transactionsEnabled())
transactionManager.commit();
StaticTestingErrorHandler.assertAllGood(cache1, cache2);
found = createSelectAllQuery(cache2).execute().list();
assertEquals(4, found.size());
assertFalse(found.contains(null));
}
use of org.infinispan.query.test.Person in project infinispan by infinispan.
the class ClusteredCacheTest method testPutAsync.
public void testPutAsync() throws Exception {
prepareTestData();
assertEquals(3, createSelectAllQuery(cache2).execute().list().size());
person4 = new Person();
person4.setName("New Goat");
person4.setBlurb("Also eats grass");
Future<Person> f = cache2.putAsync("newGoat", person4);
f.get();
assertTrue(f.isDone());
List<Person> found = createSelectAllQuery(cache2).execute().list();
assertEquals(4, found.size());
assertTrue(found.contains(person4));
Person person5 = new Person();
person5.setName("Abnormal Goat");
person5.setBlurb("Plays with grass.");
f = cache2.putAsync("newGoat", person5);
f.get();
assertTrue(f.isDone());
found = createSelectAllQuery(cache2).execute().list();
assertEquals(4, found.size());
assertFalse(found.contains(person4));
assertTrue(found.contains(person5));
StaticTestingErrorHandler.assertAllGood(cache1, cache2);
}
use of org.infinispan.query.test.Person in project infinispan by infinispan.
the class ClusteredCacheTest method testAdded.
public void testAdded() throws Exception {
prepareTestData();
cacheQuery = createQuery(cache2, "blurb:'eats'");
List<Person> found = cacheQuery.execute().list();
assertEquals(2, found.size());
assertTrue(found.contains(person2));
assertTrue(found.contains(person3));
assertFalse("This should not contain object person4", found.contains(person4));
person4 = new Person();
person4.setName("Mighty Goat");
person4.setBlurb("Also eats grass");
cache1.put("mighty", person4);
cacheQuery = createQuery(cache2, "blurb:'eats'");
found = cacheQuery.execute().list();
assertEquals(3, found.size());
assertTrue(found.contains(person2));
assertTrue(found.contains(person3));
assertTrue("This should now contain object person4", found.contains(person4));
StaticTestingErrorHandler.assertAllGood(cache1, cache2);
}
use of org.infinispan.query.test.Person in project infinispan by infinispan.
the class ClusteredCacheTest method testComputeIfPresent.
public void testComputeIfPresent() throws Exception {
prepareTestData();
TransactionManager transactionManager = cache2.getAdvancedCache().getTransactionManager();
assertEquals(3, createSelectAllQuery(cache2).execute().list().size());
// compute and replace existing key
BiFunction<Object, Person, Person> remappingExisting = (BiFunction<Object, Person, Person> & Serializable) (k, v) -> new Person("replaced", "personOneChanged", 42);
if (transactionsEnabled())
transactionManager.begin();
cache2.computeIfPresent(key1, remappingExisting);
if (transactionsEnabled())
transactionManager.commit();
StaticTestingErrorHandler.assertAllGood(cache1, cache2);
List<Person> found = createSelectAllQuery(cache2).execute().list();
assertEquals(3, found.size());
assertFalse(found.contains(person1));
assertTrue(found.contains(new Person("replaced", "personOneChanged", 42)));
String newKey = "newGoat";
person4 = new Person(newKey, "eats something", 42);
// do nothing for non existing keys
BiFunction<Object, Person, Person> remappingFunction = (BiFunction<Object, Person, Person> & Serializable) (k, v) -> new Person(k.toString(), "eats something", 42);
if (transactionsEnabled())
transactionManager.begin();
cache2.computeIfPresent(newKey, remappingFunction);
if (transactionsEnabled())
transactionManager.commit();
StaticTestingErrorHandler.assertAllGood(cache1, cache2);
found = createSelectAllQuery(cache2).execute().list();
assertEquals(3, found.size());
assertFalse(found.contains(person4));
// remove if compute result is null
BiFunction<Object, Person, Person> remappingToNull = (BiFunction<Object, Person, Person> & Serializable) (k, v) -> null;
if (transactionsEnabled())
transactionManager.begin();
cache2.computeIfPresent(key2, remappingToNull);
if (transactionsEnabled())
transactionManager.commit();
StaticTestingErrorHandler.assertAllGood(cache1, cache2);
found = createSelectAllQuery(cache2).execute().list();
assertFalse(found.contains(person2));
}
Aggregations