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; |
| 6 | import de.ids_mannheim.korap.resource.RewriteProcessor; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 7 | import de.ids_mannheim.korap.resource.rewrite.*; |
| 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 | 704211e | 2015-06-19 07:26:04 +0200 | [diff] [blame] | 17 | public class RewriteTest { |
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() { |
| 35 | RewriteProcessor processor = new RewriteProcessor(config); |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 36 | QuerySerializer s = new QuerySerializer(); |
| 37 | s.setQuery(simple_add_query, "poliqarp"); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 38 | String result = processor.process(s.toJSON()); |
| 39 | JsonNode node = JsonUtils.readTree(result); |
| 40 | assert !node.at("/query/wrap/foundry").isMissingNode(); |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Test |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 44 | 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 Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Test |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 55 | public void testFoundryInjectPosNoErrors() { |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 56 | QuerySerializer s = new QuerySerializer(); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 57 | 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 Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 66 | } |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 67 | |
| 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 Hanl | 704211e | 2015-06-19 07:26:04 +0200 | [diff] [blame] | 195 | } |