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