Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame^] | 1 | import com.fasterxml.jackson.core.JsonGenerationException; |
| 2 | import com.fasterxml.jackson.databind.JsonMappingException; |
| 3 | import de.ids_mannheim.korap.query.serialize.JsonGenerator; |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 4 | import de.ids_mannheim.korap.query.serialize.MetaCollectionSerializer; |
| 5 | import de.ids_mannheim.korap.query.serialize.MetaQuerySerializer; |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 6 | import org.joda.time.DateTime; |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 7 | import org.junit.Test; |
| 8 | import org.junit.runner.RunWith; |
| 9 | import org.junit.runners.JUnit4; |
| 10 | |
| 11 | import java.io.IOException; |
| 12 | import java.util.HashMap; |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 13 | import java.util.LinkedHashMap; |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 14 | import java.util.Map; |
| 15 | |
| 16 | /** |
| 17 | * @author hanl |
| 18 | * @date 04/12/2013 |
| 19 | */ |
| 20 | |
| 21 | @RunWith(JUnit4.class) |
| 22 | public class MetaQuerySerializationTest { |
| 23 | |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 24 | private MetaQuerySerializer querySerializer; |
| 25 | private MetaCollectionSerializer collSerializer; |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 26 | |
| 27 | public MetaQuerySerializationTest() { |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 28 | querySerializer = new MetaQuerySerializer(); |
| 29 | collSerializer = new MetaCollectionSerializer(); |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | @Test |
| 33 | public void test() throws IOException { |
| 34 | Map<String, String> j = new HashMap(); |
| 35 | j.put("author", "Goethe"); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 36 | j.put("pubPlace", "Erfurt"); |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 37 | j.put("textClass", "wissenschaft"); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 38 | String s = querySerializer.stringify(j, MetaQuerySerializer.TYPE.FILTER); |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 39 | // System.out.println("value reference " + s); |
| 40 | } |
| 41 | |
| 42 | @Test |
| 43 | public void testSingle() throws IOException { |
| 44 | Map<String, String> j = new HashMap(); |
| 45 | j.put("textClass", "wissenschaft"); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 46 | String s = querySerializer.stringify(j, MetaQuerySerializer.TYPE.FILTER); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame^] | 47 | System.out.println("------ TEXT SINGLE " + s); |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Test |
| 51 | public void testResourceMeta() throws IOException { |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 52 | String s = collSerializer.serialize("25"); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame^] | 53 | System.out.println(" --- RESULT JSON " + s); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 54 | } |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 55 | |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 56 | @Test |
| 57 | public void testDates() throws IOException { |
| 58 | Map<String, String> queries = new LinkedHashMap<>(); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame^] | 59 | queries.put("pubDate", String.valueOf(new DateTime().getMillis()) + "~" |
| 60 | + String.valueOf(new DateTime().getMillis() + 2)); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 61 | queries.put("author", "Goethe"); |
| 62 | String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame^] | 63 | System.out.println("value until/since : " + f); |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 64 | } |
Michael Hanl | 08b93ed | 2013-12-05 18:04:45 +0000 | [diff] [blame] | 65 | |
| 66 | @Test |
| 67 | public void testUntil() throws IOException { |
| 68 | Map<String, String> queries = new LinkedHashMap<>(); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame^] | 69 | queries.put("pubDate", ">" + String.valueOf(new DateTime().getMillis())); |
Michael Hanl | 08b93ed | 2013-12-05 18:04:45 +0000 | [diff] [blame] | 70 | queries.put("author", "Hesse"); |
| 71 | String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER); |
| 72 | System.out.println("value until : " + f); |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void testSince() throws IOException { |
| 77 | Map<String, String> queries = new LinkedHashMap<>(); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame^] | 78 | queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis())); |
Michael Hanl | 08b93ed | 2013-12-05 18:04:45 +0000 | [diff] [blame] | 79 | queries.put("author", "Kafka"); |
| 80 | String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER); |
| 81 | System.out.println("value since : " + f); |
| 82 | } |
| 83 | |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame^] | 84 | //@Test |
| 85 | public void testGenerator() { |
| 86 | /* |
| 87 | * just for testing... |
| 88 | */ |
| 89 | JsonGenerator jg = new JsonGenerator(); |
| 90 | int i = 0; |
| 91 | String[] queries; |
| 92 | queries = new String[]{ |
| 93 | "shrink({[base=foo]})", |
| 94 | "shrink({[base=foo]}[orth=bar])", |
| 95 | "shrink(1:[base=Der]{1:[base=Mann]})", |
| 96 | }; |
| 97 | |
| 98 | for (String q : queries) { |
| 99 | i++; |
| 100 | try { |
| 101 | System.out.println(q); |
| 102 | jg.run(q, "poliqarp", System.getProperty("user.home") + "/bsp" + i + ".json"); |
| 103 | System.out.println(); |
| 104 | } catch (NullPointerException npe) { |
| 105 | npe.printStackTrace(); |
| 106 | System.out.println("null\n"); |
| 107 | } catch (JsonGenerationException e) { |
| 108 | e.printStackTrace(); |
| 109 | } catch (JsonMappingException e) { |
| 110 | e.printStackTrace(); |
| 111 | } catch (IOException e) { |
| 112 | e.printStackTrace(); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 117 | } |