blob: f8cd1b9a8042bb3dfa5a4b392be94a478a743706 [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;
4import de.ids_mannheim.korap.interfaces.db.UserDataDbIface;
Michael Hanld034af02016-01-28 14:02:41 +01005import de.ids_mannheim.korap.user.*;
6import org.junit.AfterClass;
7import org.junit.Before;
8import org.junit.BeforeClass;
9import org.junit.Test;
Michael Hanl0320dca2016-01-27 21:08:25 +010010
11/**
12 * @author hanl
13 * @date 27/01/2016
14 */
15public 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 Hanl5dd931a2016-01-29 16:40:38 +010040 UserDetails d = new UserDetails(1);
Michael Hanl0320dca2016-01-27 21:08:25 +010041 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 Hanl5dd931a2016-01-29 16:40:38 +010051 UserDetails d = new UserDetails(1);
Michael Hanl0320dca2016-01-27 21:08:25 +010052 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 Hanld034af02016-01-28 14:02:41 +010064 @Test
Michael Hanlc2a9f622016-01-28 16:40:06 +010065 public void testDataValidation() {
Michael Hanl5dd931a2016-01-29 16:40:38 +010066 Userdata data = new UserDetails(1);
Michael Hanlc2a9f622016-01-28 16:40:06 +010067 data.addField(Attributes.COUNTRY, "Germany");
68
69 String[] req = data.requiredFields();
Michael Hanl5fac8ab2016-01-29 16:33:04 +010070 String[] r = data.missing();
Michael Hanlc2a9f622016-01-28 16:40:06 +010071 assert r.length > 0;
72 assert r.length == req.length;
73 assert !data.isValid();
74 }
75
76 @Test
Michael Hanl5fac8ab2016-01-29 16:33:04 +010077 public void testSettingsValidation() {
Michael Hanl5dd931a2016-01-29 16:40:38 +010078 Userdata data = new UserSettings(1);
Michael Hanl5fac8ab2016-01-29 16:33:04 +010079 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 Hanld034af02016-01-28 14:02:41 +010090 UserDataDbIface dao = UserdataFactory
Michael Hanl5dd931a2016-01-29 16:40:38 +010091 .getDaoInstance(UserDetails.class);
Michael Hanld034af02016-01-28 14:02:41 +010092 assert UserDetailsDao.class.equals(dao.getClass());
93 }
94
Michael Hanl5fac8ab2016-01-29 16:33:04 +010095 @Test(expected = KustvaktException.class)
96 public void testUserdatafactoryError() throws KustvaktException {
Michael Hanld034af02016-01-28 14:02:41 +010097 UserdataFactory.getDaoInstance(new Userdata(1) {
98 @Override
99 public String[] requiredFields() {
100 return new String[0];
101 }
Michael Hanl5fac8ab2016-01-29 16:33:04 +0100102
103 @Override
104 public String[] defaultFields() {
105 return new String[0];
106 }
Michael Hanld034af02016-01-28 14:02:41 +0100107 }.getClass());
108 }
109
Michael Hanl0320dca2016-01-27 21:08:25 +0100110}