diff options
Diffstat (limited to 'proxy/code/src/org/fedoraproject/candlepin/model')
6 files changed, 104 insertions, 31 deletions
diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java index 6b3384b..58b5b14 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java @@ -18,12 +18,15 @@ import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToOne; +import javax.persistence.OneToOne; +import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.Table; import javax.persistence.Transient; import javax.xml.bind.annotation.XmlAccessType; @@ -68,7 +71,8 @@ public class Consumer { @Transient // TODO private List<Entitlement> entitlements; - @Transient // TODO + @OneToOne(cascade=CascadeType.ALL) + @PrimaryKeyJoinColumn private ConsumerInfo info; public Consumer(String name, Owner owner, ConsumerType type) { diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerInfo.java b/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerInfo.java index 2394bd4..11297d9 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerInfo.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerInfo.java @@ -17,11 +17,21 @@ package org.fedoraproject.candlepin.model; import java.util.HashMap; import java.util.Map; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToOne; +import javax.persistence.Table; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlTransient; +import org.hibernate.annotations.Cascade; +import org.hibernate.annotations.CollectionOfElements; +import org.hibernate.annotations.MapKeyManyToMany; + /** * ConsumerInfo contains the metadata about a given Consumer (parent). It is * a series of (name,value) pairs which allows for a more flexible model of @@ -31,24 +41,58 @@ import javax.xml.bind.annotation.XmlTransient; */ @XmlRootElement @XmlAccessorType(XmlAccessType.PROPERTY) +@Entity +@Table(name="cp_consumer_info") public class ConsumerInfo { - private Consumer parent; + // TODO: Don't know if this is a good idea, technically the consumer + metadata data + // key should be the identifier. + @Id + @GeneratedValue(strategy=GenerationType.AUTO) + private Long id; + + @OneToOne(mappedBy="info") + private Consumer consumer; + + // NOTE: Had to deviate from default EJB3 annotations here, doesn't seem possible + // to map strings without an unplesant hack: + // http://stackoverflow.com/questions/287201/how-to-persist-a-property-of-type-liststringin-jpa + @MapKeyManyToMany(targetEntity = String.class) + @CollectionOfElements(targetElement = String.class) + @Cascade(value={org.hibernate.annotations.CascadeType.ALL}) private Map<String, String> metadata; + public ConsumerInfo() { + metadata = new HashMap<String, String>(); + } + + /** + * @return the id + */ + public Long getId() { + return id; + } + + /** + * @param id the id to set + */ + public void setId(Long id) { + this.id = id; + } + /** * @return Returns the parent. */ @XmlTransient - public Consumer getParent() { - return parent; + public Consumer getConsumer() { + return consumer; } /** * @param parentIn The parent to set. */ public void setParent(Consumer parentIn) { - parent = parentIn; + consumer = parentIn; } /** 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 52de9b6..273955d 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java @@ -14,6 +14,8 @@ */ package org.fedoraproject.candlepin.model.test; +import java.util.Map; + import org.fedoraproject.candlepin.model.Consumer; import org.fedoraproject.candlepin.model.ConsumerType; import org.fedoraproject.candlepin.model.Owner; @@ -37,7 +39,7 @@ public class ConsumerTest extends DatabaseTestFixture { @Before public void setUpTestObjects() { - em.getTransaction().begin(); + beginTransaction(); String ownerName = "Example Corporation"; owner = new Owner(ownerName); @@ -48,9 +50,11 @@ public class ConsumerTest extends DatabaseTestFixture { consumerType = new ConsumerType(CONSUMER_TYPE_NAME); em.persist(consumerType); consumer = new Consumer(CONSUMER_NAME, owner, consumerType); + consumer.setMetadataField("foo", "bar"); + consumer.setMetadataField("foo1", "bar1"); em.persist(consumer); - em.getTransaction().commit(); + commitTransaction(); } @Test @@ -68,12 +72,35 @@ public class ConsumerTest extends DatabaseTestFixture { // c.addConsumedProduct(rhel); } -// @Test -// public void testProperties() { -// Owner o = TestUtil.createOwner(); -// Consumer c = TestUtil.createConsumer(o); -// c.setMetadataField("cpu", "2"); -// -// assertEquals(c.getMetadataField("cpu"), "2"); -// } + @Test + public void testInfo() { + Consumer lookedUp = (Consumer)em.createQuery( + "from Consumer c where c.name = :name"). + setParameter("name", CONSUMER_NAME). + getSingleResult(); + Map<String, String> metadata = lookedUp.getInfo().getMetadata(); + assertEquals(2, metadata.keySet().size()); + assertEquals("bar", metadata.get("foo")); + assertEquals("bar", lookedUp.getInfo().getMetadataField("foo")); + assertEquals("bar1", metadata.get("foo1")); + assertEquals("bar1", lookedUp.getInfo().getMetadataField("foo1")); + + } + + @Test + public void testModifyInfo() { + beginTransaction(); + Consumer lookedUp = (Consumer)em.createQuery( + "from Consumer c where c.name = :name"). + setParameter("name", CONSUMER_NAME). + getSingleResult(); + Map<String, String> metadata = lookedUp.getInfo().getMetadata(); + assertEquals(2, metadata.keySet().size()); + assertEquals("bar", metadata.get("foo")); + assertEquals("bar", lookedUp.getInfo().getMetadataField("foo")); + assertEquals("bar1", metadata.get("foo1")); + assertEquals("bar1", lookedUp.getInfo().getMetadataField("foo1")); + commitTransaction(); + } + } diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTypeTest.java b/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTypeTest.java index 96261ba..cf6cc5e 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTypeTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTypeTest.java @@ -8,23 +8,21 @@ import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import org.fedoraproject.candlepin.model.ConsumerType; +import org.fedoraproject.candlepin.test.DatabaseTestFixture; import org.fedoraproject.candlepin.util.EntityManagerUtil; import org.junit.Test; import static org.junit.Assert.*; -public class ConsumerTypeTest { +public class ConsumerTypeTest extends DatabaseTestFixture { @Test public void testSomething() { - EntityManager em = EntityManagerUtil.createEntityManager(); - EntityTransaction tx = null; - tx = em.getTransaction(); - tx.begin(); + beginTransaction(); ConsumerType ct = new ConsumerType("standard-system"); em.persist(ct); - tx.commit(); + commitTransaction(); List<EntityManager> results = em.createQuery("select ct from ConsumerType as ct") .getResultList(); diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/test/OwnerTest.java b/proxy/code/src/org/fedoraproject/candlepin/model/test/OwnerTest.java index 98445ec..9e8df6c 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/test/OwnerTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/OwnerTest.java @@ -48,7 +48,7 @@ public class OwnerTest extends DatabaseTestFixture { @Test public void testList() throws Exception { - em.getTransaction().begin(); + beginTransaction(); List<Owner> orgs = em.createQuery("select o from Owner as o") .getResultList(); @@ -57,7 +57,7 @@ public class OwnerTest extends DatabaseTestFixture { for (int i = 0; i < 10; i++) { em.persist(new Owner("Corp " + i)); } - em.getTransaction().commit(); + commitTransaction(); orgs = em.createQuery("select o from Owner as o") .getResultList(); diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/test/ProductTest.java b/proxy/code/src/org/fedoraproject/candlepin/model/test/ProductTest.java index 2a70275..f2ea593 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/test/ProductTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/ProductTest.java @@ -65,7 +65,7 @@ public class ProductTest extends DatabaseTestFixture { @Test public void addChildProducts() { - em.getTransaction().begin(); + beginTransaction(); Product parent = new Product("parent-product", "Parent Product"); Product child1 = new Product("child-product1", "Child Product 1"); Product child2 = new Product("child-product2", "Child Product 2"); @@ -73,7 +73,7 @@ public class ProductTest extends DatabaseTestFixture { parent.addChildProduct(child1); parent.addChildProduct(child2); em.persist(parent); - em.getTransaction().commit(); + commitTransaction(); EntityManager em2 = EntityManagerUtil.createEntityManager(); Product result = (Product)em2.createQuery( @@ -85,7 +85,7 @@ public class ProductTest extends DatabaseTestFixture { @Test(expected = PersistenceException.class) public void childHasSingleParentOnly() { - em.getTransaction().begin(); + beginTransaction(); Product parent1 = new Product("parent-product1", "Parent Product 1"); Product child1 = new Product("child-product1", "Child Product 1"); @@ -97,18 +97,18 @@ public class ProductTest extends DatabaseTestFixture { em.persist(child1); em.persist(parent1); em.persist(parent2); - em.getTransaction().commit(); + commitTransaction(); } @Test public void testCascading() { - em.getTransaction().begin(); + beginTransaction(); Product parent1 = new Product("parent-product1", "Parent Product 1"); Product child1 = new Product("child-product1", "Child Product 1"); parent1.addChildProduct(child1); em.persist(parent1); - em.getTransaction().commit(); + commitTransaction(); EntityManager em2 = EntityManagerUtil.createEntityManager(); Product result = (Product)em2.createQuery( @@ -117,9 +117,9 @@ public class ProductTest extends DatabaseTestFixture { .getSingleResult(); assertNotNull(result); - em.getTransaction().begin(); + beginTransaction(); em.remove(parent1); - em.getTransaction().commit(); + commitTransaction(); em2 = EntityManagerUtil.createEntityManager(); List<Product> results = em2.createQuery( |