britt / hivedb

HiveDB is an open source project for horizontally partitioning MySQL systems.

This URL has Read+Write access

hivedb / src / test / java / org / hivedb / hibernate / BaseDataAccessObjectTest.java
100644 505 lines (451 sloc) 23.073 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package org.hivedb.hibernate;
 
import org.hibernate.shards.strategy.access.SequentialShardAccessStrategy;
import org.hivedb.Hive;
import org.hivedb.HiveLockableException;
import org.hivedb.util.classgen.GenerateInstance;
import org.hivedb.util.classgen.GeneratedClassFactory;
import org.hivedb.util.classgen.GeneratedInstanceInterceptor;
import org.hivedb.util.classgen.ReflectionTools;
import org.hivedb.util.database.test.HiveTest;
import org.hivedb.util.database.test.HiveTest.Config;
import org.hivedb.util.database.test.WeatherReport;
import org.hivedb.util.functional.*;
import org.junit.Assert;
import static org.junit.Assert.*;
import org.junit.Test;
 
import java.util.*;
import java.util.Map.Entry;
 
@Config("hive_default")
public class BaseDataAccessObjectTest extends HiveTest {
 
  private static Random random = new Random();
 
  @SuppressWarnings("unchecked")
  @Test
  public void testGet() throws Exception {
    DataAccessObject<WeatherReport, Integer> dao = (DataAccessObject<WeatherReport, Integer>) getDao(getGeneratedClass());
    WeatherReport original = getPersistentInstance(dao);
    WeatherReport report = dao.get(original.getReportId());
    assertEquals(ReflectionTools.getDifferingFields(original, report, WeatherReport.class).toString(), original.hashCode(), report.hashCode());
  }
 
  @SuppressWarnings("unchecked")
  @Test
  public void testGetMissingRecord() throws Exception {
    DataAccessObject<WeatherReport, Integer> dao = (DataAccessObject<WeatherReport, Integer>) getDao(getGeneratedClass());
    WeatherReport original = getInstance(WeatherReport.class);
    HiveIndexer indexer = new HiveIndexer(getHive());
    indexer.insert(getEntityHiveConfig().getEntityConfig(WeatherReport.class), original);
    assertTrue(dao.exists(original.getReportId()));
    WeatherReport report = dao.get(original.getReportId());
    assertFalse(dao.exists(original.getReportId()));
    assertEquals(null, report);
// assertEquals(ReflectionTools.getDifferingFields(original, report, WeatherReport.class).toString(), original.hashCode(), report.hashCode());
  }
 
  @SuppressWarnings("unchecked")
  @Test
  public void testFindByProperty() throws Exception {
    DataAccessObject<WeatherReport, Integer> dao = (DataAccessObject<WeatherReport, Integer>) getDao(getGeneratedClass());
    WeatherReport report = new GenerateInstance<WeatherReport>(WeatherReport.class).generate();
    dao.save(report);
    int temperature = random.nextInt();
    GeneratedInstanceInterceptor.setProperty(report, "temperature", temperature);
    dao.save(report);
    WeatherReport found = Atom.getFirstOrThrow(dao.findByProperty("temperature", temperature));
    Assert.assertEquals(report.hashCode(), found.hashCode());
    found = Atom.getFirstOrThrow(dao.findByProperty("regionCode", report.getRegionCode()));
    assertEquals(report.hashCode(), found.hashCode());
    found = Atom.getFirstOrThrow(dao.findByProperty("weatherEvents", Atom.getFirstOrThrow(report.getWeatherEvents()).getEventId()));
    assertEquals(report.hashCode(), found.hashCode());
    found = Atom.getFirstOrThrow(dao.findByProperty("continent", report.getContinent()));
    assertEquals(report.hashCode(), found.hashCode());
    found = Atom.getFirstOrThrow(dao.findByProperty("sources", Atom.getFirstOrThrow(report.getSources())));
    assertEquals(report.hashCode(), found.hashCode());
    // Test find by multiple properties
    found = Atom.getFirstOrThrow(dao.findByProperties("regionCode", Transform.toMap(
        new Entry[]{
            new Pair<String, Object>("regionCode", report.getRegionCode()),
            new Pair<String, Object>("weatherEvents", Atom.getFirstOrThrow(report.getWeatherEvents()).getEventId()),
        })));
    assertEquals(report.hashCode(), found.hashCode());
  }
 
