@Test public void testGetRecord() throws Exception { String recordId = "recordIdSample"; WebTarget target = target("/" + RecordOptionListContainer.RECORDS).queryParam("recordId", recordId); Document document = DocumentHelper.createDocument(); Element root = document.addElement("root"); Element node = root.addElement("author") .addAttribute("name", "exampleName") .addText("Some Text"); Urn anyUrn = any(Urn.class); when(dataManager.getRecord(anyUrn)).thenThrow(new IOException()).thenThrow(new ObjectNotFoundException(recordId)).thenReturn(null).thenReturn(node); Response response = target.request(MediaType.APPLICATION_XML).get(); assertEquals(500, response.getStatus()); response = target.request(MediaType.APPLICATION_XML).get(); assertEquals(404, response.getStatus()); response = target.request(MediaType.APPLICATION_XML).get(); assertEquals(404, response.getStatus()); response = target.request(MediaType.APPLICATION_XML).get(); assertEquals(200, response.getStatus()); }
@GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Get specific record.", httpMethod = "GET", response = String.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a Record)"), @ApiResponse(code = 400, message = "InvalidArgumentsException"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) public Response getRecord(@ApiParam(value = "Id of record", required = true) @QueryParam("recordId") String recordId) throws DoesNotExistException, InvalidArgumentsException, IOException, InternalServerErrorException { Urn recordUrn = null; try { if (this.urn != null) recordUrn = this.urn; else recordUrn = new Urn(recordId); } catch (InvalidInputException e) { throw new InvalidArgumentsException("Invalid argument: " + e.getMessage()); } Node record = null; try { record = dataManager.getRecord(recordUrn); } catch (IOException | DocumentException | SQLException e) { throw new InternalServerErrorException("Internal Server Error : " + e.getMessage()); } catch (ObjectNotFoundException e) { throw new DoesNotExistException("Does NOT exist: " + e.getMessage()); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (record != null) { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(baos, "UTF-8"); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(outputStreamWriter, format); writer.write(record); writer.close(); } else throw new DoesNotExistException("Does NOT exist: " + "Record with id " + recordId + " NOT found!"); return Response.status(200).entity(new Result(baos.toString("UTF-8"))).build(); }
RecordsResource { @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Get specific record.", httpMethod = "GET", response = String.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a Record)"), @ApiResponse(code = 400, message = "InvalidArgumentsException"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) public Response getRecord(@ApiParam(value = "Id of record", required = true) @QueryParam("recordId") String recordId) throws DoesNotExistException, InvalidArgumentsException, IOException, InternalServerErrorException { Urn recordUrn = null; try { if (this.urn != null) recordUrn = this.urn; else recordUrn = new Urn(recordId); } catch (InvalidInputException e) { throw new InvalidArgumentsException("Invalid argument: " + e.getMessage()); } Node record = null; try { record = dataManager.getRecord(recordUrn); } catch (IOException | DocumentException | SQLException e) { throw new InternalServerErrorException("Internal Server Error : " + e.getMessage()); } catch (ObjectNotFoundException e) { throw new DoesNotExistException("Does NOT exist: " + e.getMessage()); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (record != null) { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(baos, "UTF-8"); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(outputStreamWriter, format); writer.write(record); writer.close(); } else throw new DoesNotExistException("Does NOT exist: " + "Record with id " + recordId + " NOT found!"); return Response.status(200).entity(new Result(baos.toString("UTF-8"))).build(); } }
RecordsResource { @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Get specific record.", httpMethod = "GET", response = String.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a Record)"), @ApiResponse(code = 400, message = "InvalidArgumentsException"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) public Response getRecord(@ApiParam(value = "Id of record", required = true) @QueryParam("recordId") String recordId) throws DoesNotExistException, InvalidArgumentsException, IOException, InternalServerErrorException { Urn recordUrn = null; try { if (this.urn != null) recordUrn = this.urn; else recordUrn = new Urn(recordId); } catch (InvalidInputException e) { throw new InvalidArgumentsException("Invalid argument: " + e.getMessage()); } Node record = null; try { record = dataManager.getRecord(recordUrn); } catch (IOException | DocumentException | SQLException e) { throw new InternalServerErrorException("Internal Server Error : " + e.getMessage()); } catch (ObjectNotFoundException e) { throw new DoesNotExistException("Does NOT exist: " + e.getMessage()); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (record != null) { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(baos, "UTF-8"); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(outputStreamWriter, format); writer.write(record); writer.close(); } else throw new DoesNotExistException("Does NOT exist: " + "Record with id " + recordId + " NOT found!"); return Response.status(200).entity(new Result(baos.toString("UTF-8"))).build(); } RecordsResource();  RecordsResource(DefaultDataManager dataManager, Urn urn); }
RecordsResource { @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Get specific record.", httpMethod = "GET", response = String.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a Record)"), @ApiResponse(code = 400, message = "InvalidArgumentsException"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) public Response getRecord(@ApiParam(value = "Id of record", required = true) @QueryParam("recordId") String recordId) throws DoesNotExistException, InvalidArgumentsException, IOException, InternalServerErrorException { Urn recordUrn = null; try { if (this.urn != null) recordUrn = this.urn; else recordUrn = new Urn(recordId); } catch (InvalidInputException e) { throw new InvalidArgumentsException("Invalid argument: " + e.getMessage()); } Node record = null; try { record = dataManager.getRecord(recordUrn); } catch (IOException | DocumentException | SQLException e) { throw new InternalServerErrorException("Internal Server Error : " + e.getMessage()); } catch (ObjectNotFoundException e) { throw new DoesNotExistException("Does NOT exist: " + e.getMessage()); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (record != null) { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(baos, "UTF-8"); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(outputStreamWriter, format); writer.write(record); writer.close(); } else throw new DoesNotExistException("Does NOT exist: " + "Record with id " + recordId + " NOT found!"); return Response.status(200).entity(new Result(baos.toString("UTF-8"))).build(); } RecordsResource();  RecordsResource(DefaultDataManager dataManager, Urn urn); @OPTIONS @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Get options over record conext.", httpMethod = "OPTIONS", response = RecordOptionListContainer.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a list of all available options)") }) RecordOptionListContainer getOptions(); @GET @Path("/" + RecordOptionListContainer.OPTIONS) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Get options over record conext.", httpMethod = "GET", response = RecordOptionListContainer.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a list of all available options)") }) RecordOptionListContainer getGETOptions(); @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Get specific record.", httpMethod = "GET", response = String.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a Record)"), @ApiResponse(code = 400, message = "InvalidArgumentsException"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) Response getRecord(@ApiParam(value = "Id of record", required = true) @QueryParam("recordId") String recordId); @DELETE @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Deletes (mark) or permanently erase a record.", httpMethod = "DELETE", response = String.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a String message)"), @ApiResponse(code = 400, message = "InvalidArgumentsException"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 406, message = "MissingArgumentsException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) Response removeRecord(
            @ApiParam(value = "Id of record", required = true) @QueryParam("recordId") String recordId,
            @ApiParam(value = "Delete(mark) or erase(permanent)", defaultValue = RecordOptionListContainer.DELETE, allowableValues = RecordOptionListContainer.DELETE + " , " + RecordOptionListContainer.ERASE) @DefaultValue(RecordOptionListContainer.DELETE) @QueryParam("type") String type); @POST @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes(MediaType.APPLICATION_XML) @ApiOperation(value = "Create a new record.", httpMethod = "POST", response = String.class) @ApiResponses(value = { @ApiResponse(code = 201, message = "OK (Response containing a String message)"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 406, message = "MissingArgumentsException"), // @ApiResponse(code = 409, message = "AlreadyExistsException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) Response createRecord(@ApiParam(value = "Id of dataset", required = true) @QueryParam("datasetId") String datasetId,
            @ApiParam(value = "Id of record", required = true) @QueryParam("recordId") String recordId, @ApiParam(value = "Record data", required = true) String recordString); }
