Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 1 | import com.fasterxml.jackson.databind.JsonNode; |
| 2 | import de.ids_mannheim.korap.config.BeanConfiguration; |
| 3 | import de.ids_mannheim.korap.config.KustvaktConfiguration; |
| 4 | import de.ids_mannheim.korap.query.serialize.QuerySerializer; |
| 5 | import de.ids_mannheim.korap.resource.rewrite.CollectionCleanupFilter; |
| 6 | import de.ids_mannheim.korap.resource.rewrite.CollectionConstraint; |
| 7 | import de.ids_mannheim.korap.resource.rewrite.RewriteHandler; |
| 8 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 9 | import org.junit.BeforeClass; |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 10 | |
Michael Hanl | fbd6fbb | 2015-09-14 16:10:12 +0200 | [diff] [blame] | 11 | /** |
| 12 | * @author hanl |
| 13 | * @date 03/09/2015 |
| 14 | */ |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 15 | // todo: 20.10.15 |
Michael Hanl | fbd6fbb | 2015-09-14 16:10:12 +0200 | [diff] [blame] | 16 | public class CollectionRewriteTest { |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 17 | |
| 18 | private static String simple_add_query = "[pos=ADJA]"; |
| 19 | |
| 20 | private static KustvaktConfiguration config; |
| 21 | |
| 22 | @BeforeClass |
| 23 | public static void init() { |
| 24 | BeanConfiguration.loadClasspathContext(); |
| 25 | config = BeanConfiguration.getBeans().getConfiguration(); |
| 26 | } |
| 27 | |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 28 | // @Test |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 29 | public void testCollectionNodeRemoveCorpusIdNoErrors() { |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 30 | RewriteHandler handler = new RewriteHandler(config); |
| 31 | handler.add(CollectionConstraint.class); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 32 | QuerySerializer s = new QuerySerializer(); |
| 33 | s.setQuery(simple_add_query, "poliqarp"); |
| 34 | s.setCollection("textClass=politik & corpusID=WPD"); |
| 35 | String result = s.toJSON(); |
| 36 | JsonNode node = JsonUtils.readTree(handler.apply(result, null)); |
| 37 | assert node != null; |
| 38 | assert node.at("/collection/operands").size() == 1; |
| 39 | } |
| 40 | |
Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 41 | //@Test |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 42 | public void testCollectionNodeRemoveAllCorpusIdNoErrors() { |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 43 | RewriteHandler handler = new RewriteHandler(config); |
| 44 | handler.add(CollectionConstraint.class); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 45 | QuerySerializer s = new QuerySerializer(); |
| 46 | s.setQuery(simple_add_query, "poliqarp"); |
| 47 | s.setCollection("corpusID=BRZ13 & corpusID=WPD"); |
| 48 | String result = s.toJSON(); |
| 49 | JsonNode node = JsonUtils.readTree(handler.apply(result, null)); |
| 50 | |
| 51 | assert node != null; |
| 52 | assert node.at("/collection/operands").size() == 0; |
| 53 | } |
| 54 | |
Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 55 | //@Test |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 56 | public void testCollectionNodeRemoveGroupedCorpusIdNoErrors() { |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 57 | RewriteHandler handler = new RewriteHandler(config); |
| 58 | handler.add(CollectionConstraint.class); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 59 | QuerySerializer s = new QuerySerializer(); |
| 60 | s.setQuery(simple_add_query, "poliqarp"); |
| 61 | s.setCollection( |
| 62 | "(corpusID=BRZ13 & textClass=Wissenschaft) & corpusID=WPD"); |
| 63 | String result = s.toJSON(); |
| 64 | JsonNode node = JsonUtils.readTree(handler.apply(result, null)); |
| 65 | |
| 66 | assert node != null; |
| 67 | assert node.at("/collection/operands/0/@type").asText() |
| 68 | .equals("koral:docGroup"); |
| 69 | assert node.at("/collection/operands/0/operands/0/key").asText() |
| 70 | .equals("textClass"); |
| 71 | } |
| 72 | |
| 73 | //fixme: will probably fail when one doc groups are being refactored |
Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 74 | //@Test |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 75 | public void testCollectionCleanEmptyDocGroupNoErrors() { |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 76 | RewriteHandler handler = new RewriteHandler(config); |
| 77 | handler.add(CollectionConstraint.class); |
| 78 | handler.add(CollectionCleanupFilter.class); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 79 | QuerySerializer s = new QuerySerializer(); |
| 80 | s.setQuery(simple_add_query, "poliqarp"); |
| 81 | s.setCollection( |
| 82 | "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft & textClass=Sport"); |
| 83 | String result = s.toJSON(); |
| 84 | JsonNode node = JsonUtils.readTree(handler.apply(result, null)); |
| 85 | assert node != null; |
| 86 | assert node.at("/collection/@type").asText().equals("koral:docGroup"); |
| 87 | assert node.at("/collection/operands").size() == 2; |
| 88 | assert node.at("/collection/operands/0/key").asText() |
| 89 | .equals("textClass"); |
| 90 | assert node.at("/collection/operands/1/key").asText() |
| 91 | .equals("textClass"); |
| 92 | } |
| 93 | |
Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 94 | //@Test |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 95 | public void testCollectionCleanMoveOneDocFromGroupUpNoErrors() { |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 96 | RewriteHandler handler = new RewriteHandler(config); |
| 97 | handler.add(CollectionConstraint.class); |
| 98 | handler.add(CollectionCleanupFilter.class); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 99 | QuerySerializer s = new QuerySerializer(); |
| 100 | s.setQuery(simple_add_query, "poliqarp"); |
| 101 | s.setCollection("(corpusID=BRZ13 & textClass=Wissenschaft)"); |
| 102 | String result = s.toJSON(); |
| 103 | JsonNode node = JsonUtils.readTree(handler.apply(result, null)); |
| 104 | assert node != null; |
| 105 | assert node.at("/collection/@type").asText().equals("koral:doc"); |
| 106 | } |
| 107 | |
Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 108 | //@Test |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 109 | public void testCollectionCleanEmptyGroupAndMoveOneFromGroupUpNoErrors() { |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 110 | RewriteHandler handler = new RewriteHandler(config); |
| 111 | handler.add(CollectionConstraint.class); |
| 112 | handler.add(CollectionCleanupFilter.class); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 113 | QuerySerializer s = new QuerySerializer(); |
| 114 | s.setQuery(simple_add_query, "poliqarp"); |
| 115 | s.setCollection( |
| 116 | "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft"); |
| 117 | String result = s.toJSON(); |
| 118 | JsonNode node = JsonUtils.readTree(handler.apply(result, null)); |
| 119 | assert node != null; |
| 120 | assert node.at("/collection/@type").asText().equals("koral:doc"); |
| 121 | assert node.at("/collection/key").asText().equals("textClass"); |
| 122 | } |
| 123 | |
Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 124 | //@Test |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 125 | public void testCollectionRemoveAndMoveOneFromGroupUpNoErrors() { |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 126 | RewriteHandler handler = new RewriteHandler(config); |
| 127 | handler.add(CollectionConstraint.class); |
| 128 | handler.add(CollectionCleanupFilter.class); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 129 | QuerySerializer s = new QuerySerializer(); |
| 130 | s.setQuery(simple_add_query, "poliqarp"); |
| 131 | s.setCollection( |
| 132 | "(docID=random & textClass=Wissenschaft) & corpusID=WPD"); |
| 133 | String result = s.toJSON(); |
| 134 | JsonNode node = JsonUtils.readTree(handler.apply(result, null)); |
| 135 | System.out.println("original node " + result); |
| 136 | System.out.println("result node " + node); |
| 137 | assert node != null; |
| 138 | assert node.at("/collection/@type").asText().equals("koral:docGroup"); |
| 139 | assert node.at("/collection/operands").size() == 2; |
| 140 | } |
| 141 | |
Michael Hanl | fbd6fbb | 2015-09-14 16:10:12 +0200 | [diff] [blame] | 142 | } |