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