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