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