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()); |
Michael Hanl | 5dd931a | 2016-01-29 16:40:38 +0100 | [diff] [blame^] | 40 | UserDetails d = new UserDetails(1); |
Michael Hanl | 0320dca | 2016-01-27 21:08:25 +0100 | [diff] [blame] | 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()); |
Michael Hanl | 5dd931a | 2016-01-29 16:40:38 +0100 | [diff] [blame^] | 51 | UserDetails d = new UserDetails(1); |
Michael Hanl | 0320dca | 2016-01-27 21:08:25 +0100 | [diff] [blame] | 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() { |
Michael Hanl | 5dd931a | 2016-01-29 16:40:38 +0100 | [diff] [blame^] | 66 | Userdata data = new UserDetails(1); |
Michael Hanl | c2a9f62 | 2016-01-28 16:40:06 +0100 | [diff] [blame] | 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() { |
Michael Hanl | 5dd931a | 2016-01-29 16:40:38 +0100 | [diff] [blame^] | 78 | Userdata data = new UserSettings(1); |
Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 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 |
Michael Hanl | 5dd931a | 2016-01-29 16:40:38 +0100 | [diff] [blame^] | 91 | .getDaoInstance(UserDetails.class); |
Michael Hanl | d034af0 | 2016-01-28 14:02:41 +0100 | [diff] [blame] | 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 | } |