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