Resource resource = new Resource("testPVCSResource", "testPVCSResource", rt);
resource.setUuid("" + new Random().nextInt());
Architecture arch = new Architecture("testPVCSInsertArch");
PackageType pt = new PackageType("testPVCSInsertPT", resource.getResourceType());
Package pkg = new Package("testPVCSInsertPackage", pt);
PackageBits bits = createPackageBits(em);
PackageVersion pv = new PackageVersion(pkg, "version", arch);
ContentSourceType cst = new ContentSourceType("testPVCSContentSourceType");
ContentSource cs = new ContentSource("testPVCSContentSource", cst);
PackageVersionContentSource pvcs = new PackageVersionContentSource(pv, cs, "fooLocation");
Configuration csConfig = new Configuration();
csConfig.put(new PropertySimple("csConfig1", "csConfig1Value"));
cs.setConfiguration(csConfig);
Configuration pvConfig = new Configuration();
pvConfig.put(new PropertySimple("pvConfig1", "pvConfig1Value"));
pv.setExtraProperties(pvConfig);
bits.getBlob().setBits("testDeleteOrphanedPV".getBytes());
pv.setPackageBits(bits);
em.persist(rt);
em.persist(resource);
em.persist(arch);
em.persist(pt);
em.persist(pkg);
em.persist(pv);
em.persist(cst);
em.persist(cs);
em.persist(pvcs);
em.flush();
em.clear();
PackageVersionContentSourcePK pk = new PackageVersionContentSourcePK(pv, cs);
PackageVersionContentSource pvcsDup = em.find(PackageVersionContentSource.class, pk);
em.clear();
assert pvcsDup != null;
assert pvcsDup.equals(pvcs);
assert pvcsDup.getLocation().equals("fooLocation");
Query q = em.createNamedQuery(PackageVersionContentSource.DELETE_BY_CONTENT_SOURCE_ID);
q.setParameter("contentSourceId", cs.getId());
assert 1 == q.executeUpdate();
em.clear();
PackageBits findBits = em.find(PackageBits.class, bits.getId());
assert findBits != null : "The bits did not cascade-persist for some reason";
assert findBits.getId() > 0 : "The package bits did not cascade-persist for some reason!";
assert new String(bits.getBlob().getBits()).equals(new String(findBits.getBlob().getBits()));
q = em.createNamedQuery(PackageVersion.DELETE_IF_NO_CONTENT_SOURCES_OR_REPOS);
assert 1 == q.executeUpdate();
em.clear();