  @SuppressWarnings("unchecked")
  @Test
  public void testFindByPropertyPaged() throws Exception {
    final DataAccessObject<WeatherReport, Integer> dao = (DataAccessObject<WeatherReport, Integer>) getDao(getGeneratedClass());
 
    Collection<WeatherReport> reports =
        Generate.create(new Generator<WeatherReport>() {
          public WeatherReport generate() {
            WeatherReport report = new GenerateInstance<WeatherReport>(WeatherReport.class).generate();
            GeneratedInstanceInterceptor.setProperty(report, "continent", "Derkaderkastan");
            GeneratedInstanceInterceptor.setProperty(report, "temperature", 101);
            GeneratedInstanceInterceptor.setProperty(report, "sources", Arrays.asList(new Integer[]{101, 102, 103}));
            return dao.save(report);
          }
        }, new NumberIterator(12));
 
    for (final String property : new String[]{"temperature", "sources"}) {
      Assert.assertEquals(dao.findByProperty(property, 101).size(), 12);
      final Collection<WeatherReport> results = Filter.grepUnique(Transform.flatten(Transform.map(new Unary<Integer, Collection<WeatherReport>>() {
        public Collection<WeatherReport> f(Integer i) {
          return dao.findByProperty(property, 101, (i - 1) * 4, 4);
        }
      }, new NumberIterator(3))));
      Assert.assertEquals(
          new HashSet<WeatherReport>(results).hashCode(),
          new HashSet(reports).hashCode());
    }
  }
 
  @SuppressWarnings("unchecked")
  @Test
  public void testFindByPropertyRange() throws Exception {
 
    Collection<WeatherReport> reports =
        Generate.create(new Generator<WeatherReport>() {
          public WeatherReport generate() {
            return new GenerateInstance<WeatherReport>(WeatherReport.class).generate();
          }
        }, new NumberIterator(5));
 
    DataAccessObject<WeatherReport, Integer> dao = (DataAccessObject<WeatherReport, Integer>) getDao(getGeneratedClass());
    dao.saveAll(reports);
    //Collection<WeatherReport> x = dao.findByProperty("temperature", Atom.getFirst(reports).getTemperature());
    Integer min = Amass.min(
        new Unary<WeatherReport, Integer>() {
          public Integer f(WeatherReport weatherReport) {
            return weatherReport.getTemperature();
          }
        },
        reports,
        Integer.class);
    Integer max = Amass.max(
        new Unary<WeatherReport, Integer>() {
          public Integer f(WeatherReport weatherReport) {
            return weatherReport.getTemperature();
          }
        },
        reports,
        Integer.class);
    Collection<WeatherReport> range = dao.findByPropertyRange("temperature", min, max);
    assertEquals(reports.size(), range.size());
 
    Collection<WeatherReport> smallerRange =
        dao.findByPropertyRange("temperature", Atom.getFirst(reports).getTemperature(), Atom.getFirst(reports).getTemperature());
    assertEquals(1, smallerRange.size());
  }
 
  @SuppressWarnings("unchecked")
  @Test
  public void testFindByPropertyRangePaged() throws Exception {
    final DataAccessObject<WeatherReport, Integer> dao = (DataAccessObject<WeatherReport, Integer>) getDao(getGeneratedClass());
    final int INSTANCE_COUNT = 12;
    Collection<WeatherReport> set = new HashSet<WeatherReport>();
    int min = 0, max = 0;
    for (int i = 0; i < INSTANCE_COUNT; i++) {
      int temperature = random.nextInt();
      min = Math.min(min, temperature);
      max = Math.max(max, temperature);
      WeatherReport report = new GenerateInstance<WeatherReport>(WeatherReport.class).generate();
      GeneratedInstanceInterceptor.setProperty(report, "temperature", temperature);
      dao.save(report);
      set.add(report);
    }
    final int finalMin = min;
    final int finalMax = max;
    Assert.assertEquals(
        new HashSet<WeatherReport>(Transform.flatten(Transform.map(new Unary<Integer, Collection<WeatherReport>>() {
          public Collection<WeatherReport> f(Integer i) {
            final Collection<WeatherReport> value = dao.findByPropertyRange("temperature", finalMin, finalMax, (i - 1) * 4, 4);
            return value;
          }
        }, new NumberIterator(3)))).hashCode(),
        set.hashCode());
  }
 
