Exemplo n.º 1
0
  public Object extractEntity(ClientContext context, Object... args) {
    ClientResponse response = context.getClientResponse();

    // only release connection if it is not an instance of an
    // InputStream
    boolean releaseConnectionAfter = response.getStatus() >= 200 && response.getStatus() < 300;
    try {
      // void methods should be handled before this method gets called, but it's worth being
      // defensive
      if (method.getReturnType() == null) {
        throw new RuntimeException(
            "No type information to extract entity with.  You use other getEntity() methods");
      }
      GenericType gt = null;
      if (method.getGenericReturnType() != null) {
        gt = new GenericType(method.getGenericReturnType());
      } else {
        gt = new GenericType(method.getReturnType());
      }
      Object obj = ClientInvocation.extractResult(gt, response, method.getAnnotations());
      if (obj instanceof InputStream || obj instanceof Reader) releaseConnectionAfter = false;
      return obj;
    } finally {
      if (releaseConnectionAfter) response.close();
    }
  }
  @Before
  public void init() throws Exception {
    httpClient = HttpClientBuilder.create().build();

    engine = new CustomApacheHttpClient4Engine(httpClient);

    clientBuilder = new CustomResteasyClientBuilder();
    client = clientBuilder.build();
    client.register(JacksonConfigurationProvider.class);

    ResteasyProviderFactory resteasyProviderFactory = new ResteasyProviderFactory();
    clientConfiguration = new ClientConfiguration(resteasyProviderFactory);
    headers = new ClientRequestHeaders(clientConfiguration);

    uri = new URI("http://127.0.0.1");

    // request = new ClientInvocation(client, uri, headers, clientConfiguration);
    request = mock(ClientInvocation.class);
    when(request.getHeaders()).thenReturn(headers);
    when(request.getClientConfiguration()).thenReturn(clientConfiguration);
  }
  @Test
  public void shouldBeAbleToLoadHttpMethodForPost() throws Exception {

    headers.setMediaType(MediaType.TEXT_PLAIN_TYPE);

    when(request.getDelegatingOutputStream()).thenReturn(new DelegatingOutputStream());

    InputStream requestBody =
        getClass().getClassLoader().getResourceAsStream("mocks/pull_request_1.json");
    ByteArrayOutputStream requestBodyOut = new ByteArrayOutputStream();
    int bytes = IOUtils.copy(requestBody, requestBodyOut);

    when(request.getEntityStream()).thenReturn(requestBodyOut);

    OauthAccessToken accessToken = new OauthAccessToken();
    when(request.getEntity()).thenReturn(accessToken);

    HttpPost post = new HttpPost(uri);

    engine.loadHttpMethod(request, post);

    verify(request).writeRequestBody(requestBodyOut);
  }
  @Test
  public void shouldNotBeAbleToLoadHttpMethodForGet() throws Exception {

    OauthAccessToken accessToken = new OauthAccessToken();

    when(request.getEntity()).thenReturn(accessToken);

    HttpRequestBase httpMethod = new HttpGet(uri);

    try {
      engine.loadHttpMethod(request, httpMethod);

      Assert.fail();
    } catch (ProcessingException e) {
      assertTrue(e.getMessage().contains("RESTEASY004565"));
    }
  }