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() {
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();
72 account.setCreatedAtItem(new Date());
73 AccountTenant tenant = new AccountTenant();
74 tenant.setTenantId("123");
75 List<AccountTenant> tList = new ArrayList<AccountTenant>();
77 account.setTenants(tList);
78 em.getTransaction().begin();
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());
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());
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();
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());
128 @SuppressWarnings("unchecked")
129 @Test(dataProvider = "testName", dataProviderClass = AccountTest.class,
130 dependsOnMethods = {"update"})
131 public void delete(String testName) throws Exception {
133 em.getTransaction().begin();
134 AccountsCommon account = findAccount("john");
136 if (logger.isDebugEnabled()) {
137 logger.debug("deleting account "
140 // Commit the transaction
141 em.getTransaction().commit();
142 if (logger.isDebugEnabled()) {
143 logger.debug("deleted account "
149 * Returns the name of the currently running test.
151 * Note: although the return type is listed as Object[][],
152 * this method instead returns a String.
154 * @param m The currently running test method.
156 * @return The name of the currently running test method.
158 @DataProvider(name = "testName")
159 public static Object[][] testName(Method m) {
160 return new Object[][]{
161 new Object[]{m.getName()}