  @SuppressWarnings("unchecked")
  @Test
  public void testGetCount() throws Exception {
 
    final int temperature = random.nextInt();
    final List<String> partitionDimensionKeys = Arrays.asList(new String[]{"Asia", "Andromida"});
    Collection<WeatherReport> reports =
        Generate.create(new Generator<WeatherReport>() {
          public WeatherReport generate() {
            WeatherReport weatherReport = new GenerateInstance<WeatherReport>(WeatherReport.class).generate();
            // Set the same temperature for each partition dimension id. The partition dimension id will be calculated from the report id
            GeneratedInstanceInterceptor.setProperty(weatherReport, "temperature", temperature + weatherReport.getReportId() % 2);
            GeneratedInstanceInterceptor.setProperty(weatherReport, "continent", partitionDimensionKeys.get(weatherReport.getReportId() % 2));
            GeneratedInstanceInterceptor.setProperty(weatherReport, "regionCode", 4);
            return weatherReport;
          }
        }, new NumberIterator(5));
 
    DataAccessObject<WeatherReport, Integer> dao = (DataAccessObject<WeatherReport, Integer>) getDao(getGeneratedClass());
    dao.saveAll(reports);
    Assert.assertEquals(dao.getCount("temperature", temperature) + dao.getCount("temperature", temperature + 1), 5);
    Assert.assertEquals(dao.getCountByRange("temperature", temperature, temperature + 1), (Integer) 5);
    Assert.assertEquals((Integer) (dao.getCountByProperties("temperature", Transform.toMap(
        new Entry[]{
            new Pair<String, Object>("temperature", temperature),
            new Pair<String, Object>("regionCode", 4),
        })) +
        dao.getCountByProperties("temperature", Transform.toMap(
            new Entry[]{
                new Pair<String, Object>("temperature", temperature + 1),
                new Pair<String, Object>("regionCode", 4),
            }))), (Integer) 5);
  }
 
  @SuppressWarnings("unchecked")
  @Test
  public void testDelete() throws Exception {
    DataAccessObject<WeatherReport, Integer> dao = (DataAccessObject<WeatherReport, Integer>) getDao(getGeneratedClass());
    WeatherReport original = getPersistentInstance(dao);
    dao.delete(original.getReportId());
    assertNull(dao.get(original.getReportId()));
  }
 
  @SuppressWarnings("unchecked")
  @Test
  public void testInsert() {
    DataAccessObject<WeatherReport, Integer> dao = (DataAccessObject<WeatherReport, Integer>) getDao(getGeneratedClass());
    WeatherReport report = new GenerateInstance<WeatherReport>(WeatherReport.class).generate();
    dao.save(report);
    WeatherReport savedReport = dao.get(report.getReportId());
    assertNotNull(savedReport);
    assertEquals(report.hashCode(), savedReport.hashCode());
  }
 
  @SuppressWarnings("unchecked")
  private Class getGeneratedClass() {
    return GeneratedClassFactory.newInstance(WeatherReport.class).getClass();
  }
 
  private <T> T getInstance(Class<T> clazz) throws Exception {
    return new GenerateInstance<T>(clazz).generate();
  }
 
  private WeatherReport getPersistentInstance(DataAccessObject<WeatherReport, Integer> dao) throws Exception {
    return dao.save(getInstance(WeatherReport.class));
  }
 
