]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
68da82ac2b2ee09bf027c577c3698f781764ec5b
[tmp/jakarta-migration.git] /
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5 package org.collectionspace.services.account.client.test;
6
7 import java.lang.reflect.Method;
8 import java.util.ArrayList;
9 import java.util.Date;
10 import java.util.List;
11 import java.util.UUID;
12 import javax.persistence.EntityManager;
13 import javax.persistence.EntityManagerFactory;
14 import javax.persistence.NoResultException;
15 import javax.persistence.Persistence;
16
17 import javax.persistence.Query;
18 import org.collectionspace.services.account.AccountsCommon;
19 import org.collectionspace.services.account.AccountsCommon.Tenant;
20 import org.collectionspace.services.account.Status;
21 import org.testng.annotations.AfterMethod;
22 import org.testng.annotations.BeforeMethod;
23 import org.testng.annotations.Test;
24
25
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.testng.Assert;
29 import org.testng.annotations.DataProvider;
30
31 /**
32  *
33  * @author 
34  */
35 public class AccountTest {
36
37     private final Logger logger = LoggerFactory.getLogger(AccountTest.class);
38     private EntityManagerFactory emf;
39     private EntityManager em;
40     private String id;
41
42     @BeforeMethod
43     public void init() {
44
45         emf = Persistence.createEntityManagerFactory("org.collectionspace.services.account");
46
47         em = emf.createEntityManager();
48 //        if (logger.isDebugEnabled()) {
49 //            logger.debug("created entity manager");
50 //        }
51     }
52
53     @AfterMethod
54     public void cleanup() {
55         if (em != null) {
56             em.close();
57         }
58     }
59
60     @SuppressWarnings("unchecked")
61     @Test(dataProvider = "testName", dataProviderClass = AccountTest.class)
62     public void createTest(String testName) throws Exception {
63         AccountsCommon account = null;
64         try {
65             account = findAccount("test");
66             if (account != null) {
67                 return;
68             }
69         } catch (NoResultException nre) {
70             //ignore
71         }
72         if (account == null) {
73             account = new AccountsCommon();
74         }
75         account.setScreenName("test");
76         account.setPersonRefName("test hello");
77         account.setEmail("test.test@berkeley.edu");
78         account.setUserId("test");
79         account.setStatus(Status.ACTIVE);
80         id = UUID.randomUUID().toString();
81         account.setCsid(id);
82
83         Tenant tenant = new Tenant();
84         tenant.setId("1");
85         tenant.setName("movingimages.us");
86         List<Tenant> lt = new ArrayList<Tenant>();
87         lt.add(tenant);
88         account.setTenant(lt);
89         em.getTransaction().begin();
90         em.persist(account);
91         // Commit the transaction
92         em.getTransaction().commit();
93         if (logger.isDebugEnabled()) {
94             logger.debug("created/updated account "
95                     + " screen name=" + account.getScreenName()
96                     + " email=" + account.getEmail());
97         }
98     }
99
100     @SuppressWarnings("unchecked")
101     @Test(dataProvider = "testName", dataProviderClass = AccountTest.class)
102     public void create(String testName) throws Exception {
103         AccountsCommon account = new AccountsCommon();
104         account.setScreenName("john");
105         account.setEmail("john.doe@berkeley.edu");
106         account.setUserId("johndoe");
107         account.setStatus(Status.ACTIVE);
108         id = UUID.randomUUID().toString();
109         account.setCsid(id);
110         account.setCreatedAtItem(new Date());
111         Tenant tenant = new Tenant();
112         tenant.setId("123");
113         tenant.setName("movingimages.us.standalone");
114         List<Tenant> lt = new ArrayList<Tenant>();
115         lt.add(tenant);
116         account.setTenant(lt);
117         em.getTransaction().begin();
118         em.persist(account);
119         // Commit the transaction
120         em.getTransaction().commit();
121         if (logger.isDebugEnabled()) {
122             logger.debug("created account "
123                     + " screen name=" + account.getScreenName()
124                     + " email=" + account.getEmail());
125         }
126     }
127
128     @SuppressWarnings("unchecked")
129     @Test(dataProvider = "testName", dataProviderClass = AccountTest.class,
130     dependsOnMethods = {"create"})
131     public void read(String testName) throws Exception {
132         AccountsCommon account = findAccount("john");
133         Assert.assertNotNull(account);
134         if (logger.isDebugEnabled()) {
135             logger.debug("read account "
136                     + " screen name=" + account.getScreenName());
137         }
138     }
139
140     private AccountsCommon findAccount(String screenName) throws Exception {
141         Query q = em.createQuery("select a from org.collectionspace.services.account.AccountsCommon a where a.screenName = :screenname");
142         q.setParameter("screenname", screenName);
143         return (AccountsCommon) q.getSingleResult();
144
145     }
146
147     @SuppressWarnings("unchecked")
148     @Test(dataProvider = "testName", dataProviderClass = AccountTest.class,
149     dependsOnMethods = {"read"})
150     public void update(String testName) throws Exception {
151         Query q = em.createQuery("update org.collectionspace.services.account.AccountsCommon set email= :email where csid=:csid");
152         q.setParameter("email", "john@berkeley.edu");
153         q.setParameter("csid", id);
154         em.getTransaction().begin();
155         int no = q.executeUpdate();
156         // Commit the transaction
157         em.getTransaction().commit();
158         Assert.assertEquals(no, 1);
159         AccountsCommon account = findAccount("john");
160         if (logger.isDebugEnabled()) {
161             logger.debug("updated account "
162                     + " screen name=" + account.getScreenName()
163                     + " email=" + account.getEmail());
164         }
165     }
166
167     @SuppressWarnings("unchecked")
168     @Test(dataProvider = "testName", dataProviderClass = AccountTest.class,
169     dependsOnMethods = {"update"})
170     public void delete(String testName) throws Exception {
171         // Begin transaction
172         em.getTransaction().begin();
173         AccountsCommon account = findAccount("john");
174         em.remove(account);
175         if (logger.isDebugEnabled()) {
176             logger.debug("deleting account "
177                     + " csid=" + id);
178         }
179         // Commit the transaction
180         em.getTransaction().commit();
181         if (logger.isDebugEnabled()) {
182             logger.debug("deleted account "
183                     + " csid=" + id);
184         }
185     }
186
187     /**
188      * Returns the name of the currently running test.
189      *
190      * Note: although the return type is listed as Object[][],
191      * this method instead returns a String.
192      *
193      * @param   m  The currently running test method.
194      *
195      * @return  The name of the currently running test method.
196      */
197     @DataProvider(name = "testName")
198     public static Object[][] testName(Method m) {
199         return new Object[][]{
200                     new Object[]{m.getName()}
201                 };
202     }
203 }