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