assert results.get(0).getBundleName().equals(b1.getName());
assert results.get(0).getBundleDescription().equals(b1.getDescription());
assert results.get(0).getLatestVersion() == null;
assert results.get(0).getVersionsCount().longValue() == 0L;
BundleVersion bv1 = createBundleVersion(b1.getName() + "-1", "1.0", b1);
assertNotNull(bv1);
assertEquals("1.0", bv1.getVersion());
assert 0 == bv1.getVersionOrder();
results = bundleManager.findBundlesWithLatestVersionCompositesByCriteria(overlord, criteria);
assert results.get(0).getBundleId().equals(b1.getId());
assert results.get(0).getBundleName().equals(b1.getName());
assert results.get(0).getBundleDescription().equals(b1.getDescription());
assert results.get(0).getLatestVersion().equals("1.0");
assert results.get(0).getVersionsCount().longValue() == 1L;
BundleVersion bv2 = createBundleVersion(b1.getName() + "-2", "2.0", b1);
assertNotNull(bv2);
assertEquals("2.0", bv2.getVersion());
assert 1 == bv2.getVersionOrder();
results = bundleManager.findBundlesWithLatestVersionCompositesByCriteria(overlord, criteria);
assert results.get(0).getBundleId().equals(b1.getId());
assert results.get(0).getBundleName().equals(b1.getName());
assert results.get(0).getBundleDescription().equals(b1.getDescription());
assert results.get(0).getLatestVersion().equals("2.0");
assert results.get(0).getVersionsCount().longValue() == 2L;
BundleVersion bv3 = createBundleVersion(b1.getName() + "-3", "1.5", b1);
assertNotNull(bv3);
assertEquals("1.5", bv3.getVersion());
assert 1 == bv3.getVersionOrder();
results = bundleManager.findBundlesWithLatestVersionCompositesByCriteria(overlord, criteria);
assert results.get(0).getBundleId().equals(b1.getId());
assert results.get(0).getBundleName().equals(b1.getName());
assert results.get(0).getBundleDescription().equals(b1.getDescription());
assert results.get(0).getLatestVersion().equals("2.0");
assert results.get(0).getVersionsCount().longValue() == 3L;
BundleVersionCriteria c = new BundleVersionCriteria();
PageList<BundleVersion> bvs;
c.addFilterId(bv1.getId()); // 1.0
bvs = bundleManager.findBundleVersionsByCriteria(overlord, c);
assertNotNull(bvs);
assertEquals(1, bvs.size());
assert bvs.get(0).getVersionOrder() == 0; // 1st is the 1.0 version
c.addFilterId(bv2.getId()); // 2.0
bvs = bundleManager.findBundleVersionsByCriteria(overlord, c);
assertNotNull(bvs);
assertEquals(1, bvs.size());
assert bvs.get(0).getVersionOrder() == 2; // 3rd is the 2.0 version
c.addFilterId(bv3.getId()); // 1.5
bvs = bundleManager.findBundleVersionsByCriteria(overlord, c);
assertNotNull(bvs);
assertEquals(1, bvs.size());
assert bvs.get(0).getVersionOrder() == 1; // 2nd is the 1.5 version
// see that we can create a really old bundle and versionOrder gets updated properly
BundleVersion bv4 = createBundleVersion(b1.getName() + "-4", "0.5", b1);
assertNotNull(bv4);
assertEquals("0.5", bv4.getVersion());
c.addFilterId(bv4.getId()); //0.5
bvs = bundleManager.findBundleVersionsByCriteria(overlord, c);
assertNotNull(bvs);
assertEquals(1, bvs.size());
assert bvs.get(0).getVersionOrder() == 0; // 1st is the 0.5 version
c.addFilterId(bv1.getId()); // 1.0
bvs = bundleManager.findBundleVersionsByCriteria(overlord, c);
assertNotNull(bvs);
assertEquals(1, bvs.size());
assert bvs.get(0).getVersionOrder() == 1; // 2nd is the 1.0 version
c.addFilterId(bv3.getId()); // 1.5
bvs = bundleManager.findBundleVersionsByCriteria(overlord, c);
assertNotNull(bvs);
assertEquals(1, bvs.size());
assert bvs.get(0).getVersionOrder() == 2; // 3nd is the 1.5 version
c.addFilterId(bv2.getId()); // 2.0
bvs = bundleManager.findBundleVersionsByCriteria(overlord, c);
assertNotNull(bvs);
assertEquals(1, bvs.size());
assert bvs.get(0).getVersionOrder() == 3; // 4th is the 2.0 version
// verify our composite criteria query can return more than one item
Bundle b2 = createBundle("two");
assertNotNull(b2);
results = bundleManager.findBundlesWithLatestVersionCompositesByCriteria(overlord, criteria);
assert results.size() == 2 : results;
assert results.get(0).getBundleId().equals(b1.getId());
assert results.get(0).getBundleName().equals(b1.getName());
assert results.get(0).getBundleDescription().equals(b1.getDescription());
assert results.get(0).getLatestVersion().equals("2.0");
assert results.get(0).getVersionsCount().longValue() == 4L;
assert results.get(1).getBundleId().equals(b2.getId());
assert results.get(1).getBundleName().equals(b2.getName());
assert results.get(1).getBundleDescription().equals(b2.getDescription());
assert results.get(1).getLatestVersion() == null;
assert results.get(1).getVersionsCount().longValue() == 0L;
BundleVersion b2_bv1 = createBundleVersion(b2.getName() + "-5", "9.1", b2);
assertNotNull(b2_bv1);
assertEquals("9.1", b2_bv1.getVersion());
results = bundleManager.findBundlesWithLatestVersionCompositesByCriteria(overlord, criteria);
assert results.size() == 2 : results;
assert results.get(0).getBundleId().equals(b1.getId());
assert results.get(0).getBundleName().equals(b1.getName());
assert results.get(0).getBundleDescription().equals(b1.getDescription());
assert results.get(0).getLatestVersion().equals("2.0");
assert results.get(0).getVersionsCount().longValue() == 4L;
assert results.get(1).getBundleId().equals(b2.getId());
assert results.get(1).getBundleName().equals(b2.getName());
assert results.get(1).getBundleDescription().equals(b2.getDescription());
assert results.get(1).getLatestVersion().equals("9.1");
assert results.get(1).getVersionsCount().longValue() == 1L;
// test sorting of the BundleWithLastestVersionComposite
criteria.addSortName(PageOrdering.DESC);
results = bundleManager.findBundlesWithLatestVersionCompositesByCriteria(overlord, criteria);
assert results.size() == 2 : results;
assert results.get(1).getBundleId().equals(b1.getId());
assert results.get(1).getBundleName().equals(b1.getName());
assert results.get(1).getBundleDescription().equals(b1.getDescription());
assert results.get(1).getLatestVersion().equals("2.0");
assert results.get(1).getVersionsCount().longValue() == 4L;
assert results.get(0).getBundleId().equals(b2.getId());
assert results.get(0).getBundleName().equals(b2.getName());
assert results.get(0).getBundleDescription().equals(b2.getDescription());
assert results.get(0).getLatestVersion().equals("9.1");
assert results.get(0).getVersionsCount().longValue() == 1L;
criteria.addSortName(PageOrdering.ASC);
results = bundleManager.findBundlesWithLatestVersionCompositesByCriteria(overlord, criteria);
assert results.size() == 2 : results;
assert results.get(0).getBundleId().equals(b1.getId());
assert results.get(0).getBundleName().equals(b1.getName());
assert results.get(0).getBundleDescription().equals(b1.getDescription());
assert results.get(0).getLatestVersion().equals("2.0");
assert results.get(0).getVersionsCount().longValue() == 4L;
assert results.get(1).getBundleId().equals(b2.getId());
assert results.get(1).getBundleName().equals(b2.getName());
assert results.get(1).getBundleDescription().equals(b2.getDescription());
assert results.get(1).getLatestVersion().equals("9.1");
assert results.get(1).getVersionsCount().longValue() == 1L;
// now delete one of the older bundle versions and make sure the ordering remains intact
bundleManager.deleteBundleVersion(overlord, bv1.getId(), true);
results = bundleManager.findBundlesWithLatestVersionCompositesByCriteria(overlord, criteria);
assert results.size() == 2 : results;
assert results.get(0).getBundleId().equals(b1.getId());
assert results.get(0).getBundleName().equals(b1.getName());
assert results.get(0).getBundleDescription().equals(b1.getDescription());
assert results.get(0).getLatestVersion().equals("2.0");
assert results.get(0).getVersionsCount().longValue() == 3L;
assert results.get(1).getBundleId().equals(b2.getId());
assert results.get(1).getBundleName().equals(b2.getName());
assert results.get(1).getBundleDescription().equals(b2.getDescription());
assert results.get(1).getLatestVersion().equals("9.1");
assert results.get(1).getVersionsCount().longValue() == 1L;
// now add another bundle version and make sure the ordering is updated properly [BZ 828905]
BundleVersion bv5 = createBundleVersion(b1.getName() + "-5", "5.0", b1);
assertNotNull(bv5);
assertEquals("5.0", bv5.getVersion());
assert 3 == bv5.getVersionOrder();
results = bundleManager.findBundlesWithLatestVersionCompositesByCriteria(overlord, criteria);
assert results.get(0).getBundleId().equals(b1.getId());
assert results.get(0).getBundleName().equals(b1.getName());
assert results.get(0).getBundleDescription().equals(b1.getDescription());
assert results.get(0).getLatestVersion().equals("5.0");
assert results.get(0).getVersionsCount().longValue() == 4L;
// delete the latest bundle version and make sure we didn't screw up the order
bundleManager.deleteBundleVersion(overlord, bv5.getId(), true);
results = bundleManager.findBundlesWithLatestVersionCompositesByCriteria(overlord, criteria);
assert results.get(0).getBundleId().equals(b1.getId());
assert results.get(0).getBundleName().equals(b1.getName());
assert results.get(0).getBundleDescription().equals(b1.getDescription());
assert results.get(0).getLatestVersion().equals("2.0");