blob: c1c88b11085bdf952759229886da8c21ba97e241 [file] [log] [blame]
Michael Hanl66ca71c2015-09-14 23:11:30 +02001import com.fasterxml.jackson.databind.JsonNode;
2import de.ids_mannheim.korap.config.BeanConfiguration;
3import de.ids_mannheim.korap.config.KustvaktConfiguration;
4import de.ids_mannheim.korap.query.serialize.QuerySerializer;
5import de.ids_mannheim.korap.resource.rewrite.CollectionCleanupFilter;
6import de.ids_mannheim.korap.resource.rewrite.CollectionConstraint;
7import de.ids_mannheim.korap.resource.rewrite.RewriteHandler;
8import de.ids_mannheim.korap.utils.JsonUtils;
9import org.junit.BeforeClass;
Michael Hanl66ca71c2015-09-14 23:11:30 +020010
Michael Hanlfbd6fbb2015-09-14 16:10:12 +020011/**
12 * @author hanl
13 * @date 03/09/2015
14 */
Michael Hanl59bff812015-10-27 23:10:32 +010015// todo: 20.10.15
Michael Hanlfbd6fbb2015-09-14 16:10:12 +020016public class CollectionRewriteTest {
Michael Hanl66ca71c2015-09-14 23:11:30 +020017
18 private static String simple_add_query = "[pos=ADJA]";
19
20 private static KustvaktConfiguration config;
21
22 @BeforeClass
23 public static void init() {
24 BeanConfiguration.loadClasspathContext();
25 config = BeanConfiguration.getBeans().getConfiguration();
26 }
27
Michael Hanl59bff812015-10-27 23:10:32 +010028 // @Test
Michael Hanl66ca71c2015-09-14 23:11:30 +020029 public void testCollectionNodeRemoveCorpusIdNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +010030 RewriteHandler handler = new RewriteHandler(config);
31 handler.add(CollectionConstraint.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +020032 QuerySerializer s = new QuerySerializer();
33 s.setQuery(simple_add_query, "poliqarp");
34 s.setCollection("textClass=politik & corpusID=WPD");
35 String result = s.toJSON();
36 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
37 assert node != null;
38 assert node.at("/collection/operands").size() == 1;
39 }
40
Michael Hanlf21773f2015-10-16 23:02:31 +020041 //@Test
Michael Hanl66ca71c2015-09-14 23:11:30 +020042 public void testCollectionNodeRemoveAllCorpusIdNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +010043 RewriteHandler handler = new RewriteHandler(config);
44 handler.add(CollectionConstraint.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +020045 QuerySerializer s = new QuerySerializer();
46 s.setQuery(simple_add_query, "poliqarp");
47 s.setCollection("corpusID=BRZ13 & corpusID=WPD");
48 String result = s.toJSON();
49 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
50
51 assert node != null;
52 assert node.at("/collection/operands").size() == 0;
53 }
54
Michael Hanlf21773f2015-10-16 23:02:31 +020055 //@Test
Michael Hanl66ca71c2015-09-14 23:11:30 +020056 public void testCollectionNodeRemoveGroupedCorpusIdNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +010057 RewriteHandler handler = new RewriteHandler(config);
58 handler.add(CollectionConstraint.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +020059 QuerySerializer s = new QuerySerializer();
60 s.setQuery(simple_add_query, "poliqarp");
61 s.setCollection(
62 "(corpusID=BRZ13 & textClass=Wissenschaft) & corpusID=WPD");
63 String result = s.toJSON();
64 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
65
66 assert node != null;
67 assert node.at("/collection/operands/0/@type").asText()
68 .equals("koral:docGroup");
69 assert node.at("/collection/operands/0/operands/0/key").asText()
70 .equals("textClass");
71 }
72
73 //fixme: will probably fail when one doc groups are being refactored
Michael Hanlf21773f2015-10-16 23:02:31 +020074 //@Test
Michael Hanl66ca71c2015-09-14 23:11:30 +020075 public void testCollectionCleanEmptyDocGroupNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +010076 RewriteHandler handler = new RewriteHandler(config);
77 handler.add(CollectionConstraint.class);
78 handler.add(CollectionCleanupFilter.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +020079 QuerySerializer s = new QuerySerializer();
80 s.setQuery(simple_add_query, "poliqarp");
81 s.setCollection(
82 "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft & textClass=Sport");
83 String result = s.toJSON();
84 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
85 assert node != null;
86 assert node.at("/collection/@type").asText().equals("koral:docGroup");
87 assert node.at("/collection/operands").size() == 2;
88 assert node.at("/collection/operands/0/key").asText()
89 .equals("textClass");
90 assert node.at("/collection/operands/1/key").asText()
91 .equals("textClass");
92 }
93
Michael Hanlf21773f2015-10-16 23:02:31 +020094 //@Test
Michael Hanl66ca71c2015-09-14 23:11:30 +020095 public void testCollectionCleanMoveOneDocFromGroupUpNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +010096 RewriteHandler handler = new RewriteHandler(config);
97 handler.add(CollectionConstraint.class);
98 handler.add(CollectionCleanupFilter.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +020099 QuerySerializer s = new QuerySerializer();
100 s.setQuery(simple_add_query, "poliqarp");
101 s.setCollection("(corpusID=BRZ13 & textClass=Wissenschaft)");
102 String result = s.toJSON();
103 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
104 assert node != null;
105 assert node.at("/collection/@type").asText().equals("koral:doc");
106 }
107
Michael Hanlf21773f2015-10-16 23:02:31 +0200108 //@Test
Michael Hanl66ca71c2015-09-14 23:11:30 +0200109 public void testCollectionCleanEmptyGroupAndMoveOneFromGroupUpNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +0100110 RewriteHandler handler = new RewriteHandler(config);
111 handler.add(CollectionConstraint.class);
112 handler.add(CollectionCleanupFilter.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +0200113 QuerySerializer s = new QuerySerializer();
114 s.setQuery(simple_add_query, "poliqarp");
115 s.setCollection(
116 "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft");
117 String result = s.toJSON();
118 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
119 assert node != null;
120 assert node.at("/collection/@type").asText().equals("koral:doc");
121 assert node.at("/collection/key").asText().equals("textClass");
122 }
123
Michael Hanlf21773f2015-10-16 23:02:31 +0200124 //@Test
Michael Hanl66ca71c2015-09-14 23:11:30 +0200125 public void testCollectionRemoveAndMoveOneFromGroupUpNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +0100126 RewriteHandler handler = new RewriteHandler(config);
127 handler.add(CollectionConstraint.class);
128 handler.add(CollectionCleanupFilter.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +0200129 QuerySerializer s = new QuerySerializer();
130 s.setQuery(simple_add_query, "poliqarp");
131 s.setCollection(
132 "(docID=random & textClass=Wissenschaft) & corpusID=WPD");
133 String result = s.toJSON();
134 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
135 System.out.println("original node " + result);
136 System.out.println("result node " + node);
137 assert node != null;
138 assert node.at("/collection/@type").asText().equals("koral:docGroup");
139 assert node.at("/collection/operands").size() == 2;
140 }
141
Michael Hanlfbd6fbb2015-09-14 16:10:12 +0200142}