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