blob: 2649d1e5e8481ab7c3a8405dd65e7d1836ddfbd0 [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;
Michael Hanl9592b0a2015-10-28 19:04:08 +01009import org.junit.AfterClass;
Michael Hanl66ca71c2015-09-14 23:11:30 +020010import org.junit.BeforeClass;
Michael Hanl66ca71c2015-09-14 23:11:30 +020011
Michael Hanlfbd6fbb2015-09-14 16:10:12 +020012/**
13 * @author hanl
14 * @date 03/09/2015
15 */
Michael Hanl59bff812015-10-27 23:10:32 +010016// todo: 20.10.15
Michael Hanlfbd6fbb2015-09-14 16:10:12 +020017public class CollectionRewriteTest {
Michael Hanl66ca71c2015-09-14 23:11:30 +020018
19 private static String simple_add_query = "[pos=ADJA]";
20
21 private static KustvaktConfiguration config;
22
23 @BeforeClass
24 public static void init() {
25 BeanConfiguration.loadClasspathContext();
26 config = BeanConfiguration.getBeans().getConfiguration();
27 }
28
Michael Hanl9592b0a2015-10-28 19:04:08 +010029 @AfterClass
30 public static void close() {
31 BeanConfiguration.closeApplication();
32 }
33
34
Michael Hanl59bff812015-10-27 23:10:32 +010035 // @Test
Michael Hanl66ca71c2015-09-14 23:11:30 +020036 public void testCollectionNodeRemoveCorpusIdNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +010037 RewriteHandler handler = new RewriteHandler(config);
38 handler.add(CollectionConstraint.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +020039 QuerySerializer s = new QuerySerializer();
40 s.setQuery(simple_add_query, "poliqarp");
41 s.setCollection("textClass=politik & corpusID=WPD");
42 String result = s.toJSON();
43 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
44 assert node != null;
45 assert node.at("/collection/operands").size() == 1;
46 }
47
Michael Hanlf21773f2015-10-16 23:02:31 +020048 //@Test
Michael Hanl66ca71c2015-09-14 23:11:30 +020049 public void testCollectionNodeRemoveAllCorpusIdNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +010050 RewriteHandler handler = new RewriteHandler(config);
51 handler.add(CollectionConstraint.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +020052 QuerySerializer s = new QuerySerializer();
53 s.setQuery(simple_add_query, "poliqarp");
54 s.setCollection("corpusID=BRZ13 & corpusID=WPD");
55 String result = s.toJSON();
56 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
57
58 assert node != null;
59 assert node.at("/collection/operands").size() == 0;
60 }
61
Michael Hanlf21773f2015-10-16 23:02:31 +020062 //@Test
Michael Hanl66ca71c2015-09-14 23:11:30 +020063 public void testCollectionNodeRemoveGroupedCorpusIdNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +010064 RewriteHandler handler = new RewriteHandler(config);
65 handler.add(CollectionConstraint.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +020066 QuerySerializer s = new QuerySerializer();
67 s.setQuery(simple_add_query, "poliqarp");
68 s.setCollection(
69 "(corpusID=BRZ13 & textClass=Wissenschaft) & corpusID=WPD");
70 String result = s.toJSON();
71 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
72
73 assert node != null;
74 assert node.at("/collection/operands/0/@type").asText()
75 .equals("koral:docGroup");
76 assert node.at("/collection/operands/0/operands/0/key").asText()
77 .equals("textClass");
78 }
79
80 //fixme: will probably fail when one doc groups are being refactored
Michael Hanlf21773f2015-10-16 23:02:31 +020081 //@Test
Michael Hanl66ca71c2015-09-14 23:11:30 +020082 public void testCollectionCleanEmptyDocGroupNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +010083 RewriteHandler handler = new RewriteHandler(config);
84 handler.add(CollectionConstraint.class);
85 handler.add(CollectionCleanupFilter.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +020086 QuerySerializer s = new QuerySerializer();
87 s.setQuery(simple_add_query, "poliqarp");
88 s.setCollection(
89 "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft & textClass=Sport");
90 String result = s.toJSON();
91 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
92 assert node != null;
93 assert node.at("/collection/@type").asText().equals("koral:docGroup");
94 assert node.at("/collection/operands").size() == 2;
95 assert node.at("/collection/operands/0/key").asText()
96 .equals("textClass");
97 assert node.at("/collection/operands/1/key").asText()
98 .equals("textClass");
99 }
100
Michael Hanlf21773f2015-10-16 23:02:31 +0200101 //@Test
Michael Hanl66ca71c2015-09-14 23:11:30 +0200102 public void testCollectionCleanMoveOneDocFromGroupUpNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +0100103 RewriteHandler handler = new RewriteHandler(config);
104 handler.add(CollectionConstraint.class);
105 handler.add(CollectionCleanupFilter.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +0200106 QuerySerializer s = new QuerySerializer();
107 s.setQuery(simple_add_query, "poliqarp");
108 s.setCollection("(corpusID=BRZ13 & textClass=Wissenschaft)");
109 String result = s.toJSON();
110 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
111 assert node != null;
112 assert node.at("/collection/@type").asText().equals("koral:doc");
113 }
114
Michael Hanlf21773f2015-10-16 23:02:31 +0200115 //@Test
Michael Hanl66ca71c2015-09-14 23:11:30 +0200116 public void testCollectionCleanEmptyGroupAndMoveOneFromGroupUpNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +0100117 RewriteHandler handler = new RewriteHandler(config);
118 handler.add(CollectionConstraint.class);
119 handler.add(CollectionCleanupFilter.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +0200120 QuerySerializer s = new QuerySerializer();
121 s.setQuery(simple_add_query, "poliqarp");
122 s.setCollection(
123 "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft");
124 String result = s.toJSON();
125 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
126 assert node != null;
127 assert node.at("/collection/@type").asText().equals("koral:doc");
128 assert node.at("/collection/key").asText().equals("textClass");
129 }
130
Michael Hanlf21773f2015-10-16 23:02:31 +0200131 //@Test
Michael Hanl66ca71c2015-09-14 23:11:30 +0200132 public void testCollectionRemoveAndMoveOneFromGroupUpNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +0100133 RewriteHandler handler = new RewriteHandler(config);
134 handler.add(CollectionConstraint.class);
135 handler.add(CollectionCleanupFilter.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +0200136 QuerySerializer s = new QuerySerializer();
137 s.setQuery(simple_add_query, "poliqarp");
138 s.setCollection(
139 "(docID=random & textClass=Wissenschaft) & corpusID=WPD");
140 String result = s.toJSON();
141 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
142 System.out.println("original node " + result);
143 System.out.println("result node " + node);
144 assert node != null;
145 assert node.at("/collection/@type").asText().equals("koral:docGroup");
146 assert node.at("/collection/operands").size() == 2;
147 }
148
Michael Hanlfbd6fbb2015-09-14 16:10:12 +0200149}