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