blob: 2db2838bd6efd15918cc8d5bacc1cd9561788a7c [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 Hanl6fdba052016-01-16 19:06:40 +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 Hanl022543e2015-11-17 21:25:25 +010039 @Deprecated
Michael Hanlf0785322015-11-13 16:14:45 +010040 @Test
41 public void test2() {
42 Pattern p = Pattern.compile("([\\.\\w]+)\\((.+)\\)");
43 String cl = de.ids_mannheim.korap.security.ac.SecurityManager.class
44 .getCanonicalName();
45 Matcher m = p.matcher(cl);
Michael Hanlf0785322015-11-13 16:14:45 +010046 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 Hanl022543e2015-11-17 21:25:25 +010059 JsonNode node = JsonUtils.readTree(handler.preProcess(result,
60 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 Hanl022543e2015-11-17 21:25:25 +010073 JsonNode node = JsonUtils.readTree(handler.preProcess(result,
74 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 Hanl022543e2015-11-17 21:25:25 +010089 JsonNode node = JsonUtils.readTree(handler.preProcess(result,
90 User.UserFactory.getUser("test_user")));
Michael Hanl66ca71c2015-09-14 23:11:30 +020091
92 assert node != null;
93 assert node.at("/collection/operands/0/@type").asText()
94 .equals("koral:docGroup");
95 assert node.at("/collection/operands/0/operands/0/key").asText()
96 .equals("textClass");
97 }
98
99 //fixme: will probably fail when one doc groups are being refactored
Michael Hanlf0785322015-11-13 16:14:45 +0100100 @Test
Michael Hanl66ca71c2015-09-14 23:11:30 +0200101 public void testCollectionCleanEmptyDocGroupNoErrors() {
Michael Hanl59bff812015-10-27 23:10:32 +0100102 RewriteHandler handler = new RewriteHandler(config);
103 handler.add(CollectionConstraint.class);
104 handler.add(CollectionCleanupFilter.class);
Michael Hanl66ca71c2015-09-14 23:11:30 +0200105 QuerySerializer s = new QuerySerializer();
106 s.setQuery(simple_add_query, "poliqarp");
107 s.setCollection(
108 "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft & textClass=Sport");
109 String result = s.toJSON();
Michael Hanl022543e2015-11-17 21:25:25 +0100110 JsonNode node = JsonUtils.readTree(handler.preProcess(result,
111 User.UserFactory.getUser("test_user")));
112
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 Hanl022543e2015-11-17 21:25:25 +0100131 JsonNode node = JsonUtils.readTree(handler.preProcess(result,
132 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 Hanl022543e2015-11-17 21:25:25 +0100147 JsonNode node = JsonUtils.readTree(handler.preProcess(result,
148 User.UserFactory.getUser("test_user")));
Michael Hanlf0785322015-11-13 16:14:45 +0100149
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 Hanl022543e2015-11-17 21:25:25 +0100165 JsonNode node = JsonUtils.readTree(handler.preProcess(result,
166 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;
Michael Hanl022543e2015-11-17 21:25:25 +0100172 assert node.at("/collection/operands/0/@type").asText()
173 .equals("koral:doc");
Michael Hanl66ca71c2015-09-14 23:11:30 +0200174 }
175
Michael Hanlfbd6fbb2015-09-14 16:10:12 +0200176}