blob: 6d239642a6f7d77ab0de8bbe658ea1269a338932 [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 Hanl9592b0a2015-10-28 19:04:08 +01009import org.junit.AfterClass;
Michael Hanlbadd79c2015-06-19 07:41:03 +020010import org.junit.BeforeClass;
11import org.junit.Test;
12
Michael Hanl704211e2015-06-19 07:26:04 +020013/**
14 * @author hanl
15 * @date 18/06/2015
16 */
Michael Hanl1e18cb42015-08-06 20:57:35 +020017//todo: refactor and structure tests
Michael Hanlfbd6fbb2015-09-14 16:10:12 +020018public class FoundryRewriteTest {
Michael Hanlbadd79c2015-06-19 07:41:03 +020019
20 private static String simple_add_query = "[pos=ADJA]";
21 private static String simple_rewrite_query = "[base=Haus]";
22 private static String complex_rewrite_query = "<c=INFC>";
23 private static String complex_rewrite_query2 = "[orth=laufe/i & base!=Lauf]";
Michael Hanl378af4a2015-06-19 08:03:55 +020024 private static String complex_rewrite_query3 = "[(base=laufen | base=gehen) & tt/pos=VVFIN]";
Michael Hanlbadd79c2015-06-19 07:41:03 +020025
Michael Hanl1e18cb42015-08-06 20:57:35 +020026 private static KustvaktConfiguration config;
Michael Hanlbadd79c2015-06-19 07:41:03 +020027
28 @BeforeClass
29 public static void init() {
Michael Hanl1e18cb42015-08-06 20:57:35 +020030 BeanConfiguration.loadClasspathContext();
31 config = BeanConfiguration.getBeans().getConfiguration();
Michael Hanlbadd79c2015-06-19 07:41:03 +020032 }
33
Michael Hanl9592b0a2015-10-28 19:04:08 +010034 @AfterClass
35 public static void close() {
36 BeanConfiguration.closeApplication();
37 }
38
Michael Hanlbadd79c2015-06-19 07:41:03 +020039 @Test
Michael Hanl1e18cb42015-08-06 20:57:35 +020040 public void testSimpleFoundryAddThrowsNoError() {
Michael Hanl59bff812015-10-27 23:10:32 +010041 RewriteHandler processor = new RewriteHandler(config);
42 processor.add(FoundryInject.class);
Michael Hanlbadd79c2015-06-19 07:41:03 +020043 QuerySerializer s = new QuerySerializer();
44 s.setQuery(simple_add_query, "poliqarp");
Michael Hanl4c62d922015-09-14 23:14:18 +020045 String result = processor.apply(s.toJSON(), null);
Michael Hanl1e18cb42015-08-06 20:57:35 +020046 JsonNode node = JsonUtils.readTree(result);
Michael Hanl0f6ffd72015-08-27 19:23:15 +020047
48 assert node != null;
Michael Hanl1e18cb42015-08-06 20:57:35 +020049 assert !node.at("/query/wrap/foundry").isMissingNode();
Michael Hanlbadd79c2015-06-19 07:41:03 +020050 }
51
52 @Test
Michael Hanl1e18cb42015-08-06 20:57:35 +020053 public void testDefaultLayerMapperThrowsNoException() {
54 LayerMapper m = new LayerMapper(config);
55
56 assert m.findFoundry("lemma").equals(config.getDefault_lemma());
57 assert m.findFoundry("pos").equals(config.getDefault_pos());
Michael Hanlf21773f2015-10-16 23:02:31 +020058 assert m.findFoundry("surface").equals(config.getDefault_token());
Michael Hanl1e18cb42015-08-06 20:57:35 +020059 assert m.findFoundry("d").equals(config.getDefault_dep());
60 assert m.findFoundry("c").equals(config.getDefault_const());
Michael Hanlbadd79c2015-06-19 07:41:03 +020061 }
62
63 @Test
Michael Hanl1e18cb42015-08-06 20:57:35 +020064 public void testFoundryInjectPosNoErrors() {
Michael Hanlbadd79c2015-06-19 07:41:03 +020065 QuerySerializer s = new QuerySerializer();
Michael Hanl59bff812015-10-27 23:10:32 +010066 RewriteHandler handler = new RewriteHandler(config);
Michael Hanl1e18cb42015-08-06 20:57:35 +020067 s.setQuery("[pos=ADJA]", "poliqarp");
Michael Hanl59bff812015-10-27 23:10:32 +010068 assert handler.add(FoundryInject.class);
Michael Hanl1e18cb42015-08-06 20:57:35 +020069 String result = handler.apply(s.toJSON(), null);
70 JsonNode node = JsonUtils.readTree(result);
Michael Hanl0f6ffd72015-08-27 19:23:15 +020071
72 assert node != null;
Michael Hanl1e18cb42015-08-06 20:57:35 +020073 assert !node.at("/query/wrap/foundry").isMissingNode();
74 assert !node.at("/query/wrap/rewrites").isMissingNode();
75 assert node.at("/query/wrap/rewrites/0/@type").asText()
76 .equals("koral:rewrite");
Michael Hanlbadd79c2015-06-19 07:41:03 +020077 }
Michael Hanl1e18cb42015-08-06 20:57:35 +020078
79 @Test
80 public void testFoundryInjectJoinedQueryNoErrors() {
81 QuerySerializer s = new QuerySerializer();
Michael Hanl59bff812015-10-27 23:10:32 +010082 RewriteHandler handler = new RewriteHandler(config);
Michael Hanl1e18cb42015-08-06 20:57:35 +020083 s.setQuery("[orth=laufe/i & base!=Lauf]", "poliqarp");
Michael Hanl59bff812015-10-27 23:10:32 +010084 assert handler.add(FoundryInject.class);
Michael Hanl1e18cb42015-08-06 20:57:35 +020085 String result = handler.apply(s.toJSON(), null);
86 JsonNode node = JsonUtils.readTree(result);
Michael Hanl0f6ffd72015-08-27 19:23:15 +020087
88 assert node != null;
Michael Hanl1e18cb42015-08-06 20:57:35 +020089 assert node.at("/query/wrap/@type").asText().equals("koral:termGroup");
90 assert !node.at("/query/wrap/operands/0/foundry").isMissingNode();
91 assert !node.at("/query/wrap/operands/0/rewrites").isMissingNode();
92 assert !node.at("/query/wrap/operands/1/foundry").isMissingNode();
93 assert !node.at("/query/wrap/operands/1/rewrites").isMissingNode();
94 }
95
96 @Test
97 public void testFoundryInjectGroupedQueryNoErrors() {
98 QuerySerializer s = new QuerySerializer();
Michael Hanl59bff812015-10-27 23:10:32 +010099 RewriteHandler handler = new RewriteHandler(config);
Michael Hanl1e18cb42015-08-06 20:57:35 +0200100 s.setQuery("[(base=laufen | base=gehen) & tt/pos=VVFIN]", "poliqarp");
Michael Hanl59bff812015-10-27 23:10:32 +0100101 assert handler.add(FoundryInject.class);
Michael Hanl1e18cb42015-08-06 20:57:35 +0200102 String result = handler.apply(s.toJSON(), null);
103 JsonNode node = JsonUtils.readTree(result);
104
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200105 assert node != null;
Michael Hanl1e18cb42015-08-06 20:57:35 +0200106 assert node.at("/query/wrap/@type").asText().equals("koral:termGroup");
107 assert !node.at("/query/wrap/operands/0/operands/0/foundry")
108 .isMissingNode();
109 assert !node.at("/query/wrap/operands/0/operands/0/rewrites")
110 .isMissingNode();
111 assert !node.at("/query/wrap/operands/0/operands/1/foundry")
112 .isMissingNode();
113 assert !node.at("/query/wrap/operands/0/operands/1/rewrites")
114 .isMissingNode();
115
116 assert !node.at("/query/wrap/operands/1/foundry").isMissingNode();
117 assert node.at("/query/wrap/operands/1/rewrites").isMissingNode();
118 }
119
Michael Hanl704211e2015-06-19 07:26:04 +0200120}