blob: 995cdf7733d0041d5b4cdb88e04262b21921010e [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;
Michael Hanlf0785322015-11-13 16:14:45 +01008import de.ids_mannheim.korap.user.User;
Michael Hanl66ca71c2015-09-14 23:11:30 +02009import de.ids_mannheim.korap.utils.JsonUtils;
Michael Hanl9592b0a2015-10-28 19:04:08 +010010import org.junit.AfterClass;
Michael Hanl66ca71c2015-09-14 23:11:30 +020011import org.junit.BeforeClass;
Michael Hanlf0785322015-11-13 16:14:45 +010012import org.junit.Test;
13
14import java.util.regex.Matcher;
15import java.util.regex.Pattern;
Michael Hanl66ca71c2015-09-14 23:11:30 +020016
Michael Hanlfbd6fbb2015-09-14 16:10:12 +020017/**
18 * @author hanl
19 * @date 03/09/2015
20 */
Michael Hanl59bff812015-10-27 23:10:32 +010021// todo: 20.10.15
Michael Hanlfbd6fbb2015-09-14 16:10:12 +020022public class CollectionRewriteTest {
Michael Hanl66ca71c2015-09-14 23:11:30 +020023
24 private static String simple_add_query = "[pos=ADJA]";
25
26 private static KustvaktConfiguration config;
27
28 @BeforeClass
29 public static void init() {
Michael Hanlf0785322015-11-13 16:14:45 +010030 BeanConfiguration.loadClasspathContext("default-config.xml");
Michael Hanl66ca71c2015-09-14 23:11:30 +020031 config = BeanConfiguration.getBeans().getConfiguration();
32 }
33
Michael Hanl9592b0a2015-10-28 19:04:08 +010034 @AfterClass
35 public static void close() {
36 BeanConfiguration.closeApplication();
37 }
38
Michael Hanlf0785322015-11-13 16:14:45 +010039 @Test
40 public void test2() {
41 Pattern p = Pattern.compile("([\\.\\w]+)\\((.+)\\)");
42 String cl = de.ids_mannheim.korap.security.ac.SecurityManager.class
43 .getCanonicalName();
44 Matcher m = p.matcher(cl);
45 System.out.println("FOUND SOMETHING?! " + m.find());
46 while (m.find())
47 System.out.println("group 1 " + m.group(1));
Michael Hanl9592b0a2015-10-28 19:04:08 +010048
Michael Hanlf0785322015-11-13 16:14:45 +010049 }
50
51 @Test
Michael Hanl66ca71c2015-09-14 23:11:30 +020052 public void testCollectionNodeRemoveCorpusIdNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +010053 RewriteHandler handler = new RewriteHandler(config);
54 handler.add(CollectionConstraint.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +020055 QuerySerializer s = new QuerySerializer();
56 s.setQuery(simple_add_query, "poliqarp");
57 s.setCollection("textClass=politik & corpusID=WPD");
58 String result = s.toJSON();
Michael Hanlf0785322015-11-13 16:14:45 +010059 JsonNode node = JsonUtils.readTree(
60 handler.preProcess(result, User.UserFactory.getUser("test_user")));
Michael Hanl66ca71c2015-09-14 23:11:30 +020061 assert node != null;
62 assert node.at("/collection/operands").size() == 1;
63 }
64
Michael Hanlf0785322015-11-13 16:14:45 +010065 @Test
Michael Hanl66ca71c2015-09-14 23:11:30 +020066 public void testCollectionNodeRemoveAllCorpusIdNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +010067 RewriteHandler handler = new RewriteHandler(config);
68 handler.add(CollectionConstraint.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +020069 QuerySerializer s = new QuerySerializer();
70 s.setQuery(simple_add_query, "poliqarp");
71 s.setCollection("corpusID=BRZ13 & corpusID=WPD");
72 String result = s.toJSON();
Michael Hanlf0785322015-11-13 16:14:45 +010073 JsonNode node = JsonUtils.readTree(
74 handler.preProcess(result, User.UserFactory.getUser("test_user")));
75 System.out.println("RESULTING REWR NODE " + node);
Michael Hanl66ca71c2015-09-14 23:11:30 +020076 assert node != null;
77 assert node.at("/collection/operands").size() == 0;
78 }
79
Michael Hanlf0785322015-11-13 16:14:45 +010080 @Test
Michael Hanl66ca71c2015-09-14 23:11:30 +020081 public void testCollectionNodeRemoveGroupedCorpusIdNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +010082 RewriteHandler handler = new RewriteHandler(config);
83 handler.add(CollectionConstraint.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +020084 QuerySerializer s = new QuerySerializer();
85 s.setQuery(simple_add_query, "poliqarp");
86 s.setCollection(
87 "(corpusID=BRZ13 & textClass=Wissenschaft) & corpusID=WPD");
88 String result = s.toJSON();
Michael Hanlf0785322015-11-13 16:14:45 +010089 JsonNode node = JsonUtils.readTree(
90 handler.preProcess(result, User.UserFactory.getUser("test_user")));
Michael Hanl66ca71c2015-09-14 23:11:30 +020091
Michael Hanlf0785322015-11-13 16:14:45 +010092 System.out.println("COLLECTION NODE " + result);
Michael Hanl66ca71c2015-09-14 23:11:30 +020093 assert node != null;
94 assert node.at("/collection/operands/0/@type").asText()
95 .equals("koral:docGroup");
96 assert node.at("/collection/operands/0/operands/0/key").asText()
97 .equals("textClass");
98 }
99
100 //fixme: will probably fail when one doc groups are being refactored
Michael Hanlf0785322015-11-13 16:14:45 +0100101 @Test
Michael Hanl66ca71c2015-09-14 23:11:30 +0200102 public void testCollectionCleanEmptyDocGroupNoErrors() {
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(
109 "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft & textClass=Sport");
110 String result = s.toJSON();
Michael Hanlf0785322015-11-13 16:14:45 +0100111 JsonNode node = JsonUtils.readTree(
112 handler.preProcess(result, User.UserFactory.getUser("test_user")));
Michael Hanl66ca71c2015-09-14 23:11:30 +0200113 assert node != null;
114 assert node.at("/collection/@type").asText().equals("koral:docGroup");
115 assert node.at("/collection/operands").size() == 2;
116 assert node.at("/collection/operands/0/key").asText()
117 .equals("textClass");
118 assert node.at("/collection/operands/1/key").asText()
119 .equals("textClass");
120 }
121
Michael Hanlf0785322015-11-13 16:14:45 +0100122 @Test
Michael Hanl66ca71c2015-09-14 23:11:30 +0200123 public void testCollectionCleanMoveOneDocFromGroupUpNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +0100124 RewriteHandler handler = new RewriteHandler(config);
125 handler.add(CollectionConstraint.class);
126 handler.add(CollectionCleanupFilter.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +0200127 QuerySerializer s = new QuerySerializer();
128 s.setQuery(simple_add_query, "poliqarp");
129 s.setCollection("(corpusID=BRZ13 & textClass=Wissenschaft)");
130 String result = s.toJSON();
Michael Hanlf0785322015-11-13 16:14:45 +0100131 JsonNode node = JsonUtils.readTree(
132 handler.preProcess(result, User.UserFactory.getUser("test_user")));
Michael Hanl66ca71c2015-09-14 23:11:30 +0200133 assert node != null;
134 assert node.at("/collection/@type").asText().equals("koral:doc");
135 }
136
Michael Hanlf0785322015-11-13 16:14:45 +0100137 @Test
Michael Hanl66ca71c2015-09-14 23:11:30 +0200138 public void testCollectionCleanEmptyGroupAndMoveOneFromGroupUpNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +0100139 RewriteHandler handler = new RewriteHandler(config);
140 handler.add(CollectionConstraint.class);
141 handler.add(CollectionCleanupFilter.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +0200142 QuerySerializer s = new QuerySerializer();
143 s.setQuery(simple_add_query, "poliqarp");
144 s.setCollection(
145 "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft");
146 String result = s.toJSON();
Michael Hanlf0785322015-11-13 16:14:45 +0100147 JsonNode node = JsonUtils.readTree(
148 handler.preProcess(result, User.UserFactory.getUser("test_user")));
149
Michael Hanl66ca71c2015-09-14 23:11:30 +0200150 assert node != null;
151 assert node.at("/collection/@type").asText().equals("koral:doc");
152 assert node.at("/collection/key").asText().equals("textClass");
153 }
154
Michael Hanlf0785322015-11-13 16:14:45 +0100155 @Test
Michael Hanl66ca71c2015-09-14 23:11:30 +0200156 public void testCollectionRemoveAndMoveOneFromGroupUpNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +0100157 RewriteHandler handler = new RewriteHandler(config);
158 handler.add(CollectionConstraint.class);
159 handler.add(CollectionCleanupFilter.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +0200160 QuerySerializer s = new QuerySerializer();
161 s.setQuery(simple_add_query, "poliqarp");
162 s.setCollection(
163 "(docID=random & textClass=Wissenschaft) & corpusID=WPD");
164 String result = s.toJSON();
Michael Hanlf0785322015-11-13 16:14:45 +0100165 JsonNode node = JsonUtils.readTree(
166 handler.preProcess(result, User.UserFactory.getUser("test_user")));
Michael Hanl66ca71c2015-09-14 23:11:30 +0200167 System.out.println("original node " + result);
168 System.out.println("result node " + node);
169 assert node != null;
170 assert node.at("/collection/@type").asText().equals("koral:docGroup");
171 assert node.at("/collection/operands").size() == 2;
172 }
173
Michael Hanlfbd6fbb2015-09-14 16:10:12 +0200174}