diff options
author | Devan Goodwin <dgoodwin@redhat.com> | 2009-12-03 10:33:07 -0400 |
---|---|---|
committer | Devan Goodwin <dgoodwin@redhat.com> | 2009-12-03 10:33:07 -0400 |
commit | b4c989272f444850e8f819b2b7a77a21cc9afad7 (patch) | |
tree | 03dbc584e1508df7d6ef19d0c5b4831354816506 | |
parent | ee276a09c0130918ba1a0e5a30780a865f0f2b00 (diff) | |
download | candlepin-b4c989272f444850e8f819b2b7a77a21cc9afad7.tar.gz candlepin-b4c989272f444850e8f819b2b7a77a21cc9afad7.tar.xz candlepin-b4c989272f444850e8f819b2b7a77a21cc9afad7.zip |
Map Consumer entitlements.
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<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) 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; + } + } |