blob: 390ae007073643f783707f445264b3811a763a4a [file] [log] [blame]
Michael Hanldeb3c212015-10-16 23:02:24 +02001package de.ids_mannheim.korap.config;
2
3import de.ids_mannheim.korap.exceptions.KustvaktException;
Michael Hanlcb2d3f92016-06-02 17:34:06 +02004import de.ids_mannheim.korap.interfaces.EncryptionIface;
Michael Hanldaf86602016-05-12 14:31:52 +02005import de.ids_mannheim.korap.utils.ServiceInfo;
Michael Hanldeb3c212015-10-16 23:02:24 +02006import de.ids_mannheim.korap.utils.TimeUtils;
Michael Hanldaf86602016-05-12 14:31:52 +02007import de.ids_mannheim.korap.web.service.BootableBeanInterface;
Michael Hanlcb2d3f92016-06-02 17:34:06 +02008import org.codehaus.plexus.util.IOUtil;
9import org.junit.Assert;
Michael Hanlc0ed00f2016-06-23 14:33:10 +020010import org.junit.Ignore;
Michael Hanldeb3c212015-10-16 23:02:24 +020011import org.junit.Test;
12
Michael Hanlc0ed00f2016-06-23 14:33:10 +020013import java.io.*;
Michael Hanlcb2d3f92016-06-02 17:34:06 +020014import java.security.NoSuchAlgorithmException;
Michael Hanlc0ed00f2016-06-23 14:33:10 +020015import java.util.*;
Michael Hanldaf86602016-05-12 14:31:52 +020016
17import static org.junit.Assert.*;
18
Michael Hanldeb3c212015-10-16 23:02:24 +020019/**
20 * @author hanl
21 * @date 02/09/2015
22 */
Michael Hanldaf86602016-05-12 14:31:52 +020023public class ConfigTest extends BeanConfigTest {
Michael Hanlf1e85e72016-01-21 16:55:45 +010024
Michael Hanlcb2d3f92016-06-02 17:34:06 +020025
26 @Test
Michael Hanlc0ed00f2016-06-23 14:33:10 +020027 public void testConfigLoader () {
28 InputStream stream = ConfigLoader.loadConfigStream("kustvakt.conf");
29 assertNotNull(stream);
30 }
31
32
33 @Test
34 public void testPropertyLoader () throws IOException {
35 Properties p = ConfigLoader.loadProperties("kustvakt.conf");
36 assertNotNull(p);
37 }
38
39
40 @Test
Michael Hanlcb2d3f92016-06-02 17:34:06 +020041 public void testAdminHash () throws IOException, KustvaktException,
42 NoSuchAlgorithmException {
43 AdminSetup setup = AdminSetup.getInstance();
44 String hash = setup.getHash();
45 File f = new File("./admin_token");
46 FileInputStream stream = new FileInputStream(f);
47 String token = IOUtil.toString(stream);
48 assertNotEquals("", hash);
49 assertNotEquals("", token);
50 EncryptionIface crypto = helper().getContext().getEncryption();
51 assertTrue(crypto.checkHash(token, hash));
52 }
53
54
Michael Hanlf1e85e72016-01-21 16:55:45 +010055 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020056 public void testServiceInfo () {
Michael Hanldaf86602016-05-12 14:31:52 +020057 String version = ServiceInfo.getInfo().getVersion();
58 String name = ServiceInfo.getInfo().getName();
59 assertNotEquals("wrong version", "UNKNOWN", version);
60 assertNotEquals("wrong name", "UNKNOWN", name);
Michael Hanldeb3c212015-10-16 23:02:24 +020061 }
62
Michael Hanl8abaf9e2016-05-23 16:46:35 +020063
Michael Hanldeb3c212015-10-16 23:02:24 +020064 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020065 public void testProperties () {
66 assertEquals("token layer does not match", "opennlp", helper()
67 .getContext().getConfiguration().getDefault_token());
Michael Hanldaf86602016-05-12 14:31:52 +020068 assertEquals("token expiration does not match",
Michael Hanl8abaf9e2016-05-23 16:46:35 +020069 TimeUtils.convertTimeToSeconds("1D"), helper().getContext()
70 .getConfiguration().getLongTokenTTL());
Michael Hanldeb3c212015-10-16 23:02:24 +020071 }
72
Michael Hanl8abaf9e2016-05-23 16:46:35 +020073
Michael Hanldeb3c212015-10-16 23:02:24 +020074 @Test(expected = KustvaktException.class)
Michael Hanlc0ed00f2016-06-23 14:33:10 +020075 @Ignore
Michael Hanl8abaf9e2016-05-23 16:46:35 +020076 public void testBeanOverrideInjection () throws KustvaktException {
77 helper().getContext()
78 .getConfiguration()
79 .setPropertiesAsStream(
80 ConfigTest.class.getClassLoader().getResourceAsStream(
81 "kustvakt.conf"));
Michael Hanldeb3c212015-10-16 23:02:24 +020082 }
Michael Hanlc4446022016-02-12 18:03:17 +010083
Michael Hanl8abaf9e2016-05-23 16:46:35 +020084
Michael Hanldaf86602016-05-12 14:31:52 +020085 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020086 public void testBootConfigRun () {
Michael Hanldaf86602016-05-12 14:31:52 +020087 helper().runBootInterfaces();
88 helper().setupAccount();
89 assertNotNull(helper().getUser());
Michael Hanlc4446022016-02-12 18:03:17 +010090
Michael Hanldaf86602016-05-12 14:31:52 +020091 Set<Class<? extends BootableBeanInterface>> set = KustvaktClassLoader
92 .loadSubTypes(BootableBeanInterface.class);
93
94 int check = set.size();
95 List<String> tracker = new ArrayList<>();
96 List<BootableBeanInterface> list = new ArrayList<>(set.size());
97 for (Class cl : set) {
98 BootableBeanInterface iface;
99 try {
100 iface = (BootableBeanInterface) cl.newInstance();
101 list.add(iface);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200102 }
103 catch (InstantiationException | IllegalAccessException e) {
Michael Hanldaf86602016-05-12 14:31:52 +0200104 // do nothing
105 }
106 }
107
108 while (!set.isEmpty()) {
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200109 out_loop: for (BootableBeanInterface iface : new ArrayList<>(list)) {
Michael Hanldaf86602016-05-12 14:31:52 +0200110 for (Class cl : iface.getDependencies()) {
111 if (set.contains(cl))
112 continue out_loop;
113 }
114 tracker.add(iface.getClass().getSimpleName());
115 set.remove(iface.getClass());
116 list.remove(iface);
117 }
118 }
119 assertEquals(check, tracker.size());
120 }
121
Michael Hanlc0ed00f2016-06-23 14:33:10 +0200122 // todo:
123 @Test
124 @Ignore
125 public void testKustvaktValueValidation() {
126 Map m = KustvaktConfiguration.KUSTVAKT_USER;
127 EncryptionIface crypto = helper().getContext().getEncryption();
128
129
130 }
131
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200132
Michael Hanldaf86602016-05-12 14:31:52 +0200133 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200134 public void testBootConfigDependencyOrder () {
Michael Hanldaf86602016-05-12 14:31:52 +0200135 // todo:
136
137 }
138
139 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200140 public void initMethod () throws KustvaktException {
Michael Hanldaf86602016-05-12 14:31:52 +0200141
142 }
Michael Hanldeb3c212015-10-16 23:02:24 +0200143}