]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
1f6ff42027f2469c7420789c18df51b5aafd7eed
[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
62     @SuppressWarnings("unchecked")
63     @Test(dataProvider = "testName", dataProviderClass = AccountTest.class)
64     public void create(String testName) throws Exception {
65         AccountsCommon account = new AccountsCommon();
66         account.setScreenName("john");
67         account.setEmail("john.doe@berkeley.edu");
68         account.setUserId("johndoe");
69         account.setStatus(Status.ACTIVE);
70         id = UUID.randomUUID().toString();
71         account.setCsid(id);
72         account.setCreatedAtItem(new Date());
73         AccountTenant tenant = new AccountTenant();
74         tenant.setTenantId("123");
75         List<AccountTenant> tList = new ArrayList<AccountTenant>();
76         tList.add(tenant);
77         account.setTenants(tList);
78         em.getTransaction().begin();
79         em.persist(account);
80         // Commit the transaction
81         em.getTransaction().commit();
82         if (logger.isDebugEnabled()) {
83             logger.debug("created account "
84                     + " screen name=" + account.getScreenName()
85                     + " email=" + account.getEmail());
86         }
87     }
88
89     @SuppressWarnings("unchecked")
90     @Test(dataProvider = "testName", dataProviderClass = AccountTest.class,
91     dependsOnMethods = {"create"})
92     public void read(String testName) throws Exception {
93         AccountsCommon account = findAccount("john");
94         Assert.assertNotNull(account);
95         if (logger.isDebugEnabled()) {
96             logger.debug("read account "
97                     + " screen name=" + account.getScreenName());
98         }
99     }
100
101     private AccountsCommon findAccount(String screenName) throws Exception {
102         Query q = em.createQuery("select a from org.collectionspace.services.account.AccountsCommon a where a.screenName = :screenname");
103         q.setParameter("screenname", screenName);
104         return (AccountsCommon) q.getSingleResult();
105
106     }
107
108     @SuppressWarnings("unchecked")
109     @Test(dataProvider = "testName", dataProviderClass = AccountTest.class,
110     dependsOnMethods = {"read"})
111     public void update(String testName) throws Exception {
112         Query q = em.createQuery("update org.collectionspace.services.account.AccountsCommon set email= :email where csid=:csid");
113         q.setParameter("email", "john@berkeley.edu");
114         q.setParameter("csid", id);
115         em.getTransaction().begin();
116         int no = q.executeUpdate();
117         // Commit the transaction
118         em.getTransaction().commit();
119         Assert.assertEquals(no, 1);
120         AccountsCommon account = findAccount("john");
121         if (logger.isDebugEnabled()) {
122             logger.debug("updated 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 = {"update"})
131     public void delete(String testName) throws Exception {
132         // Begin transaction
133         em.getTransaction().begin();
134         AccountsCommon account = findAccount("john");
135         em.remove(account);
136         if (logger.isDebugEnabled()) {
137             logger.debug("deleting account "
138                     + " csid=" + id);
139         }
140         // Commit the transaction
141         em.getTransaction().commit();
142         if (logger.isDebugEnabled()) {
143             logger.debug("deleted account "
144                     + " csid=" + id);
145         }
146     }
147
148     /**
149      * Returns the name of the currently running test.
150      *
151      * Note: although the return type is listed as Object[][],
152      * this method instead returns a String.
153      *
154      * @param   m  The currently running test method.
155      *
156      * @return  The name of the currently running test method.
157      */
158     @DataProvider(name = "testName")
159     public static Object[][] testName(Method m) {
160         return new Object[][]{
161                     new Object[]{m.getName()}
162                 };
163     }
164 }