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