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