Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 1 | import com.fasterxml.jackson.databind.JsonNode; |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 2 | import de.ids_mannheim.korap.config.BeanConfiguration; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 3 | import de.ids_mannheim.korap.config.KustvaktConfiguration; |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 4 | import de.ids_mannheim.korap.query.serialize.QuerySerializer; |
| 5 | import de.ids_mannheim.korap.resource.LayerMapper; |
Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 6 | import de.ids_mannheim.korap.resource.rewrite.FoundryInject; |
| 7 | import de.ids_mannheim.korap.resource.rewrite.RewriteHandler; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 8 | import de.ids_mannheim.korap.utils.JsonUtils; |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 9 | import org.junit.BeforeClass; |
| 10 | import org.junit.Test; |
| 11 | |
Michael Hanl | 704211e | 2015-06-19 07:26:04 +0200 | [diff] [blame] | 12 | /** |
| 13 | * @author hanl |
| 14 | * @date 18/06/2015 |
| 15 | */ |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 16 | //todo: refactor and structure tests |
Michael Hanl | fbd6fbb | 2015-09-14 16:10:12 +0200 | [diff] [blame] | 17 | public class FoundryRewriteTest { |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 18 | |
| 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 Hanl | 378af4a | 2015-06-19 08:03:55 +0200 | [diff] [blame] | 23 | private static String complex_rewrite_query3 = "[(base=laufen | base=gehen) & tt/pos=VVFIN]"; |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 24 | |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 25 | private static KustvaktConfiguration config; |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 26 | |
| 27 | @BeforeClass |
| 28 | public static void init() { |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 29 | BeanConfiguration.loadClasspathContext(); |
| 30 | config = BeanConfiguration.getBeans().getConfiguration(); |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | @Test |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 34 | public void testSimpleFoundryAddThrowsNoError() { |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame^] | 35 | RewriteHandler processor = new RewriteHandler(config); |
| 36 | processor.add(FoundryInject.class); |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 37 | QuerySerializer s = new QuerySerializer(); |
| 38 | s.setQuery(simple_add_query, "poliqarp"); |
Michael Hanl | 4c62d92 | 2015-09-14 23:14:18 +0200 | [diff] [blame] | 39 | String result = processor.apply(s.toJSON(), null); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 40 | JsonNode node = JsonUtils.readTree(result); |
Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 41 | |
| 42 | assert node != null; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 43 | assert !node.at("/query/wrap/foundry").isMissingNode(); |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | @Test |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 47 | 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()); |
Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 52 | assert m.findFoundry("surface").equals(config.getDefault_token()); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 53 | assert m.findFoundry("d").equals(config.getDefault_dep()); |
| 54 | assert m.findFoundry("c").equals(config.getDefault_const()); |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Test |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 58 | public void testFoundryInjectPosNoErrors() { |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 59 | QuerySerializer s = new QuerySerializer(); |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame^] | 60 | RewriteHandler handler = new RewriteHandler(config); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 61 | s.setQuery("[pos=ADJA]", "poliqarp"); |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame^] | 62 | assert handler.add(FoundryInject.class); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 63 | String result = handler.apply(s.toJSON(), null); |
| 64 | JsonNode node = JsonUtils.readTree(result); |
Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 65 | |
| 66 | assert node != null; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 67 | 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 Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 71 | } |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 72 | |
| 73 | @Test |
| 74 | public void testFoundryInjectJoinedQueryNoErrors() { |
| 75 | QuerySerializer s = new QuerySerializer(); |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame^] | 76 | RewriteHandler handler = new RewriteHandler(config); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 77 | s.setQuery("[orth=laufe/i & base!=Lauf]", "poliqarp"); |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame^] | 78 | assert handler.add(FoundryInject.class); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 79 | String result = handler.apply(s.toJSON(), null); |
| 80 | JsonNode node = JsonUtils.readTree(result); |
Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 81 | |
| 82 | assert node != null; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 83 | 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(); |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame^] | 93 | RewriteHandler handler = new RewriteHandler(config); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 94 | s.setQuery("[(base=laufen | base=gehen) & tt/pos=VVFIN]", "poliqarp"); |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame^] | 95 | assert handler.add(FoundryInject.class); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 96 | String result = handler.apply(s.toJSON(), null); |
| 97 | JsonNode node = JsonUtils.readTree(result); |
| 98 | |
Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 99 | assert node != null; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 100 | 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 Hanl | 704211e | 2015-06-19 07:26:04 +0200 | [diff] [blame] | 114 | } |