summaryrefslogtreecommitdiffstats
path: root/proxy/code/src/org/fedoraproject/candlepin/model/test/ProductTest.java
blob: f1799aacfec5e3cb53ce30c1665a1a6690049687 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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.util.EntityManagerUtil;
import org.junit.Test;

public class ProductTest extends ModelTestFixture {
    
    
    @Test
    public void normalCreate() { 
    
        Product prod = new Product("cptest-label", "My Product");
        persistAndCommit(prod);
        
        List<Product> results = em.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() {
        em.getTransaction().begin();
        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);
        em.persist(parent);
        em.getTransaction().commit();
        
        EntityManager em2 = EntityManagerUtil.createEntityManager();
        Product result = (Product)em2.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() {
        em.getTransaction().begin();
        
        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
        
        em.persist(child1);
        em.persist(parent1);
        em.persist(parent2);
        em.getTransaction().commit();
    }
    
    @Test
    public void testCascading() {
        em.getTransaction().begin();
        
        Product parent1 = new Product("parent-product1", "Parent Product 1");
        Product child1 = new Product("child-product1", "Child Product 1");
        parent1.addChildProduct(child1);
        em.persist(parent1);
        em.getTransaction().commit();
        
        EntityManager em2 = EntityManagerUtil.createEntityManager();
        Product result = (Product)em2.createQuery(
                "select p from Product as p where name = :name")
                .setParameter("name", child1.getName())
                .getSingleResult();
        assertNotNull(result);
        
        em.getTransaction().begin();
        em.remove(parent1);
        em.getTransaction().commit();

        em2 = EntityManagerUtil.createEntityManager();
        List<Product> results = em2.createQuery(
                "select p from Product as p where name = :name")
                .setParameter("name", child1.getName())
                .getResultList();
        assertEquals(0, results.size());
    }

}