| Michael Hanl | deb3c21 | 2015-10-16 23:02:24 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.config; |
| 2 | |
| 3 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 4 | import de.ids_mannheim.korap.interfaces.EncryptionIface; |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 5 | import de.ids_mannheim.korap.utils.ServiceInfo; |
| Michael Hanl | deb3c21 | 2015-10-16 23:02:24 +0200 | [diff] [blame] | 6 | import de.ids_mannheim.korap.utils.TimeUtils; |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 7 | import de.ids_mannheim.korap.web.service.BootableBeanInterface; |
| Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 8 | import org.codehaus.plexus.util.IOUtil; |
| 9 | import org.junit.Assert; |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 10 | import org.junit.Ignore; |
| Michael Hanl | deb3c21 | 2015-10-16 23:02:24 +0200 | [diff] [blame] | 11 | import org.junit.Test; |
| 12 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 13 | import java.io.*; |
| Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 14 | import java.security.NoSuchAlgorithmException; |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 15 | import java.util.*; |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 16 | |
| 17 | import static org.junit.Assert.*; |
| 18 | |
| Michael Hanl | deb3c21 | 2015-10-16 23:02:24 +0200 | [diff] [blame] | 19 | /** |
| 20 | * @author hanl |
| 21 | * @date 02/09/2015 |
| 22 | */ |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 23 | public class ConfigTest extends BeanConfigTest { |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 24 | |
| Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 25 | |
| 26 | @Test |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 27 | 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 Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 41 | 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 Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 55 | @Test |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 56 | public void testServiceInfo () { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 57 | String version = ServiceInfo.getInfo().getVersion(); |
| 58 | String name = ServiceInfo.getInfo().getName(); |
| 59 | assertNotEquals("wrong version", "UNKNOWN", version); |
| 60 | assertNotEquals("wrong name", "UNKNOWN", name); |
| Michael Hanl | deb3c21 | 2015-10-16 23:02:24 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 63 | |
| Michael Hanl | deb3c21 | 2015-10-16 23:02:24 +0200 | [diff] [blame] | 64 | @Test |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 65 | public void testProperties () { |
| 66 | assertEquals("token layer does not match", "opennlp", helper() |
| 67 | .getContext().getConfiguration().getDefault_token()); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 68 | assertEquals("token expiration does not match", |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 69 | TimeUtils.convertTimeToSeconds("1D"), helper().getContext() |
| 70 | .getConfiguration().getLongTokenTTL()); |
| Michael Hanl | deb3c21 | 2015-10-16 23:02:24 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 73 | |
| Michael Hanl | deb3c21 | 2015-10-16 23:02:24 +0200 | [diff] [blame] | 74 | @Test(expected = KustvaktException.class) |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 75 | @Ignore |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 76 | public void testBeanOverrideInjection () throws KustvaktException { |
| 77 | helper().getContext() |
| 78 | .getConfiguration() |
| 79 | .setPropertiesAsStream( |
| 80 | ConfigTest.class.getClassLoader().getResourceAsStream( |
| 81 | "kustvakt.conf")); |
| Michael Hanl | deb3c21 | 2015-10-16 23:02:24 +0200 | [diff] [blame] | 82 | } |
| Michael Hanl | c444602 | 2016-02-12 18:03:17 +0100 | [diff] [blame] | 83 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 84 | |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 85 | @Test |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 86 | public void testBootConfigRun () { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 87 | helper().runBootInterfaces(); |
| 88 | helper().setupAccount(); |
| 89 | assertNotNull(helper().getUser()); |
| Michael Hanl | c444602 | 2016-02-12 18:03:17 +0100 | [diff] [blame] | 90 | |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 91 | 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 Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 102 | } |
| 103 | catch (InstantiationException | IllegalAccessException e) { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 104 | // do nothing |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | while (!set.isEmpty()) { |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 109 | out_loop: for (BootableBeanInterface iface : new ArrayList<>(list)) { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 110 | 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 Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 122 | // 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 Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 132 | |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 133 | @Test |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 134 | public void testBootConfigDependencyOrder () { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 135 | // todo: |
| 136 | |
| 137 | } |
| 138 | |
| 139 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 140 | public void initMethod () throws KustvaktException { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 141 | |
| 142 | } |
| Michael Hanl | deb3c21 | 2015-10-16 23:02:24 +0200 | [diff] [blame] | 143 | } |