|
14 | 14 | import com.marklogic.client.io.StringHandle; |
15 | 15 | import com.marklogic.client.query.*; |
16 | 16 | import com.marklogic.client.util.EditableNamespaceContext; |
| 17 | +import org.jdom2.Namespace; |
| 18 | +import org.jdom2.input.SAXBuilder; |
17 | 19 | import org.junit.jupiter.api.BeforeAll; |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
18 | 21 | import org.junit.jupiter.api.Test; |
19 | 22 | import org.w3c.dom.Document; |
20 | 23 | import org.w3c.dom.Element; |
21 | 24 | import org.xml.sax.SAXException; |
22 | 25 |
|
23 | 26 | import java.io.IOException; |
| 27 | +import java.io.StringReader; |
24 | 28 | import java.util.concurrent.atomic.AtomicInteger; |
25 | 29 |
|
26 | 30 | import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; |
27 | 31 | import static org.junit.jupiter.api.Assertions.*; |
28 | 32 |
|
29 | 33 | public class StructuredSearchTest { |
30 | 34 |
|
| 35 | + private QueryManager queryManager; |
| 36 | + private StructuredQueryBuilder queryBuilder; |
| 37 | + |
31 | 38 | @BeforeAll |
32 | 39 | public static void beforeClass() { |
33 | 40 | Common.connect(); |
34 | 41 | } |
35 | 42 |
|
| 43 | + @BeforeEach |
| 44 | + void setup() { |
| 45 | + queryManager = Common.client.newQueryManager(); |
| 46 | + queryBuilder = queryManager.newStructuredQueryBuilder(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void trueQuery() { |
| 51 | + long total = queryManager.search(queryBuilder.and( |
| 52 | + queryBuilder.trueQuery(), |
| 53 | + queryBuilder.collection("zipcode") |
| 54 | + ), new SearchHandle()).getTotalResults(); |
| 55 | + assertEquals(2, total, "Expecting 2 documents in the zipcode collection, and the trueQuery should not affect that."); |
| 56 | + |
| 57 | + total = queryManager.search(queryBuilder.trueQuery(), new SearchHandle()).getTotalResults(); |
| 58 | + assertTrue(total > 2, "Expecting all documents to be returned, which should be more than the 2 in the " + |
| 59 | + "zipcode collection. Actual count: " + total); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void falseQuery() { |
| 64 | + long total = queryManager.search(queryBuilder.and( |
| 65 | + queryBuilder.falseQuery(), |
| 66 | + queryBuilder.collection("zipcode") |
| 67 | + ), new SearchHandle()).getTotalResults(); |
| 68 | + assertEquals(0, total, "Expecting 0 documents due to the false query."); |
| 69 | + |
| 70 | + total = queryManager.search(queryBuilder.falseQuery(), new SearchHandle()).getTotalResults(); |
| 71 | + assertEquals(0, total, "A false-query should return zero documents."); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void operatorState() throws Exception { |
| 76 | + StructuredQueryDefinition query = queryBuilder.operatorState("sort", "date"); |
| 77 | + String xml = query.serialize(); |
| 78 | + |
| 79 | + org.jdom2.Document doc = new SAXBuilder().build(new StringReader(xml)); |
| 80 | + Namespace ns = Namespace.getNamespace("http://marklogic.com/appservices/search"); |
| 81 | + org.jdom2.Element operatorState = doc.getRootElement().getChild("operator-state", ns); |
| 82 | + assertEquals("sort", operatorState.getChildText("operator-name", ns)); |
| 83 | + assertEquals("date", operatorState.getChildText("state-name", ns)); |
| 84 | + } |
| 85 | + |
36 | 86 | @Test |
37 | 87 | public void testStructuredSearch() { |
38 | 88 | QueryManager queryMgr = Common.client.newQueryManager(); |
|
0 commit comments