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 | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame^] | 7 | import de.ids_mannheim.korap.resource.rewrite.CollectionCleanupFilter; |
| 8 | import de.ids_mannheim.korap.resource.rewrite.CollectionConstraint; |
| 9 | import de.ids_mannheim.korap.resource.rewrite.FoundryInject; |
| 10 | import de.ids_mannheim.korap.resource.rewrite.RewriteHandler; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 11 | import de.ids_mannheim.korap.utils.JsonUtils; |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 12 | import org.junit.BeforeClass; |
| 13 | import org.junit.Test; |
| 14 | |
Michael Hanl | 704211e | 2015-06-19 07:26:04 +0200 | [diff] [blame] | 15 | /** |
| 16 | * @author hanl |
| 17 | * @date 18/06/2015 |
| 18 | */ |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 19 | //todo: refactor and structure tests |
Michael Hanl | 704211e | 2015-06-19 07:26:04 +0200 | [diff] [blame] | 20 | public class RewriteTest { |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 21 | |
| 22 | private static String simple_add_query = "[pos=ADJA]"; |
| 23 | private static String simple_rewrite_query = "[base=Haus]"; |
| 24 | private static String complex_rewrite_query = "<c=INFC>"; |
| 25 | private static String complex_rewrite_query2 = "[orth=laufe/i & base!=Lauf]"; |
Michael Hanl | 378af4a | 2015-06-19 08:03:55 +0200 | [diff] [blame] | 26 | 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] | 27 | |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 28 | private static KustvaktConfiguration config; |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 29 | |
| 30 | @BeforeClass |
| 31 | public static void init() { |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 32 | BeanConfiguration.loadClasspathContext(); |
| 33 | config = BeanConfiguration.getBeans().getConfiguration(); |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | @Test |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 37 | public void testSimpleFoundryAddThrowsNoError() { |
| 38 | RewriteProcessor processor = new RewriteProcessor(config); |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 39 | QuerySerializer s = new QuerySerializer(); |
| 40 | s.setQuery(simple_add_query, "poliqarp"); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 41 | String result = processor.process(s.toJSON()); |
| 42 | JsonNode node = JsonUtils.readTree(result); |
Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame^] | 43 | |
| 44 | assert node != null; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 45 | assert !node.at("/query/wrap/foundry").isMissingNode(); |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | @Test |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 49 | public void testDefaultLayerMapperThrowsNoException() { |
| 50 | LayerMapper m = new LayerMapper(config); |
| 51 | |
| 52 | assert m.findFoundry("lemma").equals(config.getDefault_lemma()); |
| 53 | assert m.findFoundry("pos").equals(config.getDefault_pos()); |
| 54 | assert m.findFoundry("surface").equals(config.getDefault_surface()); |
| 55 | assert m.findFoundry("d").equals(config.getDefault_dep()); |
| 56 | assert m.findFoundry("c").equals(config.getDefault_const()); |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Test |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 60 | public void testFoundryInjectPosNoErrors() { |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 61 | QuerySerializer s = new QuerySerializer(); |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 62 | RewriteHandler handler = new RewriteHandler(); |
| 63 | s.setQuery("[pos=ADJA]", "poliqarp"); |
| 64 | handler.add(new FoundryInject(config)); |
| 65 | String result = handler.apply(s.toJSON(), null); |
| 66 | JsonNode node = JsonUtils.readTree(result); |
Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame^] | 67 | |
| 68 | assert node != null; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 69 | assert !node.at("/query/wrap/foundry").isMissingNode(); |
| 70 | assert !node.at("/query/wrap/rewrites").isMissingNode(); |
| 71 | assert node.at("/query/wrap/rewrites/0/@type").asText() |
| 72 | .equals("koral:rewrite"); |
Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 73 | } |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 74 | |
| 75 | @Test |
| 76 | public void testFoundryInjectJoinedQueryNoErrors() { |
| 77 | QuerySerializer s = new QuerySerializer(); |
| 78 | RewriteHandler handler = new RewriteHandler(); |
| 79 | s.setQuery("[orth=laufe/i & base!=Lauf]", "poliqarp"); |
| 80 | handler.add(new FoundryInject(config)); |
| 81 | String result = handler.apply(s.toJSON(), null); |
| 82 | JsonNode node = JsonUtils.readTree(result); |
Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame^] | 83 | |
| 84 | assert node != null; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 85 | assert node.at("/query/wrap/@type").asText().equals("koral:termGroup"); |
| 86 | assert !node.at("/query/wrap/operands/0/foundry").isMissingNode(); |
| 87 | assert !node.at("/query/wrap/operands/0/rewrites").isMissingNode(); |
| 88 | assert !node.at("/query/wrap/operands/1/foundry").isMissingNode(); |
| 89 | assert !node.at("/query/wrap/operands/1/rewrites").isMissingNode(); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testFoundryInjectGroupedQueryNoErrors() { |
| 94 | QuerySerializer s = new QuerySerializer(); |
| 95 | RewriteHandler handler = new RewriteHandler(); |
| 96 | s.setQuery("[(base=laufen | base=gehen) & tt/pos=VVFIN]", "poliqarp"); |
| 97 | handler.add(new FoundryInject(config)); |
| 98 | String result = handler.apply(s.toJSON(), null); |
| 99 | JsonNode node = JsonUtils.readTree(result); |
| 100 | |
Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame^] | 101 | assert node != null; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 102 | assert node.at("/query/wrap/@type").asText().equals("koral:termGroup"); |
| 103 | assert !node.at("/query/wrap/operands/0/operands/0/foundry") |
| 104 | .isMissingNode(); |
| 105 | assert !node.at("/query/wrap/operands/0/operands/0/rewrites") |
| 106 | .isMissingNode(); |
| 107 | assert !node.at("/query/wrap/operands/0/operands/1/foundry") |
| 108 | .isMissingNode(); |
| 109 | assert !node.at("/query/wrap/operands/0/operands/1/rewrites") |
| 110 | .isMissingNode(); |
| 111 | |
| 112 | assert !node.at("/query/wrap/operands/1/foundry").isMissingNode(); |
| 113 | assert node.at("/query/wrap/operands/1/rewrites").isMissingNode(); |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void testCollectionNodeRemoveCorpusIdNoErrors() { |
| 118 | RewriteHandler handler = new RewriteHandler(); |
| 119 | handler.add(new CollectionConstraint()); |
| 120 | QuerySerializer s = new QuerySerializer(); |
| 121 | s.setQuery(simple_add_query, "poliqarp"); |
| 122 | s.setCollection("textClass=politik & corpusID=WPD"); |
| 123 | String result = s.toJSON(); |
| 124 | JsonNode node = JsonUtils.readTree(handler.apply(result, null)); |
Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame^] | 125 | assert node != null; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 126 | assert node.at("/collection/operands").size() == 1; |
| 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void testCollectionNodeRemoveAllCorpusIdNoErrors() { |
| 131 | RewriteHandler handler = new RewriteHandler(); |
| 132 | handler.add(new CollectionConstraint()); |
| 133 | QuerySerializer s = new QuerySerializer(); |
| 134 | s.setQuery(simple_add_query, "poliqarp"); |
| 135 | s.setCollection("corpusID=BRZ13 & corpusID=WPD"); |
| 136 | String result = s.toJSON(); |
| 137 | JsonNode node = JsonUtils.readTree(handler.apply(result, null)); |
Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame^] | 138 | |
| 139 | assert node != null; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 140 | assert node.at("/collection/operands").size() == 0; |
| 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void testCollectionNodeRemoveGroupedCorpusIdNoErrors() { |
| 145 | RewriteHandler handler = new RewriteHandler(); |
| 146 | handler.add(new CollectionConstraint()); |
| 147 | QuerySerializer s = new QuerySerializer(); |
| 148 | s.setQuery(simple_add_query, "poliqarp"); |
| 149 | s.setCollection( |
| 150 | "(corpusID=BRZ13 & textClass=Wissenschaft) & corpusID=WPD"); |
| 151 | String result = s.toJSON(); |
| 152 | JsonNode node = JsonUtils.readTree(handler.apply(result, null)); |
| 153 | |
Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame^] | 154 | assert node != null; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 155 | assert node.at("/collection/operands/0/@type").asText() |
| 156 | .equals("koral:docGroup"); |
| 157 | assert node.at("/collection/operands/0/operands/0/key").asText() |
| 158 | .equals("textClass"); |
| 159 | } |
| 160 | |
| 161 | //fixme: will probably fail when one doc groups are being refactored |
| 162 | @Test |
| 163 | public void testCollectionCleanEmptyDocGroupNoErrors() { |
| 164 | RewriteHandler handler = new RewriteHandler(); |
| 165 | handler.add(new CollectionConstraint()); |
| 166 | handler.add(new CollectionCleanupFilter()); |
| 167 | QuerySerializer s = new QuerySerializer(); |
| 168 | s.setQuery(simple_add_query, "poliqarp"); |
| 169 | s.setCollection( |
| 170 | "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft & textClass=Sport"); |
| 171 | String result = s.toJSON(); |
| 172 | JsonNode node = JsonUtils.readTree(handler.apply(result, null)); |
Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame^] | 173 | assert node != null; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 174 | assert node.at("/collection/@type").asText().equals("koral:docGroup"); |
| 175 | assert node.at("/collection/operands").size() == 2; |
| 176 | assert node.at("/collection/operands/0/key").asText() |
| 177 | .equals("textClass"); |
| 178 | assert node.at("/collection/operands/1/key").asText() |
| 179 | .equals("textClass"); |
| 180 | } |
| 181 | |
| 182 | @Test |
| 183 | public void testCollectionCleanMoveOneDocFromGroupUpNoErrors() { |
| 184 | RewriteHandler handler = new RewriteHandler(); |
| 185 | handler.add(new CollectionConstraint()); |
| 186 | handler.add(new CollectionCleanupFilter()); |
| 187 | QuerySerializer s = new QuerySerializer(); |
| 188 | s.setQuery(simple_add_query, "poliqarp"); |
| 189 | s.setCollection("(corpusID=BRZ13 & textClass=Wissenschaft)"); |
| 190 | String result = s.toJSON(); |
| 191 | JsonNode node = JsonUtils.readTree(handler.apply(result, null)); |
Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame^] | 192 | assert node != null; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 193 | assert node.at("/collection/@type").asText().equals("koral:doc"); |
| 194 | } |
| 195 | |
| 196 | @Test |
| 197 | public void testCollectionCleanEmptyGroupAndMoveOneDocFromGroupUpNoErrors() { |
| 198 | RewriteHandler handler = new RewriteHandler(); |
| 199 | handler.add(new CollectionConstraint()); |
| 200 | handler.add(new CollectionCleanupFilter()); |
| 201 | QuerySerializer s = new QuerySerializer(); |
| 202 | s.setQuery(simple_add_query, "poliqarp"); |
| 203 | s.setCollection( |
| 204 | "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft"); |
| 205 | String result = s.toJSON(); |
| 206 | JsonNode node = JsonUtils.readTree(handler.apply(result, null)); |
Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame^] | 207 | assert node != null; |
Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 208 | assert node.at("/collection/@type").asText().equals("koral:doc"); |
| 209 | assert node.at("/collection/key").asText().equals("textClass"); |
| 210 | } |
| 211 | |
Michael Hanl | 704211e | 2015-06-19 07:26:04 +0200 | [diff] [blame] | 212 | } |