blob: c3cc6b8819b6c1e5868726be0c3a0bf7f0025d85 [file] [log] [blame]
Michael Hanl0320dca2016-01-27 21:08:25 +01001import de.ids_mannheim.korap.config.BeanConfiguration;
Michael Hanl5fac8ab2016-01-29 16:33:04 +01002import de.ids_mannheim.korap.exceptions.KustvaktException;
Michael Hanl415276b2016-01-29 16:39:37 +01003import de.ids_mannheim.korap.handlers.UserDetailsDao;
Michael Hanl25aac542016-02-01 18:16:44 +01004import de.ids_mannheim.korap.handlers.UserSettingsDao;
Michael Hanl415276b2016-01-29 16:39:37 +01005import de.ids_mannheim.korap.interfaces.db.UserDataDbIface;
Michael Hanld034af02016-01-28 14:02:41 +01006import de.ids_mannheim.korap.user.*;
7import org.junit.AfterClass;
8import org.junit.Before;
9import org.junit.BeforeClass;
10import org.junit.Test;
Michael Hanl0320dca2016-01-27 21:08:25 +010011
12/**
13 * @author hanl
14 * @date 27/01/2016
15 */
16public 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 Hanl25aac542016-02-01 18:16:44 +010032 UserSettingsDao sdao = new UserSettingsDao(
33 BeanConfiguration.getBeans().getPersistenceClient());
Michael Hanl0320dca2016-01-27 21:08:25 +010034 assert dao.deleteAll() != -1;
Michael Hanl25aac542016-02-01 18:16:44 +010035 assert sdao.deleteAll() != -1;
Michael Hanl0320dca2016-01-27 21:08:25 +010036 }
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 Hanl5dd931a2016-01-29 16:40:38 +010044 UserDetails d = new UserDetails(1);
Michael Hanl0320dca2016-01-27 21:08:25 +010045 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 Hanl5dd931a2016-01-29 16:40:38 +010055 UserDetails d = new UserDetails(1);
Michael Hanl0320dca2016-01-27 21:08:25 +010056 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 Hanld034af02016-01-28 14:02:41 +010068 @Test
Michael Hanlc2a9f622016-01-28 16:40:06 +010069 public void testDataValidation() {
Michael Hanl5dd931a2016-01-29 16:40:38 +010070 Userdata data = new UserDetails(1);
Michael Hanlc2a9f622016-01-28 16:40:06 +010071 data.addField(Attributes.COUNTRY, "Germany");
72
73 String[] req = data.requiredFields();
Michael Hanl5fac8ab2016-01-29 16:33:04 +010074 String[] r = data.missing();
Michael Hanlc2a9f622016-01-28 16:40:06 +010075 assert r.length > 0;
76 assert r.length == req.length;
77 assert !data.isValid();
78 }
79
80 @Test
Michael Hanl5fac8ab2016-01-29 16:33:04 +010081 public void testSettingsValidation() {
Michael Hanl5dd931a2016-01-29 16:40:38 +010082 Userdata data = new UserSettings(1);
Michael Hanl5fac8ab2016-01-29 16:33:04 +010083 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 Hanl25aac542016-02-01 18:16:44 +010094 UserDataDbIface dao = UserdataFactory.getDaoInstance(UserDetails.class);
Michael Hanld034af02016-01-28 14:02:41 +010095 assert UserDetailsDao.class.equals(dao.getClass());
96 }
97
Michael Hanl5fac8ab2016-01-29 16:33:04 +010098 @Test(expected = KustvaktException.class)
99 public void testUserdatafactoryError() throws KustvaktException {
Michael Hanld034af02016-01-28 14:02:41 +0100100 UserdataFactory.getDaoInstance(new Userdata(1) {
101 @Override
102 public String[] requiredFields() {
103 return new String[0];
104 }
Michael Hanl5fac8ab2016-01-29 16:33:04 +0100105
106 @Override
107 public String[] defaultFields() {
108 return new String[0];
109 }
Michael Hanld034af02016-01-28 14:02:41 +0100110 }.getClass());
111 }
112
Michael Hanl0320dca2016-01-27 21:08:25 +0100113}