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