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 | 9592b0a | 2015-10-28 19:04:08 +0100 | [diff] [blame^] | 9 | import org.junit.AfterClass; |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 10 | import org.junit.BeforeClass; |
| 11 | import org.junit.Test; |
| 12 | |
Michael Hanl | 704211e | 2015-06-19 07:26:04 +0200 | [diff] [blame] | 13 | /** |
| 14 | * @author hanl |
| 15 | * @date 18/06/2015 |
| 16 | */ |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 17 | //todo: refactor and structure tests |
Michael Hanl | fbd6fbb | 2015-09-14 16:10:12 +0200 | [diff] [blame] | 18 | public class FoundryRewriteTest { |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 19 | |
| 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 Hanl | 378af4a | 2015-06-19 08:03:55 +0200 | [diff] [blame] | 24 | 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] | 25 | |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 26 | private static KustvaktConfiguration config; |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 27 | |
| 28 | @BeforeClass |
| 29 | public static void init() { |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 30 | BeanConfiguration.loadClasspathContext(); |
| 31 | config = BeanConfiguration.getBeans().getConfiguration(); |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 32 | } |
| 33 | |
Michael Hanl | 9592b0a | 2015-10-28 19:04:08 +0100 | [diff] [blame^] | 34 | @AfterClass |
| 35 | public static void close() { |
| 36 | BeanConfiguration.closeApplication(); |
| 37 | } |
| 38 | |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 39 | @Test |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 40 | public void testSimpleFoundryAddThrowsNoError() { |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 41 | RewriteHandler processor = new RewriteHandler(config); |
| 42 | processor.add(FoundryInject.class); |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 43 | QuerySerializer s = new QuerySerializer(); |
| 44 | s.setQuery(simple_add_query, "poliqarp"); |
Michael Hanl | 4c62d92 | 2015-09-14 23:14:18 +0200 | [diff] [blame] | 45 | String result = processor.apply(s.toJSON(), null); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 46 | JsonNode node = JsonUtils.readTree(result); |
Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 47 | |
| 48 | assert node != null; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 49 | assert !node.at("/query/wrap/foundry").isMissingNode(); |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Test |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 53 | 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 Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 58 | assert m.findFoundry("surface").equals(config.getDefault_token()); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 59 | assert m.findFoundry("d").equals(config.getDefault_dep()); |
| 60 | assert m.findFoundry("c").equals(config.getDefault_const()); |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Test |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 64 | public void testFoundryInjectPosNoErrors() { |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 65 | QuerySerializer s = new QuerySerializer(); |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 66 | RewriteHandler handler = new RewriteHandler(config); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 67 | s.setQuery("[pos=ADJA]", "poliqarp"); |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 68 | assert handler.add(FoundryInject.class); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 69 | String result = handler.apply(s.toJSON(), null); |
| 70 | JsonNode node = JsonUtils.readTree(result); |
Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 71 | |
| 72 | assert node != null; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 73 | 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 Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 77 | } |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 78 | |
| 79 | @Test |
| 80 | public void testFoundryInjectJoinedQueryNoErrors() { |
| 81 | QuerySerializer s = new QuerySerializer(); |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 82 | RewriteHandler handler = new RewriteHandler(config); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 83 | s.setQuery("[orth=laufe/i & base!=Lauf]", "poliqarp"); |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 84 | assert handler.add(FoundryInject.class); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 85 | String result = handler.apply(s.toJSON(), null); |
| 86 | JsonNode node = JsonUtils.readTree(result); |
Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 87 | |
| 88 | assert node != null; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 89 | 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 Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 99 | RewriteHandler handler = new RewriteHandler(config); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 100 | s.setQuery("[(base=laufen | base=gehen) & tt/pos=VVFIN]", "poliqarp"); |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 101 | assert handler.add(FoundryInject.class); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 102 | String result = handler.apply(s.toJSON(), null); |
| 103 | JsonNode node = JsonUtils.readTree(result); |
| 104 | |
Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 105 | assert node != null; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 106 | 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 Hanl | 704211e | 2015-06-19 07:26:04 +0200 | [diff] [blame] | 120 | } |