Exemple #1
0
  @Test
  public void testGetPosts() throws Exception {
    ListenableFuture futu;
    futu = mock(ListenableFuture.class);

    // mock the get method to return our special future
    when(instance.http.get(any(String.class))).thenReturn(futu);

    // the special future's addListener method
    // needs to be hacked functional
    when(futu.addListener(any(Runnable.class), any(Executor.class)))
        .thenAnswer(
            new Answer() {
              @Override
              public Object answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                Runnable run = (Runnable) args[0];
                run.run();
                return null;
              }
            });

    // finally as the listener runs we feed it our special JSON
    when(futu.get())
        .thenReturn(
            new JSONObject() {
              {
                put(
                    "data",
                    new JSONObject() {
                      {
                        put(
                            "children",
                            new JSONArray() {
                              {
                                add(
                                    new JSONObject() {
                                      {
                                        put(
                                            "data",
                                            new JSONObject() {
                                              {
                                                put("author", "hylje");
                                                put("url", "http://www.google.com/");
                                                put("id", "abcde");
                                                put("title", "Yet another fake Reddit post");
                                              }
                                            });
                                      }
                                    });
                                add(
                                    new JSONObject() {
                                      {
                                        put(
                                            "data",
                                            new JSONObject() {
                                              {
                                                put("author", "hylje");
                                                put("url", "http://www.bash.org/");
                                                put("id", "abcdf");
                                                put("title", "You gotta be kidding me");
                                              }
                                            });
                                      }
                                    });
                              }
                            });
                      }
                    });
              }
            });

    ListenableFuture<List<Post>> post_futu = instance.get_posts("foo");
    List<Post> posts = post_futu.get();
    assertEquals(posts.get(0).author, "hylje");
    assertEquals(posts.get(0).id, "abcde");
    assertEquals(posts.get(1).title, "You gotta be kidding me");
  }
  private <T> T executeAndWait(
      final BoundRequestBuilder builder, final int timeoutSec, final Class<T> clazz)
      throws KillBillClientException {
    Response response;
    final ListenableFuture<Response> futureStatus;
    try {
      futureStatus =
          builder.execute(
              new AsyncCompletionHandler<Response>() {
                @Override
                public Response onCompleted(final Response response) throws Exception {
                  return response;
                }
              });
      response = futureStatus.get(timeoutSec, TimeUnit.SECONDS);
    } catch (final InterruptedException e) {
      throw new KillBillClientException(e);
    } catch (final ExecutionException e) {
      throw new KillBillClientException(e);
    } catch (final TimeoutException e) {
      throw new KillBillClientException(e);
    }

    if (response != null && response.getStatusCode() == 401) {
      throw new KillBillClientException(
          new IllegalArgumentException(
              "Unauthorized - did you configure your RBAC and/or tenant credentials?"),
          response);
    } else if (response != null
        && (response.getStatusCode() == 404 || response.getStatusCode() == 204)) {
      // Return empty list for KillBillObjects instead of null for convenience
      if (Iterable.class.isAssignableFrom(clazz)) {
        for (final Constructor constructor : clazz.getConstructors()) {
          if (constructor.getParameterTypes().length == 0) {
            try {
              return (T) constructor.newInstance();
            } catch (InstantiationException e) {
              return null;
            } catch (IllegalAccessException e) {
              return null;
            } catch (InvocationTargetException e) {
              return null;
            }
          }
        }
        return null;
      } else {
        return null;
      }
    } else if (response != null && response.getStatusCode() >= 400) {
      final BillingException exception = deserializeResponse(response, BillingException.class);
      log.warn("Error " + response.getStatusCode() + " from Kill Bill: " + exception.getMessage());
      throw new KillBillClientException(exception, response);
    }

    // No deserialization required?
    if (Response.class.isAssignableFrom(clazz)) {
      return (T) response;
    }

    return deserializeResponse(response, clazz);
  }