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; |
| 10 | import org.junit.Test; |
| 11 | |
Michael Hanl | fbd6fbb | 2015-09-14 16:10:12 +0200 | [diff] [blame] | 12 | /** |
| 13 | * @author hanl |
| 14 | * @date 03/09/2015 |
| 15 | */ |
| 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 | |
| 28 | @Test |
| 29 | public void testCollectionNodeRemoveCorpusIdNoErrors() { |
| 30 | RewriteHandler handler = new RewriteHandler(); |
| 31 | handler.add(new CollectionConstraint()); |
| 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 | |
| 41 | @Test |
| 42 | public void testCollectionNodeRemoveAllCorpusIdNoErrors() { |
| 43 | RewriteHandler handler = new RewriteHandler(); |
| 44 | handler.add(new CollectionConstraint()); |
| 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 | |
| 55 | @Test |
| 56 | public void testCollectionNodeRemoveGroupedCorpusIdNoErrors() { |
| 57 | RewriteHandler handler = new RewriteHandler(); |
| 58 | handler.add(new CollectionConstraint()); |
| 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 |
| 74 | @Test |
| 75 | public void testCollectionCleanEmptyDocGroupNoErrors() { |
| 76 | RewriteHandler handler = new RewriteHandler(); |
| 77 | handler.add(new CollectionConstraint()); |
| 78 | handler.add(new CollectionCleanupFilter()); |
| 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 | |
| 94 | @Test |
| 95 | public void testCollectionCleanMoveOneDocFromGroupUpNoErrors() { |
| 96 | RewriteHandler handler = new RewriteHandler(); |
| 97 | handler.add(new CollectionConstraint()); |
| 98 | handler.add(new CollectionCleanupFilter()); |
| 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 | |
| 108 | @Test |
| 109 | public void testCollectionCleanEmptyGroupAndMoveOneFromGroupUpNoErrors() { |
| 110 | RewriteHandler handler = new RewriteHandler(); |
| 111 | handler.add(new CollectionConstraint()); |
| 112 | handler.add(new CollectionCleanupFilter()); |
| 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 | |
| 124 | @Test |
| 125 | public void testCollectionRemoveAndMoveOneFromGroupUpNoErrors() { |
| 126 | RewriteHandler handler = new RewriteHandler(); |
| 127 | handler.add(new CollectionConstraint()); |
| 128 | handler.add(new CollectionCleanupFilter()); |
| 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 | } |