  @SuppressWarnings("unchecked")
  @Test
  public void testUpdate() throws Exception {
    DataAccessObject<WeatherReport, Integer> dao = (DataAccessObject<WeatherReport, Integer>) getDao(getGeneratedClass());
    WeatherReport original = getPersistentInstance(dao);
    WeatherReport updated = dao.get(original.getReportId());
    GeneratedInstanceInterceptor.setProperty(updated, "latitude", new Double(30));
    GeneratedInstanceInterceptor.setProperty(updated, "longitude", new Double(30));
    /* TODO this fails because we currently don't support one-to-many relationships. We need to reenable the
deleteOrphanItems in our BaseDataAccessObject to support this
*/
    /*
// Test collection item updates
List<WeatherEvent> weatherEvents = new ArrayList<WeatherEvent>(original.getWeatherEvents());
// Delete the first
weatherEvents.remove(0);
// Update the second
weatherEvents.get(0).setName("foobar");
// Add a third
weatherEvents.add(new GenerateInstance<WeatherEvent>(WeatherEvent.class).generate());
GeneratedInstanceInterceptor.setProperty(updated, "weatherEvents", weatherEvents);
*/
    dao.save(updated);
    //final WeatherReport persisted = dao.get(updated.getReportId());
    assertFalse(updated.getLatitude().equals(original.getLatitude()));
    // Check the updated collection
    // size should be equal
    //assertEquals(original.getWeatherEvents().size(), persisted.getWeatherEvents().size());
    // first item should be removed
    //assertFalse(Filter.grepItemAgainstList(Atom.getFirst(original.getWeatherEvents()), persisted.getWeatherEvents()));
    // should be an updated item named foobar
    /* assertTrue(Filter.grepItemAgainstList("foobar",
Transform.map(new Unary<WeatherEvent,String>() {
public String f(WeatherEvent weatherEvent) {
return weatherEvent.getName();
}}, persisted.getWeatherEvents()))); */
    // new item should exist
    //assertTrue(Filter.grepItemAgainstList(Atom.getLast(weatherEvents), persisted.getWeatherEvents()));
 
    String newPrimaryIndexKey = findPrimaryKeyOnDifferentNode(original);
    GeneratedInstanceInterceptor.setProperty(updated, "continent", new Integer(newPrimaryIndexKey).toString());
    Assert.assertEquals(getHive().directory().getNodeIdsOfResourceId("WeatherReport", original.getReportId()).size(), 1);
    dao.save(updated);
    final Collection<Integer> nodeIdsOfResourceId = getHive().directory().getNodeIdsOfResourceId("WeatherReport", updated.getReportId());
    // Make sure the resource is only on 1 hive node
    Assert.assertEquals(nodeIdsOfResourceId.size(), 1);
    // Make sure the node matches the new node
    Assert.assertEquals(Atom.getFirstOrThrow(nodeIdsOfResourceId), Atom.getFirstOrThrow(getHive().directory().getNodeIdsOfPrimaryIndexKey(newPrimaryIndexKey)));
    // Make sure the entity can be fetched on the new node
    Assert.assertNotNull(dao.get(updated.getReportId()));
  }
 
  private String findPrimaryKeyOnDifferentNode(WeatherReport original)
      throws HiveLockableException {
    // Update the primary index key
    int i = 1;
    // Get a primary index key on a different node
    for (; i < 100; i++) {
      getHive().directory().insertPrimaryIndexKey(new Integer(i).toString());
      if (!Atom.getFirstOrThrow(getHive().directory().getNodeIdsOfPrimaryIndexKey(new Integer(i).toString())).equals(
          Atom.getFirstOrThrow(getHive().directory().getNodeIdsOfPrimaryIndexKey(original.getContinent()))))
        break;
    }
    return new Integer(i).toString();
  }
 
  @SuppressWarnings("unchecked")
  @Test
  public void testSaveAll() throws Exception {
    Collection<WeatherReport> reports = new ArrayList<WeatherReport>();
    for (int i = 0; i < 54; i++) {
      WeatherReport report = new GenerateInstance<WeatherReport>(WeatherReport.class).generate();
      GeneratedInstanceInterceptor.setProperty(report, "reportId", i);
      reports.add(report);
    }
    DataAccessObject<WeatherReport, Integer> dao = (DataAccessObject<WeatherReport, Integer>) getDao(getGeneratedClass());
    dao.saveAll(reports);
 
    for (WeatherReport report : reports)
      assertEquals(report, dao.get(report.getReportId()));
  }
 
