summaryrefslogtreecommitdiffstats
path: root/proxy/src/test/java/org/fedoraproject/candlepin/test
diff options
context:
space:
mode:
Diffstat (limited to 'proxy/src/test/java/org/fedoraproject/candlepin/test')
-rw-r--r--proxy/src/test/java/org/fedoraproject/candlepin/test/DatabaseTestFixture.java187
-rw-r--r--proxy/src/test/java/org/fedoraproject/candlepin/test/TestUtil.java126
2 files changed, 313 insertions, 0 deletions
diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/test/DatabaseTestFixture.java b/proxy/src/test/java/org/fedoraproject/candlepin/test/DatabaseTestFixture.java
new file mode 100644
index 0000000..a373489
--- /dev/null
+++ b/proxy/src/test/java/org/fedoraproject/candlepin/test/DatabaseTestFixture.java
@@ -0,0 +1,187 @@
+package org.fedoraproject.candlepin.test;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+
+import org.fedoraproject.candlepin.model.Certificate;
+import org.fedoraproject.candlepin.model.CertificateCurator;
+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.Entitlement;
+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.model.User;
+import org.fedoraproject.candlepin.resource.test.CandlepinTestingModule;
+import org.junit.Before;
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.wideplay.warp.persist.PersistenceService;
+import com.wideplay.warp.persist.UnitOfWork;
+
+/**
+ * Test fixture for test classes requiring access to the database.
+ */
+public class DatabaseTestFixture {
+
+ protected EntityManagerFactory emf;
+ protected Injector injector;
+
+ protected OwnerCurator ownerCurator;
+ protected ProductCurator productCurator;
+ protected ConsumerCurator consumerCurator;
+ protected ConsumerTypeCurator consumerTypeCurator;
+ protected CertificateCurator certificateCurator;
+ protected EntitlementPoolCurator entitlementPoolCurator;
+
+
+ @Before
+ public void setUp() {
+ injector = Guice.createInjector(
+ new CandlepinTestingModule(),
+ PersistenceService.usingJpa()
+ .across(UnitOfWork.TRANSACTION)
+ .buildModule()
+ );
+
+ injector.getInstance(EntityManagerFactory.class);
+ emf = injector.getProvider(EntityManagerFactory.class).get();
+
+ ownerCurator = injector.getInstance(OwnerCurator.class);
+ productCurator = injector.getInstance(ProductCurator.class);
+ consumerCurator = injector.getInstance(ConsumerCurator.class);
+ consumerTypeCurator = injector.getInstance(ConsumerTypeCurator.class);
+ certificateCurator = injector.getInstance(CertificateCurator.class);
+ entitlementPoolCurator = injector.getInstance(EntitlementPoolCurator.class);
+ }
+
+ /*
+ * As long as we are using in-memory db we don't need to clean it out;
+ * a new instance will be created for each test. cleanDb() is *not* being currently invoked.
+ *
+ * Cleans out everything in the database. Currently we test against an
+ * in-memory hsqldb, so this should be ok.
+ *
+ * WARNING: Don't flip the persistence unit to point to an actual live
+ * DB or you'll lose everything.
+ *
+ * WARNING: If you're creating objects in tables that have static
+ * pre-populated content, we may not want to wipe those tables here.
+ * This situation hasn't arisen yet.
+ */
+ @SuppressWarnings("unchecked")
+ public void cleanDb() {
+ EntityManager em = entityManager();
+ if (!em.getTransaction().isActive()) {
+ beginTransaction();
+ }
+
+ List<User> users = em.createQuery("from User u").getResultList();
+ for (User u : users) {
+ em.remove(u);
+ }
+
+ List<Entitlement> ents = em.createQuery("from Entitlement e").
+ getResultList();
+ for (Entitlement e : ents) {
+ em.remove(e);
+ }
+
+ List<EntitlementPool> pools = em.createQuery("from EntitlementPool p").getResultList();
+ for (EntitlementPool p : pools) {
+ em.remove(p);
+ }
+
+ // TODO: Would rather be doing this, but such a bulk delete does not seem to respect
+ // the cascade to child products and thus fails.
+ // em.createQuery("delete from Product").executeUpdate();
+ List<Product> products = em.createQuery("from Product p").getResultList();
+ for (Product p : products) {
+ em.remove(p);
+ }
+
+ List<Consumer> consumers = em.createQuery("from Consumer c").getResultList();
+ for (Consumer c : consumers) {
+ em.remove(c);
+ }
+
+ List<Certificate> certificates = em.createQuery("from Certificate c").getResultList();
+ for (Certificate c : certificates){
+ em.remove(c);
+ }
+
+ List<Owner> owners = em.createQuery("from Owner o").getResultList();
+ for (Owner o : owners) {
+ em.remove(o);
+ }
+
+// List<ConsumerInfo> consumerInfos = em.createQuery("from ConsumerInfo c").getResultList();
+// for (ConsumerInfo c : consumerInfos) {
+// em.remove(c);
+// }
+
+ // TODO: Is this right? Or should we pre-populate default defined types, and always
+ // reference these in the tests instead of creating them everywhere?
+ List<ConsumerType> consumerTypes = em.createQuery("from ConsumerType c").
+ getResultList();
+ for (ConsumerType c : consumerTypes) {
+ em.remove(c);
+ }
+
+
+
+ commitTransaction();
+ em.close();
+ }
+
+ protected EntityManager entityManager() {
+ return injector.getProvider(EntityManager.class).get();
+ }
+
+ /**
+ * Begin a transaction, persist the given entity, and commit.
+ *
+ * @param storeMe Entity to persist.
+ */
+ protected void persistAndCommit(Object storeMe) {
+ EntityTransaction tx = null;
+
+ try {
+ tx = entityManager().getTransaction();
+ tx.begin();
+ entityManager().persist(storeMe);
+ tx.commit();
+ }
+ catch (RuntimeException e) {
+ if (tx != null && tx.isActive()) {
+ tx.rollback();
+ }
+ throw e; // or display error message
+ }
+ }
+
+ /**
+ * Helper to open a new db transaction. Pretty simple for now, but may
+ * require additional logic and error handling down the road.
+ */
+ protected void beginTransaction() {
+ entityManager().getTransaction().begin();
+ }
+
+ /**
+ * Helper to commit the current db transaction. Pretty simple for now, but may
+ * require additional logic and error handling down the road.
+ */
+ protected void commitTransaction() {
+ entityManager().getTransaction().commit();
+ }
+}
diff --git a/proxy/src/test/java/org/fedoraproject/candlepin/test/TestUtil.java b/proxy/src/test/java/org/fedoraproject/candlepin/test/TestUtil.java
new file mode 100644
index 0000000..21b005e
--- /dev/null
+++ b/proxy/src/test/java/org/fedoraproject/candlepin/test/TestUtil.java
@@ -0,0 +1,126 @@
+/**
+ * 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.test;
+
+import java.sql.Date;
+import java.util.Calendar;
+import java.util.Random;
+
+import org.apache.commons.codec.binary.Base64;
+import org.fedoraproject.candlepin.model.Consumer;
+import org.fedoraproject.candlepin.model.ConsumerType;
+import org.fedoraproject.candlepin.model.Entitlement;
+import org.fedoraproject.candlepin.model.EntitlementPool;
+import org.fedoraproject.candlepin.model.ObjectFactory;
+import org.fedoraproject.candlepin.model.Owner;
+import org.fedoraproject.candlepin.model.Product;
+import org.fedoraproject.candlepin.model.User;
+
+/**
+ * TestUtil for creating various testing objects.
+ *
+ * Objects backed by the database are not persisted, the caller is expected to persist
+ * the entities returned and any dependent objects.
+ */
+public class TestUtil {
+
+ private TestUtil() {
+ }
+
+ public static Owner createOwner() {
+ Owner o = new Owner("Test Owner " + randomInt());
+// o.setUuid(lookedUp);
+ ObjectFactory.get().store(o);
+ return o;
+ }
+
+ public static Consumer createConsumer(ConsumerType type, Owner owner) {
+ Consumer c = new Consumer("Test Consumer " + randomInt(), owner, type);
+ ObjectFactory.get().store(c);
+ return c;
+ }
+
+ /**
+ * Create a consumer with a new owner
+ * @return Consumer
+ */
+ public static Consumer createConsumer() {
+ return createConsumer(createConsumerType(), createOwner());
+ }
+
+ public static ConsumerType createConsumerType() {
+ return new ConsumerType("test-consumer-type-" + randomInt());
+ }
+
+ public static int randomInt() {
+ return new Random().nextInt(10000);
+ }
+
+ public static Product createProduct() {
+ int random = randomInt();
+ Product rhel = new Product("test-product-" + random,
+ "Test Product " + random);
+ ObjectFactory.get().store(rhel);
+ return rhel;
+ }
+
+ public static EntitlementPool createEntitlementPool() {
+ EntitlementPool pool = new EntitlementPool(createOwner(), createProduct(),
+ new Long(1000),
+ TestUtil.createDate(2009, 11, 30), TestUtil.createDate(2015, 11, 30));
+ return pool;
+ }
+
+ public static Entitlement createEntitlement(EntitlementPool pool) {
+ Entitlement e = new Entitlement(pool, pool.getOwner(), pool.getStartDate());
+ return e;
+ }
+
+ public static User createUser(Owner owner) {
+ User u = new User(owner, "testuser" + randomInt(), "sekret");
+ return u;
+ }
+
+ public static Date createDate(int year, int month, int day) {
+ Calendar cal = Calendar.getInstance();
+
+ cal.set(Calendar.YEAR, year);
+ cal.set(Calendar.MONTH, month);
+ cal.set(Calendar.DATE, day);
+
+ cal.set(Calendar.HOUR_OF_DAY, 0);
+ cal.set(Calendar.MINUTE, 0);
+ cal.set(Calendar.SECOND, 0);
+ cal.set(Calendar.MILLISECOND, 0);
+
+ Date jsqlD = new Date(cal.getTime().getTime());
+ return jsqlD;
+ }
+
+ public static String xmlToBase64String(String xml) {
+
+// byte[] bytes = Base64.encode(xml);
+ Base64 encoder = new Base64();
+ byte [] bytes = encoder.encode(xml.getBytes());
+
+ StringBuffer buf = new StringBuffer();
+ for (byte b : bytes) {
+ buf.append((char) Integer.parseInt(Integer.toHexString(b), 16));
+ }
+
+ return buf.toString();
+ }
+
+}