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; |
| 4 | import de.ids_mannheim.korap.query.serialize.JsonGenerator; |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 5 | import de.ids_mannheim.korap.query.serialize.MetaCollectionSerializer; |
| 6 | import de.ids_mannheim.korap.query.serialize.MetaQuerySerializer; |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 7 | import de.ids_mannheim.korap.query.serialize.Serializer; |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 8 | import org.joda.time.DateTime; |
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; |
| 14 | import java.util.HashMap; |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 15 | import java.util.LinkedHashMap; |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 16 | import java.util.List; |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 17 | import java.util.Map; |
| 18 | |
| 19 | /** |
| 20 | * @author hanl |
| 21 | * @date 04/12/2013 |
| 22 | */ |
| 23 | |
| 24 | @RunWith(JUnit4.class) |
| 25 | public class MetaQuerySerializationTest { |
| 26 | |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 27 | private MetaQuerySerializer querySerializer; |
| 28 | private MetaCollectionSerializer collSerializer; |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 29 | private Serializer ser; |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 30 | |
| 31 | public MetaQuerySerializationTest() { |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 32 | querySerializer = new MetaQuerySerializer(); |
| 33 | collSerializer = new MetaCollectionSerializer(); |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 34 | ser = new Serializer(); |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | @Test |
| 38 | public void test() throws IOException { |
| 39 | Map<String, String> j = new HashMap(); |
| 40 | j.put("author", "Goethe"); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 41 | j.put("pubPlace", "Erfurt"); |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 42 | j.put("textClass", "wissenschaft"); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 43 | String s = querySerializer.stringify(j, MetaQuerySerializer.TYPE.FILTER); |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 44 | // System.out.println("value reference " + s); |
| 45 | } |
| 46 | |
| 47 | @Test |
| 48 | public void testSingle() throws IOException { |
| 49 | Map<String, String> j = new HashMap(); |
| 50 | j.put("textClass", "wissenschaft"); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 51 | String s = querySerializer.stringify(j, MetaQuerySerializer.TYPE.FILTER); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 52 | System.out.println("------ TEXT SINGLE " + s); |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void testResourceMeta() throws IOException { |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 57 | String s = collSerializer.serialize("25"); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 58 | System.out.println(" --- RESULT JSON " + s); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 59 | } |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 60 | |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 61 | @Test |
| 62 | public void testDates() throws IOException { |
| 63 | Map<String, String> queries = new LinkedHashMap<>(); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 64 | queries.put("pubDate", String.valueOf(new DateTime().getMillis()) + "~" |
| 65 | + String.valueOf(new DateTime().getMillis() + 2)); |
Michael Hanl | 296a031 | 2013-12-05 17:27:51 +0000 | [diff] [blame] | 66 | queries.put("author", "Goethe"); |
| 67 | String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 68 | System.out.println("value until/since : " + f); |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 69 | } |
Michael Hanl | 08b93ed | 2013-12-05 18:04:45 +0000 | [diff] [blame] | 70 | |
| 71 | @Test |
| 72 | public void testUntil() throws IOException { |
| 73 | Map<String, String> queries = new LinkedHashMap<>(); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 74 | queries.put("pubDate", ">" + String.valueOf(new DateTime().getMillis())); |
Michael Hanl | 08b93ed | 2013-12-05 18:04:45 +0000 | [diff] [blame] | 75 | queries.put("author", "Hesse"); |
| 76 | String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER); |
| 77 | System.out.println("value until : " + f); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void testSince() throws IOException { |
| 82 | Map<String, String> queries = new LinkedHashMap<>(); |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 83 | queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis())); |
Michael Hanl | 08b93ed | 2013-12-05 18:04:45 +0000 | [diff] [blame] | 84 | queries.put("author", "Kafka"); |
| 85 | String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER); |
| 86 | System.out.println("value since : " + f); |
| 87 | } |
| 88 | |
Michael Hanl | ababa39 | 2013-12-05 21:57:37 +0000 | [diff] [blame] | 89 | //@Test |
| 90 | public void testGenerator() { |
| 91 | /* |
| 92 | * just for testing... |
| 93 | */ |
| 94 | JsonGenerator jg = new JsonGenerator(); |
| 95 | int i = 0; |
| 96 | String[] queries; |
| 97 | queries = new String[]{ |
| 98 | "shrink({[base=foo]})", |
| 99 | "shrink({[base=foo]}[orth=bar])", |
| 100 | "shrink(1:[base=Der]{1:[base=Mann]})", |
| 101 | }; |
| 102 | |
| 103 | for (String q : queries) { |
| 104 | i++; |
| 105 | try { |
| 106 | System.out.println(q); |
| 107 | jg.run(q, "poliqarp", System.getProperty("user.home") + "/bsp" + i + ".json"); |
| 108 | System.out.println(); |
| 109 | } catch (NullPointerException npe) { |
| 110 | npe.printStackTrace(); |
| 111 | System.out.println("null\n"); |
| 112 | } catch (JsonGenerationException e) { |
| 113 | e.printStackTrace(); |
| 114 | } catch (JsonMappingException e) { |
| 115 | e.printStackTrace(); |
| 116 | } catch (IOException e) { |
| 117 | e.printStackTrace(); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 122 | @Test |
| 123 | public void testLists() { |
| 124 | Map<String, String> queries = new LinkedHashMap<>(); |
| 125 | queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis())); |
| 126 | queries.put("author", "Kafka"); |
| 127 | List s = querySerializer.serializeQueries(queries, MetaQuerySerializer.TYPE.FILTER); |
| 128 | |
| 129 | queries.clear(); |
| 130 | queries.put("author", "Hesse"); |
| 131 | |
| 132 | List f = querySerializer.serializeQueries(queries, MetaQuerySerializer.TYPE.EXTEND); |
| 133 | s.addAll(f); |
| 134 | System.out.println("--- ALL " + s); |
| 135 | } |
| 136 | |
| 137 | @Test |
| 138 | public void testJSONArray() throws JsonProcessingException { |
| 139 | Map<String, String> queries = new LinkedHashMap<>(); |
| 140 | queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis())); |
| 141 | queries.put("author", "Kafka"); |
| 142 | List s = querySerializer.serializeQueries(queries, MetaQuerySerializer.TYPE.FILTER); |
| 143 | System.out.println("array repres " + ser.serializeMeta(s)); |
| 144 | } |
| 145 | |
Michael Hanl | 55191d9 | 2013-12-05 11:53:10 +0000 | [diff] [blame] | 146 | } |