DatasetRest { @GET @Produces(MediaType.APPLICATION_JSON) @RolesAllowed("user") @ApiOperation("Retrieves all DatasetRecords in the local Catalog") public Response getDatasetRecords(@Context UriInfo uriInfo, @QueryParam("offset") int offset, @QueryParam("limit") int limit, @QueryParam("sort") String sort, @DefaultValue("true") @QueryParam("ascending") boolean asc, @QueryParam("searchText") String searchText) { try { LinksUtils.validateParams(limit, offset); DatasetPaginatedSearchParams params = new DatasetPaginatedSearchParams(vf).setOffset(offset) .setAscending(asc); if (limit > 0) { params.setLimit(limit); } if (sort != null && !sort.isEmpty()) { params.setSortBy(vf.createIRI(sort)); } if (searchText != null && !searchText.isEmpty()) { params.setSearchText(searchText); } PaginatedSearchResults<DatasetRecord> results = manager.getDatasetRecords(params); JSONArray array = JSONArray.fromObject(results.getPage().stream() .map(datasetRecord -> removeContext(datasetRecord.getModel())) .map(model -> modelToSkolemizedJsonld(model, transformer, bNodeService)) .collect(Collectors.toList())); Links links = LinksUtils.buildLinks(uriInfo, array.size(), results.getTotalSize(), limit, offset); Response.ResponseBuilder response = Response.ok(array).header("X-Total-Count", results.getTotalSize()); if (links.getNext() != null) { response = response.link(links.getBase() + links.getNext(), "next"); } if (links.getPrev() != null) { response = response.link(links.getBase() + links.getPrev(), "prev"); } return response.build(); } catch (IllegalArgumentException ex) { throw ErrorUtils.sendError(ex, ex.getMessage(), Response.Status.BAD_REQUEST); } catch (MobiException ex) { throw ErrorUtils.sendError(ex, ex.getMessage(), Response.Status.INTERNAL_SERVER_ERROR); } }  @GET @Produces(MediaType.APPLICATION_JSON) @RolesAllowed("user") @ApiOperation("Retrieves all DatasetRecords in the local Catalog") Response getDatasetRecords(@Context UriInfo uriInfo,
                               @QueryParam("offset") int offset,
                               @QueryParam("limit") int limit,
                               @QueryParam("sort") String sort,
                               @DefaultValue("true") @QueryParam("ascending") boolean asc,
                               @QueryParam("searchText") String searchText); @POST @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.TEXT_PLAIN) @RolesAllowed("user") @ApiOperation("Creates a new DatasetRecord in the local Catalog and Dataset in the specified repository") Response createDatasetRecord(@Context ContainerRequestContext context,
                                 @FormDataParam("title") String title,
                                 @FormDataParam("repositoryId") String repositoryId,
                                 @FormDataParam("datasetIRI") String datasetIRI,
                                 @FormDataParam("description") String description,
                                 @FormDataParam("markdown") String markdown,
                                 @FormDataParam("keywords") List<FormDataBodyPart> keywords,
                                 @FormDataParam("ontologies") List<FormDataBodyPart> ontologies); @GET @Produces(MediaType.APPLICATION_JSON) @Path("{datasetRecordId}") @RolesAllowed("user") @ApiOperation("Gets a specific DatasetRecord from the local Catalog") Response getDatasetRecord(@PathParam("datasetRecordId") String datasetRecordId); @DELETE @Path("{datasetRecordId}") @RolesAllowed("user") @ApiOperation("Deletes a specific DatasetRecord in the local Catalog") Response deleteDatasetRecord(@Context ContainerRequestContext context,
                                 @PathParam("datasetRecordId") String datasetRecordId,
                                 @DefaultValue("false") @QueryParam("force") boolean force); @DELETE @Path("{datasetRecordId}/data") @RolesAllowed("user") @ApiOperation("Clears the data within a specific DatasetRecord in the local Catalog") Response clearDatasetRecord(@PathParam("datasetRecordId") String datasetRecordId,
                                @DefaultValue("false") @QueryParam("force") boolean force); @POST @Path("{datasetRecordId}/data") @RolesAllowed("user") @ApiOperation("Uploads the data within an RDF file to a specific DatasetRecord in the local Catalog") Response uploadData(@PathParam("datasetRecordId") String datasetRecordId,
                        @FormDataParam("file") InputStream fileInputStream,
                        @FormDataParam("file") FormDataContentDisposition fileDetail);  }
@Test public void getDatasetRecordsTest() { Response response = target().path("datasets").request().get(); assertEquals(response.getStatus(), 200); verify(datasetManager).getDatasetRecords(any(DatasetPaginatedSearchParams.class)); verify(service, atLeastOnce()).skolemize(any(Statement.class)); try { JSONArray result = JSONArray.fromObject(response.readEntity(String.class)); assertEquals(result.size(), 3); } catch (Exception e) { fail("Expected no exception, but got: " + e.getMessage()); } }
@Test public void getDatasetRecordsWithLinksTest() { when(results.getPage()).thenReturn(Collections.singletonList(record2)); when(results.getPageNumber()).thenReturn(2); when(results.getPageSize()).thenReturn(1); Response response = target().path("datasets").queryParam("offset", 1).queryParam("limit", 1).request().get(); assertEquals(response.getStatus(), 200); verify(datasetManager).getDatasetRecords(any(DatasetPaginatedSearchParams.class)); verify(service, atLeastOnce()).skolemize(any(Statement.class)); MultivaluedMap<String, Object> headers = response.getHeaders(); assertEquals(headers.get("X-Total-Count").get(0), "3"); Set<Link> links = response.getLinks(); assertEquals(links.size(), 2); assertTrue(response.hasLink("prev")); assertTrue(response.hasLink("next")); try { JSONArray result = JSONArray.fromObject(response.readEntity(String.class)); assertEquals(result.size(), 1); } catch (Exception e) { fail("Expected no exception, but got: " + e.getMessage()); } }
@Test public void getDatasetRecordsWithErrorTest() { when(datasetManager.getDatasetRecords(any(DatasetPaginatedSearchParams.class))).thenThrow(new MobiException()); Response response = target().path("datasets").request().get(); assertEquals(response.getStatus(), 500); }