Michael Hanl | 0320dca | 2016-01-27 21:08:25 +0100 | [diff] [blame] | 1 | import de.ids_mannheim.korap.config.BeanConfiguration; |
Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 2 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
Michael Hanl | 415276b | 2016-01-29 16:39:37 +0100 | [diff] [blame] | 3 | import de.ids_mannheim.korap.handlers.UserDetailsDao; |
Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 4 | import de.ids_mannheim.korap.handlers.UserSettingsDao; |
Michael Hanl | 415276b | 2016-01-29 16:39:37 +0100 | [diff] [blame] | 5 | import de.ids_mannheim.korap.interfaces.db.UserDataDbIface; |
Michael Hanl | d034af0 | 2016-01-28 14:02:41 +0100 | [diff] [blame] | 6 | import de.ids_mannheim.korap.user.*; |
| 7 | import org.junit.AfterClass; |
| 8 | import org.junit.Before; |
| 9 | import org.junit.BeforeClass; |
| 10 | import org.junit.Test; |
Michael Hanl | 0320dca | 2016-01-27 21:08:25 +0100 | [diff] [blame] | 11 | |
| 12 | /** |
| 13 | * @author hanl |
| 14 | * @date 27/01/2016 |
| 15 | */ |
| 16 | public class UserdataTest { |
| 17 | |
| 18 | @BeforeClass |
| 19 | public static void init() { |
| 20 | BeanConfiguration.loadClasspathContext("default-config.xml"); |
| 21 | } |
| 22 | |
| 23 | @AfterClass |
| 24 | public static void drop() { |
| 25 | BeanConfiguration.closeApplication(); |
| 26 | } |
| 27 | |
| 28 | @Before |
| 29 | public void clear() { |
| 30 | UserDetailsDao dao = new UserDetailsDao( |
| 31 | BeanConfiguration.getBeans().getPersistenceClient()); |
Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 32 | UserSettingsDao sdao = new UserSettingsDao( |
| 33 | BeanConfiguration.getBeans().getPersistenceClient()); |
Michael Hanl | 0320dca | 2016-01-27 21:08:25 +0100 | [diff] [blame] | 34 | assert dao.deleteAll() != -1; |
Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 35 | assert sdao.deleteAll() != -1; |
Michael Hanl | 0320dca | 2016-01-27 21:08:25 +0100 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | @Test |
| 39 | public void testDataStore() { |
| 40 | User user = new KorAPUser(); |
| 41 | user.setId(1); |
| 42 | UserDetailsDao dao = new UserDetailsDao( |
| 43 | BeanConfiguration.getBeans().getPersistenceClient()); |
Michael Hanl | 5dd931a | 2016-01-29 16:40:38 +0100 | [diff] [blame] | 44 | UserDetails d = new UserDetails(1); |
Michael Hanl | 0320dca | 2016-01-27 21:08:25 +0100 | [diff] [blame] | 45 | d.addField("key_1", "value is a value"); |
| 46 | assert dao.store(d) != -1; |
| 47 | } |
| 48 | |
| 49 | @Test |
| 50 | public void testDataGet() { |
| 51 | User user = new KorAPUser(); |
| 52 | user.setId(1); |
| 53 | UserDetailsDao dao = new UserDetailsDao( |
| 54 | BeanConfiguration.getBeans().getPersistenceClient()); |
Michael Hanl | 5dd931a | 2016-01-29 16:40:38 +0100 | [diff] [blame] | 55 | UserDetails d = new UserDetails(1); |
Michael Hanl | 0320dca | 2016-01-27 21:08:25 +0100 | [diff] [blame] | 56 | d.addField("key_1", "value is a value"); |
| 57 | assert dao.store(d) != -1; |
| 58 | |
| 59 | d = dao.get(d.getId()); |
| 60 | assert d != null; |
| 61 | assert "value is a value".equals(d.get("key_1")); |
| 62 | |
| 63 | d = dao.get(user); |
| 64 | assert d != null; |
| 65 | assert "value is a value".equals(d.get("key_1")); |
| 66 | } |
| 67 | |
Michael Hanl | d034af0 | 2016-01-28 14:02:41 +0100 | [diff] [blame] | 68 | @Test |
Michael Hanl | c2a9f62 | 2016-01-28 16:40:06 +0100 | [diff] [blame] | 69 | public void testDataValidation() { |
Michael Hanl | 5dd931a | 2016-01-29 16:40:38 +0100 | [diff] [blame] | 70 | Userdata data = new UserDetails(1); |
Michael Hanl | c2a9f62 | 2016-01-28 16:40:06 +0100 | [diff] [blame] | 71 | data.addField(Attributes.COUNTRY, "Germany"); |
| 72 | |
| 73 | String[] req = data.requiredFields(); |
Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 74 | String[] r = data.missing(); |
Michael Hanl | c2a9f62 | 2016-01-28 16:40:06 +0100 | [diff] [blame] | 75 | assert r.length > 0; |
| 76 | assert r.length == req.length; |
| 77 | assert !data.isValid(); |
| 78 | } |
| 79 | |
| 80 | @Test |
Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 81 | public void testSettingsValidation() { |
Michael Hanl | 5dd931a | 2016-01-29 16:40:38 +0100 | [diff] [blame] | 82 | Userdata data = new UserSettings(1); |
Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 83 | data.addField(Attributes.FILE_FORMAT_FOR_EXPORT, "export"); |
| 84 | |
| 85 | String[] req = data.requiredFields(); |
| 86 | String[] r = data.missing(); |
| 87 | assert r.length == 0; |
| 88 | assert r.length == req.length; |
| 89 | assert data.isValid(); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testUserdatafactory() throws KustvaktException { |
Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 94 | UserDataDbIface dao = UserdataFactory.getDaoInstance(UserDetails.class); |
Michael Hanl | d034af0 | 2016-01-28 14:02:41 +0100 | [diff] [blame] | 95 | assert UserDetailsDao.class.equals(dao.getClass()); |
| 96 | } |
| 97 | |
Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 98 | @Test(expected = KustvaktException.class) |
| 99 | public void testUserdatafactoryError() throws KustvaktException { |
Michael Hanl | d034af0 | 2016-01-28 14:02:41 +0100 | [diff] [blame] | 100 | UserdataFactory.getDaoInstance(new Userdata(1) { |
| 101 | @Override |
| 102 | public String[] requiredFields() { |
| 103 | return new String[0]; |
| 104 | } |
Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 105 | |
| 106 | @Override |
| 107 | public String[] defaultFields() { |
| 108 | return new String[0]; |
| 109 | } |
Michael Hanl | d034af0 | 2016-01-28 14:02:41 +0100 | [diff] [blame] | 110 | }.getClass()); |
| 111 | } |
| 112 | |
Michael Hanl | 0320dca | 2016-01-27 21:08:25 +0100 | [diff] [blame] | 113 | } |