Skip to content

Commit 148101a

Browse files
committed
fix #181--throw ResourceNotFoundException when nothing found on bulk read calls, add negative tests to validate that scenario
1 parent 8751b49 commit 148101a

File tree

4 files changed

+156
-118
lines changed

4 files changed

+156
-118
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,11 @@ private JerseyResultIterator getBulkDocumentsImpl(RequestLogger reqlog,
831831
}
832832
JerseyResultIterator iterator = getIteratedResourceImpl(DefaultJerseyResultIterator.class,
833833
reqlog, path, params, MultiPartMediaTypes.MULTIPART_MIXED);
834+
if ( iterator == null ) {
835+
StringBuffer uriString = new StringBuffer();
836+
for ( String uri : uris ) uriString.append(" \"" + uri + "\"");
837+
throw new ResourceNotFoundException("Could not find any documents with uris:" + uriString);
838+
}
834839
if ( iterator.getStart() == -1 ) iterator.setStart(1);
835840
if ( iterator.getSize() != -1 ) {
836841
if ( iterator.getPageSize() == -1 ) iterator.setPageSize(iterator.getSize());

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

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package com.marklogic.client.pojo;
1717

18+
import com.marklogic.client.FailedRequestException;
19+
import com.marklogic.client.ForbiddenUserException;
20+
import com.marklogic.client.ResourceNotFoundException;
1821
import com.marklogic.client.Transaction;
1922
import com.marklogic.client.io.marker.SearchReadHandle;
2023

@@ -78,84 +81,113 @@ public interface PojoRepository<T, ID extends Serializable> {
7881
* {@link com.fasterxml.jackson.databind.ObjectMapper ObjectMapper} to generate the
7982
* serialized JSON format.
8083
*/
81-
public void write(T entity);
84+
public void write(T entity)
85+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
8286
/** Does everything in {@link #write(Object) write(T)} but also adds your collections to the
8387
* persisted instance.
8488
*/
85-
public void write(T entity, String... collections);
89+
public void write(T entity, String... collections)
90+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
8691
/** Does everything in {@link #write(Object) write(T)} but in your
8792
* <a href="http://docs.marklogic.com/guide/app-dev/transactions">
8893
* multi-statement transaction</a> context.
8994
*/
90-
public void write(T entity, Transaction transaction);
95+
public void write(T entity, Transaction transaction)
96+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
9197
/** Does everything in {@link #write(Object) write(T)} but also adds your collections to the
9298
* persisted instance and performs the write in your
9399
* <a href="http://docs.marklogic.com/guide/app-dev/transactions">
94100
* multi-statement transaction</a> context.
95101
* .
96102
*/
97-
public void write(T entity, Transaction transaction, String... collections);
103+
public void write(T entity, Transaction transaction, String... collections)
104+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
98105

99106
/** @return true if a document exists in the database with the id */
100-
public boolean exists(ID id);
107+
public boolean exists(ID id)
108+
throws ForbiddenUserException, FailedRequestException;
101109

102110
/** @return in the context of transaction, true if a document exists in the database with
103111
* the id */
104-
public boolean exists(ID id, Transaction transaction);
112+
public boolean exists(ID id, Transaction transaction)
113+
throws ForbiddenUserException, FailedRequestException;
105114

106115
/** @return the number of documents of type T persisted in the database */
107-
public long count();
116+
public long count()
117+
throws ForbiddenUserException, FailedRequestException;
108118

109119
/** @return in the context of transaction, the number of documents of type T persisted in
110120
* the database */
111-
public long count(Transaction transaction);
121+
public long count(Transaction transaction)
122+
throws ForbiddenUserException, FailedRequestException;
112123

113124
/** @return the number of documents of type T persisted in the database with at least
114125
* one of the criteria collections*/
115-
public long count(String... collection);
126+
public long count(String... collection)
127+
throws ForbiddenUserException, FailedRequestException;
116128

117129
/** @return in the context of transaction, the number of documents of type T persisted in
118130
* the database with at least one of the criteria collections*/
119-
public long count(String[] collections, Transaction transaction);
131+
public long count(String[] collections, Transaction transaction)
132+
throws ForbiddenUserException, FailedRequestException;
120133

121134
/** @return the number of documents of type T persisted in the database which match
122135
* the query */
123-
public long count(PojoQueryDefinition query);
136+
public long count(PojoQueryDefinition query)
137+
throws ForbiddenUserException, FailedRequestException;
124138

125139
/** @return in the context of transaction, the number of documents of type T persisted in the
126140
* database which match the query */
127-
public long count(PojoQueryDefinition query, Transaction transaction);
141+
public long count(PojoQueryDefinition query, Transaction transaction)
142+
throws ForbiddenUserException, FailedRequestException;
128143

129144
/** Deletes from the database the documents with the corresponding ids */
130-
public void delete(ID... ids);
145+
public void delete(ID... ids)
146+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
131147

132148
/** As part of transaction, deletes from the database the documents with the corresponding ids */
133-
public void delete(ID[] ids, Transaction transaction);
149+
public void delete(ID[] ids, Transaction transaction)
150+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
134151

135152
/** Deletes from the database all documents of type T persisted by the pojo facade */
136-
public void deleteAll();
153+
public void deleteAll()
154+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
137155

138156
/** As part of transaction, deletes from the database all documents of type T persisted by
139157
* the pojo facade */
140-
public void deleteAll(Transaction transaction);
158+
public void deleteAll(Transaction transaction)
159+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
141160

142161
/* REST API does not currently support DELETE /search with multiple collection arguments
143-
public void deleteAll(String... collections);
162+
public void deleteAll(String... collections)
163+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
144164
*/
145165

146-
public T read(ID id);
147-
public T read(ID id, Transaction transaction);
148-
public PojoPage<T> read(ID[] ids);
149-
public PojoPage<T> read(ID[] ids, Transaction transaction);
150-
public PojoPage<T> readAll(long start);
151-
public PojoPage<T> readAll(long start, Transaction transaction);
166+
public T read(ID id)
167+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
168+
public T read(ID id, Transaction transaction)
169+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
170+
public PojoPage<T> read(ID[] ids)
171+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
172+
public PojoPage<T> read(ID[] ids, Transaction transaction)
173+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
174+
public PojoPage<T> readAll(long start)
175+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
176+
public PojoPage<T> readAll(long start, Transaction transaction)
177+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
152178

153-
public PojoPage<T> search(long start, String... collections);
154-
public PojoPage<T> search(long start, Transaction transaction, String... collections);
155-
public PojoPage<T> search(PojoQueryDefinition query, long start);
156-
public PojoPage<T> search(PojoQueryDefinition query, long start, Transaction transaction);
157-
public PojoPage<T> search(PojoQueryDefinition query, long start, SearchReadHandle searchHandle);
158-
public PojoPage<T> search(PojoQueryDefinition query, long start, SearchReadHandle searchHandle, Transaction transaction);
179+
public PojoPage<T> search(long start, String... collections)
180+
throws ForbiddenUserException, FailedRequestException;
181+
public PojoPage<T> search(long start, Transaction transaction, String... collections)
182+
throws ForbiddenUserException, FailedRequestException;
183+
public PojoPage<T> search(PojoQueryDefinition query, long start)
184+
throws ForbiddenUserException, FailedRequestException;
185+
public PojoPage<T> search(PojoQueryDefinition query, long start, Transaction transaction)
186+
throws ForbiddenUserException, FailedRequestException;
187+
public PojoPage<T> search(PojoQueryDefinition query, long start, SearchReadHandle searchHandle)
188+
throws ForbiddenUserException, FailedRequestException;
189+
public PojoPage<T> search(PojoQueryDefinition query, long start, SearchReadHandle searchHandle, Transaction transaction)
190+
throws ForbiddenUserException, FailedRequestException;
159191

160192
public PojoQueryBuilder<T> getQueryBuilder();
161193

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.marklogic.client.DatabaseClient;
4242
import com.marklogic.client.DatabaseClientFactory;
4343
import com.marklogic.client.DatabaseClientFactory.Authentication;
44+
import com.marklogic.client.ResourceNotFoundException;
4445
import com.marklogic.client.Transaction;
4546
import com.marklogic.client.document.DocumentDescriptor;
4647
import com.marklogic.client.document.DocumentManager.Metadata;
@@ -184,6 +185,30 @@ public void testB_BulkRead() {
184185
assertEquals("Wrong start", 1, page.getStart());
185186
assertEquals("Wrong totalPages", 1, page.getTotalPages());
186187
assertEquals("Wrong estimate", 3, page.getTotalSize());
188+
189+
// test reading a valid plus a non-existent document
190+
page = docMgr.read(DIRECTORY + "1016670.xml", "nonExistant.doc");
191+
assertEquals("Should have results", true, page.hasContent());
192+
assertEquals("Failed to report number of records expected", 1, page.size());
193+
assertEquals("Wrong only doc", DIRECTORY + "1016670.xml", page.next().getUri());
194+
195+
// test reading multiple non-existent documents
196+
boolean exceptionThrown = false;
197+
try {
198+
docMgr.read("nonExistant.doc", "nonExistant2.doc");
199+
} catch (ResourceNotFoundException e) {
200+
exceptionThrown = true;
201+
}
202+
assertTrue("ResourceNotFoundException should have been thrown", exceptionThrown);
203+
204+
// test reading a non-existent document (not actually a bulk operation)
205+
exceptionThrown = false;
206+
try {
207+
docMgr.read("nonExistant.doc", new StringHandle());
208+
} catch (ResourceNotFoundException e) {
209+
exceptionThrown = true;
210+
}
211+
assertTrue("ResourceNotFoundException should have been thrown", exceptionThrown);
187212
}
188213

189214
@Test

0 commit comments

Comments
 (0)