  @SuppressWarnings("unchecked")
  @Test
  public void testHealDataNodeOnlyRecord() throws Exception {
    WeatherReport report = new GenerateInstance<WeatherReport>(WeatherReport.class).generate();
 
    DataAccessObject<WeatherReport, Integer> dao = (DataAccessObject<WeatherReport, Integer>) getDao(getGeneratedClass());
    dao.save(report);
    assertEquals(report, dao.get(report.getReportId()));
    Hive hive = getHive();
    hive.directory().deleteResourceId(config.getEntityConfig(getGeneratedClass()).getResourceName(), report.getReportId());
    ReflectionTools.invokeSetter(report, "regionCode", report.getRegionCode() + 1);
    assertFalse(dao.exists(report.getReportId()));
    dao.save(report);
    assertEquals(report, dao.get(report.getReportId()));
  }
 
  @SuppressWarnings("unchecked")
  @Test
  public void testHealDataNodeOnlyRecordSaveAll() throws Exception {
    Collection<WeatherReport> reports = new ArrayList<WeatherReport>();
    for (int i = 0; i < 5; i++) {
      WeatherReport report = new GenerateInstance<WeatherReport>(WeatherReport.class).generate();
      GeneratedInstanceInterceptor.setProperty(report, "reportId", i);
      reports.add(report);
    }
 
    DataAccessObject<WeatherReport, Integer> dao = (DataAccessObject<WeatherReport, Integer>) getDao(getGeneratedClass());
    dao.saveAll(reports);
    for (WeatherReport report : reports)
      assertEquals(report, dao.get(report.getReportId()));
 
    WeatherReport orphan = Atom.getFirstOrThrow(reports);
 
    Hive hive = getHive();
    hive.directory().deleteResourceId(config.getEntityConfig(getGeneratedClass()).getResourceName(), orphan.getReportId());
    for (WeatherReport report : reports)
      ReflectionTools.invokeSetter(report, "regionCode", report.getRegionCode() + 1);
    assertFalse(dao.exists(orphan.getReportId()));
    dao.saveAll(reports);
 
    for (WeatherReport report : reports)
      assertEquals(report, dao.get(report.getReportId()));
 
    HiveIndexer indexer = new HiveIndexer(getHive());
 
    dao.delete(orphan.getReportId());
    indexer.insert(config.getEntityConfig(dao.getRespresentedClass()), orphan);
 
    dao.saveAll(reports);
    for (WeatherReport report : reports)
      assertEquals(report, dao.get(report.getReportId()));
  }
 
 
  @SuppressWarnings("unchecked")
  @Test
  public void testUpdateAll() throws Exception {
    Collection<WeatherReport> reports = new ArrayList<WeatherReport>();
    for (int i = 0; i < 5; i++) {
      WeatherReport report = new GenerateInstance<WeatherReport>(WeatherReport.class).generate();
      GeneratedInstanceInterceptor.setProperty(report, "reportId", i);
      reports.add(report);
    }
    DataAccessObject<WeatherReport, Integer> dao = (DataAccessObject<WeatherReport, Integer>) getDao(getGeneratedClass());
    dao.saveAll(reports);
 
 
    Collection<WeatherReport> updated = new ArrayList<WeatherReport>();
    for (WeatherReport report : reports) {
      GeneratedInstanceInterceptor.setProperty(report, "temperature", 100);
      updated.add(report);
    }
    dao.saveAll(updated);
 
    for (WeatherReport report : updated) {
      final WeatherReport weatherReport = dao.get(report.getReportId());
      assertEquals(report, weatherReport);
    }
 
    // Test changing the partition dimension key
    String newPrimaryIndexKey = findPrimaryKeyOnDifferentNode(Atom.getFirstOrThrow(reports));
    for (WeatherReport report : reports) {
      GeneratedInstanceInterceptor.setProperty(report, "continent", new Integer(newPrimaryIndexKey).toString());
      Assert.assertEquals(getHive().directory().getNodeIdsOfResourceId("WeatherReport", report.getReportId()).size(), 1);
    }
    dao.saveAll(reports);
    for (WeatherReport report : reports) {
      final Collection<Integer> nodeIdsOfResourceId = getHive().directory().getNodeIdsOfResourceId("WeatherReport", report.getReportId());
      // Make sure the resource is only on 1 hive node
      Assert.assertEquals(nodeIdsOfResourceId.size(), 1);
      // Make sure the node matches the new node
      Assert.assertEquals(Atom.getFirstOrThrow(nodeIdsOfResourceId), Atom.getFirstOrThrow(getHive().directory().getNodeIdsOfPrimaryIndexKey(newPrimaryIndexKey)));
      // Make sure the entity can be fetched on the new node
      Assert.assertNotNull(dao.get(report.getReportId()));
    }
  }
 
