From 470bd267f20cd6fb516fd9d6b158a2caf1ef2f6c Mon Sep 17 00:00:00 2001 From: "jesus m. rodriguez" Date: Mon, 9 Nov 2009 17:57:11 -0500 Subject: put the tests back in .test package --- proxy/build.xml | 15 +- proxy/buildconf/build.properties | 4 +- proxy/buildfile | 2 +- .../candlepin/model/test/ProductTest.java | 132 ---------- proxy/code/webapp/WEB-INF/web.xml | 53 ---- .../candlepin/util/EntityManagerUtil.java | 2 +- proxy/src/main/resources/WEB-INF/web.xml | 53 ++++ .../candlepin/model/ConsumerTest.java | 234 ----------------- .../candlepin/model/ConsumerTypeTest.java | 28 -- .../candlepin/model/ObjectFactoryTest.java | 100 -------- .../fedoraproject/candlepin/model/OwnerTest.java | 128 ---------- .../candlepin/model/test/ConsumerTest.java | 236 +++++++++++++++++ .../candlepin/model/test/ConsumerTypeTest.java | 28 ++ .../candlepin/model/test/ObjectFactoryTest.java | 100 ++++++++ .../candlepin/model/test/OwnerTest.java | 128 ++++++++++ .../candlepin/model/test/ProductTest.java | 138 ++++++++++ .../candlepin/resource/ConsumerResourceTest.java | 125 --------- .../candlepin/resource/EntitlementMatcherTest.java | 59 ----- .../resource/EntitlementResourceTest.java | 207 --------------- .../candlepin/resource/TestResourceTest.java | 95 ------- .../candlepin/resource/UserResourceTest.java | 77 ------ .../candlepin/resource/cert/CertTest.java | 284 --------------------- .../resource/test/ConsumerResourceTest.java | 125 +++++++++ .../resource/test/EntitlementMatcherTest.java | 59 +++++ .../resource/test/EntitlementResourceTest.java | 207 +++++++++++++++ .../candlepin/resource/test/TestResourceTest.java | 95 +++++++ .../candlepin/resource/test/UserResourceTest.java | 77 ++++++ .../resource/test/cert/test/CertTest.java | 284 +++++++++++++++++++++ 28 files changed, 1542 insertions(+), 1533 deletions(-) delete mode 100644 proxy/code/src/org/fedoraproject/candlepin/model/test/ProductTest.java delete mode 100644 proxy/code/webapp/WEB-INF/web.xml create mode 100644 proxy/src/main/resources/WEB-INF/web.xml delete mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/model/ConsumerTest.java delete mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/model/ConsumerTypeTest.java delete mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/model/ObjectFactoryTest.java delete mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/model/OwnerTest.java create mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/model/test/ConsumerTest.java create mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/model/test/ConsumerTypeTest.java create mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/model/test/ObjectFactoryTest.java create mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/model/test/OwnerTest.java create mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/model/test/ProductTest.java delete mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/resource/ConsumerResourceTest.java delete mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/resource/EntitlementMatcherTest.java delete mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/resource/EntitlementResourceTest.java delete mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/resource/TestResourceTest.java delete mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/resource/UserResourceTest.java delete mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/resource/cert/CertTest.java create mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/resource/test/ConsumerResourceTest.java create mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/resource/test/EntitlementMatcherTest.java create mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/resource/test/EntitlementResourceTest.java create mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/resource/test/TestResourceTest.java create mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/resource/test/UserResourceTest.java create mode 100644 proxy/src/test/java/org/fedoraproject/candlepin/resource/test/cert/test/CertTest.java diff --git a/proxy/build.xml b/proxy/build.xml index 0737d6d..ec73d08 100644 --- a/proxy/build.xml +++ b/proxy/build.xml @@ -407,12 +407,13 @@ Running @{banner} ... classpathref="buildjars" > - + + - + @@ -454,7 +455,7 @@ Running @{banner} ... - @@ -462,7 +463,7 @@ Running @{banner} ... - --> - - - + + diff --git a/proxy/buildconf/build.properties b/proxy/buildconf/build.properties index a0eb0f0..586e299 100644 --- a/proxy/buildconf/build.properties +++ b/proxy/buildconf/build.properties @@ -1,8 +1,8 @@ candlepin.home=${basedir} custom.log4j=${user.home}/.log4j.properties -src.dir=${candlepin.home}/code -build.dir=${candlepin.home}/build +src.dir=${candlepin.home}/src/main +build.dir=${candlepin.home}/target lib.dir=${candlepin.home}/lib build.lib.dir=${build.dir}/build-lib diff --git a/proxy/buildfile b/proxy/buildfile index d522481..657dc82 100644 --- a/proxy/buildfile +++ b/proxy/buildfile @@ -27,8 +27,8 @@ BOUNCYCASTLE = group('bcprov', :under=>'org.bouncycastle', :version=>'1.43') #repositories.remote << "http://download.java.net/maven/2/" # Specify Maven 2.0 remote repositories here, like this: -#repositories.remote << "http://www.ibiblio.org/maven2/" repositories.remote << "http://jmrodri.fedorapeople.org/ivy/candlepin" +repositories.remote << "http://www.ibiblio.org/maven2/" #repositories.remote << "http://127.0.0.1/" desc "The Proxy project" diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/test/ProductTest.java b/proxy/code/src/org/fedoraproject/candlepin/model/test/ProductTest.java deleted file mode 100644 index 3c24cbc..0000000 --- a/proxy/code/src/org/fedoraproject/candlepin/model/test/ProductTest.java +++ /dev/null @@ -1,132 +0,0 @@ -package org.fedoraproject.candlepin.model.test; - -import static org.junit.Assert.*; - -import java.util.List; - -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 DatabaseTestFixture { - - - @Test - public void normalCreate() { - - Product prod = new Product("cptest-label", "My Product"); - persistAndCommit(prod); - - List results = entityManager().createQuery("select p from Product as p") - .getResultList(); - assertEquals(1, results.size()); - } - - @Test(expected = PersistenceException.class) - public void nameRequired() { - - Product prod = new Product(); - prod.setLabel("someproductlabel"); - persistAndCommit(prod); - - } - - @Test(expected = PersistenceException.class) - public void labelRequired() { - - Product prod = new Product(); - prod.setName("My Product Name"); - persistAndCommit(prod); - } - - @Test(expected = PersistenceException.class) - public void nameUnique() { - - Product prod = new Product("label1", "name"); - persistAndCommit(prod); - - Product prod2 = new Product("label2", "name"); - persistAndCommit(prod2); - } - - @Test(expected = PersistenceException.class) - public void labelUnique() { - - Product prod = new Product("label1", "name"); - Product prod2 = new Product("label1", "name2"); - persistAndCommit(prod); - - persistAndCommit(prod2); - } - - @Test - public void addChildProducts() { - 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"); - - parent.addChildProduct(child1); - parent.addChildProduct(child2); - entityManager().persist(parent); - commitTransaction(); - - //EntityManager em2 = EntityManagerUtil.createEntityManager(); XXX not sure why we need to ask for a new EntityManager here - Product result = (Product)entityManager().createQuery( - "select p from Product as p where name = :name") - .setParameter("name", parent.getName()) - .getSingleResult(); - assertEquals(2, result.getChildProducts().size()); - } - - @Test(expected = PersistenceException.class) - public void childHasSingleParentOnly() { - beginTransaction(); - - Product parent1 = new Product("parent-product1", "Parent Product 1"); - Product child1 = new Product("child-product1", "Child Product 1"); - Product parent2 = new Product("parent-product2", "Parent Product 2"); - - parent1.addChildProduct(child1); - parent2.addChildProduct(child1); // should cause the failure - - entityManager().persist(child1); - entityManager().persist(parent1); - entityManager().persist(parent2); - commitTransaction(); - } - - @Test - public void testCascading() { - beginTransaction(); - - Product parent1 = new Product("parent-product1", "Parent Product 1"); - Product child1 = new Product("child-product1", "Child Product 1"); - parent1.addChildProduct(child1); - entityManager().persist(parent1); - commitTransaction(); - - //EntityManager em2 = EntityManagerUtil.createEntityManager(); - Product result = (Product)entityManager().createQuery( - "select p from Product as p where name = :name") - .setParameter("name", child1.getName()) - .getSingleResult(); - assertNotNull(result); - - beginTransaction(); - entityManager().remove(parent1); - commitTransaction(); - - //em2 = EntityManagerUtil.createEntityManager(); - List results = entityManager().createQuery( - "select p from Product as p where name = :name") - .setParameter("name", child1.getName()) - .getResultList(); - assertEquals(0, results.size()); - } - -} diff --git a/proxy/code/webapp/WEB-INF/web.xml b/proxy/code/webapp/WEB-INF/web.xml deleted file mode 100644 index 1466d45..0000000 --- a/proxy/code/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,53 +0,0 @@ - - - - - - - Jersey Web Application - com.sun.jersey.spi.container.servlet.ServletContainer - - - com.sun.jersey.config.property.packages - org.fedoraproject.candlepin.resource - - 1 - - - - - org.fedoraproject.candlepin.guice.JerseyGuiceConfiguration - - - Guice Filter - com.google.inject.servlet.GuiceFilter - - - Guice Filter - /* - - - diff --git a/proxy/src/main/java/org/fedoraproject/candlepin/util/EntityManagerUtil.java b/proxy/src/main/java/org/fedoraproject/candlepin/util/EntityManagerUtil.java index 61eb82a..7896caf 100644 --- a/proxy/src/main/java/org/fedoraproject/candlepin/util/EntityManagerUtil.java +++ b/proxy/src/main/java/org/fedoraproject/candlepin/util/EntityManagerUtil.java @@ -20,7 +20,7 @@ import javax.persistence.Persistence; public class EntityManagerUtil { - public static final String DEFAULT_PERSISTENCE_UNIT = "production"; + public static final String DEFAULT_PERSISTENCE_UNIT = "test"; public static final String PERSISTENCE_UNIT_PROPERTY = "persistenceUnit"; public static final EntityManagerFactory EMF = buildEntityManagerFactory(); diff --git a/proxy/src/main/resources/WEB-INF/web.xml b/proxy/src/main/resources/WEB-INF/web.xml new file mode 100644 index 0000000..1466d45 --- /dev/null +++ b/proxy/src/main/resources/WEB-INF/web.xml @@ -0,0 +1,53 @@ + + + + + + + Jersey Web Application + com.sun.jersey.spi.container.servlet.ServletContainer + + + com.sun.jersey.config.property.packages + org.fedoraproject.candlepin.resource + + 1 + + + + + org.fedoraproject.candlepin.guice.JerseyGuiceConfiguration + + + Guice Filter + com.google.inject.servlet.GuiceFilter + + + Guice Filter + /* + + + diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/model/ConsumerTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/model/ConsumerTest.java deleted file mode 100644 index 52ed50d..0000000 --- a/proxy/src/test/java/org/fedoraproject/candlepin/model/ConsumerTest.java +++ /dev/null @@ -1,234 +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; - -import static org.junit.Assert.*; - -import java.util.Map; - -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; - - - -public class ConsumerTest extends DatabaseTestFixture { - - private Owner owner; - private Product rhel; - private Product jboss; - private Consumer consumer; - private ConsumerType consumerType; - private static final String CONSUMER_TYPE_NAME = "test-consumer-type"; - private static final String CONSUMER_NAME = "Test Consumer"; - - @Before - public void setUpTestObjects() { - owner = new Owner("Example Corporation"); - rhel = new Product("rhel", "Red Hat Enterprise Linux"); - jboss = new Product("jboss", "JBoss"); - - ownerCurator.create(owner); - productCurator.create(rhel); - productCurator.create(jboss); - - consumerType = new ConsumerType(CONSUMER_TYPE_NAME); - consumerTypeCurator.create(consumerType); - consumer = new Consumer(CONSUMER_NAME, owner, consumerType); - consumer.setMetadataField("foo", "bar"); - consumer.setMetadataField("foo1", "bar1"); - consumer.addConsumedProduct(rhel); - consumer.addConsumedProduct(jboss); - consumerCurator.create(consumer); - } - - @Test(expected = PersistenceException.class) - public void testConsumerTypeRequired() { - Consumer newConsumer = new Consumer(); - newConsumer.setName("cname"); - newConsumer.setOwner(owner); - - consumerCurator.create(newConsumer); - } - - @Test - public void testLookup() throws Exception { - Consumer lookedUp = consumerCurator.find(consumer.getId()); - assertEquals(consumer.getId(), lookedUp.getId()); - assertEquals(consumer.getName(), lookedUp.getName()); - assertEquals(consumer.getType().getLabel(), lookedUp.getType().getLabel()); - assertNotNull(consumer.getUuid()); - } - - @Test - public void testInfo() { - Consumer lookedUp = consumerCurator.find(consumer.getId()); - Map 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 testMetadataInfo() { - Consumer consumer2 = new Consumer("consumer2", owner, consumerType); - consumer2.setMetadataField("foo", "bar2"); - consumerCurator.create(consumer2); - - Consumer lookedUp = consumerCurator.find(consumer.getId()); - Map 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")); - assertEquals(consumer.getId(), lookedUp.getInfo().getConsumer().getId()); - - Consumer lookedUp2 = consumerCurator.find(consumer2.getId()); - metadata = lookedUp2.getInfo().getMetadata(); - assertEquals(1, metadata.keySet().size()); - assertEquals("bar2", metadata.get("foo")); - } - - @Test - public void testModifyMetadata() { - consumer.setMetadataField("foo", "notbar"); - consumerCurator.update(consumer); - - Consumer lookedUp = consumerCurator.find(consumer.getId()); - assertEquals("notbar", lookedUp.getMetadataField("foo")); - } - - @Test - public void testMetadataDeleteCascading() { - Consumer attachedConsumer = consumerCurator.find(consumer.getId()); - Long consumerInfoId = attachedConsumer.getInfo().getId(); - - assertNotNull((ConsumerInfo)entityManager().find(ConsumerInfo.class, consumerInfoId)); - - consumerCurator.delete(attachedConsumer); - - assertNull((ConsumerInfo)entityManager().find(ConsumerInfo.class, consumerInfoId)); - } - - @Test - public void testConsumedProducts() { - entityManager().clear(); - Consumer lookedUp = (Consumer)entityManager().find(Consumer.class, consumer.getId()); - assertEquals(2, lookedUp.getConsumedProducts().size()); - } - - @Test - public void testRemoveConsumedProducts() { - consumerCurator.delete(consumerCurator.find(consumer.getId())); - assertNull(consumerCurator.find(consumer.getId())); - } - - @Test - public void testConsumerHierarchy() { - Consumer child1 = new Consumer("child1", owner, consumerType); - child1.setMetadataField("foo", "bar"); - consumerCurator.create(child1); - - Consumer child2 = new Consumer("child2", owner, consumerType); - child2.setMetadataField("foo", "bar"); - consumerCurator.create(child2); - - consumer.addChildConsumer(child1); - consumer.addChildConsumer(child2); - consumerCurator.update(consumer); - - Consumer lookedUp = consumerCurator.find(consumer.getId()); - assertEquals(2, lookedUp.getChildConsumers().size()); - } - - @Test - public void testChildDeleteNoCascade() { - Consumer child1 = new Consumer("child1", owner, consumerType); - child1.setMetadataField("foo", "bar"); - consumer.addChildConsumer(child1); - consumerCurator.update(consumer); - - child1 = consumerCurator.find(child1.getId()); - consumerCurator.delete(child1); - - assertNull(consumerCurator.find(child1.getId())); - - Consumer lookedUp = consumerCurator.find(consumer.getId()); - assertEquals(0, lookedUp.getChildConsumers().size()); - } - - @Test - public void testParentDeleteCascadesToChildren() { - Consumer child1 = new Consumer("child1", owner, consumerType); - child1.setMetadataField("foo", "bar"); - consumer.addChildConsumer(child1); - consumerCurator.update(consumer); - - consumerCurator.delete(consumer); - - assertNull(consumerCurator.find(consumer.getId())); - assertNull(consumerCurator.find(child1.getId())); - } - - // 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); - entityManager().persist(child1); - commitTransaction(); - } - - @Test - public void testAddEntitlements() { - EntitlementPool pool = TestUtil.createEntitlementPool(); - entityManager().persist(pool.getProduct()); - entityManager().persist(pool.getOwner()); - entityManager().persist(pool); - - Entitlement e1 = TestUtil.createEntitlement(pool); - Entitlement e2 = TestUtil.createEntitlement(pool); - Entitlement e3 = TestUtil.createEntitlement(pool); - entityManager().persist(e1); - entityManager().persist(e2); - entityManager().persist(e3); - - consumer.addEntitlement(e1); - consumer.addEntitlement(e2); - consumer.addEntitlement(e3); - consumerCurator.update(consumer); - - Consumer lookedUp = consumerCurator.find(consumer.getId()); - assertEquals(3, lookedUp.getEntitlements().size()); - } - -} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/model/ConsumerTypeTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/model/ConsumerTypeTest.java deleted file mode 100644 index afec5b2..0000000 --- a/proxy/src/test/java/org/fedoraproject/candlepin/model/ConsumerTypeTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.fedoraproject.candlepin.model; - -import static org.junit.Assert.*; - -import java.util.List; - -import javax.persistence.EntityManager; - -import org.fedoraproject.candlepin.model.ConsumerType; -import org.fedoraproject.candlepin.test.DatabaseTestFixture; -import org.junit.Test; - -public class ConsumerTypeTest extends DatabaseTestFixture { - - @Test - public void testSomething() { - beginTransaction(); - - ConsumerType ct = new ConsumerType("standard-system"); - entityManager().persist(ct); - - commitTransaction(); - - List results = entityManager().createQuery("select ct from ConsumerType as ct") - .getResultList(); - assertEquals(1, results.size()); - } -} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/model/ObjectFactoryTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/model/ObjectFactoryTest.java deleted file mode 100644 index 5ef8fff..0000000 --- a/proxy/src/test/java/org/fedoraproject/candlepin/model/ObjectFactoryTest.java +++ /dev/null @@ -1,100 +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; - -import static org.junit.Assert.*; - -import java.util.List; - -import org.fedoraproject.candlepin.model.ObjectFactory; -import org.fedoraproject.candlepin.model.Owner; -import org.junit.Test; - -/** - * ObjectFactoryTest - */ -public class ObjectFactoryTest { - - @Test - public void testGet() { - ObjectFactory o1 = ObjectFactory.get(); - ObjectFactory o2 = ObjectFactory.get(); - - assertNotNull(o1); - assertNotNull(o2); - assertEquals(o1, o2); - } - - @Test - public void testListObjectsByClass() { - List l = ObjectFactory.get().listObjectsByClass(Object.class); - assertNotNull(l); - assertTrue(l.isEmpty()); - - l = ObjectFactory.get().listObjectsByClass(Owner.class); - assertNotNull(l); - assertFalse(l.isEmpty()); - Object o = l.get(0); - assertNotNull(o); - assertEquals(o.getClass(), Owner.class); - } - - @Test - public void testStore() { - // make sure we don't have one stored already - List list = ObjectFactory.get().listObjectsByClass(Long.class); - assertNotNull(list); - assertTrue(list.isEmpty()); - - Long l = new Long(10); - ObjectFactory.get().store(l); - - // verify it got stored - list = ObjectFactory.get().listObjectsByClass(Long.class); - assertNotNull(list); - assertFalse(list.isEmpty()); - Long l2 = (Long) list.get(0); - assertEquals(l, l2); - } - -// @Test -// public void testLookupByUUID() { -// String uuid = BaseModel.generateUUID(); -// assertNull(ObjectFactory.get().lookupByUUID(Owner.class, uuid)); -// -// Owner owner = new Owner(uuid); -// owner.setName("unit-test-owner"); -// ObjectFactory.get().store(owner); -// Object o = ObjectFactory.get().lookupByUUID(Owner.class, uuid); -// assertNotNull(o); -// assertEquals(o.getClass(), Owner.class); -// assertEquals(((Owner)o).getUuid(), owner.getUuid()); -// } - -// @Test -// public void testLookupByFieldName() { -// String uuid = BaseModel.generateUUID(); -// assertNull(ObjectFactory.get().lookupByUUID(Owner.class, uuid)); -// -// Owner owner = new Owner(uuid); -// owner.setName("unit-test-org"); -// ObjectFactory.get().store(owner); -// -// Owner o2 = (Owner) ObjectFactory.get().lookupByFieldName( -// Owner.class, "uuid", uuid); -// assertNotNull(o2); -// assertEquals(owner, o2); -// } -} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/model/OwnerTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/model/OwnerTest.java deleted file mode 100644 index 6de47f0..0000000 --- a/proxy/src/test/java/org/fedoraproject/candlepin/model/OwnerTest.java +++ /dev/null @@ -1,128 +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; - -import static org.junit.Assert.*; - -import org.fedoraproject.candlepin.model.Consumer; -import org.fedoraproject.candlepin.model.ConsumerType; -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 org.fedoraproject.candlepin.test.TestUtil; -import org.junit.Test; - -public class OwnerTest extends DatabaseTestFixture { - - @Test - public void testCreate() throws Exception { - String ownerName = "Example Corporation"; - Owner o = new Owner(ownerName); - ownerCurator.create(o); - - Owner result = (Owner)entityManager().createQuery( - "select o from Owner o where o.name = :name") - .setParameter("name", ownerName).getSingleResult(); - - assertNotNull(result); - assertEquals(ownerName, result.getName()); - assertTrue(result.getId() > 0); - assertEquals(o.getId(), result.getId()); - } - - @Test - public void testList() throws Exception { - int beforeCount = entityManager().createQuery("select o from Owner as o").getResultList().size(); - - for (int i = 0; i < 10; i++) { - ownerCurator.create(new Owner("Corp " + i)); - } - - int afterCount = entityManager().createQuery("select o from Owner as o").getResultList().size(); - assertEquals(10, afterCount - beforeCount); - } - - @Test - public void testObjectRelationships() throws Exception { - Owner owner = new Owner("test-owner"); - // Product - Product rhel = new Product(); - rhel.setName("Red Hat Enterprise Linux"); - - // User - User u = new User(); - u.setLogin("test-login"); - u.setPassword("redhat"); - owner.addUser(u); - assertEquals(1, owner.getUsers().size()); - - // Consumer - Consumer c = new Consumer(); - c.setOwner(owner); - owner.addConsumer(c); - c.addConsumedProduct(rhel); - assertEquals(1, owner.getConsumers().size()); - assertEquals(1, c.getConsumedProducts().size()); - - // EntitlementPool - EntitlementPool pool = new EntitlementPool(); - owner.addEntitlementPool(pool); - pool.setProduct(rhel); - assertEquals(1, owner.getEntitlementPools().size()); - } - - @Test - public void bidirectionalConsumers() throws Exception { - beginTransaction(); - Owner o = TestUtil.createOwner(); - ConsumerType consumerType = TestUtil.createConsumerType(); - Consumer c1 = TestUtil.createConsumer(consumerType, o); - Consumer c2 = TestUtil.createConsumer(consumerType, o); - o.addConsumer(c1); - o.addConsumer(c2); - - ownerCurator.create(o); - consumerTypeCurator.create(consumerType); - consumerCurator.create(c1); - consumerCurator.create(c2); - - assertEquals(2, o.getConsumers().size()); - - Owner lookedUp = ownerCurator.find(o.getId()); - assertEquals(2, lookedUp.getConsumers().size()); - } - - @Test - public void bidirectionalUsers() throws Exception { - Owner o = TestUtil.createOwner(); - - User u1 = TestUtil.createUser(o); - User u2 = TestUtil.createUser(o); - - o.addUser(u1); - o.addUser(u2); - - ownerCurator.create(o); - - assertEquals(2, o.getUsers().size()); - - entityManager().clear(); - Owner lookedUp = ownerCurator.find(o.getId()); - assertEquals(2, lookedUp.getUsers().size()); - } - -} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/model/test/ConsumerTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/model/test/ConsumerTest.java new file mode 100644 index 0000000..f35cead --- /dev/null +++ b/proxy/src/test/java/org/fedoraproject/candlepin/model/test/ConsumerTest.java @@ -0,0 +1,236 @@ +/** + * 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 static org.junit.Assert.assertEquals; + +import static org.junit.Assert.*; + +import java.util.Map; + +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; + + + +public class ConsumerTest extends DatabaseTestFixture { + + private Owner owner; + private Product rhel; + private Product jboss; + private Consumer consumer; + private ConsumerType consumerType; + private static final String CONSUMER_TYPE_NAME = "test-consumer-type"; + private static final String CONSUMER_NAME = "Test Consumer"; + + @Before + public void setUpTestObjects() { + owner = new Owner("Example Corporation"); + rhel = new Product("rhel", "Red Hat Enterprise Linux"); + jboss = new Product("jboss", "JBoss"); + + ownerCurator.create(owner); + productCurator.create(rhel); + productCurator.create(jboss); + + consumerType = new ConsumerType(CONSUMER_TYPE_NAME); + consumerTypeCurator.create(consumerType); + consumer = new Consumer(CONSUMER_NAME, owner, consumerType); + consumer.setMetadataField("foo", "bar"); + consumer.setMetadataField("foo1", "bar1"); + consumer.addConsumedProduct(rhel); + consumer.addConsumedProduct(jboss); + consumerCurator.create(consumer); + } + + @Test(expected = PersistenceException.class) + public void testConsumerTypeRequired() { + Consumer newConsumer = new Consumer(); + newConsumer.setName("cname"); + newConsumer.setOwner(owner); + + consumerCurator.create(newConsumer); + } + + @Test + public void testLookup() throws Exception { + Consumer lookedUp = consumerCurator.find(consumer.getId()); + assertEquals(consumer.getId(), lookedUp.getId()); + assertEquals(consumer.getName(), lookedUp.getName()); + assertEquals(consumer.getType().getLabel(), lookedUp.getType().getLabel()); + assertNotNull(consumer.getUuid()); + } + + @Test + public void testInfo() { + Consumer lookedUp = consumerCurator.find(consumer.getId()); + Map 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 testMetadataInfo() { + Consumer consumer2 = new Consumer("consumer2", owner, consumerType); + consumer2.setMetadataField("foo", "bar2"); + consumerCurator.create(consumer2); + + Consumer lookedUp = consumerCurator.find(consumer.getId()); + Map 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")); + assertEquals(consumer.getId(), lookedUp.getInfo().getConsumer().getId()); + + Consumer lookedUp2 = consumerCurator.find(consumer2.getId()); + metadata = lookedUp2.getInfo().getMetadata(); + assertEquals(1, metadata.keySet().size()); + assertEquals("bar2", metadata.get("foo")); + } + + @Test + public void testModifyMetadata() { + consumer.setMetadataField("foo", "notbar"); + consumerCurator.update(consumer); + + Consumer lookedUp = consumerCurator.find(consumer.getId()); + assertEquals("notbar", lookedUp.getMetadataField("foo")); + } + + @Test + public void testMetadataDeleteCascading() { + Consumer attachedConsumer = consumerCurator.find(consumer.getId()); + Long consumerInfoId = attachedConsumer.getInfo().getId(); + + assertNotNull((ConsumerInfo)entityManager().find(ConsumerInfo.class, consumerInfoId)); + + consumerCurator.delete(attachedConsumer); + + assertNull((ConsumerInfo)entityManager().find(ConsumerInfo.class, consumerInfoId)); + } + + @Test + public void testConsumedProducts() { + entityManager().clear(); + Consumer lookedUp = (Consumer)entityManager().find(Consumer.class, consumer.getId()); + assertEquals(2, lookedUp.getConsumedProducts().size()); + } + + @Test + public void testRemoveConsumedProducts() { + consumerCurator.delete(consumerCurator.find(consumer.getId())); + assertNull(consumerCurator.find(consumer.getId())); + } + + @Test + public void testConsumerHierarchy() { + Consumer child1 = new Consumer("child1", owner, consumerType); + child1.setMetadataField("foo", "bar"); + consumerCurator.create(child1); + + Consumer child2 = new Consumer("child2", owner, consumerType); + child2.setMetadataField("foo", "bar"); + consumerCurator.create(child2); + + consumer.addChildConsumer(child1); + consumer.addChildConsumer(child2); + consumerCurator.update(consumer); + + Consumer lookedUp = consumerCurator.find(consumer.getId()); + assertEquals(2, lookedUp.getChildConsumers().size()); + } + + @Test + public void testChildDeleteNoCascade() { + Consumer child1 = new Consumer("child1", owner, consumerType); + child1.setMetadataField("foo", "bar"); + consumer.addChildConsumer(child1); + consumerCurator.update(consumer); + + child1 = consumerCurator.find(child1.getId()); + consumerCurator.delete(child1); + + assertNull(consumerCurator.find(child1.getId())); + + Consumer lookedUp = consumerCurator.find(consumer.getId()); + assertEquals(0, lookedUp.getChildConsumers().size()); + } + + @Test + public void testParentDeleteCascadesToChildren() { + Consumer child1 = new Consumer("child1", owner, consumerType); + child1.setMetadataField("foo", "bar"); + consumer.addChildConsumer(child1); + consumerCurator.update(consumer); + + consumerCurator.delete(consumer); + + assertNull(consumerCurator.find(consumer.getId())); + assertNull(consumerCurator.find(child1.getId())); + } + + // 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); + entityManager().persist(child1); + commitTransaction(); + } + + @Test + public void testAddEntitlements() { + EntitlementPool pool = TestUtil.createEntitlementPool(); + entityManager().persist(pool.getProduct()); + entityManager().persist(pool.getOwner()); + entityManager().persist(pool); + + Entitlement e1 = TestUtil.createEntitlement(pool); + Entitlement e2 = TestUtil.createEntitlement(pool); + Entitlement e3 = TestUtil.createEntitlement(pool); + entityManager().persist(e1); + entityManager().persist(e2); + entityManager().persist(e3); + + consumer.addEntitlement(e1); + consumer.addEntitlement(e2); + consumer.addEntitlement(e3); + consumerCurator.update(consumer); + + Consumer lookedUp = consumerCurator.find(consumer.getId()); + assertEquals(3, lookedUp.getEntitlements().size()); + } + +} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/model/test/ConsumerTypeTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/model/test/ConsumerTypeTest.java new file mode 100644 index 0000000..4974c46 --- /dev/null +++ b/proxy/src/test/java/org/fedoraproject/candlepin/model/test/ConsumerTypeTest.java @@ -0,0 +1,28 @@ +package org.fedoraproject.candlepin.model.test; + +import static org.junit.Assert.*; + +import java.util.List; + +import javax.persistence.EntityManager; + +import org.fedoraproject.candlepin.model.ConsumerType; +import org.fedoraproject.candlepin.test.DatabaseTestFixture; +import org.junit.Test; + +public class ConsumerTypeTest extends DatabaseTestFixture { + + @Test + public void testSomething() { + beginTransaction(); + + ConsumerType ct = new ConsumerType("standard-system"); + entityManager().persist(ct); + + commitTransaction(); + + List results = entityManager().createQuery("select ct from ConsumerType as ct") + .getResultList(); + assertEquals(1, results.size()); + } +} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/model/test/ObjectFactoryTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/model/test/ObjectFactoryTest.java new file mode 100644 index 0000000..5186fb6 --- /dev/null +++ b/proxy/src/test/java/org/fedoraproject/candlepin/model/test/ObjectFactoryTest.java @@ -0,0 +1,100 @@ +/** + * 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 static org.junit.Assert.*; + +import java.util.List; + +import org.fedoraproject.candlepin.model.ObjectFactory; +import org.fedoraproject.candlepin.model.Owner; +import org.junit.Test; + +/** + * ObjectFactoryTest + */ +public class ObjectFactoryTest { + + @Test + public void testGet() { + ObjectFactory o1 = ObjectFactory.get(); + ObjectFactory o2 = ObjectFactory.get(); + + assertNotNull(o1); + assertNotNull(o2); + assertEquals(o1, o2); + } + + @Test + public void testListObjectsByClass() { + List l = ObjectFactory.get().listObjectsByClass(Object.class); + assertNotNull(l); + assertTrue(l.isEmpty()); + + l = ObjectFactory.get().listObjectsByClass(Owner.class); + assertNotNull(l); + assertFalse(l.isEmpty()); + Object o = l.get(0); + assertNotNull(o); + assertEquals(o.getClass(), Owner.class); + } + + @Test + public void testStore() { + // make sure we don't have one stored already + List list = ObjectFactory.get().listObjectsByClass(Long.class); + assertNotNull(list); + assertTrue(list.isEmpty()); + + Long l = new Long(10); + ObjectFactory.get().store(l); + + // verify it got stored + list = ObjectFactory.get().listObjectsByClass(Long.class); + assertNotNull(list); + assertFalse(list.isEmpty()); + Long l2 = (Long) list.get(0); + assertEquals(l, l2); + } + +// @Test +// public void testLookupByUUID() { +// String uuid = BaseModel.generateUUID(); +// assertNull(ObjectFactory.get().lookupByUUID(Owner.class, uuid)); +// +// Owner owner = new Owner(uuid); +// owner.setName("unit-test-owner"); +// ObjectFactory.get().store(owner); +// Object o = ObjectFactory.get().lookupByUUID(Owner.class, uuid); +// assertNotNull(o); +// assertEquals(o.getClass(), Owner.class); +// assertEquals(((Owner)o).getUuid(), owner.getUuid()); +// } + +// @Test +// public void testLookupByFieldName() { +// String uuid = BaseModel.generateUUID(); +// assertNull(ObjectFactory.get().lookupByUUID(Owner.class, uuid)); +// +// Owner owner = new Owner(uuid); +// owner.setName("unit-test-org"); +// ObjectFactory.get().store(owner); +// +// Owner o2 = (Owner) ObjectFactory.get().lookupByFieldName( +// Owner.class, "uuid", uuid); +// assertNotNull(o2); +// assertEquals(owner, o2); +// } +} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/model/test/OwnerTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/model/test/OwnerTest.java new file mode 100644 index 0000000..f331d95 --- /dev/null +++ b/proxy/src/test/java/org/fedoraproject/candlepin/model/test/OwnerTest.java @@ -0,0 +1,128 @@ +/** + * 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 static org.junit.Assert.*; + +import org.fedoraproject.candlepin.model.Consumer; +import org.fedoraproject.candlepin.model.ConsumerType; +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 org.fedoraproject.candlepin.test.TestUtil; +import org.junit.Test; + +public class OwnerTest extends DatabaseTestFixture { + + @Test + public void testCreate() throws Exception { + String ownerName = "Example Corporation"; + Owner o = new Owner(ownerName); + ownerCurator.create(o); + + Owner result = (Owner)entityManager().createQuery( + "select o from Owner o where o.name = :name") + .setParameter("name", ownerName).getSingleResult(); + + assertNotNull(result); + assertEquals(ownerName, result.getName()); + assertTrue(result.getId() > 0); + assertEquals(o.getId(), result.getId()); + } + + @Test + public void testList() throws Exception { + int beforeCount = entityManager().createQuery("select o from Owner as o").getResultList().size(); + + for (int i = 0; i < 10; i++) { + ownerCurator.create(new Owner("Corp " + i)); + } + + int afterCount = entityManager().createQuery("select o from Owner as o").getResultList().size(); + assertEquals(10, afterCount - beforeCount); + } + + @Test + public void testObjectRelationships() throws Exception { + Owner owner = new Owner("test-owner"); + // Product + Product rhel = new Product(); + rhel.setName("Red Hat Enterprise Linux"); + + // User + User u = new User(); + u.setLogin("test-login"); + u.setPassword("redhat"); + owner.addUser(u); + assertEquals(1, owner.getUsers().size()); + + // Consumer + Consumer c = new Consumer(); + c.setOwner(owner); + owner.addConsumer(c); + c.addConsumedProduct(rhel); + assertEquals(1, owner.getConsumers().size()); + assertEquals(1, c.getConsumedProducts().size()); + + // EntitlementPool + EntitlementPool pool = new EntitlementPool(); + owner.addEntitlementPool(pool); + pool.setProduct(rhel); + assertEquals(1, owner.getEntitlementPools().size()); + } + + @Test + public void bidirectionalConsumers() throws Exception { + beginTransaction(); + Owner o = TestUtil.createOwner(); + ConsumerType consumerType = TestUtil.createConsumerType(); + Consumer c1 = TestUtil.createConsumer(consumerType, o); + Consumer c2 = TestUtil.createConsumer(consumerType, o); + o.addConsumer(c1); + o.addConsumer(c2); + + ownerCurator.create(o); + consumerTypeCurator.create(consumerType); + consumerCurator.create(c1); + consumerCurator.create(c2); + + assertEquals(2, o.getConsumers().size()); + + Owner lookedUp = ownerCurator.find(o.getId()); + assertEquals(2, lookedUp.getConsumers().size()); + } + + @Test + public void bidirectionalUsers() throws Exception { + Owner o = TestUtil.createOwner(); + + User u1 = TestUtil.createUser(o); + User u2 = TestUtil.createUser(o); + + o.addUser(u1); + o.addUser(u2); + + ownerCurator.create(o); + + assertEquals(2, o.getUsers().size()); + + entityManager().clear(); + Owner lookedUp = ownerCurator.find(o.getId()); + assertEquals(2, lookedUp.getUsers().size()); + } + +} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/model/test/ProductTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/model/test/ProductTest.java new file mode 100644 index 0000000..1558405 --- /dev/null +++ b/proxy/src/test/java/org/fedoraproject/candlepin/model/test/ProductTest.java @@ -0,0 +1,138 @@ +package org.fedoraproject.candlepin.model.test; + +import static org.junit.Assert.*; + +import org.fedoraproject.candlepin.model.Product; +import org.fedoraproject.candlepin.util.EntityManagerUtil; + +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +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 DatabaseTestFixture { + + + @Test + public void normalCreate() { + + Product prod = new Product("cptest-label", "My Product"); + persistAndCommit(prod); + + List results = entityManager().createQuery("select p from Product as p") + .getResultList(); + assertEquals(1, results.size()); + } + + @Test(expected = PersistenceException.class) + public void nameRequired() { + + Product prod = new Product(); + prod.setLabel("someproductlabel"); + persistAndCommit(prod); + + } + + @Test(expected = PersistenceException.class) + public void labelRequired() { + + Product prod = new Product(); + prod.setName("My Product Name"); + persistAndCommit(prod); + } + + @Test(expected = PersistenceException.class) + public void nameUnique() { + + Product prod = new Product("label1", "name"); + persistAndCommit(prod); + + Product prod2 = new Product("label2", "name"); + persistAndCommit(prod2); + } + + @Test(expected = PersistenceException.class) + public void labelUnique() { + + Product prod = new Product("label1", "name"); + Product prod2 = new Product("label1", "name2"); + persistAndCommit(prod); + + persistAndCommit(prod2); + } + + @Test + public void addChildProducts() { + 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"); + + parent.addChildProduct(child1); + parent.addChildProduct(child2); + entityManager().persist(parent); + commitTransaction(); + + //EntityManager em2 = EntityManagerUtil.createEntityManager(); XXX not sure why we need to ask for a new EntityManager here + Product result = (Product)entityManager().createQuery( + "select p from Product as p where name = :name") + .setParameter("name", parent.getName()) + .getSingleResult(); + assertEquals(2, result.getChildProducts().size()); + } + + @Test(expected = PersistenceException.class) + public void childHasSingleParentOnly() { + beginTransaction(); + + Product parent1 = new Product("parent-product1", "Parent Product 1"); + Product child1 = new Product("child-product1", "Child Product 1"); + Product parent2 = new Product("parent-product2", "Parent Product 2"); + + parent1.addChildProduct(child1); + parent2.addChildProduct(child1); // should cause the failure + + entityManager().persist(child1); + entityManager().persist(parent1); + entityManager().persist(parent2); + commitTransaction(); + } + + @Test + public void testCascading() { + beginTransaction(); + + Product parent1 = new Product("parent-product1", "Parent Product 1"); + Product child1 = new Product("child-product1", "Child Product 1"); + parent1.addChildProduct(child1); + entityManager().persist(parent1); + commitTransaction(); + + //EntityManager em2 = EntityManagerUtil.createEntityManager(); + Product result = (Product)entityManager().createQuery( + "select p from Product as p where name = :name") + .setParameter("name", child1.getName()) + .getSingleResult(); + assertNotNull(result); + + beginTransaction(); + entityManager().remove(parent1); + commitTransaction(); + + //em2 = EntityManagerUtil.createEntityManager(); + List results = entityManager().createQuery( + "select p from Product as p where name = :name") + .setParameter("name", child1.getName()) + .getResultList(); + assertEquals(0, results.size()); + } + +} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/resource/ConsumerResourceTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/resource/ConsumerResourceTest.java deleted file mode 100644 index 7eec9bf..0000000 --- a/proxy/src/test/java/org/fedoraproject/candlepin/resource/ConsumerResourceTest.java +++ /dev/null @@ -1,125 +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.resource; - -import static org.junit.Assert.*; - -import java.util.HashMap; -import java.util.Map; - -import org.fedoraproject.candlepin.model.Consumer; -import org.fedoraproject.candlepin.model.ConsumerInfo; -import org.fedoraproject.candlepin.model.ConsumerCurator; -import org.fedoraproject.candlepin.model.ConsumerType; -import org.fedoraproject.candlepin.model.ConsumerTypeCurator; -import org.fedoraproject.candlepin.model.Owner; -import org.fedoraproject.candlepin.model.OwnerCurator; -import org.fedoraproject.candlepin.resource.ConsumerResource; -import org.fedoraproject.candlepin.test.DatabaseTestFixture; -import org.fedoraproject.candlepin.test.TestUtil; -import org.junit.Before; -import org.junit.Test; - -import com.google.inject.Guice; -import com.google.inject.Injector; -import com.wideplay.warp.persist.PersistenceService; -import com.wideplay.warp.persist.UnitOfWork; - -/** - * ConsumerResourceTest - */ -public class ConsumerResourceTest extends DatabaseTestFixture { - - private ConsumerCurator consumerCurator; - private ConsumerTypeCurator consumerTypeCurator; - private OwnerCurator ownerCurator; - - private ConsumerType standardSystemType; - - @Before - public void setUp() { - super.setUp(); - - Injector injector = Guice.createInjector( - new CandlepinTestingModule(), - PersistenceService.usingJpa() - .across(UnitOfWork.TRANSACTION) - .buildModule() - ); - - - consumerCurator = injector.getInstance(ConsumerCurator.class); - consumerTypeCurator = injector.getInstance(ConsumerTypeCurator.class); - ownerCurator = injector.getInstance(OwnerCurator.class); - - standardSystemType = new ConsumerType("standard-system"); - consumerTypeCurator.create(standardSystemType); - } - - @Test - public void testCreateConsumer() throws Exception { - ConsumerResource capi = new ConsumerResource(ownerCurator, consumerCurator, - consumerTypeCurator); - - Map ci = new HashMap(); - ci.put("cpu_cores", "8"); - - Owner owner = TestUtil.createOwner(); - ownerCurator.create(owner); - - Consumer returned = capi.create(/*ci, */standardSystemType.getLabel()); - assertNotNull(returned.getUuid()); - assertEquals("standard-system", returned.getType().getLabel()); - assertEquals(owner.getId(), returned.getOwner().getId()); - - Consumer lookedUp = consumerCurator.lookupByUuid(returned.getUuid()); - assertNotNull(lookedUp); - } - - // TODO: Test no such consumer type. - -// @Test -// public void testDelete() { -// Consumer c = TestUtil.createConsumer(); -// String uuid = c.getUuid(); -// ConsumerResource capi = new ConsumerResource(); -// assertNotNull(ObjectFactory.get().lookupByUUID(c.getClass(), uuid)); -// capi.delete(uuid); -// assertNull(ObjectFactory.get().lookupByUUID(c.getClass(), uuid)); -// } - -// @Test -// public void testJSON() { -// ClientConfig cc = new DefaultClientConfig(); -// Client c = Client.create(cc); -// -// ConsumerInfo ci = new ConsumerInfo(); -// ci.setMetadataField("name", "jsontestname"); -// ci.setType(new ConsumerType("standard-system")); -// -// WebResource res = -// c.resource("http://localhost:8080/candlepin/consumer/"); -// Consumer rc = res.type("application/json").post(Consumer.class, ci); -// assertNotNull(rc); -// assertNotNull(rc.getUuid()); -// System.out.println(rc.getUuid()); - -// WebResource delres = -// c.resource("http://localhost:8080/candlepin/consumer/"); -// delres.accept("application/json").delete(rc.getUuid()); -// -// assertNull(ObjectFactory.get().lookupByUUID(c.getClass(), rc.getUuid())); -// } -} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/resource/EntitlementMatcherTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/resource/EntitlementMatcherTest.java deleted file mode 100644 index dff511d..0000000 --- a/proxy/src/test/java/org/fedoraproject/candlepin/resource/EntitlementMatcherTest.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.resource; - -import static org.junit.Assert.*; - -import java.util.List; - -import org.fedoraproject.candlepin.model.Consumer; -import org.fedoraproject.candlepin.model.ConsumerType; -import org.fedoraproject.candlepin.model.ObjectFactory; -import org.fedoraproject.candlepin.model.Product; -import org.fedoraproject.candlepin.model.ProductFactory; -import org.fedoraproject.candlepin.resource.EntitlementMatcher; -import org.fedoraproject.candlepin.test.TestUtil; -import org.junit.Test; - -/** - * EntitlementMatcherTest - * @version $Rev$ - */ -public class EntitlementMatcherTest { - - @Test - public void testIsCompatable() throws Exception { - Consumer consumer = TestUtil.createConsumer(); - ConsumerType typeSystem = ProductFactory.get().lookupConsumerTypeByLabel("system"); - consumer.setType(typeSystem); - - List f = ObjectFactory.get().listObjectsByClass(Product.class); - Product rhel = (Product) ObjectFactory.get().lookupByFieldName( - Product.class, "label", "rhel"); - Product rhelvirt = (Product) ObjectFactory.get().lookupByFieldName( - Product.class, "label", "rhel-virt"); - - EntitlementMatcher m = new EntitlementMatcher(); - - assertTrue(m.isCompatible(consumer, rhel)); - - ConsumerType vmwarehost = - ProductFactory.get().lookupConsumerTypeByLabel("vmwarehost"); - consumer.setType(vmwarehost); - - // Check that you can't use rhelvirt on a vmware host - assertFalse(m.isCompatible(consumer, rhelvirt)); - } -} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/resource/EntitlementResourceTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/resource/EntitlementResourceTest.java deleted file mode 100644 index 04a4b56..0000000 --- a/proxy/src/test/java/org/fedoraproject/candlepin/resource/EntitlementResourceTest.java +++ /dev/null @@ -1,207 +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.resource; - -import static org.junit.Assert.*; - -import java.sql.Date; -import java.util.ArrayList; -import java.util.List; - -import org.fedoraproject.candlepin.model.Consumer; -import org.fedoraproject.candlepin.model.ConsumerCurator; -import org.fedoraproject.candlepin.model.ConsumerType; -import org.fedoraproject.candlepin.model.ConsumerTypeCurator; -import org.fedoraproject.candlepin.model.EntitlementPool; -import org.fedoraproject.candlepin.model.EntitlementPoolCurator; -import org.fedoraproject.candlepin.model.Owner; -import org.fedoraproject.candlepin.model.OwnerCurator; -import org.fedoraproject.candlepin.model.Product; -import org.fedoraproject.candlepin.model.ProductCurator; -import org.fedoraproject.candlepin.resource.EntitlementResource; -import org.fedoraproject.candlepin.test.DatabaseTestFixture; -import org.fedoraproject.candlepin.test.TestUtil; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; - -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.api.client.config.ClientConfig; -import com.sun.jersey.api.client.config.DefaultClientConfig; - - -/** - * ConsumerResourceTest - */ -public class EntitlementResourceTest extends DatabaseTestFixture { - - private Consumer consumer; - private Product product; - private EntitlementPool ep; - private Owner owner; - private EntitlementResource eapi; - - @Before - public void createTestObjects() { - owner = TestUtil.createOwner(); - ownerCurator.create(owner); - - ConsumerType type = new ConsumerType("some-consumer-type"); - consumerTypeCurator.create(type); - - consumer = TestUtil.createConsumer(type, owner); - consumerCurator.create(consumer); - - product = TestUtil.createProduct(); - productCurator.create(product); - - Date pastDate = new Date(System.currentTimeMillis() - 10000000); - Date futuredate = new Date(System.currentTimeMillis() + 1000000000); - ep = new EntitlementPool(owner, product, new Long(10), pastDate, futuredate); - entitlementPoolCurator.create(ep); - - eapi = new EntitlementResource(entitlementPoolCurator, ownerCurator, consumerCurator, - productCurator); - - } - - @Ignore - public void testEntitle() throws Exception { - -// Form f = new Form(); -// f.add("consumer_id", consumer.getId()); -// f.add("product_id", product.getId()); - String cert = (String) eapi.entitle(consumer.getUuid(), product.getLabel()); - - assertNotNull(cert); - - consumer = consumerCurator.lookupByUuid(consumer.getUuid()); - assertEquals(1, consumer.getConsumedProducts().size()); - assertEquals(product.getId(), consumer.getConsumedProducts().iterator() - .next().getId()); - assertEquals(1, consumer.getEntitlements().size()); - - ep = entitlementPoolCurator.find(ep.getId()); - assertEquals(new Long(1), ep.getCurrentMembers()); - } - - @Test - public void testMaxMembership() { - - // 10 entitlements available, lets try to entitle 11 consumers. - for (int i = 0; i < ep.getMaxMembers(); i++) { - Consumer c = TestUtil.createConsumer(consumer.getType(), owner); - consumerCurator.create(c); - eapi.entitle(c.getUuid(), product.getLabel()); - } - - // Now for the 11th: - try { - Consumer c = TestUtil.createConsumer(consumer.getType(), owner); - eapi.entitle(c.getUuid(), product.getLabel()); - fail(); - } - catch (RuntimeException e) { - // expected - } - - } - - @Test - public void testEntitlementsHaveExpired() { - Product myProduct = TestUtil.createProduct(); - productCurator.create(myProduct); - Date pastDate = new Date(System.currentTimeMillis() - 10000000); - Date notSoPastDate = new Date(System.currentTimeMillis() - 100000); - EntitlementPool anotherPool = new EntitlementPool(owner, myProduct, new Long(10), - pastDate, notSoPastDate); - entitlementPoolCurator.create(anotherPool); - - try { - eapi.entitle(consumer.getUuid(), myProduct.getLabel()); - fail(); - } - catch (RuntimeException e) { - // expected - } - } - - @Test - public void testEntitleOwnerHasNoEntitlements() { - // TODO - } - - @Test - public void testEntitleOwnerHasNoAviailableEntitlements() { - // TODO - } - - @Test - public void testEntitleConsumerAlreadyEntitledForProduct() { - // TODO - } - - @Ignore - public void testHasEntitlement() { - - eapi.entitle(consumer.getUuid(), product.getLabel()); - - // TODO: Disabling this test, boils into ObjectFactory things that need - // to be fixed before we can do this check! Sorry! :) - dgoodwin -// assertTrue(eapi.hasEntitlement(consumer.getUuid(), product.getUuid())); - } - - // TODO: Re-enable once ObjectFactory is Hibernatized or removed. -// @Test -// public void testListAvailableEntitlements() { -// EntitlementResource eapi = new EntitlementResource(); -//// consumer.setType(new ConsumerType("standard-system")); -// Form f = new Form(); -// f.add("consumer_id", consumer.getId()); -// -// List avail = eapi.listAvailableEntitlements(consumer.getId()); -// assertNotNull(avail); -// assertTrue(avail.size() > 0); -// } - - @Test - @Ignore - public void testJson() { - ClientConfig cc = new DefaultClientConfig(); - Client c = Client.create(cc); - - // WebResource getresource = c.resource("http://localhost:8080/candlepin/entitle/"); - - - Object[] params = new Object[2]; - params[0] = consumer; - params[1] = product; - List aparams = new ArrayList(); - aparams.add(consumer); - aparams.add(product); - - WebResource postresource = - c.resource("http://localhost:8080/candlepin/entitlement/foo/"); - postresource.accept("application/json").type("application/json").post(consumer); - - // System.out.println(jto.getName()); - // jto = getresource.accept("application/json").get(JsonTestObject.class); - // assertEquals("testname", jto.getName()); - // assertEquals("AEF", jto.getUuid()); - } - - -} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/resource/TestResourceTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/resource/TestResourceTest.java deleted file mode 100644 index 01f0806..0000000 --- a/proxy/src/test/java/org/fedoraproject/candlepin/resource/TestResourceTest.java +++ /dev/null @@ -1,95 +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.resource; - -import static org.junit.Assert.*; - -import java.util.ArrayList; -import java.util.List; - -import org.fedoraproject.candlepin.model.ConsumerType; -import org.fedoraproject.candlepin.model.JsonTestObject; -import org.fedoraproject.candlepin.resource.TestResource; -import org.junit.Test; -import org.junit.Ignore; - -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.api.client.config.ClientConfig; -import com.sun.jersey.api.client.config.DefaultClientConfig; - - -/** - * TestResourceTest - * @version $Rev$ - */ -@Ignore -public class TestResourceTest { - - private JsonTestObject createTestObject() { - JsonTestObject jto = new JsonTestObject(); - jto.setName("testname"); - List l = new ArrayList(); - l.add("hey there"); - l.add("how are you?"); - jto.setStringList(l); - return jto; - } - -// @Test - @Ignore - public void testJson() { - ClientConfig cc = new DefaultClientConfig(); - Client c = Client.create(cc); - - - JsonTestObject jto = createTestObject(); - - WebResource postresource = c.resource("http://localhost:8080/candlepin/test/"); - postresource.accept("application/json").type("application/json").post(jto); - - WebResource getresource = c.resource("http://localhost:8080/candlepin/test/"); - System.out.println(jto.getName()); - jto = getresource.accept("application/json").get(JsonTestObject.class); - assertEquals("testname", jto.getName()); - assertNotNull(jto.getStringList()); - assertEquals(2, jto.getStringList().size()); - assertNull(jto.getParent()); - System.out.println(jto.getStringList()); - } - - // @Test - @Ignore - public void testGet() { - TestResource tr = new TestResource(); - assertNull(tr.get()); - - JsonTestObject jto = createTestObject(); - tr.create(jto); - assertEquals(jto, tr.get()); - } - - // @Test - public void testConsumerType() { - ClientConfig cc = new DefaultClientConfig(); - Client c = Client.create(cc); - - WebResource getresource = - c.resource("http://localhost:8080/candlepin/test/consumertype"); - ConsumerType ct = getresource.accept("application/json").get(ConsumerType.class); - assertNotNull(ct); - assertEquals("testtype", ct.getLabel()); - } -} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/resource/UserResourceTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/resource/UserResourceTest.java deleted file mode 100644 index 03acd18..0000000 --- a/proxy/src/test/java/org/fedoraproject/candlepin/resource/UserResourceTest.java +++ /dev/null @@ -1,77 +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.resource; - -import static org.junit.Assert.*; - -import java.util.List; - -import org.fedoraproject.candlepin.model.User; -import org.fedoraproject.candlepin.resource.UserResource; -import org.junit.Test; - - -/** - * UserResourceTest - * @version $Rev$ - */ -public class UserResourceTest { - - private UserResource api = new UserResource(); - - @Test - public void testNewUser() { - User user = api.create("candlepin", "cp_p@$sw0rd"); - assertNotNull(user); - assertEquals("candlepin", user.getLogin()); - assertEquals("cp_p@$sw0rd", user.getPassword()); - - user = api.create(null, null); - assertNotNull(user); - assertEquals(null, user.getLogin()); - assertEquals(null, user.getPassword()); - - user = api.create("", ""); - assertNotNull(user); - assertEquals("", user.getLogin()); - assertEquals("", user.getPassword()); - } - - @Test - public void testList() { - List users = api.list(); - int origSize = users.size(); - // create 1 - api.create("candlepin", "cp_p@$sw0rd"); - - // create 2 - api.create("jesusr", "n0P@$sw0rD"); - - // get the list back - users = api.list(); - System.out.println("Users: " + users.toString()); - assertNotNull(users); - assertEquals(origSize + 2, users.size()); - assertEquals(User.class, users.get(0).getClass()); - } - - @Test - public void testGet() { - User user = api.get("test-login"); - assertNotNull(user); - assertEquals("test-login", user.getLogin()); - } - -} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/resource/cert/CertTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/resource/cert/CertTest.java deleted file mode 100644 index 702446f..0000000 --- a/proxy/src/test/java/org/fedoraproject/candlepin/resource/cert/CertTest.java +++ /dev/null @@ -1,284 +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.resource.cert; - -import java.math.BigInteger; -import java.security.KeyFactory; -import java.security.KeyStore; -import java.security.PrivateKey; -import java.security.PublicKey; -import java.security.Security; -import java.security.cert.X509Certificate; -import java.security.spec.RSAPrivateCrtKeySpec; -import java.security.spec.RSAPublicKeySpec; -import java.util.Date; - -import org.bouncycastle.asn1.ASN1EncodableVector; -import org.bouncycastle.asn1.DERSequence; -import org.bouncycastle.asn1.x509.GeneralName; -import org.bouncycastle.jce.provider.BouncyCastleProvider; -import org.bouncycastle.x509.AttributeCertificateHolder; -import org.bouncycastle.x509.AttributeCertificateIssuer; -import org.bouncycastle.x509.X509Attribute; -import org.bouncycastle.x509.X509V2AttributeCertificate; -import org.bouncycastle.x509.X509V2AttributeCertificateGenerator; -import org.bouncycastle.x509.examples.AttrCertExample; -import org.fedoraproject.candlepin.resource.cert.CertGenerator; -import org.junit.Test; - -/** - * CertTest - * @version $Rev$ - */ -public class CertTest { - - @Test - public void testCertGenerator() { - String cert = CertGenerator.getCertString(); -// System.out.println("Cert: " + cert); - } - - @Test - public void testCertExample() throws Exception { - - Security.addProvider(new BouncyCastleProvider()); - - // - // personal keys - // - RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec( - new BigInteger("b4a7e46170574f16a97082b22be58b6a2a629798419" + - "be12872a4bdba626cfae9900f76abfb12139dce5de5" + - "6564fab2b6543165a040c606887420e33d91ed7ed7", 16), - new BigInteger("11", 16)); - - RSAPrivateCrtKeySpec privKeySpec = new RSAPrivateCrtKeySpec( - new BigInteger("b4a7e46170574f16a97082b22be58b6a2a629798419" + - "be12872a4bdba626cfae9900f76abfb12139dce5de5" + - "6564fab2b6543165a040c606887420e33d91ed7ed7", 16), - new BigInteger("11", 16), - new BigInteger("9f66f6b05410cd503b2709e88115d55daced94d1a34" + - "d4e32bf824d0dde6028ae79c5f07b580f5dce240d71" + - "11f7ddb130a7945cd7d957d1920994da389f490c89", 16), - new BigInteger("c0a0758cdf14256f78d4708c86becdead1b50ad4ad6" + - "c5c703e2168fbf37884cb", 16), - new BigInteger("f01734d7960ea60070f1b06f2bb81bfac48ff192ae1" + - "8451d5e56c734a5aab8a5", 16), - new BigInteger("b54bb9edff22051d9ee60f9351a48591b6500a31942" + - "9c069a3e335a1d6171391", 16), - new BigInteger("d3d83daf2a0cecd3367ae6f8ae1aeb82e9ac2f816c6" + - "fc483533d8297dd7884cd", 16), - new BigInteger("b8f52fc6f38593dabb661d3f50f8897f8106eee68b1" + - "bce78a95b132b4e5b5d19", 16)); - - // - // ca keys - // - RSAPublicKeySpec caPubKeySpec = new RSAPublicKeySpec( - new BigInteger("b259d2d6e627a768c94be36164c2d9fc79d97aab925" + - "3140e5bf17751197731d6f7540d2509e7b9ffee0a70" + - "a6e26d56e92d2edd7f85aba85600b69089f35f6bdbf" + - "3c298e05842535d9f064e6b0391cb7d306e0a2d20c4" + - "dfb4e7b49a9640bdea26c10ad69c3f05007ce2513ce" + - "e44cfe01998e62b6c3637d3fc0391079b26ee36d5", 16), - new BigInteger("11", 16)); - - RSAPrivateCrtKeySpec caPrivKeySpec = new RSAPrivateCrtKeySpec( - new BigInteger("b259d2d6e627a768c94be36164c2d9fc79d97aab925" + - "3140e5bf17751197731d6f7540d2509e7b9ffee0a70" + - "a6e26d56e92d2edd7f85aba85600b69089f35f6bdbf" + - "3c298e05842535d9f064e6b0391cb7d306e0a2d20c4" + - "dfb4e7b49a9640bdea26c10ad69c3f05007ce2513ce" + - "e44cfe01998e62b6c3637d3fc0391079b26ee36d5", 16), - new BigInteger("11", 16), - new BigInteger("92e08f83cc9920746989ca5034dcb384a094fb9c5a6" + - "288fcc4304424ab8f56388f72652d8fafc65a4b9020" + - "896f2cde297080f2a540e7b7ce5af0b3446e1258d1d" + - "d7f245cf54124b4c6e17da21b90a0ebd22605e6f45c" + - "9f136d7a13eaac1c0f7487de8bd6d924972408ebb58" + - "af71e76fd7b012a8d0e165f3ae2e5077a8648e619", 16), - new BigInteger("f75e80839b9b9379f1cf1128f321639757dba514642" + - "c206bbbd99f9a4846208b3e93fbbe5e0527cc59b1d4" + - "b929d9555853004c7c8b30ee6a213c3d1bb7415d03", 16), - new BigInteger("b892d9ebdbfc37e397256dd8a5d3123534d1f037262" + - "84743ddc6be3a709edb696fc40c7d902ed804c6eee7" + - "30eee3d5b20bf6bd8d87a296813c87d3b3cc9d7947", 16), - new BigInteger("1d1a2d3ca8e52068b3094d501c9a842fec37f54db16" + - "e9a67070a8b3f53cc03d4257ad252a1a640eadd6037" + - "24d7bf3737914b544ae332eedf4f34436cac25ceb5", 16), - new BigInteger("6c929e4e81672fef49d9c825163fec97c4b7ba7acb2" + - "6c0824638ac22605d7201c94625770984f78a56e6e2" + - "5904fe7db407099cad9b14588841b94f5ab498dded", 16), - new BigInteger("dae7651ee69ad1d081ec5e7188ae126f6004ff39556" + - "bde90e0b870962fa7b926d070686d8244fe5a9aa709" + - "a95686a104614834b0ada4b10f53197a5cb4c97339", 16)); - - // - // set up the keys - // - KeyFactory fact = KeyFactory.getInstance("RSA", "BC"); - PrivateKey caPrivKey = fact.generatePrivate(caPrivKeySpec); - PublicKey caPubKey = fact.generatePublic(caPubKeySpec); - PrivateKey privKey = fact.generatePrivate(privKeySpec); - PublicKey pubKey = fact.generatePublic(pubKeySpec); - - // - // note in this case we are using the CA certificate for both the client - // cetificate - // and the attribute certificate. This is to make the vcode simpler to - // read, in practice - // the CA for the attribute certificate should be different to that of - // the client certificate - // - X509Certificate caCert = AttrCertExample.createAcIssuerCert(caPubKey, - caPrivKey); - X509Certificate clientCert = AttrCertExample.createClientCert(pubKey, - caPrivKey, caPubKey); -// System.out.println("CaCert: " + caCert); -// System.out.println("clientCert: " + clientCert); - // Instantiate a new AC generator - X509V2AttributeCertificateGenerator acGen = - new X509V2AttributeCertificateGenerator(); - - acGen.reset(); - - // - // Holder: here we use the IssuerSerial form - // - acGen.setHolder(new AttributeCertificateHolder(clientCert)); - - // set the Issuer - acGen.setIssuer(new AttributeCertificateIssuer(caCert - .getSubjectX500Principal())); - - // - // serial number (as it's an example we don't have to keep track of the - // serials anyway - // - acGen.setSerialNumber(new BigInteger("1")); - - // not Before - acGen.setNotBefore(new Date(System.currentTimeMillis() - 50000)); - - // not After - acGen.setNotAfter(new Date(System.currentTimeMillis() + 50000)); - - // signature Algorithmus - acGen.setSignatureAlgorithm("SHA1WithRSAEncryption"); - - // the actual attributes - GeneralName roleName = new GeneralName(GeneralName.rfc822Name, - "DAU123456789"); - ASN1EncodableVector roleSyntax = new ASN1EncodableVector(); - roleSyntax.add(roleName); - - // roleSyntax OID: 2.5.24.72 - X509Attribute attributes = new X509Attribute("2.5.24.72", - new DERSequence(roleSyntax)); - - acGen.addAttribute(attributes); - - // finally create the AC - X509V2AttributeCertificate att = (X509V2AttributeCertificate) acGen - .generate(caPrivKey, "BC"); - - - - String encoded = new String(att.getEncoded()); - //System.out.println("CERT CERT: " + encoded); - KeyStore store = KeyStore.getInstance("PKCS12"); - String pass = "redhat"; - - - /*FileOutputStream fout = new FileOutputStream("/tmp/foo.file"); - store.load(null, null); - store.store(fout, pass.toCharArray()); - X509CertificateObject ccert = new - X509CertificateObject(new X509CertificateStructure(new DERSequence(att)));*/ - // - // starting here, we parse the newly generated AC - // - - // Holder - - AttributeCertificateHolder h = att.getHolder(); - if (h.match(clientCert)) { - if (h.getEntityNames() != null) { -// System.out.println(h.getEntityNames().length + -// " entity names found"); - } - if (h.getIssuer() != null) { -// System.out.println(h.getIssuer().length + -// " issuer names found, serial number " + -// h.getSerialNumber()); - } -// System.out.println("Matches original client x509 cert"); - } - - // Issuer - - AttributeCertificateIssuer issuer = att.getIssuer(); - if (issuer.match(caCert)) { - if (issuer.getPrincipals() != null) { -// System.out.println(issuer.getPrincipals().length + -// " entity names found"); - } -// System.out.println("Matches original ca x509 cert"); - } - - // Dates -// System.out.println("valid not before: " + att.getNotBefore()); -// System.out.println("valid not before: " + att.getNotAfter()); - - // check the dates, an exception is thrown in checkValidity()... - - try { - att.checkValidity(); - att.checkValidity(new Date()); - } - catch (Exception e) { - System.out.println(e); - } - - // verify - - try { - att.verify(caPubKey, "BC"); - } - catch (Exception e) { - System.out.println(e); - } - - // Attribute - X509Attribute[] attribs = att.getAttributes(); -// System.out.println("cert has " + attribs.length + " attributes:"); - for (int i = 0; i < attribs.length; i++) { - X509Attribute a = attribs[i]; -// System.out.println("OID: " + a.getOID()); - - // currently we only check for the presence of a 'RoleSyntax' - // attribute - - if (a.getOID().equals("2.5.24.72")) { -// System.out.println("rolesyntax read from cert!"); - } - } - - - - - // CertificateFactory.getInstance - } -} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/resource/test/ConsumerResourceTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/resource/test/ConsumerResourceTest.java new file mode 100644 index 0000000..1b21324 --- /dev/null +++ b/proxy/src/test/java/org/fedoraproject/candlepin/resource/test/ConsumerResourceTest.java @@ -0,0 +1,125 @@ +/** + * 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.resource.test; + +import static org.junit.Assert.*; + +import java.util.HashMap; +import java.util.Map; + +import org.fedoraproject.candlepin.model.Consumer; +import org.fedoraproject.candlepin.model.ConsumerInfo; +import org.fedoraproject.candlepin.model.ConsumerCurator; +import org.fedoraproject.candlepin.model.ConsumerType; +import org.fedoraproject.candlepin.model.ConsumerTypeCurator; +import org.fedoraproject.candlepin.model.Owner; +import org.fedoraproject.candlepin.model.OwnerCurator; +import org.fedoraproject.candlepin.resource.ConsumerResource; +import org.fedoraproject.candlepin.test.DatabaseTestFixture; +import org.fedoraproject.candlepin.test.TestUtil; +import org.junit.Before; +import org.junit.Test; + +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.wideplay.warp.persist.PersistenceService; +import com.wideplay.warp.persist.UnitOfWork; + +/** + * ConsumerResourceTest + */ +public class ConsumerResourceTest extends DatabaseTestFixture { + + private ConsumerCurator consumerCurator; + private ConsumerTypeCurator consumerTypeCurator; + private OwnerCurator ownerCurator; + + private ConsumerType standardSystemType; + + @Before + public void setUp() { + super.setUp(); + + Injector injector = Guice.createInjector( + new CandlepinTestingModule(), + PersistenceService.usingJpa() + .across(UnitOfWork.TRANSACTION) + .buildModule() + ); + + + consumerCurator = injector.getInstance(ConsumerCurator.class); + consumerTypeCurator = injector.getInstance(ConsumerTypeCurator.class); + ownerCurator = injector.getInstance(OwnerCurator.class); + + standardSystemType = new ConsumerType("standard-system"); + consumerTypeCurator.create(standardSystemType); + } + + @Test + public void testCreateConsumer() throws Exception { + ConsumerResource capi = new ConsumerResource(ownerCurator, consumerCurator, + consumerTypeCurator); + + Map ci = new HashMap(); + ci.put("cpu_cores", "8"); + + Owner owner = TestUtil.createOwner(); + ownerCurator.create(owner); + + Consumer returned = capi.create(/*ci, */standardSystemType.getLabel()); + assertNotNull(returned.getUuid()); + assertEquals("standard-system", returned.getType().getLabel()); + assertEquals(owner.getId(), returned.getOwner().getId()); + + Consumer lookedUp = consumerCurator.lookupByUuid(returned.getUuid()); + assertNotNull(lookedUp); + } + + // TODO: Test no such consumer type. + +// @Test +// public void testDelete() { +// Consumer c = TestUtil.createConsumer(); +// String uuid = c.getUuid(); +// ConsumerResource capi = new ConsumerResource(); +// assertNotNull(ObjectFactory.get().lookupByUUID(c.getClass(), uuid)); +// capi.delete(uuid); +// assertNull(ObjectFactory.get().lookupByUUID(c.getClass(), uuid)); +// } + +// @Test +// public void testJSON() { +// ClientConfig cc = new DefaultClientConfig(); +// Client c = Client.create(cc); +// +// ConsumerInfo ci = new ConsumerInfo(); +// ci.setMetadataField("name", "jsontestname"); +// ci.setType(new ConsumerType("standard-system")); +// +// WebResource res = +// c.resource("http://localhost:8080/candlepin/consumer/"); +// Consumer rc = res.type("application/json").post(Consumer.class, ci); +// assertNotNull(rc); +// assertNotNull(rc.getUuid()); +// System.out.println(rc.getUuid()); + +// WebResource delres = +// c.resource("http://localhost:8080/candlepin/consumer/"); +// delres.accept("application/json").delete(rc.getUuid()); +// +// assertNull(ObjectFactory.get().lookupByUUID(c.getClass(), rc.getUuid())); +// } +} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/resource/test/EntitlementMatcherTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/resource/test/EntitlementMatcherTest.java new file mode 100644 index 0000000..4bb31b6 --- /dev/null +++ b/proxy/src/test/java/org/fedoraproject/candlepin/resource/test/EntitlementMatcherTest.java @@ -0,0 +1,59 @@ +/** + * 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.resource.test; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.fedoraproject.candlepin.model.Consumer; +import org.fedoraproject.candlepin.model.ConsumerType; +import org.fedoraproject.candlepin.model.ObjectFactory; +import org.fedoraproject.candlepin.model.Product; +import org.fedoraproject.candlepin.model.ProductFactory; +import org.fedoraproject.candlepin.resource.EntitlementMatcher; +import org.fedoraproject.candlepin.test.TestUtil; +import org.junit.Test; + +/** + * EntitlementMatcherTest + * @version $Rev$ + */ +public class EntitlementMatcherTest { + + @Test + public void testIsCompatable() throws Exception { + Consumer consumer = TestUtil.createConsumer(); + ConsumerType typeSystem = ProductFactory.get().lookupConsumerTypeByLabel("system"); + consumer.setType(typeSystem); + + List f = ObjectFactory.get().listObjectsByClass(Product.class); + Product rhel = (Product) ObjectFactory.get().lookupByFieldName( + Product.class, "label", "rhel"); + Product rhelvirt = (Product) ObjectFactory.get().lookupByFieldName( + Product.class, "label", "rhel-virt"); + + EntitlementMatcher m = new EntitlementMatcher(); + + assertTrue(m.isCompatible(consumer, rhel)); + + ConsumerType vmwarehost = + ProductFactory.get().lookupConsumerTypeByLabel("vmwarehost"); + consumer.setType(vmwarehost); + + // Check that you can't use rhelvirt on a vmware host + assertFalse(m.isCompatible(consumer, rhelvirt)); + } +} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/resource/test/EntitlementResourceTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/resource/test/EntitlementResourceTest.java new file mode 100644 index 0000000..a136a29 --- /dev/null +++ b/proxy/src/test/java/org/fedoraproject/candlepin/resource/test/EntitlementResourceTest.java @@ -0,0 +1,207 @@ +/** + * 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.resource.test; + +import static org.junit.Assert.*; + +import java.sql.Date; +import java.util.ArrayList; +import java.util.List; + +import org.fedoraproject.candlepin.model.Consumer; +import org.fedoraproject.candlepin.model.ConsumerCurator; +import org.fedoraproject.candlepin.model.ConsumerType; +import org.fedoraproject.candlepin.model.ConsumerTypeCurator; +import org.fedoraproject.candlepin.model.EntitlementPool; +import org.fedoraproject.candlepin.model.EntitlementPoolCurator; +import org.fedoraproject.candlepin.model.Owner; +import org.fedoraproject.candlepin.model.OwnerCurator; +import org.fedoraproject.candlepin.model.Product; +import org.fedoraproject.candlepin.model.ProductCurator; +import org.fedoraproject.candlepin.resource.EntitlementResource; +import org.fedoraproject.candlepin.test.DatabaseTestFixture; +import org.fedoraproject.candlepin.test.TestUtil; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.config.ClientConfig; +import com.sun.jersey.api.client.config.DefaultClientConfig; + + +/** + * ConsumerResourceTest + */ +public class EntitlementResourceTest extends DatabaseTestFixture { + + private Consumer consumer; + private Product product; + private EntitlementPool ep; + private Owner owner; + private EntitlementResource eapi; + + @Before + public void createTestObjects() { + owner = TestUtil.createOwner(); + ownerCurator.create(owner); + + ConsumerType type = new ConsumerType("some-consumer-type"); + consumerTypeCurator.create(type); + + consumer = TestUtil.createConsumer(type, owner); + consumerCurator.create(consumer); + + product = TestUtil.createProduct(); + productCurator.create(product); + + Date pastDate = new Date(System.currentTimeMillis() - 10000000); + Date futuredate = new Date(System.currentTimeMillis() + 1000000000); + ep = new EntitlementPool(owner, product, new Long(10), pastDate, futuredate); + entitlementPoolCurator.create(ep); + + eapi = new EntitlementResource(entitlementPoolCurator, ownerCurator, consumerCurator, + productCurator); + + } + + @Ignore + public void testEntitle() throws Exception { + +// Form f = new Form(); +// f.add("consumer_id", consumer.getId()); +// f.add("product_id", product.getId()); + String cert = (String) eapi.entitle(consumer.getUuid(), product.getLabel()); + + assertNotNull(cert); + + consumer = consumerCurator.lookupByUuid(consumer.getUuid()); + assertEquals(1, consumer.getConsumedProducts().size()); + assertEquals(product.getId(), consumer.getConsumedProducts().iterator() + .next().getId()); + assertEquals(1, consumer.getEntitlements().size()); + + ep = entitlementPoolCurator.find(ep.getId()); + assertEquals(new Long(1), ep.getCurrentMembers()); + } + + @Test + public void testMaxMembership() { + + // 10 entitlements available, lets try to entitle 11 consumers. + for (int i = 0; i < ep.getMaxMembers(); i++) { + Consumer c = TestUtil.createConsumer(consumer.getType(), owner); + consumerCurator.create(c); + eapi.entitle(c.getUuid(), product.getLabel()); + } + + // Now for the 11th: + try { + Consumer c = TestUtil.createConsumer(consumer.getType(), owner); + eapi.entitle(c.getUuid(), product.getLabel()); + fail(); + } + catch (RuntimeException e) { + // expected + } + + } + + @Test + public void testEntitlementsHaveExpired() { + Product myProduct = TestUtil.createProduct(); + productCurator.create(myProduct); + Date pastDate = new Date(System.currentTimeMillis() - 10000000); + Date notSoPastDate = new Date(System.currentTimeMillis() - 100000); + EntitlementPool anotherPool = new EntitlementPool(owner, myProduct, new Long(10), + pastDate, notSoPastDate); + entitlementPoolCurator.create(anotherPool); + + try { + eapi.entitle(consumer.getUuid(), myProduct.getLabel()); + fail(); + } + catch (RuntimeException e) { + // expected + } + } + + @Test + public void testEntitleOwnerHasNoEntitlements() { + // TODO + } + + @Test + public void testEntitleOwnerHasNoAviailableEntitlements() { + // TODO + } + + @Test + public void testEntitleConsumerAlreadyEntitledForProduct() { + // TODO + } + + @Ignore + public void testHasEntitlement() { + + eapi.entitle(consumer.getUuid(), product.getLabel()); + + // TODO: Disabling this test, boils into ObjectFactory things that need + // to be fixed before we can do this check! Sorry! :) - dgoodwin +// assertTrue(eapi.hasEntitlement(consumer.getUuid(), product.getUuid())); + } + + // TODO: Re-enable once ObjectFactory is Hibernatized or removed. +// @Test +// public void testListAvailableEntitlements() { +// EntitlementResource eapi = new EntitlementResource(); +//// consumer.setType(new ConsumerType("standard-system")); +// Form f = new Form(); +// f.add("consumer_id", consumer.getId()); +// +// List avail = eapi.listAvailableEntitlements(consumer.getId()); +// assertNotNull(avail); +// assertTrue(avail.size() > 0); +// } + + @Test + @Ignore + public void testJson() { + ClientConfig cc = new DefaultClientConfig(); + Client c = Client.create(cc); + + // WebResource getresource = c.resource("http://localhost:8080/candlepin/entitle/"); + + + Object[] params = new Object[2]; + params[0] = consumer; + params[1] = product; + List aparams = new ArrayList(); + aparams.add(consumer); + aparams.add(product); + + WebResource postresource = + c.resource("http://localhost:8080/candlepin/entitlement/foo/"); + postresource.accept("application/json").type("application/json").post(consumer); + + // System.out.println(jto.getName()); + // jto = getresource.accept("application/json").get(JsonTestObject.class); + // assertEquals("testname", jto.getName()); + // assertEquals("AEF", jto.getUuid()); + } + + +} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/resource/test/TestResourceTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/resource/test/TestResourceTest.java new file mode 100644 index 0000000..2a41973 --- /dev/null +++ b/proxy/src/test/java/org/fedoraproject/candlepin/resource/test/TestResourceTest.java @@ -0,0 +1,95 @@ +/** + * 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.resource.test; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; + +import org.fedoraproject.candlepin.model.ConsumerType; +import org.fedoraproject.candlepin.model.JsonTestObject; +import org.fedoraproject.candlepin.resource.TestResource; +import org.junit.Test; +import org.junit.Ignore; + +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.config.ClientConfig; +import com.sun.jersey.api.client.config.DefaultClientConfig; + + +/** + * TestResourceTest + * @version $Rev$ + */ +@Ignore +public class TestResourceTest { + + private JsonTestObject createTestObject() { + JsonTestObject jto = new JsonTestObject(); + jto.setName("testname"); + List l = new ArrayList(); + l.add("hey there"); + l.add("how are you?"); + jto.setStringList(l); + return jto; + } + +// @Test + @Ignore + public void testJson() { + ClientConfig cc = new DefaultClientConfig(); + Client c = Client.create(cc); + + + JsonTestObject jto = createTestObject(); + + WebResource postresource = c.resource("http://localhost:8080/candlepin/test/"); + postresource.accept("application/json").type("application/json").post(jto); + + WebResource getresource = c.resource("http://localhost:8080/candlepin/test/"); + System.out.println(jto.getName()); + jto = getresource.accept("application/json").get(JsonTestObject.class); + assertEquals("testname", jto.getName()); + assertNotNull(jto.getStringList()); + assertEquals(2, jto.getStringList().size()); + assertNull(jto.getParent()); + System.out.println(jto.getStringList()); + } + + // @Test + @Ignore + public void testGet() { + TestResource tr = new TestResource(); + assertNull(tr.get()); + + JsonTestObject jto = createTestObject(); + tr.create(jto); + assertEquals(jto, tr.get()); + } + + // @Test + public void testConsumerType() { + ClientConfig cc = new DefaultClientConfig(); + Client c = Client.create(cc); + + WebResource getresource = + c.resource("http://localhost:8080/candlepin/test/consumertype"); + ConsumerType ct = getresource.accept("application/json").get(ConsumerType.class); + assertNotNull(ct); + assertEquals("testtype", ct.getLabel()); + } +} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/resource/test/UserResourceTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/resource/test/UserResourceTest.java new file mode 100644 index 0000000..94cf31d --- /dev/null +++ b/proxy/src/test/java/org/fedoraproject/candlepin/resource/test/UserResourceTest.java @@ -0,0 +1,77 @@ +/** + * 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.resource.test; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.fedoraproject.candlepin.model.User; +import org.fedoraproject.candlepin.resource.UserResource; +import org.junit.Test; + + +/** + * UserResourceTest + * @version $Rev$ + */ +public class UserResourceTest { + + private UserResource api = new UserResource(); + + @Test + public void testNewUser() { + User user = api.create("candlepin", "cp_p@$sw0rd"); + assertNotNull(user); + assertEquals("candlepin", user.getLogin()); + assertEquals("cp_p@$sw0rd", user.getPassword()); + + user = api.create(null, null); + assertNotNull(user); + assertEquals(null, user.getLogin()); + assertEquals(null, user.getPassword()); + + user = api.create("", ""); + assertNotNull(user); + assertEquals("", user.getLogin()); + assertEquals("", user.getPassword()); + } + + @Test + public void testList() { + List users = api.list(); + int origSize = users.size(); + // create 1 + api.create("candlepin", "cp_p@$sw0rd"); + + // create 2 + api.create("jesusr", "n0P@$sw0rD"); + + // get the list back + users = api.list(); + System.out.println("Users: " + users.toString()); + assertNotNull(users); + assertEquals(origSize + 2, users.size()); + assertEquals(User.class, users.get(0).getClass()); + } + + @Test + public void testGet() { + User user = api.get("test-login"); + assertNotNull(user); + assertEquals("test-login", user.getLogin()); + } + +} diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/resource/test/cert/test/CertTest.java b/proxy/src/test/java/org/fedoraproject/candlepin/resource/test/cert/test/CertTest.java new file mode 100644 index 0000000..fd72bf9 --- /dev/null +++ b/proxy/src/test/java/org/fedoraproject/candlepin/resource/test/cert/test/CertTest.java @@ -0,0 +1,284 @@ +/** + * 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.resource.test.cert.test; + +import java.math.BigInteger; +import java.security.KeyFactory; +import java.security.KeyStore; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.Security; +import java.security.cert.X509Certificate; +import java.security.spec.RSAPrivateCrtKeySpec; +import java.security.spec.RSAPublicKeySpec; +import java.util.Date; + +import org.bouncycastle.asn1.ASN1EncodableVector; +import org.bouncycastle.asn1.DERSequence; +import org.bouncycastle.asn1.x509.GeneralName; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.x509.AttributeCertificateHolder; +import org.bouncycastle.x509.AttributeCertificateIssuer; +import org.bouncycastle.x509.X509Attribute; +import org.bouncycastle.x509.X509V2AttributeCertificate; +import org.bouncycastle.x509.X509V2AttributeCertificateGenerator; +import org.bouncycastle.x509.examples.AttrCertExample; +import org.fedoraproject.candlepin.resource.cert.CertGenerator; +import org.junit.Test; + +/** + * CertTest + * @version $Rev$ + */ +public class CertTest { + + @Test + public void testCertGenerator() { + String cert = CertGenerator.getCertString(); +// System.out.println("Cert: " + cert); + } + + @Test + public void testCertExample() throws Exception { + + Security.addProvider(new BouncyCastleProvider()); + + // + // personal keys + // + RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec( + new BigInteger("b4a7e46170574f16a97082b22be58b6a2a629798419" + + "be12872a4bdba626cfae9900f76abfb12139dce5de5" + + "6564fab2b6543165a040c606887420e33d91ed7ed7", 16), + new BigInteger("11", 16)); + + RSAPrivateCrtKeySpec privKeySpec = new RSAPrivateCrtKeySpec( + new BigInteger("b4a7e46170574f16a97082b22be58b6a2a629798419" + + "be12872a4bdba626cfae9900f76abfb12139dce5de5" + + "6564fab2b6543165a040c606887420e33d91ed7ed7", 16), + new BigInteger("11", 16), + new BigInteger("9f66f6b05410cd503b2709e88115d55daced94d1a34" + + "d4e32bf824d0dde6028ae79c5f07b580f5dce240d71" + + "11f7ddb130a7945cd7d957d1920994da389f490c89", 16), + new BigInteger("c0a0758cdf14256f78d4708c86becdead1b50ad4ad6" + + "c5c703e2168fbf37884cb", 16), + new BigInteger("f01734d7960ea60070f1b06f2bb81bfac48ff192ae1" + + "8451d5e56c734a5aab8a5", 16), + new BigInteger("b54bb9edff22051d9ee60f9351a48591b6500a31942" + + "9c069a3e335a1d6171391", 16), + new BigInteger("d3d83daf2a0cecd3367ae6f8ae1aeb82e9ac2f816c6" + + "fc483533d8297dd7884cd", 16), + new BigInteger("b8f52fc6f38593dabb661d3f50f8897f8106eee68b1" + + "bce78a95b132b4e5b5d19", 16)); + + // + // ca keys + // + RSAPublicKeySpec caPubKeySpec = new RSAPublicKeySpec( + new BigInteger("b259d2d6e627a768c94be36164c2d9fc79d97aab925" + + "3140e5bf17751197731d6f7540d2509e7b9ffee0a70" + + "a6e26d56e92d2edd7f85aba85600b69089f35f6bdbf" + + "3c298e05842535d9f064e6b0391cb7d306e0a2d20c4" + + "dfb4e7b49a9640bdea26c10ad69c3f05007ce2513ce" + + "e44cfe01998e62b6c3637d3fc0391079b26ee36d5", 16), + new BigInteger("11", 16)); + + RSAPrivateCrtKeySpec caPrivKeySpec = new RSAPrivateCrtKeySpec( + new BigInteger("b259d2d6e627a768c94be36164c2d9fc79d97aab925" + + "3140e5bf17751197731d6f7540d2509e7b9ffee0a70" + + "a6e26d56e92d2edd7f85aba85600b69089f35f6bdbf" + + "3c298e05842535d9f064e6b0391cb7d306e0a2d20c4" + + "dfb4e7b49a9640bdea26c10ad69c3f05007ce2513ce" + + "e44cfe01998e62b6c3637d3fc0391079b26ee36d5", 16), + new BigInteger("11", 16), + new BigInteger("92e08f83cc9920746989ca5034dcb384a094fb9c5a6" + + "288fcc4304424ab8f56388f72652d8fafc65a4b9020" + + "896f2cde297080f2a540e7b7ce5af0b3446e1258d1d" + + "d7f245cf54124b4c6e17da21b90a0ebd22605e6f45c" + + "9f136d7a13eaac1c0f7487de8bd6d924972408ebb58" + + "af71e76fd7b012a8d0e165f3ae2e5077a8648e619", 16), + new BigInteger("f75e80839b9b9379f1cf1128f321639757dba514642" + + "c206bbbd99f9a4846208b3e93fbbe5e0527cc59b1d4" + + "b929d9555853004c7c8b30ee6a213c3d1bb7415d03", 16), + new BigInteger("b892d9ebdbfc37e397256dd8a5d3123534d1f037262" + + "84743ddc6be3a709edb696fc40c7d902ed804c6eee7" + + "30eee3d5b20bf6bd8d87a296813c87d3b3cc9d7947", 16), + new BigInteger("1d1a2d3ca8e52068b3094d501c9a842fec37f54db16" + + "e9a67070a8b3f53cc03d4257ad252a1a640eadd6037" + + "24d7bf3737914b544ae332eedf4f34436cac25ceb5", 16), + new BigInteger("6c929e4e81672fef49d9c825163fec97c4b7ba7acb2" + + "6c0824638ac22605d7201c94625770984f78a56e6e2" + + "5904fe7db407099cad9b14588841b94f5ab498dded", 16), + new BigInteger("dae7651ee69ad1d081ec5e7188ae126f6004ff39556" + + "bde90e0b870962fa7b926d070686d8244fe5a9aa709" + + "a95686a104614834b0ada4b10f53197a5cb4c97339", 16)); + + // + // set up the keys + // + KeyFactory fact = KeyFactory.getInstance("RSA", "BC"); + PrivateKey caPrivKey = fact.generatePrivate(caPrivKeySpec); + PublicKey caPubKey = fact.generatePublic(caPubKeySpec); + PrivateKey privKey = fact.generatePrivate(privKeySpec); + PublicKey pubKey = fact.generatePublic(pubKeySpec); + + // + // note in this case we are using the CA certificate for both the client + // cetificate + // and the attribute certificate. This is to make the vcode simpler to + // read, in practice + // the CA for the attribute certificate should be different to that of + // the client certificate + // + X509Certificate caCert = AttrCertExample.createAcIssuerCert(caPubKey, + caPrivKey); + X509Certificate clientCert = AttrCertExample.createClientCert(pubKey, + caPrivKey, caPubKey); +// System.out.println("CaCert: " + caCert); +// System.out.println("clientCert: " + clientCert); + // Instantiate a new AC generator + X509V2AttributeCertificateGenerator acGen = + new X509V2AttributeCertificateGenerator(); + + acGen.reset(); + + // + // Holder: here we use the IssuerSerial form + // + acGen.setHolder(new AttributeCertificateHolder(clientCert)); + + // set the Issuer + acGen.setIssuer(new AttributeCertificateIssuer(caCert + .getSubjectX500Principal())); + + // + // serial number (as it's an example we don't have to keep track of the + // serials anyway + // + acGen.setSerialNumber(new BigInteger("1")); + + // not Before + acGen.setNotBefore(new Date(System.currentTimeMillis() - 50000)); + + // not After + acGen.setNotAfter(new Date(System.currentTimeMillis() + 50000)); + + // signature Algorithmus + acGen.setSignatureAlgorithm("SHA1WithRSAEncryption"); + + // the actual attributes + GeneralName roleName = new GeneralName(GeneralName.rfc822Name, + "DAU123456789"); + ASN1EncodableVector roleSyntax = new ASN1EncodableVector(); + roleSyntax.add(roleName); + + // roleSyntax OID: 2.5.24.72 + X509Attribute attributes = new X509Attribute("2.5.24.72", + new DERSequence(roleSyntax)); + + acGen.addAttribute(attributes); + + // finally create the AC + X509V2AttributeCertificate att = (X509V2AttributeCertificate) acGen + .generate(caPrivKey, "BC"); + + + + String encoded = new String(att.getEncoded()); + //System.out.println("CERT CERT: " + encoded); + KeyStore store = KeyStore.getInstance("PKCS12"); + String pass = "redhat"; + + + /*FileOutputStream fout = new FileOutputStream("/tmp/foo.file"); + store.load(null, null); + store.store(fout, pass.toCharArray()); + X509CertificateObject ccert = new + X509CertificateObject(new X509CertificateStructure(new DERSequence(att)));*/ + // + // starting here, we parse the newly generated AC + // + + // Holder + + AttributeCertificateHolder h = att.getHolder(); + if (h.match(clientCert)) { + if (h.getEntityNames() != null) { +// System.out.println(h.getEntityNames().length + +// " entity names found"); + } + if (h.getIssuer() != null) { +// System.out.println(h.getIssuer().length + +// " issuer names found, serial number " + +// h.getSerialNumber()); + } +// System.out.println("Matches original client x509 cert"); + } + + // Issuer + + AttributeCertificateIssuer issuer = att.getIssuer(); + if (issuer.match(caCert)) { + if (issuer.getPrincipals() != null) { +// System.out.println(issuer.getPrincipals().length + +// " entity names found"); + } +// System.out.println("Matches original ca x509 cert"); + } + + // Dates +// System.out.println("valid not before: " + att.getNotBefore()); +// System.out.println("valid not before: " + att.getNotAfter()); + + // check the dates, an exception is thrown in checkValidity()... + + try { + att.checkValidity(); + att.checkValidity(new Date()); + } + catch (Exception e) { + System.out.println(e); + } + + // verify + + try { + att.verify(caPubKey, "BC"); + } + catch (Exception e) { + System.out.println(e); + } + + // Attribute + X509Attribute[] attribs = att.getAttributes(); +// System.out.println("cert has " + attribs.length + " attributes:"); + for (int i = 0; i < attribs.length; i++) { + X509Attribute a = attribs[i]; +// System.out.println("OID: " + a.getOID()); + + // currently we only check for the presence of a 'RoleSyntax' + // attribute + + if (a.getOID().equals("2.5.24.72")) { +// System.out.println("rolesyntax read from cert!"); + } + } + + + + + // CertificateFactory.getInstance + } +} -- cgit