diff options
author | Devan Goodwin <dgoodwin@redhat.com> | 2009-11-27 15:02:22 -0400 |
---|---|---|
committer | Devan Goodwin <dgoodwin@redhat.com> | 2009-11-27 15:02:22 -0400 |
commit | 445c96e191e83eaa670cb0a00cb00d653f8fb71f (patch) | |
tree | 47a516a27e8b822a1306a4c92567556845b51afa /proxy/code/src/org/fedoraproject | |
parent | 1fccab2a728e3f7416fe30dcfb976f4e79565bb5 (diff) | |
download | candlepin-445c96e191e83eaa670cb0a00cb00d653f8fb71f.tar.gz candlepin-445c96e191e83eaa670cb0a00cb00d653f8fb71f.tar.xz candlepin-445c96e191e83eaa670cb0a00cb00d653f8fb71f.zip |
Map consumed products.
Diffstat (limited to 'proxy/code/src/org/fedoraproject')
3 files changed, 84 insertions, 44 deletions
diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java index 58b5b14..45b7d7b 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java @@ -15,8 +15,8 @@ package org.fedoraproject.candlepin.model; import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; +import java.util.HashSet; +import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; @@ -24,7 +24,10 @@ import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.Table; @@ -64,12 +67,17 @@ public class Consumer { // TODO: Is this worth mapping? Do we need a hierarchy amidst consumers? @Transient private Consumer parent; + + // TODO: Are we sure we want to track this explicitly? Wouldn't we examine the + // entitlements we're consuming and the products associated to them for this info? + @OneToMany + @JoinTable(name="cp_consumer_products", + joinColumns=@JoinColumn(name="consumer_id"), + inverseJoinColumns=@JoinColumn(name="product_id")) + private Set<Product> consumedProducts; @Transient // TODO - private List<Product> consumedProducts; - - @Transient // TODO - private List<Entitlement> entitlements; + private Set<Entitlement> entitlements; @OneToOne(cascade=CascadeType.ALL) @PrimaryKeyJoinColumn @@ -82,11 +90,15 @@ public class Consumer { this.info = new ConsumerInfo(); this.info.setParent(this); // TODO: ??? + this.consumedProducts = new HashSet<Product>(); + this.entitlements = new HashSet<Entitlement>(); } public Consumer() { this.info = new ConsumerInfo(); this.info.setParent(this); // TODO: ??? + this.consumedProducts = new HashSet<Product>(); + this.entitlements = new HashSet<Entitlement>(); } /** @@ -148,18 +160,26 @@ public class Consumer { /** * @return the consumedProducts */ - public List<Product> getConsumedProducts() { + public Set<Product> getConsumedProducts() { return consumedProducts; } /** * @param consumedProducts the consumedProducts to set */ - public void setConsumedProducts(List<Product> consumedProducts) { + public void setConsumedProducts(Set<Product> consumedProducts) { this.consumedProducts = consumedProducts; } /** + * Add a Product to this Consumer. + * @param p Product to be consumed. + */ + public void addConsumedProduct(Product p) { + this.consumedProducts.add(p); + } + + /** * @return the owner */ @XmlTransient @@ -175,18 +195,6 @@ public class Consumer { } /** - * Add a Product to this Consumer. - * @param p Product to be consumed. - */ - public void addConsumedProduct(Product p) { - if (this.consumedProducts == null) { - this.consumedProducts = new LinkedList<Product>(); - } - this.consumedProducts.add(p); - - } - - /** * {@inheritDoc} */ @Override @@ -238,7 +246,7 @@ public class Consumer { /** * @return Returns the entitlements. */ - public List<Entitlement> getEntitlements() { + public Set<Entitlement> getEntitlements() { return entitlements; } @@ -246,7 +254,7 @@ public class Consumer { /** * @param entitlementsIn The entitlements to set. */ - public void setEntitlements(List<Entitlement> entitlementsIn) { + public void setEntitlements(Set<Entitlement> entitlementsIn) { entitlements = entitlementsIn; } @@ -256,9 +264,6 @@ public class Consumer { * */ public void addEntitlement(Entitlement entitlementIn) { - if (this.entitlements == null) { - this.entitlements = new LinkedList<Entitlement>(); - } this.entitlements.add(entitlementIn); } diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java b/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java index 273955d..d633c6a 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java @@ -17,6 +17,7 @@ package org.fedoraproject.candlepin.model.test; import java.util.Map; import org.fedoraproject.candlepin.model.Consumer; +import org.fedoraproject.candlepin.model.ConsumerInfo; import org.fedoraproject.candlepin.model.ConsumerType; import org.fedoraproject.candlepin.model.Owner; import org.fedoraproject.candlepin.model.Product; @@ -32,6 +33,7 @@ public class ConsumerTest extends DatabaseTestFixture { private Owner owner; private Product rhel; + private Product jboss; private Consumer consumer; private ConsumerType consumerType; private static final String CONSUMER_TYPE_NAME = "test-consumer-type"; @@ -43,15 +45,19 @@ public class ConsumerTest extends DatabaseTestFixture { String ownerName = "Example Corporation"; owner = new Owner(ownerName); - rhel = new Product("label", "Red Hat Enterprise Linux"); + rhel = new Product("rhel", "Red Hat Enterprise Linux"); + jboss = new Product("jboss", "JBoss"); em.persist(owner); em.persist(rhel); + em.persist(jboss); consumerType = new ConsumerType(CONSUMER_TYPE_NAME); em.persist(consumerType); consumer = new Consumer(CONSUMER_NAME, owner, consumerType); consumer.setMetadataField("foo", "bar"); consumer.setMetadataField("foo1", "bar1"); + consumer.addConsumedProduct(rhel); + consumer.addConsumedProduct(jboss); em.persist(consumer); commitTransaction(); @@ -60,24 +66,15 @@ public class ConsumerTest extends DatabaseTestFixture { @Test public void testLookup() throws Exception { - Consumer lookedUp = (Consumer)em.createQuery( - "from Consumer c where c.name = :name"). - setParameter("name", CONSUMER_NAME). - getSingleResult(); + Consumer lookedUp = (Consumer)em.find(Consumer.class, consumer.getId()); assertEquals(consumer.getId(), lookedUp.getId()); assertEquals(consumer.getName(), lookedUp.getName()); assertEquals(consumer.getType().getLabel(), lookedUp.getType().getLabel()); -// -// Consumer c = TestUtil.createConsumer(o); -// c.addConsumedProduct(rhel); } @Test public void testInfo() { - Consumer lookedUp = (Consumer)em.createQuery( - "from Consumer c where c.name = :name"). - setParameter("name", CONSUMER_NAME). - getSingleResult(); + Consumer lookedUp = (Consumer)em.find(Consumer.class, consumer.getId()); Map<String, String> metadata = lookedUp.getInfo().getMetadata(); assertEquals(2, metadata.keySet().size()); assertEquals("bar", metadata.get("foo")); @@ -88,12 +85,9 @@ public class ConsumerTest extends DatabaseTestFixture { } @Test - public void testModifyInfo() { + public void testMetadataInfo() { beginTransaction(); - Consumer lookedUp = (Consumer)em.createQuery( - "from Consumer c where c.name = :name"). - setParameter("name", CONSUMER_NAME). - getSingleResult(); + Consumer lookedUp = (Consumer)em.find(Consumer.class, consumer.getId()); Map<String, String> metadata = lookedUp.getInfo().getMetadata(); assertEquals(2, metadata.keySet().size()); assertEquals("bar", metadata.get("foo")); @@ -102,5 +96,48 @@ public class ConsumerTest extends DatabaseTestFixture { assertEquals("bar1", lookedUp.getInfo().getMetadataField("foo1")); commitTransaction(); } + + @Test + public void testModifyMetadata() { + beginTransaction(); + consumer.setMetadataField("foo", "notbar"); + commitTransaction(); + + Consumer lookedUp = (Consumer)em.find(Consumer.class, consumer.getId()); + assertEquals("notbar", lookedUp.getMetadataField("foo")); + } + + @Test + public void testMetadataDeleteCascading() { + ConsumerInfo info = consumer.getInfo(); + Long infoId = info.getId(); + + ConsumerInfo lookedUp = (ConsumerInfo)em.find(ConsumerInfo.class, infoId); + assertNotNull(lookedUp); + + beginTransaction(); + em.remove(consumer); + commitTransaction(); + + lookedUp = (ConsumerInfo)em.find(ConsumerInfo.class, infoId); + assertNull(lookedUp); + } + @Test + public void testConsumedProducts() { + em.clear(); + Consumer lookedUp = (Consumer)em.find(Consumer.class, consumer.getId()); + assertEquals(2, lookedUp.getConsumedProducts().size()); + } + + @Test + public void testRemoveConsumedProducts() { + beginTransaction(); + em.remove(consumer); + commitTransaction(); + + Consumer lookedUp = (Consumer)em.find(Consumer.class, consumer.getId()); + assertNull(lookedUp); + } + } diff --git a/proxy/code/src/org/fedoraproject/candlepin/test/DatabaseTestFixture.java b/proxy/code/src/org/fedoraproject/candlepin/test/DatabaseTestFixture.java index d99d171..6f8d474 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/test/DatabaseTestFixture.java +++ b/proxy/code/src/org/fedoraproject/candlepin/test/DatabaseTestFixture.java @@ -108,7 +108,5 @@ public class DatabaseTestFixture { */ protected void commitTransaction() { em.getTransaction().commit(); -// em.close(); -// em = EntityManagerUtil.createEntityManager(); } } |