From ce5b41ea6b626d2a41b78b832e44135847ef3c06 Mon Sep 17 00:00:00 2001 From: Devan Goodwin Date: Wed, 2 Dec 2009 11:44:59 -0400 Subject: Map Consumer hierarchy. --- proxy/code/src/META-INF/persistence.xml | 4 +--- .../fedoraproject/candlepin/model/Consumer.java | 27 ++++++++++++---------- .../candlepin/model/test/ConsumerTest.java | 27 ++++++++++++++++++++++ 3 files changed, 43 insertions(+), 15 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 @@ - + diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java index 5e11a6a..3e3cc66 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java @@ -66,8 +66,11 @@ public class Consumer { private Owner owner; // TODO: Is this worth mapping? Do we need a hierarchy amidst consumers? - @Transient - private Consumer parent; + @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 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? @@ -91,6 +94,7 @@ public class Consumer { this.info = new ConsumerInfo(); this.info.setConsumer(this); // TODO: ??? + this.childConsumers = new HashSet(); this.consumedProducts = new HashSet(); this.entitlements = new HashSet(); } @@ -98,6 +102,7 @@ public class Consumer { public Consumer() { this.info = new ConsumerInfo(); this.info.setConsumer(this); // TODO: ??? + this.childConsumers = new HashSet(); this.consumedProducts = new HashSet(); this.entitlements = new HashSet(); } @@ -144,18 +149,16 @@ public class Consumer { type = typeIn; } - /** - * @return the parent - */ - public Consumer getParent() { - return parent; + public Set getChildConsumers() { + return childConsumers; } - /** - * @param parent the parent to set - */ - public void setParent(Consumer parent) { - this.parent = parent; + public void setChildConsumers(Set childConsumers) { + this.childConsumers = childConsumers; + } + + public void addChildConsumer(Consumer child) { + this.childConsumers.add(child); } /** 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..7fccd33 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java @@ -152,4 +152,31 @@ public class ConsumerTest extends DatabaseTestFixture { assertNull(lookedUp); } + @Test + public void testConsumerHierarchy() { + beginTransaction(); + + Consumer child1 = new Consumer("child1asd", owner, consumerType); + child1.setMetadataField("foo6", "bar"); +// child1.addConsumedProduct(rhel); + em.persist(child1); + commitTransaction(); + + beginTransaction(); + Consumer child2 = new Consumer("child2", owner, consumerType); + child2.setMetadataField("foo87", "bar"); +// child2.addConsumedProduct(rhel); + 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()); + } + } -- cgit From ee276a09c0130918ba1a0e5a30780a865f0f2b00 Mon Sep 17 00:00:00 2001 From: Devan Goodwin Date: Wed, 2 Dec 2009 14:05:47 -0400 Subject: Fix bad Consumer to Product mapping. Should be ManyToMany, not OneToMany. Doh! --- .../org/fedoraproject/candlepin/model/Consumer.java | 7 ++++--- .../candlepin/model/test/ConsumerTest.java | 21 ++++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java index 3e3cc66..ecc829e 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java @@ -26,6 +26,7 @@ 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; @@ -72,9 +73,9 @@ public class Consumer { inverseJoinColumns=@JoinColumn(name="CHILD_CONSUMER_ID")) private Set 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 + // Separate mapping because in theory, a consumer could be consuming products they're + // not entitled to. + @ManyToMany @JoinTable(name="cp_consumer_products", joinColumns=@JoinColumn(name="consumer_id"), inverseJoinColumns=@JoinColumn(name="product_id")) 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 7fccd33..0ed5364 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java @@ -156,16 +156,14 @@ public class ConsumerTest extends DatabaseTestFixture { public void testConsumerHierarchy() { beginTransaction(); - Consumer child1 = new Consumer("child1asd", owner, consumerType); - child1.setMetadataField("foo6", "bar"); -// child1.addConsumedProduct(rhel); + 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("foo87", "bar"); -// child2.addConsumedProduct(rhel); + child2.setMetadataField("foo", "bar"); em.persist(child2); commitTransaction(); @@ -178,5 +176,18 @@ public class ConsumerTest extends DatabaseTestFixture { 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(); + } } -- cgit From b4c989272f444850e8f819b2b7a77a21cc9afad7 Mon Sep 17 00:00:00 2001 From: Devan Goodwin Date: Thu, 3 Dec 2009 10:33:07 -0400 Subject: Map Consumer entitlements. --- .../fedoraproject/candlepin/model/Consumer.java | 9 ++-- .../candlepin/model/EntitlementPool.java | 1 + .../candlepin/model/test/ConsumerTest.java | 27 ++++++++++ .../candlepin/model/test/EntitlementPoolTest.java | 37 +++----------- .../org/fedoraproject/candlepin/test/TestUtil.java | 58 ++++++++++++++++++++-- 5 files changed, 95 insertions(+), 37 deletions(-) diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java index ecc829e..83f3670 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java @@ -32,7 +32,6 @@ 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; @@ -66,7 +65,8 @@ public class Consumer { @ManyToOne private Owner owner; - // TODO: Is this worth mapping? Do we need a hierarchy amidst consumers? + // 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"), @@ -81,7 +81,10 @@ public class Consumer { inverseJoinColumns=@JoinColumn(name="product_id")) private Set consumedProducts; - @Transient // TODO + @OneToMany + @JoinTable(name="cp_consumer_entitlements", + joinColumns=@JoinColumn(name="consumer_id"), + inverseJoinColumns=@JoinColumn(name="entitlement_id")) private Set entitlements; @OneToOne(cascade=CascadeType.ALL) diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java b/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java index 7829048..71eef9f 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 0ed5364..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; @@ -189,5 +192,29 @@ public class ConsumerTest extends DatabaseTestFixture { 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; + } + } -- cgit