blob: c9e63f2a7d3b79f83b55a08594aa909f84e6c903 [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;
Michael Hanl0f6ffd72015-08-27 19:23:15 +02006import de.ids_mannheim.korap.resource.rewrite.FoundryInject;
7import de.ids_mannheim.korap.resource.rewrite.RewriteHandler;
Michael Hanl1e18cb42015-08-06 20:57:35 +02008import 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 Hanlfbd6fbb2015-09-14 16:10:12 +020017public class FoundryRewriteTest {
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() {
Michael Hanl4c62d922015-09-14 23:14:18 +020035 RewriteHandler processor = new RewriteHandler();
36 processor.add(new FoundryInject(config));
Michael Hanlbadd79c2015-06-19 07:41:03 +020037 QuerySerializer s = new QuerySerializer();
38 s.setQuery(simple_add_query, "poliqarp");
Michael Hanl4c62d922015-09-14 23:14:18 +020039 String result = processor.apply(s.toJSON(), null);
Michael Hanl1e18cb42015-08-06 20:57:35 +020040 JsonNode node = JsonUtils.readTree(result);
Michael Hanl0f6ffd72015-08-27 19:23:15 +020041
42 assert node != null;
Michael Hanl1e18cb42015-08-06 20:57:35 +020043 assert !node.at("/query/wrap/foundry").isMissingNode();
Michael Hanlbadd79c2015-06-19 07:41:03 +020044 }
45
46 @Test
Michael Hanl1e18cb42015-08-06 20:57:35 +020047 public void testDefaultLayerMapperThrowsNoException() {
48 LayerMapper m = new LayerMapper(config);
49
50 assert m.findFoundry("lemma").equals(config.getDefault_lemma());
51 assert m.findFoundry("pos").equals(config.getDefault_pos());
52 assert m.findFoundry("surface").equals(config.getDefault_surface());
53 assert m.findFoundry("d").equals(config.getDefault_dep());
54 assert m.findFoundry("c").equals(config.getDefault_const());
Michael Hanlbadd79c2015-06-19 07:41:03 +020055 }
56
57 @Test
Michael Hanl1e18cb42015-08-06 20:57:35 +020058 public void testFoundryInjectPosNoErrors() {
Michael Hanlbadd79c2015-06-19 07:41:03 +020059 QuerySerializer s = new QuerySerializer();
Michael Hanl1e18cb42015-08-06 20:57:35 +020060 RewriteHandler handler = new RewriteHandler();
61 s.setQuery("[pos=ADJA]", "poliqarp");
62 handler.add(new FoundryInject(config));
63 String result = handler.apply(s.toJSON(), null);
64 JsonNode node = JsonUtils.readTree(result);
Michael Hanl0f6ffd72015-08-27 19:23:15 +020065
66 assert node != null;
Michael Hanl1e18cb42015-08-06 20:57:35 +020067 assert !node.at("/query/wrap/foundry").isMissingNode();
68 assert !node.at("/query/wrap/rewrites").isMissingNode();
69 assert node.at("/query/wrap/rewrites/0/@type").asText()
70 .equals("koral:rewrite");
Michael Hanlbadd79c2015-06-19 07:41:03 +020071 }
Michael Hanl1e18cb42015-08-06 20:57:35 +020072
73 @Test
74 public void testFoundryInjectJoinedQueryNoErrors() {
75 QuerySerializer s = new QuerySerializer();
76 RewriteHandler handler = new RewriteHandler();
77 s.setQuery("[orth=laufe/i & base!=Lauf]", "poliqarp");
78 handler.add(new FoundryInject(config));
79 String result = handler.apply(s.toJSON(), null);
80 JsonNode node = JsonUtils.readTree(result);
Michael Hanl0f6ffd72015-08-27 19:23:15 +020081
82 assert node != null;
Michael Hanl1e18cb42015-08-06 20:57:35 +020083 assert node.at("/query/wrap/@type").asText().equals("koral:termGroup");
84 assert !node.at("/query/wrap/operands/0/foundry").isMissingNode();
85 assert !node.at("/query/wrap/operands/0/rewrites").isMissingNode();
86 assert !node.at("/query/wrap/operands/1/foundry").isMissingNode();
87 assert !node.at("/query/wrap/operands/1/rewrites").isMissingNode();
88 }
89
90 @Test
91 public void testFoundryInjectGroupedQueryNoErrors() {
92 QuerySerializer s = new QuerySerializer();
93 RewriteHandler handler = new RewriteHandler();
94 s.setQuery("[(base=laufen | base=gehen) & tt/pos=VVFIN]", "poliqarp");
95 handler.add(new FoundryInject(config));
96 String result = handler.apply(s.toJSON(), null);
97 JsonNode node = JsonUtils.readTree(result);
98
Michael Hanl0f6ffd72015-08-27 19:23:15 +020099 assert node != null;
Michael Hanl1e18cb42015-08-06 20:57:35 +0200100 assert node.at("/query/wrap/@type").asText().equals("koral:termGroup");
101 assert !node.at("/query/wrap/operands/0/operands/0/foundry")
102 .isMissingNode();
103 assert !node.at("/query/wrap/operands/0/operands/0/rewrites")
104 .isMissingNode();
105 assert !node.at("/query/wrap/operands/0/operands/1/foundry")
106 .isMissingNode();
107 assert !node.at("/query/wrap/operands/0/operands/1/rewrites")
108 .isMissingNode();
109
110 assert !node.at("/query/wrap/operands/1/foundry").isMissingNode();
111 assert node.at("/query/wrap/operands/1/rewrites").isMissingNode();
112 }
113
Michael Hanl704211e2015-06-19 07:26:04 +0200114}