blob: 19cea60bed9064e7342f2a9d066a199b094769c8 [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 */
15public class CollectionRewriteTest {
Michael Hanl66ca71c2015-09-14 23:11:30 +020016
17 private static String simple_add_query = "[pos=ADJA]";
18
19 private static KustvaktConfiguration config;
20
21 @BeforeClass
22 public static void init() {
23 BeanConfiguration.loadClasspathContext();
24 config = BeanConfiguration.getBeans().getConfiguration();
25 }
26
Michael Hanl4c935b32015-10-16 23:09:41 +020027// @Test
Michael Hanl66ca71c2015-09-14 23:11:30 +020028 public void testCollectionNodeRemoveCorpusIdNoErrors() {
29 RewriteHandler handler = new RewriteHandler();
30 handler.add(new CollectionConstraint());
31 QuerySerializer s = new QuerySerializer();
32 s.setQuery(simple_add_query, "poliqarp");
33 s.setCollection("textClass=politik & corpusID=WPD");
34 String result = s.toJSON();
35 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
36 assert node != null;
37 assert node.at("/collection/operands").size() == 1;
38 }
39
Michael Hanlf21773f2015-10-16 23:02:31 +020040 //@Test
Michael Hanl66ca71c2015-09-14 23:11:30 +020041 public void testCollectionNodeRemoveAllCorpusIdNoErrors() {
42 RewriteHandler handler = new RewriteHandler();
43 handler.add(new CollectionConstraint());
44 QuerySerializer s = new QuerySerializer();
45 s.setQuery(simple_add_query, "poliqarp");
46 s.setCollection("corpusID=BRZ13 & corpusID=WPD");
47 String result = s.toJSON();
48 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
49
50 assert node != null;
51 assert node.at("/collection/operands").size() == 0;
52 }
53
Michael Hanlf21773f2015-10-16 23:02:31 +020054 //@Test
Michael Hanl66ca71c2015-09-14 23:11:30 +020055 public void testCollectionNodeRemoveGroupedCorpusIdNoErrors() {
56 RewriteHandler handler = new RewriteHandler();
57 handler.add(new CollectionConstraint());
58 QuerySerializer s = new QuerySerializer();
59 s.setQuery(simple_add_query, "poliqarp");
60 s.setCollection(
61 "(corpusID=BRZ13 & textClass=Wissenschaft) & corpusID=WPD");
62 String result = s.toJSON();
63 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
64
65 assert node != null;
66 assert node.at("/collection/operands/0/@type").asText()
67 .equals("koral:docGroup");
68 assert node.at("/collection/operands/0/operands/0/key").asText()
69 .equals("textClass");
70 }
71
72 //fixme: will probably fail when one doc groups are being refactored
Michael Hanlf21773f2015-10-16 23:02:31 +020073 //@Test
Michael Hanl66ca71c2015-09-14 23:11:30 +020074 public void testCollectionCleanEmptyDocGroupNoErrors() {
75 RewriteHandler handler = new RewriteHandler();
76 handler.add(new CollectionConstraint());
77 handler.add(new CollectionCleanupFilter());
78 QuerySerializer s = new QuerySerializer();
79 s.setQuery(simple_add_query, "poliqarp");
80 s.setCollection(
81 "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft & textClass=Sport");
82 String result = s.toJSON();
83 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
84 assert node != null;
85 assert node.at("/collection/@type").asText().equals("koral:docGroup");
86 assert node.at("/collection/operands").size() == 2;
87 assert node.at("/collection/operands/0/key").asText()
88 .equals("textClass");
89 assert node.at("/collection/operands/1/key").asText()
90 .equals("textClass");
91 }
92
Michael Hanlf21773f2015-10-16 23:02:31 +020093 //@Test
Michael Hanl66ca71c2015-09-14 23:11:30 +020094 public void testCollectionCleanMoveOneDocFromGroupUpNoErrors() {
95 RewriteHandler handler = new RewriteHandler();
96 handler.add(new CollectionConstraint());
97 handler.add(new CollectionCleanupFilter());
98 QuerySerializer s = new QuerySerializer();
99 s.setQuery(simple_add_query, "poliqarp");
100 s.setCollection("(corpusID=BRZ13 & textClass=Wissenschaft)");
101 String result = s.toJSON();
102 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
103 assert node != null;
104 assert node.at("/collection/@type").asText().equals("koral:doc");
105 }
106
Michael Hanlf21773f2015-10-16 23:02:31 +0200107 //@Test
Michael Hanl66ca71c2015-09-14 23:11:30 +0200108 public void testCollectionCleanEmptyGroupAndMoveOneFromGroupUpNoErrors() {
109 RewriteHandler handler = new RewriteHandler();
110 handler.add(new CollectionConstraint());
111 handler.add(new CollectionCleanupFilter());
112 QuerySerializer s = new QuerySerializer();
113 s.setQuery(simple_add_query, "poliqarp");
114 s.setCollection(
115 "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft");
116 String result = s.toJSON();
117 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
118 assert node != null;
119 assert node.at("/collection/@type").asText().equals("koral:doc");
120 assert node.at("/collection/key").asText().equals("textClass");
121 }
122
Michael Hanlf21773f2015-10-16 23:02:31 +0200123 //@Test
Michael Hanl66ca71c2015-09-14 23:11:30 +0200124 public void testCollectionRemoveAndMoveOneFromGroupUpNoErrors() {
125 RewriteHandler handler = new RewriteHandler();
126 handler.add(new CollectionConstraint());
127 handler.add(new CollectionCleanupFilter());
128 QuerySerializer s = new QuerySerializer();
129 s.setQuery(simple_add_query, "poliqarp");
130 s.setCollection(
131 "(docID=random & textClass=Wissenschaft) & corpusID=WPD");
132 String result = s.toJSON();
133 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
134 System.out.println("original node " + result);
135 System.out.println("result node " + node);
136 assert node != null;
137 assert node.at("/collection/@type").asText().equals("koral:docGroup");
138 assert node.at("/collection/operands").size() == 2;
139 }
140
Michael Hanlfbd6fbb2015-09-14 16:10:12 +0200141}