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; |
Michael Hanl | 53b0fd0 | 2013-12-06 21:07:52 +0000 | [diff] [blame^] | 4 | import de.ids_mannheim.korap.query.serialize.*; |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 5 | import org.joda.time.DateTime; |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 6 | import org.junit.Test; |
| 7 | import org.junit.runner.RunWith; |
| 8 | import org.junit.runners.JUnit4; |
| 9 | |
| 10 | import java.io.IOException; |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame] | 11 | import java.util.*; |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 12 | |
| 13 | /** |
| 14 | * @author hanl |
| 15 | * @date 04/12/2013 |
| 16 | */ |
| 17 | |
| 18 | @RunWith(JUnit4.class) |
| 19 | public class MetaQuerySerializationTest { |
| 20 | |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 21 | private MetaQuerySerializer querySerializer; |
| 22 | private MetaCollectionSerializer collSerializer; |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 23 | private Serializer ser; |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 24 | |
| 25 | public MetaQuerySerializationTest() { |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 26 | querySerializer = new MetaQuerySerializer(); |
| 27 | collSerializer = new MetaCollectionSerializer(); |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 28 | ser = new Serializer(); |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | @Test |
| 32 | public void test() throws IOException { |
| 33 | Map<String, String> j = new HashMap(); |
| 34 | j.put("author", "Goethe"); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 35 | j.put("pubPlace", "Erfurt"); |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 36 | j.put("textClass", "wissenschaft"); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 37 | String s = querySerializer.stringify(j, MetaQuerySerializer.TYPE.FILTER); |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 38 | // System.out.println("value reference " + s); |
| 39 | } |
| 40 | |
| 41 | @Test |
| 42 | public void testSingle() throws IOException { |
| 43 | Map<String, String> j = new HashMap(); |
| 44 | j.put("textClass", "wissenschaft"); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 45 | String s = querySerializer.stringify(j, MetaQuerySerializer.TYPE.FILTER); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 46 | System.out.println("------ TEXT SINGLE " + s); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame] | 47 | System.out.println(); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 48 | } |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 49 | |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 50 | @Test |
| 51 | public void testDates() throws IOException { |
| 52 | Map<String, String> queries = new LinkedHashMap<>(); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 53 | queries.put("pubDate", String.valueOf(new DateTime().getMillis()) + "~" |
| 54 | + String.valueOf(new DateTime().getMillis() + 2)); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 55 | queries.put("author", "Goethe"); |
| 56 | String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 57 | System.out.println("value until/since : " + f); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame] | 58 | System.out.println(); |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 59 | } |
Michael Hanl | 08b93ed | 2013-12-05 18:04:45 +0000 | [diff] [blame] | 60 | |
| 61 | @Test |
| 62 | public void testUntil() throws IOException { |
| 63 | Map<String, String> queries = new LinkedHashMap<>(); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 64 | queries.put("pubDate", ">" + String.valueOf(new DateTime().getMillis())); |
Michael Hanl | 08b93ed | 2013-12-05 18:04:45 +0000 | [diff] [blame] | 65 | queries.put("author", "Hesse"); |
| 66 | String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER); |
| 67 | System.out.println("value until : " + f); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame] | 68 | System.out.println(); |
Michael Hanl | 08b93ed | 2013-12-05 18:04:45 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void testSince() throws IOException { |
| 73 | Map<String, String> queries = new LinkedHashMap<>(); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 74 | queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis())); |
Michael Hanl | 08b93ed | 2013-12-05 18:04:45 +0000 | [diff] [blame] | 75 | queries.put("author", "Kafka"); |
| 76 | String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER); |
| 77 | System.out.println("value since : " + f); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame] | 78 | System.out.println(); |
Michael Hanl | 08b93ed | 2013-12-05 18:04:45 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Michael Hanl | 53b0fd0 | 2013-12-06 21:07:52 +0000 | [diff] [blame^] | 81 | @Test |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 82 | public void testGenerator() { |
| 83 | /* |
| 84 | * just for testing... |
| 85 | */ |
| 86 | JsonGenerator jg = new JsonGenerator(); |
| 87 | int i = 0; |
| 88 | String[] queries; |
| 89 | queries = new String[]{ |
| 90 | "shrink({[base=foo]})", |
| 91 | "shrink({[base=foo]}[orth=bar])", |
| 92 | "shrink(1:[base=Der]{1:[base=Mann]})", |
| 93 | }; |
| 94 | |
| 95 | for (String q : queries) { |
| 96 | i++; |
| 97 | try { |
| 98 | System.out.println(q); |
| 99 | jg.run(q, "poliqarp", System.getProperty("user.home") + "/bsp" + i + ".json"); |
| 100 | System.out.println(); |
| 101 | } catch (NullPointerException npe) { |
| 102 | npe.printStackTrace(); |
| 103 | System.out.println("null\n"); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame] | 104 | System.out.println(); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 105 | } catch (JsonGenerationException e) { |
| 106 | e.printStackTrace(); |
| 107 | } catch (JsonMappingException e) { |
| 108 | e.printStackTrace(); |
| 109 | } catch (IOException e) { |
| 110 | e.printStackTrace(); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 115 | @Test |
| 116 | public void testLists() { |
| 117 | Map<String, String> queries = new LinkedHashMap<>(); |
| 118 | queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis())); |
| 119 | queries.put("author", "Kafka"); |
| 120 | List s = querySerializer.serializeQueries(queries, MetaQuerySerializer.TYPE.FILTER); |
| 121 | |
| 122 | queries.clear(); |
| 123 | queries.put("author", "Hesse"); |
| 124 | |
| 125 | List f = querySerializer.serializeQueries(queries, MetaQuerySerializer.TYPE.EXTEND); |
| 126 | s.addAll(f); |
| 127 | System.out.println("--- ALL " + s); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame] | 128 | System.out.println(); |
| 129 | |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Test |
| 133 | public void testJSONArray() throws JsonProcessingException { |
| 134 | Map<String, String> queries = new LinkedHashMap<>(); |
| 135 | queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis())); |
| 136 | queries.put("author", "Kafka"); |
| 137 | List s = querySerializer.serializeQueries(queries, MetaQuerySerializer.TYPE.FILTER); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame] | 138 | System.out.println("array repres " + ser.serializeToMeta(s)); |
| 139 | System.out.println(); |
| 140 | } |
| 141 | |
| 142 | @Test |
| 143 | public void testCollections() throws IOException { |
| 144 | Map<String, String> query = new LinkedHashMap<>(); |
| 145 | Serializer ser = new Serializer(); |
Michael Hanl | 53b0fd0 | 2013-12-06 21:07:52 +0000 | [diff] [blame^] | 146 | query.put("corpusID", "A00"); |
| 147 | List<Map> l = ser.serializeQueries(query, MetaQuerySerializer.TYPE.FILTER); |
| 148 | query.clear(); |
| 149 | query.put("corpusID", "A01"); |
| 150 | List<Map> u = ser.serializeQueries(query, MetaQuerySerializer.TYPE.EXTEND); |
| 151 | l.addAll(u); |
| 152 | String val = ser.stringify(l); |
| 153 | System.out.println("results " + val); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame] | 154 | System.out.println(); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame] | 155 | |
Michael Hanl | 53b0fd0 | 2013-12-06 21:07:52 +0000 | [diff] [blame^] | 156 | |
| 157 | String meta = ser.queryToMeta(val); |
| 158 | |
| 159 | System.out.println("meta query " + meta); |
| 160 | // List<String> list = new ArrayList<>(); |
| 161 | // list.add(val); |
| 162 | // List s = collSerializer.serializeResource(list); |
| 163 | System.out.println("printable list "); |
| 164 | System.out.println(); |
| 165 | |
| 166 | } |
| 167 | |
| 168 | @Test |
| 169 | public void testResources() throws IOException { |
| 170 | String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]"; |
| 171 | List s = new ArrayList(); |
| 172 | s.add(meta); |
| 173 | List fin = ser.serializeResources(s); |
| 174 | |
| 175 | String string = ser.serializeToMeta(fin); |
| 176 | System.out.println("meta string " + string); |
| 177 | |
| 178 | } |
| 179 | |
| 180 | @Test |
| 181 | public void testA00() throws IOException { |
| 182 | Map<String, String> query = new LinkedHashMap<>(); |
| 183 | Serializer ser = new Serializer(); |
| 184 | query.put("corpusID", "A00"); |
| 185 | List<Map> l = ser.serializeQueries(query, MetaQuerySerializer.TYPE.FILTER); |
| 186 | query.clear(); |
| 187 | query.put("corpusID", "A01"); |
| 188 | List<Map> u = ser.serializeQueries(query, MetaQuerySerializer.TYPE.EXTEND); |
| 189 | l.addAll(u); |
| 190 | String val = ser.stringify(l); |
| 191 | List s = new ArrayList(); |
| 192 | s.add(val); |
| 193 | |
| 194 | List fin = ser.serializeResources(s); |
| 195 | System.out.println("A00 meta: " + fin); |
| 196 | |
| 197 | } |
| 198 | |
| 199 | |
| 200 | @Test |
| 201 | public void testnewMetaQuery() throws IOException { |
| 202 | Map<String, String> qu = new LinkedHashMap<>(); |
| 203 | Serializer ser = new Serializer(); |
| 204 | qu.put("corpusID", "A00"); |
| 205 | List<Map> l = ser.serializeQueries(qu, MetaQuerySerializer.TYPE.FILTER); |
| 206 | qu.clear(); |
| 207 | qu.put("corpusID", "A01"); |
| 208 | List<Map> u = ser.serializeQueries(qu, MetaQuerySerializer.TYPE.EXTEND); |
| 209 | l.addAll(u); |
| 210 | String val = ser.stringify(l); |
| 211 | List s = new ArrayList(); |
| 212 | s.add(val); |
| 213 | |
| 214 | List fin = ser.serializeResources(s); |
| 215 | |
| 216 | |
| 217 | System.out.println("======= METAQUERY TESTING BEGIN ======="); |
| 218 | |
| 219 | MetaQuery query = new MetaQuery(); |
| 220 | query.addMetaFilter("textClass", "wissenschaft"); |
| 221 | query.addResources(s); |
| 222 | System.out.println("query "+ query.toMeta()); |
| 223 | System.out.println("string "+ query.stringify()); |
| 224 | System.out.println("======= METAQUERY TESTING END ======="); |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 227 | } |