diff options
author | Adrian Likins <adrian@alikins.usersys.redhat.com> | 2009-12-03 10:24:45 -0500 |
---|---|---|
committer | Adrian Likins <adrian@alikins.usersys.redhat.com> | 2009-12-03 10:24:45 -0500 |
commit | 744109c91948fc68bf3a63f52b88801e9d2bc438 (patch) | |
tree | c11d096c09cdd9373ae85e47f3833374e28e4e95 /proxy/code/src | |
parent | c26e628a4fd5d8796c777b0f7daa0f43110f96e9 (diff) | |
parent | b4c989272f444850e8f819b2b7a77a21cc9afad7 (diff) | |
download | candlepin-744109c91948fc68bf3a63f52b88801e9d2bc438.tar.gz candlepin-744109c91948fc68bf3a63f52b88801e9d2bc438.tar.xz candlepin-744109c91948fc68bf3a63f52b88801e9d2bc438.zip |
Merge branch 'master' of ssh://alikins@git.fedorahosted.org/git/candlepin
Conflicts:
proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java
Diffstat (limited to 'proxy/code/src')
6 files changed, 155 insertions, 57 deletions
diff --git a/proxy/code/src/META-INF/persistence.xml b/proxy/code/src/META-INF/persistence.xml index a28f6ca..47559cb 100644 --- a/proxy/code/src/META-INF/persistence.xml +++ b/proxy/code/src/META-INF/persistence.xml @@ -12,9 +12,7 @@ <property name="hibernate.hbm2ddl.auto" value="create-drop"/> <property name="hibernate.connection.username" value="sa"/> <property name="hibernate.connection.password" value=""/> - <!-- - <property name="hibernate.show_sql" value="false" /> - --> + <property name="hibernate.show_sql" value="true" /> </properties> diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java index b22249d..f1d3335 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java @@ -26,12 +26,12 @@ import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.Table; -import javax.persistence.Transient; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; @@ -68,22 +68,29 @@ public class Consumer { @ForeignKey(name="fk_consumer_owner") private Owner owner; - // TODO: Is this worth mapping? Do we need a hierarchy amidst consumers? - @Transient - private Consumer parent; + // Consumer hierarchy it meant to be useful to represent the relationship between + // hosts and guests. + @OneToMany(targetEntity=Consumer.class, cascade=CascadeType.ALL) + @JoinTable(name="cp_consumer_hierarchy", + joinColumns=@JoinColumn(name="PARENT_CONSUMER_ID"), + inverseJoinColumns=@JoinColumn(name="CHILD_CONSUMER_ID")) + private Set<Consumer> childConsumers; - // 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 - @ForeignKey(name = "fk_consumer_product_consumer_id", - inverseName = "fk_consumer_product_product_id") + // Separate mapping because in theory, a consumer could be consuming products they're + // not entitled to. + @ManyToMany + @ForeignKey(name = "fk_consumer_product_consumer_id", + inverseName = "fk_consumer_product_product_id") @JoinTable(name="cp_consumer_products", joinColumns=@JoinColumn(name="consumer_id"), inverseJoinColumns=@JoinColumn(name="product_id")) private Set<Product> consumedProducts; - @Transient // TODO + @OneToMany + @JoinTable(name="cp_consumer_entitlements", + joinColumns=@JoinColumn(name="consumer_id"), + inverseJoinColumns=@JoinColumn(name="entitlement_id")) private Set<Entitlement> entitlements; @OneToOne(cascade=CascadeType.ALL) @@ -97,6 +104,7 @@ public class Consumer { this.info = new ConsumerInfo(); this.info.setConsumer(this); // TODO: ??? + this.childConsumers = new HashSet<Consumer>(); this.consumedProducts = new HashSet<Product>(); this.entitlements = new HashSet<Entitlement>(); } @@ -104,6 +112,7 @@ public class Consumer { public Consumer() { this.info = new ConsumerInfo(); this.info.setConsumer(this); // TODO: ??? + this.childConsumers = new HashSet<Consumer>(); this.consumedProducts = new HashSet<Product>(); this.entitlements = new HashSet<Entitlement>(); } @@ -150,18 +159,16 @@ public class Consumer { type = typeIn; } - /** - * @return the parent - */ - public Consumer getParent() { - return parent; + public Set<Consumer> getChildConsumers() { + return childConsumers; } - /** - * @param parent the parent to set - */ - public void setParent(Consumer parent) { - this.parent = parent; + public void setChildConsumers(Set<Consumer> childConsumers) { + this.childConsumers = childConsumers; + } + + public void addChildConsumer(Consumer child) { + this.childConsumers.add(child); } /** diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java b/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java index 80b949b..9619e2b 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java @@ -16,6 +16,7 @@ package org.fedoraproject.candlepin.model; import java.util.Date; +import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; 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 aedb49d..dc03056 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java @@ -21,9 +21,12 @@ import javax.persistence.PersistenceException; import org.fedoraproject.candlepin.model.Consumer; import org.fedoraproject.candlepin.model.ConsumerInfo; import org.fedoraproject.candlepin.model.ConsumerType; +import org.fedoraproject.candlepin.model.Entitlement; +import org.fedoraproject.candlepin.model.EntitlementPool; import org.fedoraproject.candlepin.model.Owner; import org.fedoraproject.candlepin.model.Product; import org.fedoraproject.candlepin.test.DatabaseTestFixture; +import org.fedoraproject.candlepin.test.TestUtil; import org.junit.Before; import org.junit.Test; @@ -152,4 +155,66 @@ public class ConsumerTest extends DatabaseTestFixture { assertNull(lookedUp); } + @Test + public void testConsumerHierarchy() { + beginTransaction(); + + Consumer child1 = new Consumer("child1", owner, consumerType); + child1.setMetadataField("foo", "bar"); + em.persist(child1); + commitTransaction(); + + beginTransaction(); + Consumer child2 = new Consumer("child2", owner, consumerType); + child2.setMetadataField("foo", "bar"); + em.persist(child2); + commitTransaction(); + + beginTransaction(); + consumer.addChildConsumer(child1); + consumer.addChildConsumer(child2); + em.persist(consumer); + commitTransaction(); + + Consumer lookedUp = (Consumer)em.find(Consumer.class, consumer.getId()); + assertEquals(2, lookedUp.getChildConsumers().size()); + } + + // This this looks like a stupid test but this was actually failing at one point. :) + @Test + public void testMultipleConsumersSameConsumedProduct() { + beginTransaction(); + + // Default consumer already consumes RHEL: + Consumer child1 = new Consumer("child1", owner, consumerType); + child1.setMetadataField("foo", "bar"); + child1.addConsumedProduct(rhel); + em.persist(child1); + commitTransaction(); + } + + @Test + public void testEntitlements() { + beginTransaction(); + EntitlementPool pool = TestUtil.createEntitlementPool(); + em.persist(pool.getProduct()); + em.persist(pool.getOwner()); + em.persist(pool); + + Entitlement e1 = TestUtil.createEntitlement(pool); + Entitlement e2 = TestUtil.createEntitlement(pool); + Entitlement e3 = TestUtil.createEntitlement(pool); + em.persist(e1); + em.persist(e2); + em.persist(e3); + + consumer.addEntitlement(e1); + consumer.addEntitlement(e2); + consumer.addEntitlement(e3); + commitTransaction(); + + Consumer lookedUp = (Consumer)em.find(Consumer.class, consumer.getId()); + assertEquals(3, lookedUp.getEntitlements().size()); + } + } diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/test/EntitlementPoolTest.java b/proxy/code/src/org/fedoraproject/candlepin/model/test/EntitlementPoolTest.java index 2517d3b..2391ce0 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/test/EntitlementPoolTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/EntitlementPoolTest.java @@ -14,15 +14,12 @@ */ package org.fedoraproject.candlepin.model.test; -import java.sql.Date; - -import java.util.Calendar; - import org.fedoraproject.candlepin.model.EntitlementPool; import org.fedoraproject.candlepin.model.Owner; import org.fedoraproject.candlepin.model.Product; import org.fedoraproject.candlepin.test.DatabaseTestFixture; +import org.fedoraproject.candlepin.test.TestUtil; import org.junit.Before; import org.junit.Test; @@ -38,42 +35,24 @@ public class EntitlementPoolTest extends DatabaseTestFixture { @Before public void createObjects() { beginTransaction(); - String ownerName = "Example Corporation"; - owner = new Owner(ownerName); + + pool = TestUtil.createEntitlementPool(); + owner = pool.getOwner(); + prod = pool.getProduct(); em.persist(owner); - prod = new Product("cptest-label", "My Product"); em.persist(prod); - commitTransaction(); - beginTransaction(); - pool = new EntitlementPool(owner, prod, new Long(1000), - createDate(2009, 11, 30), createDate(2015, 11, 30)); em.persist(pool); + commitTransaction(); } - private Date createDate(int year, int month, int day) { - Calendar cal = Calendar.getInstance(); - - cal.set(Calendar.YEAR, year); - cal.set(Calendar.MONTH, month); - cal.set(Calendar.DATE, day); - - cal.set(Calendar.HOUR_OF_DAY, 0); - cal.set(Calendar.MINUTE, 0); - cal.set(Calendar.SECOND, 0); - cal.set(Calendar.MILLISECOND, 0); - - Date jsqlD = new Date(cal.getTime().getTime()); - return jsqlD; - } - @Test public void testCreate() { - EntitlementPool lookedUp = (EntitlementPool)em.find(EntitlementPool.class, pool.getId()); + EntitlementPool lookedUp = (EntitlementPool)em.find(EntitlementPool.class, + pool.getId()); assertNotNull(lookedUp); assertEquals(owner.getId(), lookedUp.getOwner().getId()); assertEquals(prod.getId(), lookedUp.getProduct().getId()); - } } diff --git a/proxy/code/src/org/fedoraproject/candlepin/test/TestUtil.java b/proxy/code/src/org/fedoraproject/candlepin/test/TestUtil.java index f71d36a..30f8e85 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/test/TestUtil.java +++ b/proxy/code/src/org/fedoraproject/candlepin/test/TestUtil.java @@ -14,27 +14,39 @@ */ package org.fedoraproject.candlepin.test; +import java.sql.Date; + +import java.util.Calendar; +import java.util.Random; + import org.fedoraproject.candlepin.model.Consumer; import org.fedoraproject.candlepin.model.ConsumerType; +import org.fedoraproject.candlepin.model.Entitlement; +import org.fedoraproject.candlepin.model.EntitlementPool; import org.fedoraproject.candlepin.model.ObjectFactory; import org.fedoraproject.candlepin.model.Owner; import org.fedoraproject.candlepin.model.Product; -// TODO: Do we want to keep this style of creating objects for testing? +/** + * TestUtil for creating various testing objects. + * + * Objects backed by the database are not persisted, the caller is expected to persist + * the entities returned and any dependent objects. + */ public class TestUtil { private TestUtil() { } public static Owner createOwner() { - Owner o = new Owner("Test Owner"); + Owner o = new Owner("Test Owner " + randomInt()); // o.setUuid(lookedUp); ObjectFactory.get().store(o); return o; } public static Consumer createConsumer(ConsumerType type, Owner owner) { - Consumer c = new Consumer("Consumer Name", owner, type); + Consumer c = new Consumer("Test Consumer " + randomInt(), owner, type); ObjectFactory.get().store(c); return c; } @@ -44,12 +56,48 @@ public class TestUtil { * @return Consumer */ public static Consumer createConsumer() { - return createConsumer(new ConsumerType("some-consumer-type"), createOwner()); + return createConsumer(new ConsumerType("test-consumer-type-" + randomInt()), + createOwner()); + } + + public static int randomInt() { + return new Random().nextInt(10000); } public static Product createProduct() { - Product rhel = new Product("rhel-label", "Red Hat Enterprise Linux"); + int random = randomInt(); + Product rhel = new Product("test-product-" + random, + "Test Product " + random); ObjectFactory.get().store(rhel); return rhel; } + + public static EntitlementPool createEntitlementPool() { + EntitlementPool pool = new EntitlementPool(createOwner(), createProduct(), + new Long(1000), + TestUtil.createDate(2009, 11, 30), TestUtil.createDate(2015, 11, 30)); + return pool; + } + + public static Entitlement createEntitlement(EntitlementPool pool) { + Entitlement e = new Entitlement(pool, pool.getOwner(), pool.getStartDate()); + return e; + } + + public static Date createDate(int year, int month, int day) { + Calendar cal = Calendar.getInstance(); + + cal.set(Calendar.YEAR, year); + cal.set(Calendar.MONTH, month); + cal.set(Calendar.DATE, day); + + cal.set(Calendar.HOUR_OF_DAY, 0); + cal.set(Calendar.MINUTE, 0); + cal.set(Calendar.SECOND, 0); + cal.set(Calendar.MILLISECOND, 0); + + Date jsqlD = new Date(cal.getTime().getTime()); + return jsqlD; + } + } |