Ejemplo n.º 1
0
  @Test
  public void testGetLegacyPodcasts()
      throws JsonGenerationException, JsonMappingException, IOException {

    ClientConfig clientConfig = new ClientConfig();
    clientConfig.register(JacksonFeature.class);

    Client client = ClientBuilder.newClient(clientConfig);

    WebTarget webTarget =
        client.target("http://localhost:8888/demo-rest-jersey-spring/legacy/podcasts/");

    Builder request = webTarget.request();
    request.header("Content-type", MediaType.APPLICATION_JSON);

    Response response = request.get();
    Assert.assertTrue(response.getStatus() == 200);

    List<Podcast> podcasts = response.readEntity(new GenericType<List<Podcast>>() {});

    ObjectMapper mapper = new ObjectMapper();
    System.out.print(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(podcasts));

    Assert.assertTrue("At least one podcast is present in the LEGACY", podcasts.size() > 0);
  }
  /**
   * Returns the information of an specific board.
   *
   * @param boardId Board identifier.
   * @return Board information.
   */
  public Board getBoard(String boardId) {
    String uri = getConfiguration().getHostAndContext() + ALL_BOARDS_RESOURCE + "/{boardId}";
    WebTarget target = getClient().target(uri).resolveTemplate("boardId", boardId);
    Builder builder = getBuilder(target);

    return builder.get(Board.class);
  }
Ejemplo n.º 3
0
 void doExternalEntityExpansionFails() throws Exception {
   // System.out.println("entering doExternalEntityExpansionFails()");
   Builder builder = client.target(generateURL("/test")).request();
   Response response = builder.post(Entity.entity(externalEntityDoc, "application/xml"));
   // System.out.println("status: " + response.getStatus());
   String entity = response.readEntity(String.class);
   // System.out.println("doExternalEntityExpansionFails() result: " + entity);
   Assert.assertEquals(200, response.getStatus());
   Assert.assertEquals("", entity);
 }
Ejemplo n.º 4
0
 void doMaxAttributesPasses() throws Exception {
   // System.out.println("entering doMaxAttributesPasses()");
   Builder builder = client.target(generateURL("/test")).request();
   Response response = builder.post(Entity.entity(bigAttributeDoc, "application/xml"));
   // System.out.println("doMaxAttributesPasses() status: " + response.getStatus());
   String entity = response.readEntity(String.class);
   // System.out.println("doMaxAttributesPasses() result: " + entity);
   Assert.assertEquals(200, response.getStatus());
   Assert.assertEquals("bar", entity);
 }
Ejemplo n.º 5
0
 void doEntityExpansionFails() throws Exception {
   // System.out.println("entering doEntityExpansionFails()");
   Builder builder = client.target(generateURL("/test")).request();
   Response response = builder.post(Entity.entity(bigExpansionDoc, "application/xml"));
   // System.out.println("status: " + response.getStatus());
   String entity = response.readEntity(String.class);
   // System.out.println("doEntityExpansionFails() result: " + entity);
   Assert.assertEquals(400, response.getStatus());
   Assert.assertTrue(entity.contains("org.xml.sax.SAXParseException"));
 }
Ejemplo n.º 6
0
 void doEntityExpansionPasses() throws Exception {
   // System.out.println("entering doEntityExpansionFails()");
   Builder builder = client.target(generateURL("/test")).request();
   Response response = builder.post(Entity.entity(bigExpansionDoc, "application/xml"));
   // System.out.println("status: " + response.getStatus());
   String entity = response.readEntity(String.class);
   int len = Math.min(entity.length(), 30);
   // System.out.println("doEntityExpansionPasses() result: " + entity.substring(0, len) + "...");
   Assert.assertEquals(200, response.getStatus());
   Assert.assertTrue(countFoos(entity) > 64000);
 }
Ejemplo n.º 7
0
 @Override
 public Builder invocation(Link link) {
   checkNull(link);
   checkClosed();
   Builder builder = target(link.getUriBuilder()).request();
   String type = link.getType();
   if (type != null) {
     builder.accept(type);
   }
   return builder;
 }