RecordsResource { @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Get specific record.", httpMethod = "GET", response = String.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a Record)"), @ApiResponse(code = 400, message = "InvalidArgumentsException"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) public Response getRecord(@ApiParam(value = "Id of record", required = true) @QueryParam("recordId") String recordId) throws DoesNotExistException, InvalidArgumentsException, IOException, InternalServerErrorException { Urn recordUrn = null; try { if (this.urn != null) recordUrn = this.urn; else recordUrn = new Urn(recordId); } catch (InvalidInputException e) { throw new InvalidArgumentsException("Invalid argument: " + e.getMessage()); } Node record = null; try { record = dataManager.getRecord(recordUrn); } catch (IOException | DocumentException | SQLException e) { throw new InternalServerErrorException("Internal Server Error : " + e.getMessage()); } catch (ObjectNotFoundException e) { throw new DoesNotExistException("Does NOT exist: " + e.getMessage()); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (record != null) { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(baos, "UTF-8"); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(outputStreamWriter, format); writer.write(record); writer.close(); } else throw new DoesNotExistException("Does NOT exist: " + "Record with id " + recordId + " NOT found!"); return Response.status(200).entity(new Result(baos.toString("UTF-8"))).build(); } RecordsResource();  RecordsResource(DefaultDataManager dataManager, Urn urn); @OPTIONS @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Get options over record conext.", httpMethod = "OPTIONS", response = RecordOptionListContainer.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a list of all available options)") }) RecordOptionListContainer getOptions(); @GET @Path("/" + RecordOptionListContainer.OPTIONS) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Get options over record conext.", httpMethod = "GET", response = RecordOptionListContainer.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a list of all available options)") }) RecordOptionListContainer getGETOptions(); @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Get specific record.", httpMethod = "GET", response = String.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a Record)"), @ApiResponse(code = 400, message = "InvalidArgumentsException"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) Response getRecord(@ApiParam(value = "Id of record", required = true) @QueryParam("recordId") String recordId); @DELETE @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Deletes (mark) or permanently erase a record.", httpMethod = "DELETE", response = String.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a String message)"), @ApiResponse(code = 400, message = "InvalidArgumentsException"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 406, message = "MissingArgumentsException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) Response removeRecord(
            @ApiParam(value = "Id of record", required = true) @QueryParam("recordId") String recordId,
            @ApiParam(value = "Delete(mark) or erase(permanent)", defaultValue = RecordOptionListContainer.DELETE, allowableValues = RecordOptionListContainer.DELETE + " , " + RecordOptionListContainer.ERASE) @DefaultValue(RecordOptionListContainer.DELETE) @QueryParam("type") String type); @POST @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes(MediaType.APPLICATION_XML) @ApiOperation(value = "Create a new record.", httpMethod = "POST", response = String.class) @ApiResponses(value = { @ApiResponse(code = 201, message = "OK (Response containing a String message)"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 406, message = "MissingArgumentsException"), // @ApiResponse(code = 409, message = "AlreadyExistsException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) Response createRecord(@ApiParam(value = "Id of dataset", required = true) @QueryParam("datasetId") String datasetId,
            @ApiParam(value = "Id of record", required = true) @QueryParam("recordId") String recordId, @ApiParam(value = "Record data", required = true) String recordString); public DefaultDataManager dataManager; public Urn urn; }