コード例 #1
0
 @Test
 public void canProvideStreamingJsonOutputFormat() throws Exception {
   Response response = mock(Response.class);
   final AtomicReference<StreamingOutput> ref = new AtomicReference<>();
   final Response.ResponseBuilder responseBuilder = mockResponsBuilder(response, ref);
   OutputFormat format =
       repository.outputFormat(asList(MediaType.APPLICATION_JSON_TYPE), null, streamingHeader());
   assertNotNull(format);
   Response returnedResponse =
       format.response(responseBuilder, new MapRepresentation(map("a", "test")));
   assertSame(response, returnedResponse);
   StreamingOutput streamingOutput = ref.get();
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   streamingOutput.write(baos);
   assertEquals("{\"a\":\"test\"}", baos.toString());
 }
コード例 #2
0
  @Test
  public void auditServiceTestXML() throws WebApplicationException, IOException, JAXBException {

    deleteIfExists(XmlTestOutputFile);

    DummyAuditLogRetriever dbAuditLogRetriever = new DummyAuditLogRetriever();
    AuditService auditResource = new AuditService();
    auditResource.setAuditLogRetriever(dbAuditLogRetriever);

    DummyHttpHeaders header = new DummyHttpHeaders(MediaType.APPLICATION_XML_TYPE);

    Response r = auditResource.getAuditLogs("2012-08-08T00:00", "en_US", header);

    Assert.assertNotNull(r);
    Assert.assertEquals(Status.OK.getStatusCode(), r.getStatus());
    Assert.assertTrue(r.getEntity() instanceof StreamingOutput);

    StreamingOutput so = (StreamingOutput) r.getEntity();

    File of = new File(XmlTestOutputFile);

    OutputStream os = new FileOutputStream(of);
    so.write(os);

    os.close();

    JAXBContext context = null;
    Unmarshaller unmarshaller = null;
    context = JAXBContext.newInstance(AuditLogs.class);
    unmarshaller = context.createUnmarshaller();

    Object o = unmarshaller.unmarshal(new File(XmlTestOutputFile));
    Assert.assertTrue(o instanceof AuditLogs);

    AuditLogs auditLogs = (AuditLogs) o;

    // expected number of auditLogs unmarshaled
    Assert.assertEquals(100, auditLogs.auditLogs.size());
    deleteIfExists(XmlTestOutputFile);
  }
コード例 #3
0
  @Test
  public void auditServiceTestJSON()
      throws WebApplicationException, IOException, JsonParseException {

    deleteIfExists(JsonTestOutputFile);

    DummyAuditLogRetriever dbAuditLogRetriever = new DummyAuditLogRetriever();
    AuditService auditResource = new AuditService();
    auditResource.setAuditLogRetriever(dbAuditLogRetriever);

    DummyHttpHeaders header = new DummyHttpHeaders(MediaType.APPLICATION_JSON_TYPE);

    Response r = auditResource.getAuditLogs("2012-08-08T00", "en_US", header);

    Assert.assertNotNull(r);
    Assert.assertEquals(Status.OK.getStatusCode(), r.getStatus());
    Assert.assertTrue(r.getEntity() instanceof StreamingOutput);

    StreamingOutput so = (StreamingOutput) r.getEntity();

    File of = new File(JsonTestOutputFile);

    OutputStream os = new FileOutputStream(of);
    so.write(os);
    os.close();

    ObjectMapper mapper = null;
    mapper = new ObjectMapper();
    AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
    mapper.getDeserializationConfig().withAnnotationIntrospector(introspector);

    AuditLogs auditLogs = mapper.readValue(new File(JsonTestOutputFile), AuditLogs.class);

    Assert.assertEquals(100, auditLogs.auditLogs.size());
    deleteIfExists(JsonTestOutputFile);
  }