Skip to content

Commit dc2e8b5

Browse files
committed
add more tests of querying pojos, and related fixes
1 parent ae720f4 commit dc2e8b5

File tree

6 files changed

+67
-13
lines changed

6 files changed

+67
-13
lines changed

src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,19 @@ public StructuredQueryDefinition value(String pojoField, String[] options,
6767
{
6868
return value(jsonProperty(pojoField), null, options, weight, values);
6969
}
70-
public StructuredQueryDefinition word(String pojoField, String... words) {
71-
return word(jsonProperty(pojoField), words);
70+
public StructuredQueryDefinition word(String pojoField, String[] words) {
71+
return super.word(jsonProperty(pojoField), words);
7272
}
7373
public StructuredQueryDefinition word(String pojoField, String[] options,
7474
double weight, String... words)
7575
{
76-
return word(jsonProperty(pojoField), null, options, weight, words);
76+
return super.word(jsonProperty(pojoField), null, options, weight, words);
7777
}
7878
public StructuredQueryDefinition word(String... words) {
79-
return word(words);
79+
return super.word(jsonProperty(classWrapper), words);
8080
}
8181
public StructuredQueryDefinition word(String[] options, double weight, String... words) {
82-
return word(options, weight, words);
82+
return super.word(jsonProperty(classWrapper), null, options, weight, words);
8383
}
8484

8585
public String getRangeIndexType(String fieldName) {

src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ public long count() {
9898

9999
public long count(String... collections) {
100100
if ( collections == null ) return 0l;
101-
return count(qb.collection(collections));
101+
return count(wrapQuery(qb.collection(collections)));
102102
}
103103
public long count(QueryDefinition query) {
104104
long pageLength = getPageLength();
105105
setPageLength(0);
106-
DocumentPage page = docMgr.search(wrapQuery(query), 1);
106+
PojoPage page = search(query, 1);
107107
setPageLength(pageLength);
108108
return page.getTotalSize();
109109
}
@@ -202,6 +202,10 @@ public PojoPage<T> search(QueryDefinition query, long start, SearchReadHandle se
202202
responseHandle.setQueryCriteria(query);
203203
}
204204
docMgr.setResponseFormat(searchBase.getFormat());
205+
} else {
206+
// TODO: remove this once REST API only considers Content-type header
207+
// (not format parameter) for expceted payload format
208+
docMgr.setResponseFormat(Format.XML);
205209
}
206210

207211
String tid = transaction == null ? null : transaction.getTransactionId();
@@ -213,6 +217,10 @@ public PojoPage<T> search(QueryDefinition query, long start, SearchReadHandle se
213217
return pojoPage;
214218
}
215219

220+
public PojoQueryBuilder getQueryBuilder() {
221+
return qb;
222+
}
223+
216224
public long getPageLength() {
217225
return docMgr.getPageLength();
218226
}

src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public StructuredQueryDefinition range(String pojoField, String[] options,
2525
public StructuredQueryDefinition value(String pojoField, String... values);
2626
public StructuredQueryDefinition value(String pojoField, String[] options,
2727
double weight, String... values);
28-
public StructuredQueryDefinition word(String pojoField, String... words);
28+
public StructuredQueryDefinition word(String pojoField, String[] words);
2929
public StructuredQueryDefinition word(String pojoField, String[] options,
3030
double weight, String... words);
3131
public StructuredQueryDefinition word(String... words);

src/main/java/com/marklogic/client/pojo/PojoRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public interface PojoRepository<T, ID extends Serializable> {
3838
public PojoPage<T> search(QueryDefinition query, long start, SearchReadHandle searchHandle);
3939
public PojoPage<T> search(QueryDefinition query, long start, SearchReadHandle searchHandle, Transaction transaction);
4040

41+
public PojoQueryBuilder getQueryBuilder();
42+
4143
public long getPageLength(); // default: 50
4244
public void setPageLength(long length);
4345

src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,6 +2252,7 @@ static private void writeTextList(XMLStreamWriter serializer,
22522252
return;
22532253
}
22542254
for (Object object: objects) {
2255+
if ( object == null ) continue;
22552256
serializer.writeStartElement(container);
22562257
serializer.writeCharacters(
22572258
(object instanceof String) ?

src/test/java/com/marklogic/client/test/PojoFacadeTest.java

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.Assert.assertNotNull;
2020
import static org.junit.Assert.assertTrue;
2121

22+
import java.util.Arrays;
2223
import java.util.Iterator;
2324

2425
import org.junit.FixMethodOrder;
@@ -29,7 +30,9 @@
2930

3031
import com.marklogic.client.pojo.PojoPage;
3132
import com.marklogic.client.pojo.PojoRepository;
33+
import com.marklogic.client.query.QueryDefinition;
3234
import com.marklogic.client.query.StringQueryDefinition;
35+
import com.marklogic.client.pojo.PojoQueryBuilder;
3336
import com.marklogic.client.test.BulkReadWriteTest;
3437
import com.marklogic.client.test.BulkReadWriteTest.City;
3538
import com.marklogic.client.test.BulkReadWriteTest.CityWriter;
@@ -45,7 +48,7 @@ public class PojoFacadeTest {
4548
public static void beforeClass() {
4649
Common.connect();
4750
repository = Common.client.newPojoRepository(City.class, Integer.class);
48-
//System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
51+
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
4952
}
5053
@AfterClass
5154
public static void afterClass() {
@@ -90,17 +93,57 @@ public void testB_ReadPojos() throws Exception {
9093
}
9194

9295
@Test
93-
public void testC_DeletePojos() throws Exception {
96+
public void testC_QueryPojos() throws Exception {
97+
PojoQueryBuilder<City> qb = repository.getQueryBuilder();
98+
QueryDefinition query = qb.word("Tungi", "Dalatando", "Chittagong");
99+
PojoPage<City> page = repository.search(query, 1);
100+
Iterator<City> iterator = page.iterator();
101+
int numRead = 0;
102+
while ( iterator.hasNext() ) {
103+
numRead++;
104+
}
105+
assertEquals("Failed to find number of records expected", 3, numRead);
106+
assertEquals("PojoPage failed to report number of records expected", numRead, page.size());
107+
108+
qb = repository.getQueryBuilder();
109+
query = qb.value("continent", "AF");
110+
page = repository.search(query, 1);
111+
iterator = page.iterator();
112+
numRead = 0;
113+
while ( iterator.hasNext() ) {
114+
City city = iterator.next();
115+
assertEquals("Wrong continent", "AF", city.getContinent());
116+
numRead++;
117+
}
118+
assertEquals("Failed to find number of records expected", 7, numRead);
119+
assertEquals("PojoPage failed to report number of records expected", numRead, page.size());
120+
121+
query = qb.containerQuery("alternateNames", qb.word("San", "Santo"));
122+
page = repository.search(query, 1);
123+
iterator = page.iterator();
124+
numRead = 0;
125+
while ( iterator.hasNext() ) {
126+
City city = iterator.next();
127+
String alternateNames = Arrays.asList(city.getAlternateNames()).toString();
128+
assertTrue("Should contain San", alternateNames.contains("San"));
129+
numRead++;
130+
}
131+
assertEquals("Failed to find number of records expected", 11, numRead);
132+
assertEquals("PojoPage failed to report number of records expected", numRead, page.size());
133+
}
134+
135+
@Test
136+
public void testD_DeletePojos() throws Exception {
94137
repository.delete(1185098, 2239076);
95138
StringQueryDefinition query = Common.client.newQueryManager().newStringDefinition();
96139
query.setCriteria("Tungi OR Dalatando OR Chittagong");
97140
PojoPage<City> page = repository.search(query, 1);
98141
assertEquals("Failed to read number of records expected", 1, page.getTotalSize());
99142

100143
// now delete them all
101-
repository.delete((String)null);
102-
page = repository.search(query, 1);
103-
assertEquals("Failed to read number of records expected", 0, page.getTotalSize());
144+
//repository.delete((String)null);
145+
long count = repository.count((String) null);
146+
assertEquals("Failed to read number of records expected", 0, count);
104147
}
105148

106149
private void validateCity(City city) {

0 commit comments

Comments
 (0)