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.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;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.testng.Assert;
30 import org.testng.annotations.DataProvider;
36 public class AccountTest {
38 private final Logger logger = LoggerFactory.getLogger(AccountTest.class);
39 private EntityManagerFactory emf;
40 private EntityManager em;
46 emf = Persistence.createEntityManagerFactory("org.collectionspace.services.account");
48 em = emf.createEntityManager();
49 // if (logger.isDebugEnabled()) {
50 // logger.debug("created entity manager");
55 public void cleanup() {
61 @SuppressWarnings("unchecked")
62 @Test(dataProvider = "testName", dataProviderClass = AccountTest.class)
63 public void createTest(String testName) throws Exception {
64 AccountsCommon account = null;
66 account = findAccount("test");
67 if (account != null) {
70 } catch (NoResultException nre) {
73 if (account == null) {
74 account = new AccountsCommon();
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();
84 AccountTenant tenant = new AccountTenant();
85 tenant.setTenantId("1");
86 List<AccountTenant> tList = new ArrayList<AccountTenant>();
88 account.setTenants(tList);
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 AccountTenant tenant = new AccountTenant();
112 tenant.setTenantId("123");
113 List<AccountTenant> tList = new ArrayList<AccountTenant>();
115 account.setTenants(tList);
116 em.getTransaction().begin();
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());
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());
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();
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());
166 @SuppressWarnings("unchecked")
167 @Test(dataProvider = "testName", dataProviderClass = AccountTest.class,
168 dependsOnMethods = {"update"})
169 public void delete(String testName) throws Exception {
171 em.getTransaction().begin();
172 AccountsCommon account = findAccount("john");
174 if (logger.isDebugEnabled()) {
175 logger.debug("deleting account "
178 // Commit the transaction
179 em.getTransaction().commit();
180 if (logger.isDebugEnabled()) {
181 logger.debug("deleted account "
187 * Returns the name of the currently running test.
189 * Note: although the return type is listed as Object[][],
190 * this method instead returns a String.
192 * @param m The currently running test method.
194 * @return The name of the currently running test method.
196 @DataProvider(name = "testName")
197 public static Object[][] testName(Method m) {
198 return new Object[][]{
199 new Object[]{m.getName()}