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