  @SuppressWarnings("unchecked")
  @Test
  public void testExists() throws Exception {
    DataAccessObject<WeatherReport, Integer> dao = (DataAccessObject<WeatherReport, Integer>) getDao(getGeneratedClass());
    assertFalse(dao.exists(88));
    WeatherReport original = getPersistentInstance(dao);
    assertTrue(dao.exists(original.getReportId()));
  }
 
  @SuppressWarnings("unchecked")
  @Test
  public void testAllShardsQuery() {
    final DataAccessObject<WeatherReport, Integer> dao = (DataAccessObject<WeatherReport, Integer>) getDao(getGeneratedClass());
    final int INSTANCES = 20;
    Collection<WeatherReport> reports = Generate.create(new Generator<WeatherReport>() {
      public WeatherReport generate() {
        WeatherReport report = new GenerateInstance<WeatherReport>(WeatherReport.class).generate();
        GeneratedInstanceInterceptor.setProperty(report, "latitude", 1d);
        GeneratedInstanceInterceptor.setProperty(report, "regionCode", 1); // used to verify node spread
        dao.save(report);
        return report;
      }
    }, new NumberIterator(INSTANCES));
 
    Hive hive = getHive();
    // Make sure we saved to > 1 node
    Assert.assertTrue(1 < hive.directory().getNodeIdsOfSecondaryIndexKey("WeatherReport", "regionCode", Atom.getFirstOrThrow(reports).getRegionCode()).size());
    // Make sure all instances are found across nodes
    Assert.assertEquals(INSTANCES, dao.findByProperty("latitude", 1d).size());
  }
 
  /**
* Test illustrating bug 6520-6522.
* <p/>
* The count code issues a count query to each node. The broken code was then looking only at the count returned by the first node. This was
* only the correct behavior 1/n percent of the time. The correct behavior is to sum the counts from all the nodes.
*/
  @Test
  public void shouldSumCountsFromAllNodes() throws Exception {
    final QueryCountStubDAO dao = new QueryCountStubDAO(getGeneratedClass());
 
    assertEquals(5, dao.getCount("foo", "bar").intValue());
    assertEquals(5, dao.getCount("foo", "bar").intValue());
    assertEquals(5, dao.getCount("foo", "bar").intValue());
 
    assertEquals(3, dao.callCount);
  }
 
  /**
* Overrides the counts returned in support of the shouldSumCountsFromAllNodes test.
*/
  public class QueryCountStubDAO extends BaseDataAccessObject {
    int callCount = 0;
 
    public QueryCountStubDAO(Class clazz) {
      super(
          config.getEntityConfig(clazz),
          hive,
          new HiveSessionFactoryBuilderImpl(
              config,
              getMappedClasses(),
              hive,
              new SequentialShardAccessStrategy()));
    }
 
    @Override
    protected Collection<Object> queryByProperties(String partitioningPropertyName, Map<String, Object> propertyNameValueMap, Integer firstResult, Integer maxResults, boolean justCount) {
      ArrayList<Object> list = new ArrayList<Object>();
      switch (callCount++) {
        case 0:
          list.add(0);
          list.add(5);
          return list;
        case 1:
          list.add(5);
          list.add(0);
          return list;
        case 2:
          list.add(2);
          list.add(3);
          return list;
        default:
          throw new RuntimeException("unexpect call");
      }
    }
  }
}