EntitlementCertificateCurator extends AbstractHibernateCurator<EntitlementCertificate> { public int deleteByEntitlementIds(String... entitlementIds) { return entitlementIds != null ? this.deleteByEntitlementIds(Arrays.asList(entitlementIds)) : 0; } @Inject  EntitlementCertificateCurator(); @SuppressWarnings("unchecked") @Transactional List<EntitlementCertificate> listForEntitlement(Entitlement e); @SuppressWarnings("unchecked") @Transactional List<EntitlementCertificate> listForConsumer(Consumer c); @Transactional void delete(EntitlementCertificate cert); int deleteByEntitlementIds(String... entitlementIds); @Transactional int deleteByEntitlementIds(Iterable<String> entitlementIds);  }
@Test public void testDeleteSingleCertBySingleEntitlementId() { Entitlement ent1 = this.createEntitlement(this.owner, this.consumer, this.pool); Entitlement ent2 = this.createEntitlement(this.owner, this.consumer, this.pool); EntitlementCertificate cert1 = this.createEntitlementCertificate(ent1, "key1", "cert1"); EntitlementCertificate cert2a = this.createEntitlementCertificate(ent2, "key2a", "cert2a"); EntitlementCertificate cert2b = this.createEntitlementCertificate(ent2, "key2b", "cert2b"); assertNotNull(ent1.getId()); assertNotNull(ent2.getId()); assertNotEquals(ent1.getId(), ent2.getId()); assertNotNull(cert1.getId()); assertNotNull(cert2a.getId()); assertNotNull(cert2b.getId()); this.entitlementCertificateCurator.clear(); Collection<EntitlementCertificate> certs = this.entitlementCertificateCurator.listAll().list(); assertNotNull(certs); assertEquals(3, certs.size()); this.entitlementCertificateCurator.clear(); int count = this.entitlementCertificateCurator.deleteByEntitlementIds(ent1.getId()); assertEquals(1, count); certs = this.entitlementCertificateCurator.listAll().list(); assertNotNull(certs); assertEquals(2, certs.size()); ent1 = this.entitlementCurator.get(ent1.getId()); assertNotNull(ent1); assertNotNull(ent1.getCertificates()); assertEquals(0, ent1.getCertificates().size()); ent2 = this.entitlementCurator.get(ent2.getId()); assertNotNull(ent2); assertNotNull(ent2.getCertificates()); assertEquals(2, ent2.getCertificates().size()); Set<String> remaining = ent2.getCertificates().stream() .map(c -> c.getId()) .collect(Collectors.toSet()); assertTrue(remaining.contains(cert2a.getId())); assertTrue(remaining.contains(cert2b.getId())); }
@Test public void testDeleteMultipleCertsBySingleEntitlementId() { Entitlement ent1 = this.createEntitlement(this.owner, this.consumer, this.pool); Entitlement ent2 = this.createEntitlement(this.owner, this.consumer, this.pool); EntitlementCertificate cert1 = this.createEntitlementCertificate(ent1, "key1", "cert1"); EntitlementCertificate cert2a = this.createEntitlementCertificate(ent2, "key2a", "cert2a"); EntitlementCertificate cert2b = this.createEntitlementCertificate(ent2, "key2b", "cert2b"); assertNotNull(ent1.getId()); assertNotNull(ent2.getId()); assertNotEquals(ent1.getId(), ent2.getId()); assertNotNull(cert1.getId()); assertNotNull(cert2a.getId()); assertNotNull(cert2b.getId()); this.entitlementCertificateCurator.clear(); Collection<EntitlementCertificate> certs = this.entitlementCertificateCurator.listAll().list(); assertNotNull(certs); assertEquals(3, certs.size()); this.entitlementCertificateCurator.clear(); int count = this.entitlementCertificateCurator.deleteByEntitlementIds(ent2.getId()); assertEquals(2, count); certs = this.entitlementCertificateCurator.listAll().list(); assertNotNull(certs); assertEquals(1, certs.size()); ent1 = this.entitlementCurator.get(ent1.getId()); assertNotNull(ent1); assertNotNull(ent1.getCertificates()); assertEquals(1, ent1.getCertificates().size()); Set<String> remaining = ent1.getCertificates().stream() .map(c -> c.getId()) .collect(Collectors.toSet()); assertTrue(remaining.contains(cert1.getId())); ent2 = this.entitlementCurator.get(ent2.getId()); assertNotNull(ent2); assertNotNull(ent2.getCertificates()); assertEquals(0, ent2.getCertificates().size()); }
@Test public void testDeleteDoesntAffectUnsavedCerts() { Entitlement ent1 = this.createEntitlement(this.owner, this.consumer, this.pool); EntitlementCertificate cert1 = this.createEntitlementCertificate(ent1, "key1", "cert1"); assertNotNull(ent1.getId()); assertNotNull(cert1.getId()); EntitlementCertificate certN = new EntitlementCertificate(); CertificateSerial certSerial = new CertificateSerial(new Date()); this.certSerialCurator.create(certSerial); certN.setKeyAsBytes("keyN".getBytes()); certN.setCertAsBytes("certN".getBytes()); certN.setSerial(certSerial); ent1.addCertificate(certN); int count = this.entitlementCertificateCurator.deleteByEntitlementIds(ent1.getId()); assertEquals(1, count); ent1 = this.entitlementCurator.merge(ent1); this.entitlementCurator.clear(); ent1 = this.entitlementCurator.get(ent1.getId()); assertNotNull(ent1); assertNotNull(ent1.getCertificates()); assertEquals(1, ent1.getCertificates().size()); EntitlementCertificate remaining = ent1.getCertificates().iterator().next(); CertificateSerial remSerial = remaining.getSerial(); assertEquals(certSerial.getId(), remSerial.getId()); }