Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 1 | import com.fasterxml.jackson.databind.JsonNode; |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 2 | import de.ids_mannheim.korap.query.serialize.QuerySerializer; |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 3 | import de.ids_mannheim.korap.utils.CollectionQueryBuilder3; |
| 4 | import de.ids_mannheim.korap.utils.JsonUtils; |
Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame^] | 5 | import org.junit.Assert; |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 6 | import org.junit.Test; |
| 7 | |
Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame^] | 8 | import static org.junit.Assert.assertEquals; |
| 9 | import static org.junit.Assert.assertNotNull; |
| 10 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 11 | /** |
| 12 | * @author hanl |
| 13 | * @date 12/08/2015 |
| 14 | */ |
| 15 | public class CollectionQueryBuilderTest { |
| 16 | |
| 17 | @Test |
| 18 | public void testsimpleAdd() { |
| 19 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 20 | b.addQuery("corpusID=WPD"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 21 | |
| 22 | JsonNode node = JsonUtils.readTree(b.toJSON()); |
| 23 | |
Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame^] | 24 | assertNotNull(node); |
| 25 | assertEquals("koral:doc",node.at("/collection/@type").asText()); |
| 26 | assertEquals("corpusID",node.at("/collection/key").asText()); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | @Test |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 30 | public void testSimpleConjunction() { |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 31 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 32 | b.addQuery("corpusID=WPD & textClass=freizeit"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 33 | JsonNode node = JsonUtils.readTree(b.toJSON()); |
| 34 | |
Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame^] | 35 | assertNotNull(node); |
| 36 | assertEquals("koral:docGroup",node.at("/collection/@type").asText()); |
| 37 | assertEquals("operation:and",node.at("/collection/operation").asText()); |
| 38 | |
| 39 | assertEquals("corpusID",node.at("/collection/operands/0/key").asText()); |
| 40 | assertEquals("textClass",node.at("/collection/operands/1/key").asText()); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Test |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 44 | public void testSimpleDisjunction() { |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 45 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 46 | b.addQuery("corpusID=WPD | textClass=freizeit"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 47 | JsonNode node = JsonUtils.readTree(b.toJSON()); |
| 48 | |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 49 | System.out.println("_____________________________________________"); |
| 50 | System.out.println(node); |
| 51 | |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 52 | assert node != null; |
| 53 | assert node.at("/collection/operation").asText().equals("operation:or"); |
| 54 | assert node.at("/collection/operands/0/key").asText() |
| 55 | .equals("corpusID"); |
| 56 | assert node.at("/collection/operands/1/key").asText() |
| 57 | .equals("textClass"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Test |
| 61 | public void testComplexSubQuery() { |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 62 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 63 | b.addQuery( |
| 64 | "(corpusID=WPD) | (textClass=freizeit & corpusID=WPD)"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 65 | JsonNode node = JsonUtils.readTree(b.toJSON()); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 66 | |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 67 | System.out.println( |
| 68 | "_____________________________________________ COMPLEX"); |
| 69 | System.out.println(node); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 70 | assert node != null; |
| 71 | assert node.at("/collection/operation").asText().equals("operation:or"); |
| 72 | assert node.at("/collection/operands/0/key").asText() |
| 73 | .equals("corpusID"); |
| 74 | assert node.at("/collection/operands/1/@type").asText() |
| 75 | .equals("koral:docGroup"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 76 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Test |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 80 | public void testAddResourceQueryAfter() { |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 81 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 82 | b.addQuery("(corpusID=ADF) | (textClass=freizeit & corpusID=WPD)"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 83 | |
| 84 | CollectionQueryBuilder3 c = new CollectionQueryBuilder3(); |
| 85 | c.setBaseQuery(b.toJSON()); |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 86 | c.addQuery("textClass=wissenschaft"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 87 | |
| 88 | JsonNode node = JsonUtils.readTree(c.toJSON()); |
| 89 | |
| 90 | assert node != null; |
| 91 | assert node.at("/collection/operands/2/@type").asText() |
| 92 | .equals("koral:doc"); |
| 93 | assert node.at("/collection/operands/2/value").asText() |
| 94 | .equals("wissenschaft"); |
| 95 | } |
| 96 | |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 97 | @Test |
| 98 | public void testAddComplexResourceQueryAfter() { |
| 99 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 100 | b.addQuery("(corpusID=ADF) | (textClass=freizeit & corpusID=WPD)"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 101 | |
| 102 | CollectionQueryBuilder3 c = new CollectionQueryBuilder3(); |
| 103 | c.setBaseQuery(b.toJSON()); |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 104 | c.addQuery("(textClass=politik & corpusID=AZPS)"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 105 | |
| 106 | JsonNode node = JsonUtils.readTree(c.toJSON()); |
| 107 | |
| 108 | assert node != null; |
| 109 | assert node.at("/collection/operands/2/@type").asText() |
| 110 | .equals("koral:docGroup"); |
| 111 | assert node.at("/collection/operands/2/operands/0/value").asText() |
| 112 | .equals("politik"); |
| 113 | assert node.at("/collection/operands/2/operands/1/value").asText() |
| 114 | .equals("AZPS"); |
| 115 | |
| 116 | } |
| 117 | |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 118 | @Test |
| 119 | public void buildQuery() { |
| 120 | String query = "[base=Haus]"; |
| 121 | QuerySerializer s = new QuerySerializer(); |
| 122 | s.setQuery(query, "poliqarp"); |
| 123 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
| 124 | b.addQuery("corpusID=WPD"); |
| 125 | s.setCollection("corpusID=WPD"); |
| 126 | |
| 127 | System.out.println("QUERY " + s.toJSON()); |
| 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void testBaseQueryBuild() { |
| 132 | |
| 133 | } |
| 134 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 135 | } |