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