2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
5 package org.collectionspace.services.account.client.test;
7 import java.lang.reflect.Method;
8 import java.util.ArrayList;
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;
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;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.testng.Assert;
29 import org.testng.annotations.DataProvider;
35 public class AccountTest {
37 private final Logger logger = LoggerFactory.getLogger(AccountTest.class);
38 private EntityManagerFactory emf;
39 private EntityManager em;
45 emf = Persistence.createEntityManagerFactory("org.collectionspace.services.account");
47 em = emf.createEntityManager();
48 // if (logger.isDebugEnabled()) {
49 // logger.debug("created entity manager");
54 public void cleanup() {
60 @SuppressWarnings("unchecked")
61 @Test(dataProvider = "testName", dataProviderClass = AccountTest.class)
62 public void createTest(String testName) throws Exception {
63 AccountsCommon account = null;
65 account = findAccount("test");
66 if (account != null) {
69 } catch (NoResultException nre) {
72 if (account == null) {
73 account = new AccountsCommon();
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();
83 Tenant tenant = new Tenant();
85 tenant.setName("movingimages.us");
86 List<Tenant> lt = new ArrayList<Tenant>();
88 account.setTenant(lt);
89 em.getTransaction().begin();
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());
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();
110 account.setCreatedAtItem(new Date());
111 Tenant tenant = new Tenant();
113 tenant.setName("movingimages.us.standalone");
114 List<Tenant> lt = new ArrayList<Tenant>();
116 account.setTenant(lt);
117 em.getTransaction().begin();
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());
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());
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();
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());
167 @SuppressWarnings("unchecked")
168 @Test(dataProvider = "testName", dataProviderClass = AccountTest.class,
169 dependsOnMethods = {"update"})
170 public void delete(String testName) throws Exception {
172 em.getTransaction().begin();
173 AccountsCommon account = findAccount("john");
175 if (logger.isDebugEnabled()) {
176 logger.debug("deleting account "
179 // Commit the transaction
180 em.getTransaction().commit();
181 if (logger.isDebugEnabled()) {
182 logger.debug("deleted account "
188 * Returns the name of the currently running test.
190 * Note: although the return type is listed as Object[][],
191 * this method instead returns a String.
193 * @param m The currently running test method.
195 * @return The name of the currently running test method.
197 @DataProvider(name = "testName")
198 public static Object[][] testName(Method m) {
199 return new Object[][]{
200 new Object[]{m.getName()}