blob: e5b598fb2bb641966fb19cfcaa0c33c656bf7ea9 [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 Hanl7368aa42016-02-05 18:15:47 +01003import de.ids_mannheim.korap.exceptions.dbException;
Michael Hanl415276b2016-01-29 16:39:37 +01004import de.ids_mannheim.korap.handlers.UserDetailsDao;
Michael Hanl25aac542016-02-01 18:16:44 +01005import de.ids_mannheim.korap.handlers.UserSettingsDao;
Michael Hanl415276b2016-01-29 16:39:37 +01006import de.ids_mannheim.korap.interfaces.db.UserDataDbIface;
Michael Hanld034af02016-01-28 14:02:41 +01007import de.ids_mannheim.korap.user.*;
8import org.junit.AfterClass;
9import org.junit.Before;
10import org.junit.BeforeClass;
11import org.junit.Test;
Michael Hanl0320dca2016-01-27 21:08:25 +010012
13/**
14 * @author hanl
15 * @date 27/01/2016
16 */
17public 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 Hanl25aac542016-02-01 18:16:44 +010033 UserSettingsDao sdao = new UserSettingsDao(
34 BeanConfiguration.getBeans().getPersistenceClient());
Michael Hanl0320dca2016-01-27 21:08:25 +010035 assert dao.deleteAll() != -1;
Michael Hanl25aac542016-02-01 18:16:44 +010036 assert sdao.deleteAll() != -1;
Michael Hanl0320dca2016-01-27 21:08:25 +010037 }
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 Hanl5dd931a2016-01-29 16:40:38 +010045 UserDetails d = new UserDetails(1);
Michael Hanl0320dca2016-01-27 21:08:25 +010046 d.addField("key_1", "value is a value");
47 assert dao.store(d) != -1;
48 }
49
50 @Test
Michael Hanl7368aa42016-02-05 18:15:47 +010051 public void testDataGet() throws dbException {
Michael Hanl0320dca2016-01-27 21:08:25 +010052 User user = new KorAPUser();
53 user.setId(1);
54 UserDetailsDao dao = new UserDetailsDao(
55 BeanConfiguration.getBeans().getPersistenceClient());
Michael Hanl5dd931a2016-01-29 16:40:38 +010056 UserDetails d = new UserDetails(1);
Michael Hanl0320dca2016-01-27 21:08:25 +010057 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 Hanld034af02016-01-28 14:02:41 +010069 @Test
Michael Hanlc2a9f622016-01-28 16:40:06 +010070 public void testDataValidation() {
Michael Hanl5dd931a2016-01-29 16:40:38 +010071 Userdata data = new UserDetails(1);
Michael Hanlc2a9f622016-01-28 16:40:06 +010072 data.addField(Attributes.COUNTRY, "Germany");
73
74 String[] req = data.requiredFields();
Michael Hanl5fac8ab2016-01-29 16:33:04 +010075 String[] r = data.missing();
Michael Hanlc2a9f622016-01-28 16:40:06 +010076 assert r.length > 0;
77 assert r.length == req.length;
78 assert !data.isValid();
79 }
80
81 @Test
Michael Hanl5fac8ab2016-01-29 16:33:04 +010082 public void testSettingsValidation() {
Michael Hanl5dd931a2016-01-29 16:40:38 +010083 Userdata data = new UserSettings(1);
Michael Hanl5fac8ab2016-01-29 16:33:04 +010084 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 Hanl25aac542016-02-01 18:16:44 +010095 UserDataDbIface dao = UserdataFactory.getDaoInstance(UserDetails.class);
Michael Hanld034af02016-01-28 14:02:41 +010096 assert UserDetailsDao.class.equals(dao.getClass());
97 }
98
Michael Hanl5fac8ab2016-01-29 16:33:04 +010099 @Test(expected = KustvaktException.class)
100 public void testUserdatafactoryError() throws KustvaktException {
Michael Hanld034af02016-01-28 14:02:41 +0100101 UserdataFactory.getDaoInstance(new Userdata(1) {
102 @Override
103 public String[] requiredFields() {
104 return new String[0];
105 }
Michael Hanl5fac8ab2016-01-29 16:33:04 +0100106
107 @Override
108 public String[] defaultFields() {
109 return new String[0];
110 }
Michael Hanld034af02016-01-28 14:02:41 +0100111 }.getClass());
112 }
113
Michael Hanl0320dca2016-01-27 21:08:25 +0100114}