blob: efe2fd8d45ae9255e63884919780d6a877c76aeb [file] [log] [blame]
Michael Hanl19390652016-01-16 11:01:24 +01001package de.ids_mannheim.korap.config;
2
margaretha0c47c652017-04-19 18:44:40 +02003import static org.junit.Assert.assertNotEquals;
4import static org.junit.Assert.assertNotNull;
Michael Hanl19390652016-01-16 11:01:24 +01005
Michael Hanldaf86602016-05-12 14:31:52 +02006import java.io.File;
7import java.io.IOException;
Michael Hanl19390652016-01-16 11:01:24 +01008import java.sql.ResultSet;
9import java.sql.SQLException;
margaretha0c47c652017-04-19 18:44:40 +020010import java.util.Arrays;
11import java.util.HashMap;
12import java.util.HashSet;
margaretha0c47c652017-04-19 18:44:40 +020013import java.util.Map;
14import java.util.Set;
Michael Hanl19390652016-01-16 11:01:24 +010015
margaretha0c47c652017-04-19 18:44:40 +020016import org.apache.commons.dbcp2.BasicDataSource;
17import org.flywaydb.core.Flyway;
18import org.joda.time.DateTime;
19import org.slf4j.Logger;
20import org.slf4j.LoggerFactory;
21import org.springframework.context.ApplicationContext;
margaretha0c47c652017-04-19 18:44:40 +020022import org.springframework.jdbc.core.RowCallbackHandler;
23import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
24import org.springframework.jdbc.datasource.SingleConnectionDataSource;
25
margaretha139d0f72017-11-14 18:56:22 +010026import de.ids_mannheim.korap.authentication.KustvaktAuthenticationManager;
margaretha0c47c652017-04-19 18:44:40 +020027import de.ids_mannheim.korap.exceptions.KustvaktException;
margaretha0c47c652017-04-19 18:44:40 +020028import de.ids_mannheim.korap.handlers.JDBCClient;
margaretha69e8adc2018-03-15 15:14:22 +010029import de.ids_mannheim.korap.interfaces.EntityHandlerIface;
margaretha0e8f4e72018-04-05 14:11:52 +020030import de.ids_mannheim.korap.interfaces.KustvaktBaseDaoInterface;
margaretha0c47c652017-04-19 18:44:40 +020031import de.ids_mannheim.korap.interfaces.db.PersistenceClient;
margaretha0c47c652017-04-19 18:44:40 +020032import de.ids_mannheim.korap.user.User;
33import de.ids_mannheim.korap.utils.TimeUtils;
Michael Hanldaf86602016-05-12 14:31:52 +020034
Michael Hanl19390652016-01-16 11:01:24 +010035/**
36 * creates a test user that can be used to access protected functions
Michael Hanl8abaf9e2016-05-23 16:46:35 +020037 *
Michael Hanl19390652016-01-16 11:01:24 +010038 * @author hanl
39 * @date 16/10/2015
40 */
Michael Hanlf1e85e72016-01-21 16:55:45 +010041public class TestHelper {
Michael Hanl19390652016-01-16 11:01:24 +010042
Michael Hanl08aa7722016-06-03 13:34:37 +020043 private static Logger jlog = LoggerFactory.getLogger(TestHelper.class);
Michael Hanlc0ed00f2016-06-23 14:33:10 +020044 private static final Map<String, Object> data = new HashMap<>();
45 static {
Bodmo3d6bd352017-04-25 11:31:39 +020046 data.put(Attributes.ID, 3); // 2);
47 data.put(Attributes.USERNAME, "testUser1"); // bodmer funktioniert noch nicht
Michael Hanlc0ed00f2016-06-23 14:33:10 +020048 data.put(Attributes.PASSWORD, "testPass2015");
49 data.put(Attributes.FIRSTNAME, "test");
50 data.put(Attributes.LASTNAME, "user");
51 data.put(Attributes.EMAIL, "test@ids-mannheim.de");
52 data.put(Attributes.ADDRESS, "Mannheim");
margaretha4fa4b062019-01-28 19:43:30 +010053 data.put(Attributes.DEFAULT_FOUNDRY_LEMMA, "test_l");
54 data.put(Attributes.DEFAULT_FOUNDRY_POS, "test_p");
55 data.put(Attributes.DEFAULT_FOUNDRY_CONSTITUENT, "test_const");
Michael Hanlc0ed00f2016-06-23 14:33:10 +020056 }
Michael Hanl19390652016-01-16 11:01:24 +010057
Michael Hanldaf86602016-05-12 14:31:52 +020058 private ContextHolder beansHolder;
Michael Hanl19390652016-01-16 11:01:24 +010059
Michael Hanl8abaf9e2016-05-23 16:46:35 +020060 public static TestHelper newInstance (ApplicationContext ctx)
Michael Hanldaf86602016-05-12 14:31:52 +020061 throws Exception {
62 TestHelper b = new TestHelper();
Michael Hanl8abaf9e2016-05-23 16:46:35 +020063 b.beansHolder = new ContextHolder(ctx) {};
Michael Hanldaf86602016-05-12 14:31:52 +020064 return b;
Michael Hanl7368aa42016-02-05 18:15:47 +010065 }
66
Michael Hanl8abaf9e2016-05-23 16:46:35 +020067
68 public <T> T getBean (Class<T> type) {
Michael Hanldaf86602016-05-12 14:31:52 +020069 return this.beansHolder.getBean(type);
70 }
71
Michael Hanl8abaf9e2016-05-23 16:46:35 +020072
73 public ContextHolder getContext () {
Michael Hanldaf86602016-05-12 14:31:52 +020074 return this.beansHolder;
75 }
76
Michael Hanl8abaf9e2016-05-23 16:46:35 +020077
78 public <T> T getBean (String name) {
Michael Hanldaf86602016-05-12 14:31:52 +020079 return (T) this.beansHolder.getBean(name);
80 }
81
Michael Hanl8abaf9e2016-05-23 16:46:35 +020082
margarethae7badb32017-06-22 15:16:00 +020083 public TestHelper setupAccount () throws KustvaktException {
Michael Hanldaf86602016-05-12 14:31:52 +020084 KustvaktBaseDaoInterface dao = getBean(ContextHolder.KUSTVAKT_USERDB);
85
Michael Hanl8abaf9e2016-05-23 16:46:35 +020086 KustvaktAuthenticationManager manager = getBean(ContextHolder.KUSTVAKT_AUTHENTICATION_MANAGER);
margarethae7badb32017-06-22 15:16:00 +020087// manager.createUserAccount(KustvaktConfiguration.KUSTVAKT_USER, false);
Michael Hanldaf86602016-05-12 14:31:52 +020088 try {
margaretha65b67142017-05-29 16:23:16 +020089 User user = getUser();
Michael Hanl08aa7722016-06-03 13:34:37 +020090 jlog.debug("found user, skipping setup ...");
margaretha65b67142017-05-29 16:23:16 +020091 if (!user.getUsername().equals(data.get(Attributes.USERNAME))){
92 return this;
93 }
Michael Hanl8abaf9e2016-05-23 16:46:35 +020094 }
95 catch (RuntimeException e) {
Michael Hanldaf86602016-05-12 14:31:52 +020096 // do nothing and continue
97 }
98
Michael Hanlc0ed00f2016-06-23 14:33:10 +020099 Map m = getUserCredentials();
Michael Hanldaf86602016-05-12 14:31:52 +0200100 assertNotNull("userdatabase handler must not be null", dao);
101
margaretha48426732019-01-21 16:58:28 +0100102// try {
103// manager.createUserAccount(m, false);
104// }
105// catch (KustvaktException e) {
106// throw new RuntimeException(e);
107// /*// do nothing
108// jlog.error("Error: {}", e.string());
109// assertNotNull("Test user could not be set up", null);*/
110// }
Michael Hanldaf86602016-05-12 14:31:52 +0200111 assertNotEquals(0, dao.size());
112 return this;
113 }
114
margaretha69e8adc2018-03-15 15:14:22 +0100115 // EM
116 @Deprecated
117// public TestHelper setupSimpleAccount (String username, String password) {
118// KustvaktBaseDaoInterface dao = getBean(ContextHolder.KUSTVAKT_USERDB);
119// EntityHandlerIface edao = (EntityHandlerIface) dao;
120// try {
121// edao.getAccount(username);
122// }
123// catch (EmptyResultException e) {
124// // do nothing
125// }
126// catch (KustvaktException ex) {
127// assertNull("Test user could not be set up", true);
128// }
129//
130// Map m = new HashMap<>();
131// m.put(Attributes.USERNAME, username);
132//
133// try {
134// String hash = ((EncryptionIface) getBean(ContextHolder.KUSTVAKT_ENCRYPTION))
135// .secureHash(password);
136// m.put(Attributes.PASSWORD, hash);
137// }
138// catch (NoSuchAlgorithmException | UnsupportedEncodingException
139// | KustvaktException e) {
140// // do nohting
141// assertNotNull("Exception thrown", null);
142// }
143// assertNotNull("userdatabase handler must not be null", dao);
144//
145// try {
146//
147// int i = edao.createAccount(User.UserFactory.toKorAPUser(m));
148// assert BeansFactory.getKustvaktContext().getUserDBHandler()
149// .getAccount((String) data.get(Attributes.USERNAME)) != null;
150// assertEquals(1, i);
151// }
152// catch (KustvaktException e) {
153// // do nothing
154// assertNull("Test user could not be set up", true);
155// }
156// return this;
157// }
Michael Hanl19390652016-01-16 11:01:24 +0100158
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200159
160 public User getUser () {
Michael Hanldaf86602016-05-12 14:31:52 +0200161 try {
162 return ((EntityHandlerIface) getBean(ContextHolder.KUSTVAKT_USERDB))
Michael Hanlc0ed00f2016-06-23 14:33:10 +0200163 .getAccount((String) data.get(Attributes.USERNAME));
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200164 }
165 catch (KustvaktException e) {
Michael Hanldaf86602016-05-12 14:31:52 +0200166 // do nothing
Michael Hanl25aac542016-02-01 18:16:44 +0100167 }
168 throw new RuntimeException("User could not be retrieved!");
169 }
170
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200171
172 public TestHelper dropUser (String ... usernames) throws KustvaktException {
Michael Hanldaf86602016-05-12 14:31:52 +0200173 if (usernames == null || usernames.length == 0) {
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200174 KustvaktBaseDaoInterface dao = getBean(ContextHolder.KUSTVAKT_USERDB);
Michael Hanldaf86602016-05-12 14:31:52 +0200175 dao.truncate();
Michael Hanl19390652016-01-16 11:01:24 +0100176 }
Michael Hanldaf86602016-05-12 14:31:52 +0200177 for (String name : Arrays.asList(usernames)) {
178 if (remove(name))
179 break;
180 }
181 return this;
Michael Hanl83356752016-02-06 14:33:50 +0100182 }
183
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200184
185 private boolean remove (String username) throws KustvaktException {
Michael Hanldaf86602016-05-12 14:31:52 +0200186 EntityHandlerIface dao = getBean(ContextHolder.KUSTVAKT_USERDB);
187 User us = dao.getAccount(username);
188 dao.deleteAccount(us.getId());
189 return true;
Michael Hanl19390652016-01-16 11:01:24 +0100190 }
191
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200192
193 public TestHelper truncateAll () {
Michael Hanldaf86602016-05-12 14:31:52 +0200194 String sql = "SELECT Concat('TRUNCATE TABLE ', TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES";
195 final Set<String> queries = new HashSet<>();
196 PersistenceClient cl = getBean(ContextHolder.KUSTVAKT_POLICIES);
197 NamedParameterJdbcTemplate source = (NamedParameterJdbcTemplate) cl
198 .getSource();
Michael Hanl7368aa42016-02-05 18:15:47 +0100199
Michael Hanldaf86602016-05-12 14:31:52 +0200200 source.query(sql, new RowCallbackHandler() {
201 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200202 public void processRow (ResultSet rs) throws SQLException {
Michael Hanldaf86602016-05-12 14:31:52 +0200203 queries.add(rs.getString(1));
Michael Hanl19390652016-01-16 11:01:24 +0100204
Michael Hanldaf86602016-05-12 14:31:52 +0200205 }
206 });
margaretha0a45be12018-07-12 15:06:30 +0200207// System.out.println(queries);
Michael Hanldaf86602016-05-12 14:31:52 +0200208 for (String query : queries)
209 source.update(query, new HashMap<String, Object>());
210 return this;
Michael Hanl19390652016-01-16 11:01:24 +0100211 }
212
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200213
Michael Hanlc0ed00f2016-06-23 14:33:10 +0200214 public static Map<String, Object> getUserCredentials () {
215 return new HashMap<>(data);
Michael Hanl19390652016-01-16 11:01:24 +0100216 }
217
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200218 public void close () {
Michael Hanldaf86602016-05-12 14:31:52 +0200219 BeansFactory.closeApplication();
Michael Hanlc4446022016-02-12 18:03:17 +0100220 }
221
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200222
223 private TestHelper () {
Michael Hanldaf86602016-05-12 14:31:52 +0200224
225 }
226
margarethae6f079d2017-10-09 15:57:27 +0200227 // EM: use Spring XML config
228 @Deprecated
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200229 private static PersistenceClient mysql_db () throws IOException {
Michael Hanldaf86602016-05-12 14:31:52 +0200230 BasicDataSource dataSource = new BasicDataSource();
231 dataSource.setDriverClassName("com.mysql.jdbc.Driver");
232 dataSource.setUrl("jdbc:mysql://localhost:3306/kustvakt_test");
233 dataSource.setUsername("mhanl");
234 dataSource.setPassword("password");
235 JDBCClient client = new JDBCClient(dataSource);
236 client.setDatabase("mariadb");
237
238 Flyway fl = new Flyway();
239 fl.setDataSource(dataSource);
240 fl.setLocations("db.mysql");
241 fl.migrate();
242
243 return client;
244 }
245
margarethae6f079d2017-10-09 15:57:27 +0200246 // EM: use Spring XML config
247 @Deprecated
margarethaf68daa62017-09-21 02:11:24 +0200248 protected static PersistenceClient sqlite_db (boolean memory)
Michael Hanldaf86602016-05-12 14:31:52 +0200249 throws InterruptedException {
250 SingleConnectionDataSource dataSource = new SingleConnectionDataSource();
251 dataSource.setDriverClassName("org.sqlite.JDBC");
Michael Hanlc0ed00f2016-06-23 14:33:10 +0200252 DateTime t = TimeUtils.getNow();
Michael Hanl08aa7722016-06-03 13:34:37 +0200253 //String name = testclass != null ? testclass.getSimpleName() + "_" : "";
Michael Hanldaf86602016-05-12 14:31:52 +0200254
255 if (memory)
256 dataSource.setUrl("jdbc:sqlite::memory:");
257 else {
258 File tmp = new File("tmp");
259 if (!tmp.exists())
260 tmp.mkdirs();
Michael Hanl08aa7722016-06-03 13:34:37 +0200261 dataSource.setUrl("jdbc:sqlite:tmp/sqlite_" + t.getMillis()
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200262 + ".sqlite");
Michael Hanldaf86602016-05-12 14:31:52 +0200263 }
264 dataSource.setSuppressClose(true);
265
266 Flyway fl = new Flyway();
267 fl.setDataSource(dataSource);
268 fl.setLocations("db.sqlite");
269 fl.migrate();
270
271 JDBCClient client = new JDBCClient(dataSource);
272 client.setDatabase("sqlite");
273 return client;
274 }
275
margarethae6f079d2017-10-09 15:57:27 +0200276 // EM: use Spring XML config
277 @Deprecated
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200278 public static PersistenceClient sqlite_db_norm (boolean memory) {
Michael Hanldaf86602016-05-12 14:31:52 +0200279 BasicDataSource dataSource = new BasicDataSource();
280 dataSource.setDriverClassName("org.sqlite.JDBC");
281 dataSource.setMaxTotal(1);
282 dataSource.setInitialSize(1);
283 dataSource.setMaxIdle(1);
284 dataSource.addConnectionProperty("lazy-init", "true");
Michael Hanlc0ed00f2016-06-23 14:33:10 +0200285 DateTime t = TimeUtils.getNow();
Michael Hanldaf86602016-05-12 14:31:52 +0200286 if (memory)
287 dataSource.setUrl("jdbc:sqlite::memory:");
288 else {
289 File tmp = new File("tmp");
290 if (!tmp.exists())
291 tmp.mkdirs();
292 dataSource.setUrl("jdbc:sqlite:tmp/sqlite_" + t.toString());
293 }
294
295 Flyway fl = new Flyway();
296 fl.setDataSource(dataSource);
297 fl.setLocations("db.sqlite");
298 fl.migrate();
299
300 JDBCClient client = new JDBCClient(dataSource);
301 client.setDatabase("sqlite");
302 return client;
303 }
304
margarethae6f079d2017-10-09 15:57:27 +0200305 // EM: use Spring XML config
306 @Deprecated
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200307 public static PersistenceClient h2_emb () throws SQLException {
Michael Hanldaf86602016-05-12 14:31:52 +0200308 BasicDataSource dataSource = new BasicDataSource();
309 dataSource.setUrl("jdbc:h2:mem:");
310 dataSource.getConnection().nativeSQL("SET MODE MySQL;");
311 dataSource.getConnection().commit();
312 dataSource.setUsername("sa");
313 dataSource.setPassword("");
314 dataSource.setDriverClassName("org.h2.Driver");
315
316 Flyway fl = new Flyway();
317 fl.setDataSource(dataSource);
318 fl.setLocations("db.mysql");
319 fl.migrate();
320 JDBCClient client = new JDBCClient(dataSource);
321 client.setDatabase("h2");
322 return client;
323 }
324
Michael Hanl19390652016-01-16 11:01:24 +0100325 }