public void testPackageBits() throws Throwable {
executeInTransaction(new TransactionCallback() {
public void execute() throws Exception {
LoadedPackageBitsComposite composite;
try {
Resource resource = SessionTestHelper.createNewResource(em, "testPkgBitsResource");
PackageType pkgType = new PackageType("testPkgBitsPT", resource.getResourceType());
org.rhq.core.domain.content.Package pkg = new Package("testPkgBitsP", pkgType);
Architecture arch = new Architecture("testPkgArch");
PackageVersion pkgVer = new PackageVersion(pkg, "1", arch);
em.persist(pkgType);
em.persist(pkg);
em.persist(arch);
em.persist(pkgVer);
em.flush();
// test that no bits are available right now
composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer.getId());
assert composite != null;
assert composite.getPackageVersionId() == pkgVer.getId();
assert composite.getPackageBitsId() == null;
assert !composite.isPackageBitsAvailable();
assert !composite.isPackageBitsInDatabase();
// pretend we loaded the bits, but we stored them somewhere other then the DB
PackageBits packageBits = createPackageBits();
pkgVer.setPackageBits(packageBits);
pkgVer = em.merge(pkgVer);
em.flush();
// test that the bits are available, but are not stored in the DB
composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer.getId());
assert composite != null;
assert composite.getPackageVersionId() == pkgVer.getId();
assert composite.getPackageBitsId() == packageBits.getId();
assert composite.isPackageBitsAvailable();
assert !composite.isPackageBitsInDatabase();
// let's make sure there really is no data in the DB
packageBits = em.find(PackageBits.class, packageBits.getId());
assert packageBits != null;
assert packageBits.getBlob().getBits() == null;
// now lets store some bits in the DB
final String DATA = "testPackageBits data";
PackageBitsBlob packageBitsBlob = em.find(PackageBitsBlob.class, packageBits.getId());
packageBitsBlob.setBits(DATA.getBytes());
em.merge(packageBitsBlob);
em.flush();
// test that the bits are available and stored in the DB
composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer.getId());
assert composite != null;
assert composite.getPackageVersionId() == pkgVer.getId();
assert composite.getPackageBitsId() == packageBits.getId();
assert composite.isPackageBitsAvailable();
assert composite.isPackageBitsInDatabase();
// let's make sure the data really is in the DB
packageBits = em.find(PackageBits.class, packageBits.getId());
assert packageBits != null;
assert DATA.equals(new String(packageBits.getBlob().getBits()));
////////////////////////////////////////////////////
// create another package version and test with that
////////////////////////////////////////////////////
PackageVersion pkgVer2 = new PackageVersion(pkg, "2", arch);
em.persist(pkgVer2);
em.flush();
// first make sure the query still gets the right answer for the first pkgVer
composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer.getId());
assert composite != null;
assert composite.getPackageVersionId() == pkgVer.getId();
assert composite.getPackageBitsId() == packageBits.getId();
assert composite.isPackageBitsAvailable();
assert composite.isPackageBitsInDatabase();
// test that no bits are available right now
composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer2.getId());
assert composite != null;
assert composite.getPackageVersionId() == pkgVer2.getId();
assert composite.getPackageBitsId() == null;
assert !composite.isPackageBitsAvailable();
assert !composite.isPackageBitsInDatabase();
// pretend we loaded the bits, but we stored them somewhere other then the DB
PackageBits packageBits2 = createPackageBits();
pkgVer2.setPackageBits(packageBits2);
pkgVer2 = em.merge(pkgVer2);
em.flush();
// make sure the query still gets the right answer for the first pkgVer
composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer.getId());
assert composite != null;
assert composite.getPackageVersionId() == pkgVer.getId();
assert composite.getPackageBitsId() == packageBits.getId();
assert composite.isPackageBitsAvailable();
assert composite.isPackageBitsInDatabase();
// test that the bits are available, but are not stored in the DB
composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer2.getId());
assert composite != null;
assert composite.getPackageVersionId() == pkgVer2.getId();
assert composite.getPackageBitsId() == packageBits2.getId();
assert composite.isPackageBitsAvailable();
assert !composite.isPackageBitsInDatabase();
// let's make sure there really is no data in the DB
packageBits2 = em.find(PackageBits.class, packageBits2.getId());
assert packageBits2 != null;
assert packageBits2.getBlob().getBits() == null;
// now lets store some bits in the DB
final String DATA2 = "testPackageBits more data";
packageBits2.getBlob().setBits(DATA2.getBytes());
em.merge(packageBits2.getBlob());
em.flush();
// make sure the query still gets the right answer for the first pkgVer
composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer.getId());
assert composite != null;
assert composite.getPackageVersionId() == pkgVer.getId();
assert composite.getPackageBitsId() == packageBits.getId();
assert composite.isPackageBitsAvailable();
assert composite.isPackageBitsInDatabase();
// test that the bits are available and stored in the DB
composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer2.getId());
assert composite != null;
assert composite.getPackageVersionId() == pkgVer2.getId();
assert composite.getPackageBitsId() == packageBits2.getId();
assert composite.isPackageBitsAvailable();
assert composite.isPackageBitsInDatabase();
// let's make sure the data really is in the DB
packageBits2 = em.find(PackageBits.class, packageBits2.getId());
assert packageBits2 != null;
assert DATA2.equals(new String(packageBits2.getBlob().getBits()));