Ejemplo n.º 8
0
 void doDTDFails() throws Exception {
   // System.out.println("entering doDTDFails()");
   Builder builder = client.target(generateURL("/test")).request();
   Response response = builder.post(Entity.entity(smallDtd, "application/xml"));
   // System.out.println("status: " + response.getStatus());
   String entity = response.readEntity(String.class);
   // System.out.println("doDTDFails(): result: " + entity);
   Assert.assertEquals(400, response.getStatus());
   Assert.assertTrue(entity.contains("org.xml.sax.SAXParseException"));
   Assert.assertTrue(entity.contains("DOCTYPE"));
   Assert.assertTrue(entity.contains("http://apache.org/xml/features/disallow-doctype-decl"));
   Assert.assertTrue(entity.contains("true"));
 }
Ejemplo n.º 9
0
  @Test
  public void shouldGetVdbXml() throws Exception {
    loadVdbs();

    Properties settings =
        _uriBuilder.createSettings(SettingNames.VDB_NAME, TestUtilities.PORTFOLIO_VDB_NAME);
    URI uri = _uriBuilder.buildVdbUri(LinkType.SELF, settings);
    Builder request = this.client.target(uri.toString()).request(MediaType.APPLICATION_XML);
    this.response = request.get();
    final String entity = this.response.readEntity(String.class);
    assertThat(entity, is(notNullValue()));

    // System.out.println("Response:\n" + entity);
    assertTrue(entity.contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
    assertTrue(entity.contains("<vdb name=\"Portfolio\" version=\"1\">"));
    assertTrue(entity.contains("</vdb>"));
  }
Ejemplo n.º 10
0
  @Test
  public void shouldNotGetVdbsXml() throws Exception {
    loadVdbs();

    URI uri = _uriBuilder.generateVdbsUri();
    Builder request = this.client.target(uri.toString()).request(MediaType.APPLICATION_XML);
    this.response = request.get();

    assertTrue(response.hasEntity());

    //
    // Internal server error since the server does not support
    // '/vdbs' url returning anything in xml
    //
    assertEquals(500, response.getStatus());

    final String entity = response.readEntity(String.class);
    assertThat(entity, is(notNullValue()));

    assertEquals("No match for accept header", entity);
  }
 @Test(expected = ClientErrorException.class)
 public void testGetReturns403() throws Exception {
   // Arrange
   Mockito.when(response.getStatus()).thenReturn(403);
   Mockito.when(response.getStatusInfo().getFamily())
       .thenReturn(Response.Status.Family.CLIENT_ERROR);
   Mockito.when(builder.get()).thenReturn(response);
   // Act
   classUnderTest.getOne(builder);
   // Assert
   fail("Should have thrown an Exception");
 }
Ejemplo n.º 12
0
 void doMaxAttributesFails() throws Exception {
   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
   // System.out.println("dbf.getClass(): " + dbf.getClass());
   if ("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl".equals(dbf.getClass().getName())) {
     // System.out.println("Testing with Red Hat version of Xerces, skipping max attributes test");
     return;
   }
   // System.out.println("entering doMaxAttributesFails()");
   Builder builder = client.target(generateURL("/test")).request();
   Response response = builder.post(Entity.entity(bigAttributeDoc, "application/xml"));
   // System.out.println("doMaxAttributesFails() status: " + response.getStatus());
   String entity = response.readEntity(String.class);
   // System.out.println("doMaxAttributesFails() result: " + entity);
   Assert.assertEquals(400, response.getStatus());
   Assert.assertTrue(entity.contains("org.xml.sax.SAXParseException"));
   Assert.assertTrue(entity.contains("JAXP00010002:"));
   if ("en".equals(System.getProperty("user.language"))) {
     Assert.assertTrue(entity.contains("has more than \"10,00"));
     int pos = entity.indexOf("has more than \"10,00");
     Assert.assertTrue(entity.substring(pos).contains("attributes"));
   }
 }
Ejemplo n.º 13
0
  @Test
  public void testGetPodcast() throws JsonGenerationException, JsonMappingException, IOException {

    ClientConfig clientConfig = new ClientConfig();
    clientConfig.register(JacksonFeature.class);

    Client client = ClientBuilder.newClient(clientConfig);

    WebTarget webTarget = client.target("http://localhost:8888/demo-rest-jersey-spring/podcasts/2");

    Builder request = webTarget.request(MediaType.APPLICATION_JSON);

    Response response = request.get();
    Assert.assertTrue(response.getStatus() == 200);

    Podcast podcast = response.readEntity(Podcast.class);

    ObjectMapper mapper = new ObjectMapper();
    System.out.print(
        "Received podcast from database *************************** "
            + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(podcast));
  }
  @Test
  @SuppressWarnings("unchecked")
  public void testPostReturns200() throws Exception {

    Mockito.when(response.getStatus()).thenReturn(200);
    Mockito.when(response.getStatusInfo().getFamily())
        .thenReturn(Response.Status.Family.SUCCESSFUL);
    Mockito.when(response.readEntity(Mockito.any(Class.class))).thenReturn(jsonstring);
    Mockito.when(builder.post(entity, Response.class)).thenReturn(response);
    // Act
    Issue actual = classUnderTest.postOne(builder, expected);
    // Assert
    assertEquals(expected, actual);
  }
 @Test
 @SuppressWarnings("unchecked")
 public void testPutReturns204() throws Exception {
   // Arrange
   Mockito.when(response.getStatus()).thenReturn(204);
   Mockito.when(response.readEntity(Mockito.any(Class.class))).thenReturn(entity);
   Mockito.when(response.getStatusInfo().getFamily())
       .thenReturn(Response.Status.Family.SUCCESSFUL);
   Mockito.when(builder.put(entity, Response.class)).thenReturn(response);
   // Act
   Issue putOne = classUnderTest.putOne(builder, entity);
   // Assert
   assertEquals(null, putOne);
 }
 @Test
 @SuppressWarnings("unchecked")
 public void testDeleteReturns200() throws Exception {
   // Arrange
   Mockito.when(response.getStatus()).thenReturn(200);
   Mockito.when(response.readEntity(Mockito.any(Class.class))).thenReturn(null);
   Mockito.when(response.getStatusInfo().getFamily())
       .thenReturn(Response.Status.Family.SUCCESSFUL);
   Mockito.when(builder.delete(Response.class)).thenReturn(response);
   // Act
   Issue deleteOne = classUnderTest.deleteOne(builder);
   // Assert
   assertEquals(null, deleteOne);
 }
  @Test
  @SuppressWarnings("unchecked")
  public void testGetReturns200() throws Exception {
    // Arrange
    Issue expected = new Issue(new Project("1"), "sum", "desc", new IssueType("1", "A"));
    Mockito.when(response.getStatus()).thenReturn(200);

    String jsonstring = ClientUtils.getGson().toJson(expected);
    Mockito.when(response.readEntity(Mockito.any(Class.class))).thenReturn(jsonstring);
    Mockito.when(builder.get()).thenReturn(response);
    // Act
    Issue actual = classUnderTest.getOne(builder);
    // Assert
    assertEquals(expected, actual);
  }
 public static Response delete(URI uri) {
   WebTarget target = builder.build().target(uri);
   Builder httpRequest = target.request();
   return httpRequest.delete();
 }
 public static Response post(URI uri, Entity<?> entity, MediaType acceptType) {
   WebTarget target = builder.build().target(uri);
   Builder httpRequest = target.request();
   if (acceptType != null) httpRequest.accept(acceptType);
   return httpRequest.post(entity);
 }
  /**
   * Returns all the boards on the Jira instance.
   *
   * @return List of all boards.
   */
  public ResponseList<Board> getAllBoards() {
    String uri = getConfiguration().getHostAndContext() + ALL_BOARDS_RESOURCE;

    Builder builder = getBuilder(getClient().target(uri));
    return builder.get(new GenericType<ResponseList<Board>>() {});
  }