blob: f06390ef670a14ce91fce3f2c066357711ac3235 [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 Hanl1e18cb42015-08-06 20:57:35 +02007import de.ids_mannheim.korap.resource.rewrite.*;
8import de.ids_mannheim.korap.utils.JsonUtils;
Michael Hanlbadd79c2015-06-19 07:41:03 +02009import org.junit.BeforeClass;
10import org.junit.Test;
11
Michael Hanl704211e2015-06-19 07:26:04 +020012/**
13 * @author hanl
14 * @date 18/06/2015
15 */
Michael Hanl1e18cb42015-08-06 20:57:35 +020016//todo: refactor and structure tests
Michael Hanl704211e2015-06-19 07:26:04 +020017public class RewriteTest {
Michael Hanlbadd79c2015-06-19 07:41:03 +020018
19 private static String simple_add_query = "[pos=ADJA]";
20 private static String simple_rewrite_query = "[base=Haus]";
21 private static String complex_rewrite_query = "<c=INFC>";
22 private static String complex_rewrite_query2 = "[orth=laufe/i & base!=Lauf]";
Michael Hanl378af4a2015-06-19 08:03:55 +020023 private static String complex_rewrite_query3 = "[(base=laufen | base=gehen) & tt/pos=VVFIN]";
Michael Hanlbadd79c2015-06-19 07:41:03 +020024
Michael Hanl1e18cb42015-08-06 20:57:35 +020025 private static KustvaktConfiguration config;
Michael Hanlbadd79c2015-06-19 07:41:03 +020026
27 @BeforeClass
28 public static void init() {
Michael Hanl1e18cb42015-08-06 20:57:35 +020029 BeanConfiguration.loadClasspathContext();
30 config = BeanConfiguration.getBeans().getConfiguration();
Michael Hanlbadd79c2015-06-19 07:41:03 +020031 }
32
33 @Test
Michael Hanl1e18cb42015-08-06 20:57:35 +020034 public void testSimpleFoundryAddThrowsNoError() {
35 RewriteProcessor processor = new RewriteProcessor(config);
Michael Hanlbadd79c2015-06-19 07:41:03 +020036 QuerySerializer s = new QuerySerializer();
37 s.setQuery(simple_add_query, "poliqarp");
Michael Hanl1e18cb42015-08-06 20:57:35 +020038 String result = processor.process(s.toJSON());
39 JsonNode node = JsonUtils.readTree(result);
40 assert !node.at("/query/wrap/foundry").isMissingNode();
Michael Hanlbadd79c2015-06-19 07:41:03 +020041 }
42
43 @Test
Michael Hanl1e18cb42015-08-06 20:57:35 +020044 public void testDefaultLayerMapperThrowsNoException() {
45 LayerMapper m = new LayerMapper(config);
46
47 assert m.findFoundry("lemma").equals(config.getDefault_lemma());
48 assert m.findFoundry("pos").equals(config.getDefault_pos());
49 assert m.findFoundry("surface").equals(config.getDefault_surface());
50 assert m.findFoundry("d").equals(config.getDefault_dep());
51 assert m.findFoundry("c").equals(config.getDefault_const());
Michael Hanlbadd79c2015-06-19 07:41:03 +020052 }
53
54 @Test
Michael Hanl1e18cb42015-08-06 20:57:35 +020055 public void testFoundryInjectPosNoErrors() {
Michael Hanlbadd79c2015-06-19 07:41:03 +020056 QuerySerializer s = new QuerySerializer();
Michael Hanl1e18cb42015-08-06 20:57:35 +020057 RewriteHandler handler = new RewriteHandler();
58 s.setQuery("[pos=ADJA]", "poliqarp");
59 handler.add(new FoundryInject(config));
60 String result = handler.apply(s.toJSON(), null);
61 JsonNode node = JsonUtils.readTree(result);
62 assert !node.at("/query/wrap/foundry").isMissingNode();
63 assert !node.at("/query/wrap/rewrites").isMissingNode();
64 assert node.at("/query/wrap/rewrites/0/@type").asText()
65 .equals("koral:rewrite");
Michael Hanlbadd79c2015-06-19 07:41:03 +020066 }
Michael Hanl1e18cb42015-08-06 20:57:35 +020067
68 @Test
69 public void testFoundryInjectJoinedQueryNoErrors() {
70 QuerySerializer s = new QuerySerializer();
71 RewriteHandler handler = new RewriteHandler();
72 s.setQuery("[orth=laufe/i & base!=Lauf]", "poliqarp");
73 handler.add(new FoundryInject(config));
74 String result = handler.apply(s.toJSON(), null);
75 JsonNode node = JsonUtils.readTree(result);
76 assert node.at("/query/wrap/@type").asText().equals("koral:termGroup");
77 assert !node.at("/query/wrap/operands/0/foundry").isMissingNode();
78 assert !node.at("/query/wrap/operands/0/rewrites").isMissingNode();
79 assert !node.at("/query/wrap/operands/1/foundry").isMissingNode();
80 assert !node.at("/query/wrap/operands/1/rewrites").isMissingNode();
81 }
82
83 @Test
84 public void testFoundryInjectGroupedQueryNoErrors() {
85 QuerySerializer s = new QuerySerializer();
86 RewriteHandler handler = new RewriteHandler();
87 s.setQuery("[(base=laufen | base=gehen) & tt/pos=VVFIN]", "poliqarp");
88 handler.add(new FoundryInject(config));
89 String result = handler.apply(s.toJSON(), null);
90 JsonNode node = JsonUtils.readTree(result);
91
92 assert node.at("/query/wrap/@type").asText().equals("koral:termGroup");
93 assert !node.at("/query/wrap/operands/0/operands/0/foundry")
94 .isMissingNode();
95 assert !node.at("/query/wrap/operands/0/operands/0/rewrites")
96 .isMissingNode();
97 assert !node.at("/query/wrap/operands/0/operands/1/foundry")
98 .isMissingNode();
99 assert !node.at("/query/wrap/operands/0/operands/1/rewrites")
100 .isMissingNode();
101
102 assert !node.at("/query/wrap/operands/1/foundry").isMissingNode();
103 assert node.at("/query/wrap/operands/1/rewrites").isMissingNode();
104 }
105
106 @Test
107 public void testCollectionNodeRemoveCorpusIdNoErrors() {
108 RewriteHandler handler = new RewriteHandler();
109 handler.add(new CollectionConstraint());
110 QuerySerializer s = new QuerySerializer();
111 s.setQuery(simple_add_query, "poliqarp");
112 s.setCollection("textClass=politik & corpusID=WPD");
113 String result = s.toJSON();
114 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
115 assert node.at("/collection/operands").size() == 1;
116 }
117
118 @Test
119 public void testCollectionNodeRemoveAllCorpusIdNoErrors() {
120 RewriteHandler handler = new RewriteHandler();
121 handler.add(new CollectionConstraint());
122 QuerySerializer s = new QuerySerializer();
123 s.setQuery(simple_add_query, "poliqarp");
124 s.setCollection("corpusID=BRZ13 & corpusID=WPD");
125 String result = s.toJSON();
126 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
127 assert node.at("/collection/operands").size() == 0;
128 }
129
130 @Test
131 public void testCollectionNodeRemoveGroupedCorpusIdNoErrors() {
132 RewriteHandler handler = new RewriteHandler();
133 handler.add(new CollectionConstraint());
134 QuerySerializer s = new QuerySerializer();
135 s.setQuery(simple_add_query, "poliqarp");
136 s.setCollection(
137 "(corpusID=BRZ13 & textClass=Wissenschaft) & corpusID=WPD");
138 String result = s.toJSON();
139 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
140
141 assert node.at("/collection/operands/0/@type").asText()
142 .equals("koral:docGroup");
143 assert node.at("/collection/operands/0/operands/0/key").asText()
144 .equals("textClass");
145 }
146
147 //fixme: will probably fail when one doc groups are being refactored
148 @Test
149 public void testCollectionCleanEmptyDocGroupNoErrors() {
150 RewriteHandler handler = new RewriteHandler();
151 handler.add(new CollectionConstraint());
152 handler.add(new CollectionCleanupFilter());
153 QuerySerializer s = new QuerySerializer();
154 s.setQuery(simple_add_query, "poliqarp");
155 s.setCollection(
156 "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft & textClass=Sport");
157 String result = s.toJSON();
158 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
159 assert node.at("/collection/@type").asText().equals("koral:docGroup");
160 assert node.at("/collection/operands").size() == 2;
161 assert node.at("/collection/operands/0/key").asText()
162 .equals("textClass");
163 assert node.at("/collection/operands/1/key").asText()
164 .equals("textClass");
165 }
166
167 @Test
168 public void testCollectionCleanMoveOneDocFromGroupUpNoErrors() {
169 RewriteHandler handler = new RewriteHandler();
170 handler.add(new CollectionConstraint());
171 handler.add(new CollectionCleanupFilter());
172 QuerySerializer s = new QuerySerializer();
173 s.setQuery(simple_add_query, "poliqarp");
174 s.setCollection("(corpusID=BRZ13 & textClass=Wissenschaft)");
175 String result = s.toJSON();
176 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
177 assert node.at("/collection/@type").asText().equals("koral:doc");
178 }
179
180 @Test
181 public void testCollectionCleanEmptyGroupAndMoveOneDocFromGroupUpNoErrors() {
182 RewriteHandler handler = new RewriteHandler();
183 handler.add(new CollectionConstraint());
184 handler.add(new CollectionCleanupFilter());
185 QuerySerializer s = new QuerySerializer();
186 s.setQuery(simple_add_query, "poliqarp");
187 s.setCollection(
188 "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft");
189 String result = s.toJSON();
190 JsonNode node = JsonUtils.readTree(handler.apply(result, null));
191 assert node.at("/collection/@type").asText().equals("koral:doc");
192 assert node.at("/collection/key").asText().equals("textClass");
193 }
194
Michael Hanl704211e2015-06-19 07:26:04 +0200195}