1010import static com .jayway .jsonpath .JsonPath .read ;
1111import static org .apache .commons .codec .CharEncoding .UTF_8 ;
1212import static org .apache .commons .io .IOUtils .toInputStream ;
13+ import static org .dspace .app .rest .utils .Utils .DEFAULT_PAGE_SIZE ;
1314import static org .hamcrest .Matchers .is ;
1415import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .delete ;
1516import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
5455import org .junit .Before ;
5556import org .junit .Test ;
5657import org .springframework .beans .factory .annotation .Autowired ;
58+ import org .springframework .test .web .servlet .request .MockHttpServletRequestBuilder ;
5759
5860/**
5961 * Integration tests for the Clarin License Rest Repository
6264 */
6365public class ClarinLicenseRestRepositoryIT extends AbstractControllerIntegrationTest {
6466
67+ private static final String BASE_URI = "/api/core/clarinlicenses" ;
68+
6569 @ Autowired
6670 ClarinLicenseService clarinLicenseService ;
6771
@@ -188,7 +192,7 @@ public void clarinLicensesAndLicenseLabelsAreInitialized() throws Exception {
188192 @ Test
189193 public void findAll () throws Exception {
190194 String authTokenAdmin = getAuthToken (admin .getEmail (), password );
191- getClient (authTokenAdmin ).perform (get ("/api/core/clarinlicenses" ))
195+ getClient (authTokenAdmin ).perform (get (BASE_URI ))
192196 .andExpect (status ().isOk ())
193197 .andExpect (content ().contentType (contentType ))
194198 .andExpect (jsonPath ("$._embedded.clarinlicenses" , Matchers .hasItem (
@@ -207,8 +211,7 @@ public void findAll() throws Exception {
207211 Objects .requireNonNull (getExtendedLicenseLabels (
208212 firstCLicense .getLicenseLabels ())))
209213 )))
210- .andExpect (jsonPath ("$._links.self.href" ,
211- Matchers .containsString ("/api/core/clarinlicenses" )));
214+ .andExpect (jsonPath ("$._links.self.href" , Matchers .containsString (BASE_URI )));
212215 }
213216
214217 /**
@@ -217,7 +220,7 @@ public void findAll() throws Exception {
217220 @ Test
218221 public void searchBy () throws Exception {
219222 String authTokenAdmin = getAuthToken (admin .getEmail (), password );
220- getClient (authTokenAdmin ).perform (get ("/api/core/clarinlicenses /search/byName" )
223+ getClient (authTokenAdmin ).perform (get (BASE_URI + " /search/byName" )
221224 .param ("name" , "CL Name1" ))
222225 .andExpect (status ().isOk ())
223226 .andExpect (content ().contentType (contentType ))
@@ -232,7 +235,7 @@ public void searchBy() throws Exception {
232235 @ Test
233236 public void searchByLike () throws Exception {
234237 String authTokenAdmin = getAuthToken (admin .getEmail (), password );
235- getClient (authTokenAdmin ).perform (get ("/api/core/clarinlicenses /search/byNameLike" )
238+ getClient (authTokenAdmin ).perform (get (BASE_URI + " /search/byNameLike" )
236239 .param ("name" , "Name" ))
237240 .andExpect (status ().isOk ())
238241 .andExpect (content ().contentType (contentType ))
@@ -252,8 +255,17 @@ public void searchByLike() throws Exception {
252255 Objects .requireNonNull (getExtendedLicenseLabels (
253256 firstCLicense .getLicenseLabels ())))
254257 )))
255- .andExpect (jsonPath ("$._links.self.href" ,
256- Matchers .containsString ("/api/core/clarinlicenses" )));
258+ .andExpect (jsonPath ("$._links.self.href" , Matchers .containsString (BASE_URI )));
259+ }
260+
261+ /**
262+ * This method tests requests for incorrect page parameter (e.g. page=5).
263+ */
264+ @ Test
265+ public void searchWithIncorrectPageParameter () throws Exception {
266+ testRequestWithIncorrectPageable ("" , null , 2 , 10 );
267+ testRequestWithIncorrectPageable ("/search/byName" , "CL Name1" , 1 , 20 );
268+ testRequestWithIncorrectPageable ("/search/byNameLike" , "Name" , 2 , 30 );
257269 }
258270
259271 @ Test
@@ -276,7 +288,7 @@ public void create() throws Exception {
276288 AtomicReference <Integer > idRef = new AtomicReference <>();
277289 String authTokenAdmin = getAuthToken (admin .getEmail (), password );
278290 try {
279- getClient (authTokenAdmin ).perform (post ("/api/core/clarinlicenses" )
291+ getClient (authTokenAdmin ).perform (post (BASE_URI )
280292 .content (new ObjectMapper ().writeValueAsBytes (clarinLicenseRest ))
281293 .contentType (org .springframework .http .MediaType .APPLICATION_JSON ))
282294 .andExpect (status ().isCreated ())
@@ -352,15 +364,15 @@ public void update() throws Exception {
352364 ClarinLicenseRest clarinLicenseRest = clarinLicenseConverter .convert (clarinLicenseUpdated , Projection .DEFAULT );
353365
354366 String authTokenAdmin = getAuthToken (admin .getEmail (), password );
355- getClient (authTokenAdmin ).perform (get ("/api/core/clarinlicenses /" + clarinLicense .getID ()))
367+ getClient (authTokenAdmin ).perform (get (BASE_URI + " /" + clarinLicense .getID ()))
356368 .andExpect (status ().isOk ());
357369
358- getClient (authTokenAdmin ).perform (put ("/api/core/clarinlicenses /" + clarinLicense .getID ())
370+ getClient (authTokenAdmin ).perform (put (BASE_URI + " /" + clarinLicense .getID ())
359371 .content (new ObjectMapper ().writeValueAsBytes (clarinLicenseRest ))
360372 .contentType (org .springframework .http .MediaType .APPLICATION_JSON ))
361373 .andExpect (status ().isOk ());
362374
363- getClient (authTokenAdmin ).perform (get ("/api/core/clarinlicenses /" + clarinLicense .getID ()))
375+ getClient (authTokenAdmin ).perform (get (BASE_URI + " /" + clarinLicense .getID ()))
364376 .andExpect (status ().isOk ())
365377 .andExpect (jsonPath ("$" , Matchers .is (
366378 ClarinLicenseMatcher .matchClarinLicenseWithoutId (clarinLicenseUpdated ))
@@ -387,7 +399,7 @@ public void forbiddenUpdateClarinLicense() throws Exception {
387399
388400 ClarinLicenseRest clarinLicenseRest = clarinLicenseConverter .convert (clarinLicense , Projection .DEFAULT );
389401 String authTokenUser = getAuthToken (eperson .getEmail (), password );
390- getClient (authTokenUser ).perform (delete ("/api/core/clarinlicenses /" + clarinLicense .getID ())
402+ getClient (authTokenUser ).perform (delete (BASE_URI + " /" + clarinLicense .getID ())
391403 .content (new ObjectMapper ().writeValueAsBytes (clarinLicenseRest ))
392404 .contentType (org .springframework .http .MediaType .APPLICATION_JSON ))
393405 .andExpect (status ().isForbidden ())
@@ -415,7 +427,7 @@ public void notFoundUpdateClarinLicense() throws Exception {
415427 ClarinLicenseRest clarinLicenseRest = clarinLicenseConverter .convert (clarinLicense , Projection .DEFAULT );
416428
417429 String authTokenAdmin = getAuthToken (admin .getEmail (), password );
418- getClient (authTokenAdmin ).perform (put ("/api/core/clarinlicenses /" + clarinLicense .getID () + "124679" )
430+ getClient (authTokenAdmin ).perform (put (BASE_URI + " /" + clarinLicense .getID () + "124679" )
419431 .content (new ObjectMapper ().writeValueAsBytes (clarinLicenseRest ))
420432 .contentType (org .springframework .http .MediaType .APPLICATION_JSON ))
421433 .andExpect (status ().isNotFound ())
@@ -430,13 +442,13 @@ public void deleteClarinLicense() throws Exception {
430442 context .restoreAuthSystemState ();
431443
432444 String authTokenAdmin = getAuthToken (admin .getEmail (), password );
433- getClient (authTokenAdmin ).perform (get ("/api/core/clarinlicenses /" + clarinLicense .getID ()))
445+ getClient (authTokenAdmin ).perform (get (BASE_URI + " /" + clarinLicense .getID ()))
434446 .andExpect (status ().isOk ());
435447
436- getClient (authTokenAdmin ).perform (delete ("/api/core/clarinlicenses /" + clarinLicense .getID ()))
448+ getClient (authTokenAdmin ).perform (delete (BASE_URI + " /" + clarinLicense .getID ()))
437449 .andExpect (status ().isNoContent ());
438450
439- getClient (authTokenAdmin ).perform (get ("/api/core/clarinlicenses /" + clarinLicense .getID ()))
451+ getClient (authTokenAdmin ).perform (get (BASE_URI + " /" + clarinLicense .getID ()))
440452 .andExpect (status ().isNotFound ());
441453 }
442454
@@ -447,7 +459,7 @@ public void unauthorizedDeleteClarinLicense() throws Exception {
447459 ClarinLicense clarinLicense = ClarinLicenseBuilder .createClarinLicense (context ).build ();
448460 context .restoreAuthSystemState ();
449461
450- getClient ().perform (delete ("/api/core/clarinlicenses /" + clarinLicense .getID ()))
462+ getClient ().perform (delete (BASE_URI + " /" + clarinLicense .getID ()))
451463 .andExpect (status ().isUnauthorized ())
452464 ;
453465 }
@@ -460,7 +472,7 @@ public void forbiddenDeleteClarinLicense() throws Exception {
460472 context .restoreAuthSystemState ();
461473
462474 String authTokenUser = getAuthToken (eperson .getEmail (), password );
463- getClient (authTokenUser ).perform (delete ("/api/core/clarinlicenses /" + clarinLicense .getID ()))
475+ getClient (authTokenUser ).perform (delete (BASE_URI + " /" + clarinLicense .getID ()))
464476 .andExpect (status ().isForbidden ())
465477 ;
466478 }
@@ -469,7 +481,7 @@ public void forbiddenDeleteClarinLicense() throws Exception {
469481 @ Test
470482 public void notFoundDeleteClarinLicense () throws Exception {
471483 String authTokenAdmin = getAuthToken (admin .getEmail (), password );
472- getClient (authTokenAdmin ).perform (delete ("/api/core/clarinlicenses /" + 1239990 ))
484+ getClient (authTokenAdmin ).perform (delete (BASE_URI + " /" + 1239990 ))
473485 .andExpect (status ().isNotFound ())
474486 ;
475487 }
@@ -489,7 +501,7 @@ public void findAllBitstreamsAttachedToLicense() throws Exception {
489501
490502 String tokenAdmin = getAuthToken (admin .getEmail (), password );
491503 // Check if the Clarin License was attached to the Bitstreams
492- getClient (tokenAdmin ).perform (get ("/api/core/clarinlicenses /" + firstCLicense .getID ()))
504+ getClient (tokenAdmin ).perform (get (BASE_URI + " /" + firstCLicense .getID ()))
493505 .andExpect (status ().isOk ())
494506 .andExpect (jsonPath ("$.bitstreams" , is (2 )));
495507 }
@@ -514,5 +526,26 @@ private ClarinLicenseLabel getExtendedLicenseLabels(List<ClarinLicenseLabel> cla
514526 return null ;
515527 }
516528
529+ private void testRequestWithIncorrectPageable (String uri ,
530+ String nameValue ,
531+ int expectedTotal ,
532+ int pageNumber ) throws Exception {
533+ String authTokenAdmin = getAuthToken (admin .getEmail (), password );
534+ MockHttpServletRequestBuilder mockBuilder = get (BASE_URI + uri )
535+ .param ("page" , String .valueOf (pageNumber ));
536+ if (nameValue != null ) {
537+ mockBuilder = mockBuilder .param ("name" , nameValue );
538+ }
539+
540+ getClient (authTokenAdmin ).perform (mockBuilder )
541+ .andExpect (status ().isOk ())
542+ .andExpect (content ().contentType (contentType ))
543+ .andExpect (jsonPath ("$._embedded" ).doesNotExist ())
544+ .andExpect (jsonPath ("$._links.self.href" , Matchers .containsString (BASE_URI )))
545+ .andExpect (jsonPath ("$.page.size" , Matchers .equalTo (DEFAULT_PAGE_SIZE )))
546+ .andExpect (jsonPath ("$.page.totalElements" , Matchers .equalTo (expectedTotal )))
547+ .andExpect (jsonPath ("$.page.totalPages" , Matchers .equalTo (1 )))
548+ .andExpect (jsonPath ("$.page.number" , Matchers .equalTo (pageNumber )));
549+ }
517550
518551}
0 commit comments