blob: a4372081d3b848c4ac5e6fbdeebc1a4e3ebe214f [file] [log] [blame]
Michael Hanl1e18cb42015-08-06 20:57:35 +02001import com.fasterxml.jackson.databind.JsonNode;
Michael Hanlbadd79c2015-06-19 07:41:03 +02002import de.ids_mannheim.korap.config.BeanConfiguration;
Michael Hanl1e18cb42015-08-06 20:57:35 +02003import de.ids_mannheim.korap.config.KustvaktConfiguration;
Michael Hanlbadd79c2015-06-19 07:41:03 +02004import de.ids_mannheim.korap.query.serialize.QuerySerializer;
5import de.ids_mannheim.korap.resource.LayerMapper;
6import de.ids_mannheim.korap.resource.RewriteProcessor;
Michael Hanl0f6ffd72015-08-27 19:23:15 +02007import de.ids_mannheim.korap.resource.rewrite.CollectionCleanupFilter;
8import de.ids_mannheim.korap.resource.rewrite.CollectionConstraint;
9import de.ids_mannheim.korap.resource.rewrite.FoundryInject;
10import de.ids_mannheim.korap.resource.rewrite.RewriteHandler;
Michael Hanl1e18cb42015-08-06 20:57:35 +020011import de.ids_mannheim.korap.utils.JsonUtils;
Michael Hanlbadd79c2015-06-19 07:41:03 +020012import org.junit.BeforeClass;
13import org.junit.Test;
14
Michael Hanl704211e2015-06-19 07:26:04 +020015/**
16 * @author hanl
17 * @date 18/06/2015
18 */
Michael Hanl1e18cb42015-08-06 20:57:35 +020019//todo: refactor and structure tests
Michael Hanlfbd6fbb2015-09-14 16:10:12 +020020public class FoundryRewriteTest {
Michael Hanlbadd79c2015-06-19 07:41:03 +020021
22 private static String simple_add_query = "[pos=ADJA]";
23 private static String simple_rewrite_query = "[base=Haus]";
24 private static String complex_rewrite_query = "<c=INFC>";
25 private static String complex_rewrite_query2 = "[orth=laufe/i & base!=Lauf]";
Michael Hanl378af4a2015-06-19 08:03:55 +020026 private static String complex_rewrite_query3 = "[(base=laufen | base=gehen) & tt/pos=VVFIN]";
Michael Hanlbadd79c2015-06-19 07:41:03 +020027
Michael Hanl1e18cb42015-08-06 20:57:35 +020028 private static KustvaktConfiguration config;
Michael Hanlbadd79c2015-06-19 07:41:03 +020029
30 @BeforeClass
31 public static void init() {
Michael Hanl1e18cb42015-08-06 20:57:35 +020032 BeanConfiguration.loadClasspathContext();
33 config = BeanConfiguration.getBeans().getConfiguration();
Michael Hanlbadd79c2015-06-19 07:41:03 +020034 }
35
36 @Test
Michael Hanl1e18cb42015-08-06 20:57:35 +020037 public void testSimpleFoundryAddThrowsNoError() {
38 RewriteProcessor processor = new RewriteProcessor(config);
Michael Hanlbadd79c2015-06-19 07:41:03 +020039 QuerySerializer s = new QuerySerializer();
40 s.setQuery(simple_add_query, "poliqarp");
Michael Hanl1e18cb42015-08-06 20:57:35 +020041 String result = processor.process(s.toJSON());
42 JsonNode node = JsonUtils.readTree(result);
Michael Hanl0f6ffd72015-08-27 19:23:15 +020043
44 assert node != null;
Michael Hanl1e18cb42015-08-06 20:57:35 +020045 assert !node.at("/query/wrap/foundry").isMissingNode();
Michael Hanlbadd79c2015-06-19 07:41:03 +020046 }
47
48 @Test
Michael Hanl1e18cb42015-08-06 20:57:35 +020049 public void testDefaultLayerMapperThrowsNoException() {
50 LayerMapper m = new LayerMapper(config);
51
52 assert m.findFoundry("lemma").equals(config.getDefault_lemma());
53 assert m.findFoundry("pos").equals(config.getDefault_pos());
54 assert m.findFoundry("surface").equals(config.getDefault_surface());
55 assert m.findFoundry("d").equals(config.getDefault_dep());
56 assert m.findFoundry("c").equals(config.getDefault_const());
Michael Hanlbadd79c2015-06-19 07:41:03 +020057 }
58
59 @Test
Michael Hanl1e18cb42015-08-06 20:57:35 +020060 public void testFoundryInjectPosNoErrors() {
Michael Hanlbadd79c2015-06-19 07:41:03 +020061 QuerySerializer s = new QuerySerializer();
Michael Hanl1e18cb42015-08-06 20:57:35 +020062 RewriteHandler handler = new RewriteHandler();
63 s.setQuery("[pos=ADJA]", "poliqarp");
64 handler.add(new FoundryInject(config));
65 String result = handler.apply(s.toJSON(), null);
66 JsonNode node = JsonUtils.readTree(result);
Michael Hanl0f6ffd72015-08-27 19:23:15 +020067
68 assert node != null;
Michael Hanl1e18cb42015-08-06 20:57:35 +020069 assert !node.at("/query/wrap/foundry").isMissingNode();
70 assert !node.at("/query/wrap/rewrites").isMissingNode();
71 assert node.at("/query/wrap/rewrites/0/@type").asText()
72 .equals("koral:rewrite");
Michael Hanlbadd79c2015-06-19 07:41:03 +020073 }
Michael Hanl1e18cb42015-08-06 20:57:35 +020074
75 @Test
76 public void testFoundryInjectJoinedQueryNoErrors() {
77 QuerySerializer s = new QuerySerializer();
78 RewriteHandler handler = new RewriteHandler();
79 s.setQuery("[orth=laufe/i & base!=Lauf]", "poliqarp");
80 handler.add(new FoundryInject(config));
81 String result = handler.apply(s.toJSON(), null);
82 JsonNode node = JsonUtils.readTree(result);
Michael Hanl0f6ffd72015-08-27 19:23:15 +020083
84 assert node != null;
Michael Hanl1e18cb42015-08-06 20:57:35 +020085 assert node.at("/query/wrap/@type").asText().equals("koral:termGroup");
86 assert !node.at("/query/wrap/operands/0/foundry").isMissingNode();
87 assert !node.at("/query/wrap/operands/0/rewrites").isMissingNode();
88 assert !node.at("/query/wrap/operands/1/foundry").isMissingNode();
89 assert !node.at("/query/wrap/operands/1/rewrites").isMissingNode();
90 }
91
92 @Test
93 public void testFoundryInjectGroupedQueryNoErrors() {
94 QuerySerializer s = new QuerySerializer();
95 RewriteHandler handler = new RewriteHandler();
96 s.setQuery("[(base=laufen | base=gehen) & tt/pos=VVFIN]", "poliqarp");
97 handler.add(new FoundryInject(config));
98 String result = handler.apply(s.toJSON(), null);
99 JsonNode node = JsonUtils.readTree(result);
100
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200101 assert node != null;
Michael Hanl1e18cb42015-08-06 20:57:35 +0200102 assert node.at("/query/wrap/@type").asText().equals("koral:termGroup");
103 assert !node.at("/query/wrap/operands/0/operands/0/foundry")
104 .isMissingNode();
105 assert !node.at("/query/wrap/operands/0/operands/0/rewrites")
106 .isMissingNode();
107 assert !node.at("/query/wrap/operands/0/operands/1/foundry")
108 .isMissingNode();
109 assert !node.at("/query/wrap/operands/0/operands/1/rewrites")
110 .isMissingNode();
111
112 assert !node.at("/query/wrap/operands/1/foundry").isMissingNode();
113 assert node.at("/query/wrap/operands/1/rewrites").isMissingNode();
114 }
115
116 @Test
117 public void testCollectionNodeRemoveCorpusIdNoErrors() {
118 RewriteHandler handler = new RewriteHandler();
119 handler.add(new CollectionConstraint());
120 QuerySerializer s = new QuerySerializer();
121 s.setQuery(simple_add_query, "poliqarp");
122 s.setCollection("textClass=politik & corpusID=WPD");
123 String result = s.toJSON();
124 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200125 assert node != null;
Michael Hanl1e18cb42015-08-06 20:57:35 +0200126 assert node.at("/collection/operands").size() == 1;
127 }
128
129 @Test
130 public void testCollectionNodeRemoveAllCorpusIdNoErrors() {
131 RewriteHandler handler = new RewriteHandler();
132 handler.add(new CollectionConstraint());
133 QuerySerializer s = new QuerySerializer();
134 s.setQuery(simple_add_query, "poliqarp");
135 s.setCollection("corpusID=BRZ13 & corpusID=WPD");
136 String result = s.toJSON();
137 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200138
139 assert node != null;
Michael Hanl1e18cb42015-08-06 20:57:35 +0200140 assert node.at("/collection/operands").size() == 0;
141 }
142
143 @Test
144 public void testCollectionNodeRemoveGroupedCorpusIdNoErrors() {
145 RewriteHandler handler = new RewriteHandler();
146 handler.add(new CollectionConstraint());
147 QuerySerializer s = new QuerySerializer();
148 s.setQuery(simple_add_query, "poliqarp");
149 s.setCollection(
150 "(corpusID=BRZ13 & textClass=Wissenschaft) & corpusID=WPD");
151 String result = s.toJSON();
152 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
153
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200154 assert node != null;
Michael Hanl1e18cb42015-08-06 20:57:35 +0200155 assert node.at("/collection/operands/0/@type").asText()
156 .equals("koral:docGroup");
157 assert node.at("/collection/operands/0/operands/0/key").asText()
158 .equals("textClass");
159 }
160
161 //fixme: will probably fail when one doc groups are being refactored
162 @Test
163 public void testCollectionCleanEmptyDocGroupNoErrors() {
164 RewriteHandler handler = new RewriteHandler();
165 handler.add(new CollectionConstraint());
166 handler.add(new CollectionCleanupFilter());
167 QuerySerializer s = new QuerySerializer();
168 s.setQuery(simple_add_query, "poliqarp");
169 s.setCollection(
170 "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft & textClass=Sport");
171 String result = s.toJSON();
172 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200173 assert node != null;
Michael Hanl1e18cb42015-08-06 20:57:35 +0200174 assert node.at("/collection/@type").asText().equals("koral:docGroup");
175 assert node.at("/collection/operands").size() == 2;
176 assert node.at("/collection/operands/0/key").asText()
177 .equals("textClass");
178 assert node.at("/collection/operands/1/key").asText()
179 .equals("textClass");
180 }
181
182 @Test
183 public void testCollectionCleanMoveOneDocFromGroupUpNoErrors() {
184 RewriteHandler handler = new RewriteHandler();
185 handler.add(new CollectionConstraint());
186 handler.add(new CollectionCleanupFilter());
187 QuerySerializer s = new QuerySerializer();
188 s.setQuery(simple_add_query, "poliqarp");
189 s.setCollection("(corpusID=BRZ13 & textClass=Wissenschaft)");
190 String result = s.toJSON();
191 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200192 assert node != null;
Michael Hanl1e18cb42015-08-06 20:57:35 +0200193 assert node.at("/collection/@type").asText().equals("koral:doc");
194 }
195
196 @Test
197 public void testCollectionCleanEmptyGroupAndMoveOneDocFromGroupUpNoErrors() {
198 RewriteHandler handler = new RewriteHandler();
199 handler.add(new CollectionConstraint());
200 handler.add(new CollectionCleanupFilter());
201 QuerySerializer s = new QuerySerializer();
202 s.setQuery(simple_add_query, "poliqarp");
203 s.setCollection(
204 "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft");
205 String result = s.toJSON();
206 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200207 assert node != null;
Michael Hanl1e18cb42015-08-06 20:57:35 +0200208 assert node.at("/collection/@type").asText().equals("koral:doc");
209 assert node.at("/collection/key").asText().equals("textClass");
210 }
211
Michael Hanl704211e2015-06-19 07:26:04 +0200212}