Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 1 | import com.fasterxml.jackson.core.JsonGenerationException; |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 2 | import com.fasterxml.jackson.core.JsonProcessingException; |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 3 | import com.fasterxml.jackson.databind.JsonMappingException; |
| 4 | import de.ids_mannheim.korap.query.serialize.JsonGenerator; |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 5 | import de.ids_mannheim.korap.query.serialize.MetaCollectionSerializer; |
| 6 | import de.ids_mannheim.korap.query.serialize.MetaQuerySerializer; |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 7 | import de.ids_mannheim.korap.query.serialize.Serializer; |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 8 | import org.joda.time.DateTime; |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 9 | import org.junit.Test; |
| 10 | import org.junit.runner.RunWith; |
| 11 | import org.junit.runners.JUnit4; |
| 12 | |
| 13 | import java.io.IOException; |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame^] | 14 | import java.util.*; |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 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 | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 26 | private Serializer ser; |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 27 | |
| 28 | public MetaQuerySerializationTest() { |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 29 | querySerializer = new MetaQuerySerializer(); |
| 30 | collSerializer = new MetaCollectionSerializer(); |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 31 | ser = new Serializer(); |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | @Test |
| 35 | public void test() throws IOException { |
| 36 | Map<String, String> j = new HashMap(); |
| 37 | j.put("author", "Goethe"); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 38 | j.put("pubPlace", "Erfurt"); |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 39 | j.put("textClass", "wissenschaft"); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 40 | String s = querySerializer.stringify(j, MetaQuerySerializer.TYPE.FILTER); |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 41 | // System.out.println("value reference " + s); |
| 42 | } |
| 43 | |
| 44 | @Test |
| 45 | public void testSingle() throws IOException { |
| 46 | Map<String, String> j = new HashMap(); |
| 47 | j.put("textClass", "wissenschaft"); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 48 | String s = querySerializer.stringify(j, MetaQuerySerializer.TYPE.FILTER); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 49 | System.out.println("------ TEXT SINGLE " + s); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame^] | 50 | System.out.println(); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 51 | } |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 52 | |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 53 | @Test |
| 54 | public void testDates() throws IOException { |
| 55 | Map<String, String> queries = new LinkedHashMap<>(); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 56 | queries.put("pubDate", String.valueOf(new DateTime().getMillis()) + "~" |
| 57 | + String.valueOf(new DateTime().getMillis() + 2)); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 58 | queries.put("author", "Goethe"); |
| 59 | String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 60 | System.out.println("value until/since : " + f); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame^] | 61 | System.out.println(); |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 62 | } |
Michael Hanl | 08b93ed | 2013-12-05 18:04:45 +0000 | [diff] [blame] | 63 | |
| 64 | @Test |
| 65 | public void testUntil() throws IOException { |
| 66 | Map<String, String> queries = new LinkedHashMap<>(); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 67 | queries.put("pubDate", ">" + String.valueOf(new DateTime().getMillis())); |
Michael Hanl | 08b93ed | 2013-12-05 18:04:45 +0000 | [diff] [blame] | 68 | queries.put("author", "Hesse"); |
| 69 | String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER); |
| 70 | System.out.println("value until : " + f); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame^] | 71 | System.out.println(); |
Michael Hanl | 08b93ed | 2013-12-05 18:04:45 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void testSince() throws IOException { |
| 76 | Map<String, String> queries = new LinkedHashMap<>(); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 77 | queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis())); |
Michael Hanl | 08b93ed | 2013-12-05 18:04:45 +0000 | [diff] [blame] | 78 | queries.put("author", "Kafka"); |
| 79 | String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER); |
| 80 | System.out.println("value since : " + f); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame^] | 81 | System.out.println(); |
Michael Hanl | 08b93ed | 2013-12-05 18:04:45 +0000 | [diff] [blame] | 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"); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame^] | 107 | System.out.println(); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 108 | } catch (JsonGenerationException e) { |
| 109 | e.printStackTrace(); |
| 110 | } catch (JsonMappingException e) { |
| 111 | e.printStackTrace(); |
| 112 | } catch (IOException e) { |
| 113 | e.printStackTrace(); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 118 | @Test |
| 119 | public void testLists() { |
| 120 | Map<String, String> queries = new LinkedHashMap<>(); |
| 121 | queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis())); |
| 122 | queries.put("author", "Kafka"); |
| 123 | List s = querySerializer.serializeQueries(queries, MetaQuerySerializer.TYPE.FILTER); |
| 124 | |
| 125 | queries.clear(); |
| 126 | queries.put("author", "Hesse"); |
| 127 | |
| 128 | List f = querySerializer.serializeQueries(queries, MetaQuerySerializer.TYPE.EXTEND); |
| 129 | s.addAll(f); |
| 130 | System.out.println("--- ALL " + s); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame^] | 131 | System.out.println(); |
| 132 | |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Test |
| 136 | public void testJSONArray() throws JsonProcessingException { |
| 137 | Map<String, String> queries = new LinkedHashMap<>(); |
| 138 | queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis())); |
| 139 | queries.put("author", "Kafka"); |
| 140 | List s = querySerializer.serializeQueries(queries, MetaQuerySerializer.TYPE.FILTER); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame^] | 141 | System.out.println("array repres " + ser.serializeToMeta(s)); |
| 142 | System.out.println(); |
| 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void testCollections() throws IOException { |
| 147 | Map<String, String> query = new LinkedHashMap<>(); |
| 148 | Serializer ser = new Serializer(); |
| 149 | query.put("corpusID", "WPD"); |
| 150 | |
| 151 | List<Map> p = ser.serializeQueries(query, MetaQuerySerializer.TYPE.FILTER); |
| 152 | String s = ser.stringify(p); |
| 153 | |
| 154 | System.out.println("results " + s); |
| 155 | System.out.println(); |
| 156 | List<String> l = new ArrayList<>(); |
| 157 | l.add(s); |
| 158 | collSerializer.serializeResource(l); |
| 159 | |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 162 | } |