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 | 942fa02 | 2014-02-24 19:48:57 +0000 | [diff] [blame^] | 4 | import de.ids_mannheim.korap.query.serialize.CollectionQuery; |
Michael Hanl | 4fe41cc | 2013-12-10 17:59:51 +0000 | [diff] [blame] | 5 | import de.ids_mannheim.korap.query.serialize.QuerySerializer; |
Michael Hanl | 698da8c | 2013-12-08 21:12:36 +0000 | [diff] [blame] | 6 | import de.ids_mannheim.korap.query.serialize.MetaQuery; |
Joachim Bingel | 16da4e1 | 2013-12-17 09:48:12 +0000 | [diff] [blame] | 7 | import de.ids_mannheim.korap.util.QueryException; |
| 8 | |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 9 | import org.joda.time.DateTime; |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 10 | import org.junit.Assert; |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 11 | import org.junit.Test; |
| 12 | import org.junit.runner.RunWith; |
| 13 | import org.junit.runners.JUnit4; |
| 14 | |
| 15 | import java.io.IOException; |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * @author hanl |
| 19 | * @date 04/12/2013 |
| 20 | */ |
| 21 | |
| 22 | @RunWith(JUnit4.class) |
| 23 | public class MetaQuerySerializationTest { |
| 24 | |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 25 | |
| 26 | @Test |
| 27 | public void test() throws IOException { |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 28 | StringBuffer b = new StringBuffer(); |
| 29 | b.append("author:Goethe"); |
| 30 | b.append(" AND "); |
| 31 | b.append("pubPlace:Erfurt"); |
| 32 | b.append(" AND "); |
| 33 | b.append("textClass:wissenschaft"); |
Michael Hanl | 942fa02 | 2014-02-24 19:48:57 +0000 | [diff] [blame^] | 34 | CollectionQuery qu = new CollectionQuery().addMetaFilter(b.toString()); |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 35 | // System.out.println("value reference " + qu.stringify()); |
| 36 | // System.out.println(); |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | @Test |
| 40 | public void testSingle() throws IOException { |
Michael Hanl | 942fa02 | 2014-02-24 19:48:57 +0000 | [diff] [blame^] | 41 | CollectionQuery query = new CollectionQuery().addMetaFilter("textClass", "wissenschaft"); |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 42 | // System.out.println("------ TEXT SINGLE " + query.stringify()); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame] | 43 | System.out.println(); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 44 | } |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 45 | |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 46 | @Test |
| 47 | public void testDates() throws IOException { |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 48 | StringBuffer b = new StringBuffer(); |
Michael Hanl | 4fe1346 | 2013-12-11 18:03:42 +0000 | [diff] [blame] | 49 | b.append("pubDate:>2013-04-01"); |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 50 | b.append(" AND "); |
Michael Hanl | 4fe1346 | 2013-12-11 18:03:42 +0000 | [diff] [blame] | 51 | b.append("pubDate:<2012-04-01"); |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 52 | b.append(" AND "); |
| 53 | b.append("author:Goethe"); |
Michael Hanl | 942fa02 | 2014-02-24 19:48:57 +0000 | [diff] [blame^] | 54 | CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString()); |
Michael Hanl | 4fe1346 | 2013-12-11 18:03:42 +0000 | [diff] [blame] | 55 | System.out.println("value until/since : " + query.stringify()); |
Michael Hanl | 942fa02 | 2014-02-24 19:48:57 +0000 | [diff] [blame^] | 56 | System.out.println("meta value until/since " + query.toCollections()); |
Michael Hanl | 4fe1346 | 2013-12-11 18:03:42 +0000 | [diff] [blame] | 57 | Assert.assertEquals("[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:group\",\"relation\":\"and\",\"operands\":[{\"@type\":\"korap:term\",\"@field\":\"korap:field#author\",\"@value\":\"Goethe\"},{\"@type\":\"korap:group\",\"@field\":\"korap:field#pubDate\",\"relation\":\"between\",\"operands\":[{\"@type\":\"korap:date\",\"@value\":\"2012-04-01\"},{\"@type\":\"korap:date\",\"@value\":\"2013-04-01\"}]}]}}]", query.stringify()); |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +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 { |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 63 | StringBuffer b = new StringBuffer(); |
Michael Hanl | 4fe1346 | 2013-12-11 18:03:42 +0000 | [diff] [blame] | 64 | b.append("pubDate:>2013-12-10"); |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 65 | b.append(" AND "); |
| 66 | b.append("author:Hesse"); |
Michael Hanl | 942fa02 | 2014-02-24 19:48:57 +0000 | [diff] [blame^] | 67 | CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString()); |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 68 | System.out.println("Running date check (until) with additional attribute author"); |
Joachim Bingel | 94a1ccd | 2013-12-10 10:37:29 +0000 | [diff] [blame] | 69 | Assert.assertEquals("[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:group\",\"relation\":\"and\",\"operands\":[{\"@type\":\"korap:term\",\"@field\":\"korap:field#author\",\"@value\":\"Hesse\"},{\"@type\":\"korap:group\",\"@field\":\"korap:field#pubDate\",\"relation\":\"until\",\"operands\":[{\"@type\":\"korap:date\",\"@value\":\"2013-12-10\"}]}]}}]", query.stringify()); |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 70 | // System.out.println("value until : " + query.stringify()); |
| 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 { |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 76 | StringBuffer b = new StringBuffer(); |
Michael Hanl | 4fe1346 | 2013-12-11 18:03:42 +0000 | [diff] [blame] | 77 | b.append("pubDate:<2013-12-10"); |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 78 | b.append(" AND "); |
| 79 | b.append("author:Kafka"); |
Michael Hanl | 942fa02 | 2014-02-24 19:48:57 +0000 | [diff] [blame^] | 80 | CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString()); |
Michael Hanl | 4fe1346 | 2013-12-11 18:03:42 +0000 | [diff] [blame] | 81 | System.out.println("value since : " + query.stringify()); |
Michael Hanl | 942fa02 | 2014-02-24 19:48:57 +0000 | [diff] [blame^] | 82 | System.out.println("meta value since " + query.toCollections()); |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 83 | // System.out.println(); |
| 84 | System.out.println("Running date check (since) with additional attribute author"); |
Joachim Bingel | 94a1ccd | 2013-12-10 10:37:29 +0000 | [diff] [blame] | 85 | Assert.assertEquals("[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:group\",\"relation\":\"and\",\"operands\":[{\"@type\":\"korap:term\",\"@field\":\"korap:field#author\",\"@value\":\"Kafka\"},{\"@type\":\"korap:group\",\"@field\":\"korap:field#pubDate\",\"relation\":\"since\",\"operands\":[{\"@type\":\"korap:date\",\"@value\":\"2013-12-10\"}]}]}}]", query.stringify()); |
Michael Hanl | 08b93ed | 2013-12-05 18:04:45 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Michael Hanl | 53b0fd0 | 2013-12-06 21:07:52 +0000 | [diff] [blame] | 88 | @Test |
Joachim Bingel | 16da4e1 | 2013-12-17 09:48:12 +0000 | [diff] [blame] | 89 | public void testGenerator() throws QueryException { |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 90 | /* |
| 91 | * just for testing... |
| 92 | */ |
Michael Hanl | 4fe41cc | 2013-12-10 17:59:51 +0000 | [diff] [blame] | 93 | QuerySerializer jg = new QuerySerializer(); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 94 | int i = 0; |
| 95 | String[] queries; |
| 96 | queries = new String[]{ |
| 97 | "shrink({[base=foo]})", |
| 98 | "shrink({[base=foo]}[orth=bar])", |
| 99 | "shrink(1:[base=Der]{1:[base=Mann]})", |
| 100 | }; |
| 101 | |
| 102 | for (String q : queries) { |
| 103 | i++; |
| 104 | try { |
| 105 | System.out.println(q); |
| 106 | jg.run(q, "poliqarp", System.getProperty("user.home") + "/bsp" + i + ".json"); |
| 107 | System.out.println(); |
| 108 | } catch (NullPointerException npe) { |
| 109 | npe.printStackTrace(); |
| 110 | System.out.println("null\n"); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame] | 111 | System.out.println(); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 112 | } catch (JsonGenerationException e) { |
| 113 | e.printStackTrace(); |
| 114 | } catch (JsonMappingException e) { |
| 115 | e.printStackTrace(); |
| 116 | } catch (IOException e) { |
| 117 | e.printStackTrace(); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 122 | @Test |
| 123 | public void testLists() { |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 124 | StringBuffer b = new StringBuffer(); |
Michael Hanl | 4fe1346 | 2013-12-11 18:03:42 +0000 | [diff] [blame] | 125 | b.append("pubDate:<2013-12-10"); |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 126 | b.append(" AND "); |
| 127 | b.append("author:Kafka"); |
Michael Hanl | 942fa02 | 2014-02-24 19:48:57 +0000 | [diff] [blame^] | 128 | CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString()); |
Michael Hanl | 698da8c | 2013-12-08 21:12:36 +0000 | [diff] [blame] | 129 | query.addMetaExtend("author", "Hesse"); |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 130 | |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 131 | // System.out.println("--- ALL " + query.stringify()); |
| 132 | // System.out.println(); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame] | 133 | |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Test |
| 137 | public void testJSONArray() throws JsonProcessingException { |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 138 | StringBuffer b = new StringBuffer(); |
Michael Hanl | 4fe1346 | 2013-12-11 18:03:42 +0000 | [diff] [blame] | 139 | b.append("pubDate:<2013-12-10"); |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 140 | b.append(" AND "); |
| 141 | b.append("author:Kafka"); |
Michael Hanl | 942fa02 | 2014-02-24 19:48:57 +0000 | [diff] [blame^] | 142 | CollectionQuery q = new CollectionQuery().addMetaExtend(b.toString()); |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 143 | // System.out.println("array repres " + q.toMeta()); |
| 144 | // System.out.println(); |
Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Test |
| 148 | public void testCollections() throws IOException { |
Michael Hanl | 942fa02 | 2014-02-24 19:48:57 +0000 | [diff] [blame^] | 149 | CollectionQuery q = new CollectionQuery().addMetaFilter("corpusID", "A00"); |
Michael Hanl | 698da8c | 2013-12-08 21:12:36 +0000 | [diff] [blame] | 150 | q.addMetaExtend("corpusID", "A01"); |
| 151 | |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 152 | // System.out.println("results stringified " + q.stringify()); |
| 153 | // System.out.println("results to meta" + q.toMeta()); |
| 154 | // System.out.println(); |
Michael Hanl | 53b0fd0 | 2013-12-06 21:07:52 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Michael Hanl | 698da8c | 2013-12-08 21:12:36 +0000 | [diff] [blame] | 157 | /** |
| 158 | * asserts equality. input should be equal to output, |
| 159 | * since there is no other metadata added to the meta query |
| 160 | * |
| 161 | * @throws IOException |
| 162 | */ |
Michael Hanl | 53b0fd0 | 2013-12-06 21:07:52 +0000 | [diff] [blame] | 163 | @Test |
| 164 | public void testResources() throws IOException { |
| 165 | String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]"; |
Michael Hanl | 942fa02 | 2014-02-24 19:48:57 +0000 | [diff] [blame^] | 166 | CollectionQuery q = new CollectionQuery().addResource(meta); |
Michael Hanl | 698da8c | 2013-12-08 21:12:36 +0000 | [diff] [blame] | 167 | org.junit.Assert.assertEquals("String should be empty", "", q.stringify()); |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 168 | // System.out.println("meta string " + q.toMeta()); |
| 169 | System.out.println("Testing Resource Meta data"); |
Michael Hanl | 942fa02 | 2014-02-24 19:48:57 +0000 | [diff] [blame^] | 170 | org.junit.Assert.assertEquals("{\"meta\":" + meta + "}", q.toCollections()); |
Michael Hanl | 53b0fd0 | 2013-12-06 21:07:52 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | @Test |
| 174 | public void testA00() throws IOException { |
Michael Hanl | 942fa02 | 2014-02-24 19:48:57 +0000 | [diff] [blame^] | 175 | CollectionQuery q = new CollectionQuery().addMetaExtend("corpusID", "A00").addMetaExtend("corpusID", "A01"); |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 176 | // System.out.println("A meta: " + q.stringify()); |
| 177 | // System.out.println(); |
Michael Hanl | 53b0fd0 | 2013-12-06 21:07:52 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Michael Hanl | 53b0fd0 | 2013-12-06 21:07:52 +0000 | [diff] [blame] | 180 | @Test |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 181 | public void testResources2() throws IOException { |
Michael Hanl | 698da8c | 2013-12-08 21:12:36 +0000 | [diff] [blame] | 182 | String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]"; |
Michael Hanl | 942fa02 | 2014-02-24 19:48:57 +0000 | [diff] [blame^] | 183 | CollectionQuery q = new CollectionQuery().addResource(meta); |
Michael Hanl | 698da8c | 2013-12-08 21:12:36 +0000 | [diff] [blame] | 184 | q.addMetaFilter("textClass", "wissenschaft"); |
Michael Hanl | b64fdce | 2013-12-08 23:17:19 +0000 | [diff] [blame] | 185 | // System.out.println("stringified meta " + q.stringify()); |
| 186 | // System.out.println("meta string " + q.toMeta()); |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 189 | } |