diff options
Diffstat (limited to 'proxy/code/src/org/fedoraproject/candlepin/model')
8 files changed, 97 insertions, 175 deletions
diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java index 242a6a0..3e642fd 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java @@ -18,6 +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.Table; +import javax.persistence.Transient; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; @@ -27,37 +36,78 @@ import javax.xml.bind.annotation.XmlTransient; * A Consumer is the entity that uses a given Entitlement. It can be a user, * system, or anything else we want to track as using the Entitlement. * - * Every Consumer has an Owner which may or may not own the Entitlment. The + * Every Consumer has an Owner which may or may not own the Entitlement. The * Consumer's attributes or metadata is stored in a ConsumerInfo object which * boils down to a series of name/value pairs. */ @XmlRootElement @XmlAccessorType(XmlAccessType.PROPERTY) -public class Consumer extends BaseModel { +@Entity +@Table(name="cp_consumer") +public class Consumer { + @Id + @GeneratedValue(strategy=GenerationType.AUTO) + private Long id; + @Column(nullable=false) + private String name; + + @ManyToOne private Owner owner; + + // TODO: Is this worth mapping? Do we need a hierarchy amidst consumers? + @Transient private Consumer parent; + + @Transient // TODO private List<Product> consumedProducts; + + @Transient // TODO private List<Entitlement> entitlements; + + @Transient // TODO private ConsumerInfo info; - /** - * default ctor - */ + public Consumer(String name, Owner owner) { + this.name = name; + this.owner = owner; + + this.info = new ConsumerInfo(); + this.info.setParent(this); // TODO: ??? + } + public Consumer() { - this(null); this.info = new ConsumerInfo(); - this.info.setParent(this); + this.info.setParent(this); // TODO: ??? } /** - * @param uuid unique id of consumer + * @return the id */ - public Consumer(String uuid) { - super(uuid); - this.info = new ConsumerInfo(); - this.info.setParent(this); + public Long getId() { + return id; + } + + /** + * @param id the id to set + */ + public void setId(Long id) { + this.id = id; + } + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @param name the name to set + */ + public void setName(String name) { + this.name = name; } /** @@ -144,7 +194,7 @@ public class Consumer extends BaseModel { @Override public String toString() { return "Consumer [type=" + this.getType() + ", getName()=" + - getName() + ", getUuid()=" + getUuid() + "]"; + getName() + "]"; } diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerInfo.java b/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerInfo.java index cfc7d16..5488041 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerInfo.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerInfo.java @@ -37,8 +37,6 @@ public class ConsumerInfo { private ConsumerType type; private Map<String, String> metadata; - - /** * @return Returns the parent. */ @@ -81,7 +79,6 @@ public class ConsumerInfo { * @param metadataIn The metadata to set. */ public void setMetadata(Map<String, String> metadataIn) { - System.out.println("set metadata called"); metadata = metadataIn; } @@ -91,7 +88,6 @@ public class ConsumerInfo { * @param value to set */ public void setMetadataField(String name, String value) { - System.out.println("set meta field called"); if (this.metadata == null) { metadata = new HashMap<String, String>(); } diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java b/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java index eeaf8ec..51249e4 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java @@ -60,7 +60,7 @@ public class ObjectFactory { org.addUser(u); // Consumer - Consumer c = new Consumer(BaseModel.generateUUID()); + Consumer c = new Consumer(); c.setName("fake-consumer-i386"); c.setOwner(org); org.addConsumer(c); 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 6ad75b8..90c0428 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java @@ -17,34 +17,46 @@ package org.fedoraproject.candlepin.model.test; import org.fedoraproject.candlepin.model.Consumer; import org.fedoraproject.candlepin.model.Owner; import org.fedoraproject.candlepin.model.Product; +import org.fedoraproject.candlepin.test.DatabaseTestFixture; +import org.junit.Before; import org.junit.Test; import static org.junit.Assert.*; -public class ConsumerTest { +public class ConsumerTest extends DatabaseTestFixture { + + private Owner owner; + private Product rhel; + + @Before + public void setUpTestObjects() { + em.getTransaction().begin(); + String ownerName = "Example Corporation"; + owner = new Owner(ownerName); + rhel = new Product("label", "Red Hat Enterprise Linux"); + em.persist(owner); + em.persist(rhel); + em.getTransaction().commit(); + } @Test public void testConsumedProduct() throws Exception { - Owner o = TestUtil.createOwner(); - - Product rhel = new Product(); - rhel.setName("Red Hat Enterprise Linux"); - - Consumer c = TestUtil.createConsumer(o); - c.addConsumedProduct(rhel); +// +// Consumer c = TestUtil.createConsumer(o); +// 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 testProperties() { +// Owner o = TestUtil.createOwner(); +// Consumer c = TestUtil.createConsumer(o); +// c.setMetadataField("cpu", "2"); +// +// assertEquals(c.getMetadataField("cpu"), "2"); +// } } diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/test/ModelTestFixture.java b/proxy/code/src/org/fedoraproject/candlepin/model/test/ModelTestFixture.java deleted file mode 100644 index 09752fe..0000000 --- a/proxy/code/src/org/fedoraproject/candlepin/model/test/ModelTestFixture.java +++ /dev/null @@ -1,79 +0,0 @@ -package org.fedoraproject.candlepin.model.test; - -import java.util.List; - -import javax.persistence.EntityManager; -import javax.persistence.EntityTransaction; - -import org.fedoraproject.candlepin.model.Owner; -import org.fedoraproject.candlepin.model.Product; -import org.fedoraproject.candlepin.util.EntityManagerUtil; -import org.junit.After; -import org.junit.Before; - -public class ModelTestFixture { - - protected EntityManager em; - - @Before - public void setUp() { - em = EntityManagerUtil.createEntityManager(); - } - - /* - * Cleans out everything in the database. Currently we test against an - * in-memory hsqldb, so this should be ok. - * - * WARNING: Don't flip the persistence unit to point to an actual live - * DB or you'll lose everything. - * - * WARNING: If you're creating objects in tables that have static - * pre-populated content, we may not want to wipe those tables here. - * This situation hasn't arisen yet. - */ - @After - public void cleanDb() { - if (!em.getTransaction().isActive()) { - em.getTransaction().begin(); - } - - // TODO: Would rather be doing this, but such a bulk delete does not seem to respect - // the cascade to child products and thus fails. - // em.createQuery("delete from Product").executeUpdate(); - List<Product> products = em.createQuery("from Product p").getResultList(); - for (Product p : products) { - em.remove(p); - } - - List<Owner> owners = em.createQuery("from Owner o").getResultList(); - for (Owner o : owners) { - em.remove(o); - } - em.getTransaction().commit(); - em.close(); - } - - /** - * Begin a transaction, persist the given entity, and commit. - * - * @param storeMe Entity to persist. - */ - protected void persistAndCommit(Object storeMe) { - EntityTransaction tx = null; - - try { - tx = em.getTransaction(); - tx.begin(); - em.persist(storeMe); - tx.commit(); - } - catch (RuntimeException e) { - if (tx != null && tx.isActive()) { - tx.rollback(); - } - throw e; // or display error message - } - } - - -} 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 b3ab842..98445ec 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/test/OwnerTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/OwnerTest.java @@ -20,13 +20,14 @@ import org.fedoraproject.candlepin.model.EntitlementPool; import org.fedoraproject.candlepin.model.Owner; import org.fedoraproject.candlepin.model.Product; import org.fedoraproject.candlepin.model.User; +import org.fedoraproject.candlepin.test.DatabaseTestFixture; import java.util.List; import org.junit.Test; import static org.junit.Assert.*; -public class OwnerTest extends ModelTestFixture { +public class OwnerTest extends DatabaseTestFixture { @Test public void testCreate() throws Exception { @@ -79,7 +80,7 @@ public class OwnerTest extends ModelTestFixture { assertEquals(1, owner.getUsers().size()); // Consumer - Consumer c = new Consumer(BaseModel.generateUUID()); + Consumer c = new Consumer(); c.setOwner(owner); owner.addConsumer(c); c.addConsumedProduct(rhel); 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 f1799aa..2a70275 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/test/ProductTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/ProductTest.java @@ -8,10 +8,11 @@ import javax.persistence.EntityManager; import javax.persistence.PersistenceException; import org.fedoraproject.candlepin.model.Product; +import org.fedoraproject.candlepin.test.DatabaseTestFixture; import org.fedoraproject.candlepin.util.EntityManagerUtil; import org.junit.Test; -public class ProductTest extends ModelTestFixture { +public class ProductTest extends DatabaseTestFixture { @Test diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/test/TestUtil.java b/proxy/code/src/org/fedoraproject/candlepin/model/test/TestUtil.java deleted file mode 100644 index 4df1984..0000000 --- a/proxy/code/src/org/fedoraproject/candlepin/model/test/TestUtil.java +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Copyright (c) 2009 Red Hat, Inc. - * - * This software is licensed to you under the GNU General Public License, - * version 2 (GPLv2). There is NO WARRANTY for this software, express or - * implied, including the implied warranties of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 - * along with this software; if not, see - * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - * - * Red Hat trademarks are not licensed under GPLv2. No permission is - * granted to use or replicate Red Hat trademarks that are incorporated - * in this software or its documentation. - */ -package org.fedoraproject.candlepin.model.test; - -import org.fedoraproject.candlepin.model.BaseModel; -import org.fedoraproject.candlepin.model.Consumer; -import org.fedoraproject.candlepin.model.ObjectFactory; -import org.fedoraproject.candlepin.model.Owner; -import org.fedoraproject.candlepin.model.Product; - - - -public class TestUtil { - private TestUtil() { - } - - @Deprecated - public static Owner createOwner() { - String lookedUp = BaseModel.generateUUID(); - Owner o = new Owner(); -// o.setUuid(lookedUp); - ObjectFactory.get().store(o); - return o; - } - - public static Consumer createConsumer(Owner owner) { - Consumer c = new Consumer(BaseModel.generateUUID()); - c.setOwner(owner); - ObjectFactory.get().store(c); - return c; - } - - /** - * Create a consumer with a new owner - * @return Consumer - */ - public static Consumer createConsumer() { - return createConsumer(createOwner()); - } - - public static Product createProduct() { - Product rhel = new Product(); - rhel.setName("Red Hat Enterprise Linux"); - ObjectFactory.get().store(rhel); - return rhel; - } -} |