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; |
Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 8 | import de.ids_mannheim.korap.user.User; |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 9 | import de.ids_mannheim.korap.utils.JsonUtils; |
Michael Hanl | 9592b0a | 2015-10-28 19:04:08 +0100 | [diff] [blame] | 10 | import org.junit.AfterClass; |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 11 | import org.junit.BeforeClass; |
Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 12 | import org.junit.Test; |
| 13 | |
| 14 | import java.util.regex.Matcher; |
| 15 | import java.util.regex.Pattern; |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 16 | |
Michael Hanl | fbd6fbb | 2015-09-14 16:10:12 +0200 | [diff] [blame] | 17 | /** |
| 18 | * @author hanl |
| 19 | * @date 03/09/2015 |
| 20 | */ |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 21 | // todo: 20.10.15 |
Michael Hanl | fbd6fbb | 2015-09-14 16:10:12 +0200 | [diff] [blame] | 22 | public class CollectionRewriteTest { |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 23 | |
| 24 | private static String simple_add_query = "[pos=ADJA]"; |
| 25 | |
| 26 | private static KustvaktConfiguration config; |
| 27 | |
| 28 | @BeforeClass |
| 29 | public static void init() { |
Michael Hanl | 2760cc4 | 2015-11-16 19:30:01 +0100 | [diff] [blame] | 30 | BeanConfiguration.loadClasspathContext("test-config.xml"); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 31 | config = BeanConfiguration.getBeans().getConfiguration(); |
| 32 | } |
| 33 | |
Michael Hanl | 9592b0a | 2015-10-28 19:04:08 +0100 | [diff] [blame] | 34 | @AfterClass |
| 35 | public static void close() { |
| 36 | BeanConfiguration.closeApplication(); |
| 37 | } |
| 38 | |
Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 39 | @Test |
| 40 | public void test2() { |
| 41 | Pattern p = Pattern.compile("([\\.\\w]+)\\((.+)\\)"); |
| 42 | String cl = de.ids_mannheim.korap.security.ac.SecurityManager.class |
| 43 | .getCanonicalName(); |
| 44 | Matcher m = p.matcher(cl); |
| 45 | System.out.println("FOUND SOMETHING?! " + m.find()); |
| 46 | while (m.find()) |
| 47 | System.out.println("group 1 " + m.group(1)); |
Michael Hanl | 9592b0a | 2015-10-28 19:04:08 +0100 | [diff] [blame] | 48 | |
Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Test |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 52 | public void testCollectionNodeRemoveCorpusIdNoErrors() { |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 53 | RewriteHandler handler = new RewriteHandler(config); |
| 54 | handler.add(CollectionConstraint.class); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 55 | QuerySerializer s = new QuerySerializer(); |
| 56 | s.setQuery(simple_add_query, "poliqarp"); |
| 57 | s.setCollection("textClass=politik & corpusID=WPD"); |
| 58 | String result = s.toJSON(); |
Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 59 | JsonNode node = JsonUtils.readTree( |
| 60 | handler.preProcess(result, User.UserFactory.getUser("test_user"))); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 61 | assert node != null; |
| 62 | assert node.at("/collection/operands").size() == 1; |
| 63 | } |
| 64 | |
Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 65 | @Test |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 66 | public void testCollectionNodeRemoveAllCorpusIdNoErrors() { |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 67 | RewriteHandler handler = new RewriteHandler(config); |
| 68 | handler.add(CollectionConstraint.class); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 69 | QuerySerializer s = new QuerySerializer(); |
| 70 | s.setQuery(simple_add_query, "poliqarp"); |
| 71 | s.setCollection("corpusID=BRZ13 & corpusID=WPD"); |
| 72 | String result = s.toJSON(); |
Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 73 | JsonNode node = JsonUtils.readTree( |
| 74 | handler.preProcess(result, User.UserFactory.getUser("test_user"))); |
| 75 | System.out.println("RESULTING REWR NODE " + node); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 76 | assert node != null; |
| 77 | assert node.at("/collection/operands").size() == 0; |
| 78 | } |
| 79 | |
Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 80 | @Test |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 81 | public void testCollectionNodeRemoveGroupedCorpusIdNoErrors() { |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 82 | RewriteHandler handler = new RewriteHandler(config); |
| 83 | handler.add(CollectionConstraint.class); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 84 | QuerySerializer s = new QuerySerializer(); |
| 85 | s.setQuery(simple_add_query, "poliqarp"); |
| 86 | s.setCollection( |
| 87 | "(corpusID=BRZ13 & textClass=Wissenschaft) & corpusID=WPD"); |
| 88 | String result = s.toJSON(); |
Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 89 | JsonNode node = JsonUtils.readTree( |
| 90 | handler.preProcess(result, User.UserFactory.getUser("test_user"))); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 91 | |
Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 92 | System.out.println("COLLECTION NODE " + result); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 93 | assert node != null; |
| 94 | assert node.at("/collection/operands/0/@type").asText() |
| 95 | .equals("koral:docGroup"); |
| 96 | assert node.at("/collection/operands/0/operands/0/key").asText() |
| 97 | .equals("textClass"); |
| 98 | } |
| 99 | |
| 100 | //fixme: will probably fail when one doc groups are being refactored |
Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 101 | @Test |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 102 | public void testCollectionCleanEmptyDocGroupNoErrors() { |
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( |
| 109 | "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft & textClass=Sport"); |
| 110 | String result = s.toJSON(); |
Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 111 | JsonNode node = JsonUtils.readTree( |
| 112 | handler.preProcess(result, User.UserFactory.getUser("test_user"))); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 113 | assert node != null; |
| 114 | assert node.at("/collection/@type").asText().equals("koral:docGroup"); |
| 115 | assert node.at("/collection/operands").size() == 2; |
| 116 | assert node.at("/collection/operands/0/key").asText() |
| 117 | .equals("textClass"); |
| 118 | assert node.at("/collection/operands/1/key").asText() |
| 119 | .equals("textClass"); |
| 120 | } |
| 121 | |
Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 122 | @Test |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 123 | public void testCollectionCleanMoveOneDocFromGroupUpNoErrors() { |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 124 | RewriteHandler handler = new RewriteHandler(config); |
| 125 | handler.add(CollectionConstraint.class); |
| 126 | handler.add(CollectionCleanupFilter.class); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 127 | QuerySerializer s = new QuerySerializer(); |
| 128 | s.setQuery(simple_add_query, "poliqarp"); |
| 129 | s.setCollection("(corpusID=BRZ13 & textClass=Wissenschaft)"); |
| 130 | String result = s.toJSON(); |
Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 131 | JsonNode node = JsonUtils.readTree( |
| 132 | handler.preProcess(result, User.UserFactory.getUser("test_user"))); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 133 | assert node != null; |
| 134 | assert node.at("/collection/@type").asText().equals("koral:doc"); |
| 135 | } |
| 136 | |
Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 137 | @Test |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 138 | public void testCollectionCleanEmptyGroupAndMoveOneFromGroupUpNoErrors() { |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 139 | RewriteHandler handler = new RewriteHandler(config); |
| 140 | handler.add(CollectionConstraint.class); |
| 141 | handler.add(CollectionCleanupFilter.class); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 142 | QuerySerializer s = new QuerySerializer(); |
| 143 | s.setQuery(simple_add_query, "poliqarp"); |
| 144 | s.setCollection( |
| 145 | "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft"); |
| 146 | String result = s.toJSON(); |
Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 147 | JsonNode node = JsonUtils.readTree( |
| 148 | handler.preProcess(result, User.UserFactory.getUser("test_user"))); |
| 149 | |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 150 | assert node != null; |
| 151 | assert node.at("/collection/@type").asText().equals("koral:doc"); |
| 152 | assert node.at("/collection/key").asText().equals("textClass"); |
| 153 | } |
| 154 | |
Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 155 | @Test |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 156 | public void testCollectionRemoveAndMoveOneFromGroupUpNoErrors() { |
Michael Hanl | 59bff81 | 2015-10-27 23:10:32 +0100 | [diff] [blame] | 157 | RewriteHandler handler = new RewriteHandler(config); |
| 158 | handler.add(CollectionConstraint.class); |
| 159 | handler.add(CollectionCleanupFilter.class); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 160 | QuerySerializer s = new QuerySerializer(); |
| 161 | s.setQuery(simple_add_query, "poliqarp"); |
| 162 | s.setCollection( |
| 163 | "(docID=random & textClass=Wissenschaft) & corpusID=WPD"); |
| 164 | String result = s.toJSON(); |
Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 165 | JsonNode node = JsonUtils.readTree( |
| 166 | handler.preProcess(result, User.UserFactory.getUser("test_user"))); |
Michael Hanl | 66ca71c | 2015-09-14 23:11:30 +0200 | [diff] [blame] | 167 | System.out.println("original node " + result); |
| 168 | System.out.println("result node " + node); |
| 169 | assert node != null; |
| 170 | assert node.at("/collection/@type").asText().equals("koral:docGroup"); |
| 171 | assert node.at("/collection/operands").size() == 2; |
| 172 | } |
| 173 | |
Michael Hanl | fbd6fbb | 2015-09-14 16:10:12 +0200 | [diff] [blame] | 174 | } |