Exemple #1
0
  /**
   * Further addresses the use of EJBs as JAX-RS components.
   *
   * @throws Exception
   */
  @Test
  public void testAsJaxRSResource() throws Exception {
    log.info("entering testAsJaxRSResource()");

    // Create book.
    ClientRequest request = new ClientRequest("http://localhost:8080/resteasy-ejb-test/create/");
    Book book1 = new Book("RESTEasy: the Sequel");
    request.body(Constants.MEDIA_TYPE_TEST_XML, book1);
    ClientResponse<?> response = request.post();
    log.info("Status: " + response.getStatus());
    assertEquals(200, response.getStatus());
    int id1 = response.getEntity(int.class);
    log.info("id: " + id1);
    Assert.assertEquals(Counter.INITIAL_VALUE, id1);

    // Create another book.
    request = new ClientRequest("http://localhost:8080/resteasy-ejb-test/create/");
    Book book2 = new Book("RESTEasy: It's Alive");
    request.body(Constants.MEDIA_TYPE_TEST_XML, book2);
    response = request.post();
    log.info("Status: " + response.getStatus());
    assertEquals(200, response.getStatus());
    int id2 = response.getEntity(int.class);
    log.info("id: " + id2);
    Assert.assertEquals(Counter.INITIAL_VALUE + 1, id2);

    // Retrieve first book.
    request = new ClientRequest("http://localhost:8080/resteasy-ejb-test/book/" + id1);
    request.accept(Constants.MEDIA_TYPE_TEST_XML);
    response = request.get();
    log.info("Status: " + response.getStatus());
    assertEquals(200, response.getStatus());
    Book result = response.getEntity(Book.class);
    log.info("book: " + book1);
    Assert.assertEquals(book1, result);

    // Retrieve second book.
    request = new ClientRequest("http://localhost:8080/resteasy-ejb-test/book/" + id2);
    request.accept(Constants.MEDIA_TYPE_TEST_XML);
    response = request.get();
    log.info("Status: " + response.getStatus());
    assertEquals(200, response.getStatus());
    result = response.getEntity(Book.class);
    log.info("book: " + book2);
    Assert.assertEquals(book2, result);

    // Verify that EJBBookReader and EJBBookWriter have been used, twice on each side.
    request = new ClientRequest("http://localhost:8080/resteasy-ejb-test/uses/4");
    response = request.get();
    log.info("Status: " + response.getStatus());
    assertEquals(200, response.getStatus());
    response.releaseConnection();

    // Reset counter.
    request = new ClientRequest("http://localhost:8080/resteasy-ejb-test/reset");
    response = request.get();
    log.info("Status: " + response.getStatus());
    assertEquals(204, response.getStatus());
    response.releaseConnection();
  }
  private JaxbCommandResponse<?> executeTaskCommand(String deploymentId, Command<?> command)
      throws Exception {
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(command);

    String urlString =
        new URL(
                deploymentUrl,
                deploymentUrl.getPath() + "rest/runtime/" + DEPLOYMENT_ID + "/execute")
            .toExternalForm();
    logger.info("Client request to: " + urlString);
    ClientRequest restRequest = new ClientRequest(urlString);

    JaxbCommandsRequest commandMessage = new JaxbCommandsRequest(commands);
    assertNotNull("Commands are null!", commandMessage.getCommands());
    assertTrue("Commands are empty!", commandMessage.getCommands().size() > 0);

    String body = JaxbSerializationProvider.convertJaxbObjectToString(commandMessage);
    restRequest.body(MediaType.APPLICATION_XML, body);

    ClientResponse<JaxbCommandsResponse> responseObj = restRequest.post(JaxbCommandsResponse.class);
    checkResponse(responseObj);

    JaxbCommandsResponse cmdsResp = responseObj.getEntity();
    return cmdsResp.getResponses().get(0);
  }
 public <T> T execute(Command<T> command) {
   ClientRequest restRequest = new ClientRequest(url);
   try {
     restRequest.body(MediaType.APPLICATION_XML, new JaxbCommandsRequest(deploymentId, command));
     ClientResponse<Object> response = restRequest.post(Object.class);
     if (response.getResponseStatus() == Status.OK) {
       JaxbCommandsResponse commandResponse = response.getEntity(JaxbCommandsResponse.class);
       List<JaxbCommandResponse<?>> responses = commandResponse.getResponses();
       if (responses.size() == 0) {
         return null;
       } else if (responses.size() == 1) {
         JaxbCommandResponse<?> responseObject = responses.get(0);
         if (responseObject instanceof JaxbExceptionResponse) {
           JaxbExceptionResponse exceptionResponse = (JaxbExceptionResponse) responseObject;
           String causeMessage = exceptionResponse.getCauseMessage();
           throw new RuntimeException(
               exceptionResponse.getMessage()
                   + (causeMessage == null ? "" : " Caused by: " + causeMessage));
         } else {
           return (T) responseObject.getResult();
         }
       } else {
         throw new RuntimeException("Unexpected number of results: " + responses.size());
       }
     } else {
       // TODO error handling
       throw new RuntimeException("REST request error code " + response.getResponseStatus());
     }
   } catch (Exception e) {
     throw new RuntimeException("Unable to execute REST request: " + e.getMessage(), e);
   }
 }
  @Test
  public void testRestUrlStartHumanTaskProcess() throws Exception {
    // create REST request
    String urlString =
        new URL(
                deploymentUrl,
                deploymentUrl.getPath() + "rest/runtime/test/process/org.jbpm.humantask/start")
            .toExternalForm();
    ClientRequest restRequest = new ClientRequest(urlString);

    // Get and check response
    logger.debug(">> [org.jbpm.humantask/start]" + urlString);
    ClientResponse responseObj = restRequest.post();
    assertEquals(200, responseObj.getStatus());
    JaxbProcessInstanceResponse processInstance =
        (JaxbProcessInstanceResponse) responseObj.getEntity(JaxbProcessInstanceResponse.class);
    long procInstId = processInstance.getId();

    urlString =
        new URL(deploymentUrl, deploymentUrl.getPath() + "rest/task/query?taskOwner=" + USER_ID)
            .toExternalForm();
    restRequest = new ClientRequest(urlString);
    logger.debug(">> [task/query]" + urlString);
    responseObj = restRequest.get();
    assertEquals(200, responseObj.getStatus());

    JaxbTaskSummaryListResponse taskSumlistResponse =
        (JaxbTaskSummaryListResponse) responseObj.getEntity(JaxbTaskSummaryListResponse.class);
    long taskId = findTaskId(procInstId, taskSumlistResponse.getResult());

    urlString =
        new URL(
                deploymentUrl,
                deploymentUrl.getPath() + "rest/task/" + taskId + "/start?userId=" + USER_ID)
            .toExternalForm();
    restRequest = new ClientRequest(urlString);

    // Get response
    logger.debug(">> [task/?/start] " + urlString);
    responseObj = restRequest.post();

    // Check response
    checkResponse(responseObj);
  }
 public void run() {
   try {
     ClientRequest req = new ClientRequest(url);
     req.header("Accept-Wait", acceptWaitTime);
     ClientResponse<?> response = req.post();
     response.releaseConnection();
     isFinished = true;
   } catch (Exception e) {
     failed = true;
   }
 }
 public void run() {
   try {
     isFinished = false;
     ClientRequest request = new ClientRequest(url);
     ClientResponse<?> response = request.post();
     response.releaseConnection();
     System.out.println("NPS response: " + response.getStatus());
     Assert.assertEquals(201, response.getStatus());
     isFinished = true;
   } catch (Exception e) {
     System.out.println("Exception " + e);
     failed = true;
   }
 }
  @Test
  public void testContentType() throws Exception {
    String message =
        "--boo\r\n"
            + "Content-Disposition: form-data; name=\"foo\"\r\n"
            + "Content-Transfer-Encoding: 8bit\r\n\r\n"
            + "bar\r\n"
            + "--boo--\r\n";

    ClientRequest request = new ClientRequest(TEST_URI + "/mime");
    request.body("multipart/form-data; boundary=boo", message.getBytes());
    ClientResponse<String> response = request.post(String.class);
    Assert.assertEquals("Status code is wrong.", 20, response.getStatus() / 10);
    Assert.assertEquals(
        "Response text is wrong",
        MediaType.valueOf(TEXT_PLAIN_WITH_CHARSET_UTF_8),
        MediaType.valueOf(response.getEntity()));
  }
  @Test
  public void testRestHistoryLogs() throws Exception {
    String urlString =
        new URL(
                deploymentUrl,
                deploymentUrl.getPath() + "rest/runtime/test/process/var-proc/start?map_x=initVal")
            .toExternalForm();
    ClientRequest restRequest = new ClientRequest(urlString);

    // Get and check response
    logger.debug(">> " + urlString);
    ClientResponse responseObj = restRequest.post();
    assertEquals(200, responseObj.getStatus());
    JaxbProcessInstanceResponse processInstance =
        (JaxbProcessInstanceResponse) responseObj.getEntity(JaxbProcessInstanceResponse.class);
    long procInstId = processInstance.getId();

    urlString =
        new URL(
                deploymentUrl,
                deploymentUrl.getPath()
                    + "rest/runtime/test/history/instance/"
                    + procInstId
                    + "/variable/x")
            .toExternalForm();
    restRequest = new ClientRequest(urlString);
    logger.debug(">> [history/variables]" + urlString);
    responseObj = restRequest.get();
    assertEquals(200, responseObj.getStatus());
    JaxbHistoryLogList logList =
        (JaxbHistoryLogList) responseObj.getEntity(JaxbHistoryLogList.class);
    List<AbstractJaxbHistoryObject> varLogList = logList.getHistoryLogList();
    assertEquals("Incorrect number of variable logs", 4, varLogList.size());

    for (AbstractJaxbHistoryObject<?> log : logList.getHistoryLogList()) {
      JaxbVariableInstanceLog varLog = (JaxbVariableInstanceLog) log;
      assertEquals("Incorrect variable id", "x", varLog.getVariableId());
      assertEquals("Incorrect process id", "var-proc", varLog.getProcessId());
      assertEquals("Incorrect process instance id", "var-proc", varLog.getProcessId());
    }
  }
  @Ignore
  public void testSetMember() throws Exception {
    final String Path = "/members";
    ClientRequest webResource = new ClientRequest(BASE_URI + Path);

    ClientResponse response = null;

    JSONObject json_data = new JSONObject();
    json_data.put("name", "Jose Osuna");
    json_data.put("email", "*****@*****.**");
    json_data.put("phoneNumber", "04149367375");

    System.out.println(json_data);

    ClientRequest resource = webResource;
    resource.formParameter("auth_token", AUTH_TOKEN).formParameter("member", json_data);

    response = resource.post(ClientResponse.class);

    assertTrue(response.getStatus() == HTTP_STATUS_CREATED);
  }
 public Status doPost(String url, MediaType mediaType) throws Exception {
   ClientRequest request = createClientRequest(url);
   ClientResponse<String> result = null;
   try {
     String type;
     if (mediaType != null) {
       LOGGER.info(String.format("Content-Type:%s", type = mediaType.toString()));
     } else {
       LOGGER.info(String.format("Content-Type:%s", type = "text/plain"));
     }
     addParams(request);
     addBody(request, type);
     result = request.post(String.class);
     LOGGER.info("O servico '{}' respondeu com a seguinte mensagem: {}.", url, result.getEntity());
     return result.getResponseStatus();
   } catch (Exception e) {
     LOGGER.warn("Erro durante chamada ao metodo POST da URL: {}.", url, e);
     throw e;
   } finally {
     releaseConnection(result);
   }
 }
  @Test
  public void testResteasy734() throws Exception {
    ClientRequest request = null;
    ClientResponse<String> response = null;

    request = new ClientRequest("http://localhost:8081/encoded/pathparam/bee bop");
    response = request.get(String.class);
    System.out.println("Received encoded path param: " + response.getEntity());
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("bee%20bop", response.getEntity());

    request = new ClientRequest("http://localhost:8081/decoded/pathparam/bee bop");
    response = request.get(String.class);
    System.out.println("Received decoded path param: " + response.getEntity());
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("bee bop", response.getEntity());

    request = new ClientRequest("http://localhost:8081/encoded/matrix;m=bee bop");
    response = request.get(String.class);
    System.out.println("Received encoded matrix param: " + response.getEntity());
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("bee%20bop", response.getEntity());

    request = new ClientRequest("http://localhost:8081/decoded/matrix;m=bee bop");
    response = request.get(String.class);
    System.out.println("Received decoded matrix param: " + response.getEntity());
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("bee bop", response.getEntity());

    request = new ClientRequest("http://localhost:8081/encoded/query?m=bee bop");
    response = request.get(String.class);
    System.out.println("Received encoded query param: " + response.getEntity());
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("bee%20bop", response.getEntity());

    request = new ClientRequest("http://localhost:8081/decoded/query?m=bee bop");
    response = request.get(String.class);
    System.out.println("Received decoded query param: " + response.getEntity());
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("bee bop", response.getEntity());

    request = new ClientRequest("http://localhost:8081/encoded/form");
    request.formParameter("f", "bee bop");
    response = request.post(String.class);
    System.out.println("Received encoded form param: " + response.getEntity());
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("bee%20bop", response.getEntity());

    request = new ClientRequest("http://localhost:8081/decoded/form");
    request.formParameter("f", "bee bop");
    response = request.post(String.class);
    System.out.println("Received decoded form param: " + response.getEntity());
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("bee bop", response.getEntity());

    request = new ClientRequest("http://localhost:8081/encoded/segment/bee bop");
    response = request.get(String.class);
    System.out.println("Received encoded path param from segment: " + response.getEntity());
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("bee%20bop", response.getEntity());

    request = new ClientRequest("http://localhost:8081/decoded/segment/bee bop");
    response = request.get(String.class);
    System.out.println("Received decoded path param from segment: " + response.getEntity());
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("bee bop", response.getEntity());

    request = new ClientRequest("http://localhost:8081/encoded/segment/matrix/params;m=bee bop");
    response = request.get(String.class);
    System.out.println("Received encoded matrix param from segment: " + response.getEntity());
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("bee%20bop", response.getEntity());

    request = new ClientRequest("http://localhost:8081/decoded/segment/matrix/params;m=bee bop");
    response = request.get(String.class);
    System.out.println("Received decoded matrix param from segment: " + response.getEntity());
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("bee bop", response.getEntity());
  }
  @Test
  public void testRestExecuteStartProcess() throws Exception {
    // Start process
    String urlString =
        new URL(deploymentUrl, deploymentUrl.getPath() + "rest/runtime/test/execute")
            .toExternalForm();

    ClientRequest restRequest = new ClientRequest(urlString);
    JaxbCommandsRequest commandMessage =
        new JaxbCommandsRequest(DEPLOYMENT_ID, new StartProcessCommand("org.jbpm.humantask"));
    String body = JaxbSerializationProvider.convertJaxbObjectToString(commandMessage);
    restRequest.body(MediaType.APPLICATION_XML, body);

    logger.debug(">> [startProcess] " + urlString);
    ClientResponse responseObj = restRequest.post();

    assertEquals(200, responseObj.getStatus());
    JaxbCommandsResponse cmdsResp =
        (JaxbCommandsResponse) responseObj.getEntity(JaxbCommandsResponse.class);
    long procInstId = ((ProcessInstance) cmdsResp.getResponses().get(0)).getId();

    // query tasks
    restRequest = new ClientRequest(urlString);
    commandMessage =
        new JaxbCommandsRequest(DEPLOYMENT_ID, new GetTasksByProcessInstanceIdCommand(procInstId));
    body = JaxbSerializationProvider.convertJaxbObjectToString(commandMessage);
    restRequest.body(MediaType.APPLICATION_XML, body);

    logger.debug(">> [getTasksByProcessInstanceId] " + urlString);
    responseObj = restRequest.post();
    assertEquals(200, responseObj.getStatus());
    JaxbCommandsResponse cmdResponse =
        (JaxbCommandsResponse) responseObj.getEntity(JaxbCommandsResponse.class);
    List list = (List) cmdResponse.getResponses().get(0).getResult();
    long taskId = (Long) list.get(0);

    // start task

    logger.debug(">> [startTask] " + urlString);
    restRequest = new ClientRequest(urlString);
    commandMessage = new JaxbCommandsRequest(new StartTaskCommand(taskId, USER_ID));
    body = JaxbSerializationProvider.convertJaxbObjectToString(commandMessage);
    restRequest.body(MediaType.APPLICATION_XML, commandMessage);

    // Get response
    responseObj = restRequest.post();

    // Check response
    checkResponse(responseObj);

    urlString =
        new URL(deploymentUrl, deploymentUrl.getPath() + "rest/task/execute").toExternalForm();

    restRequest = new ClientRequest(urlString);
    commandMessage = new JaxbCommandsRequest(new CompleteTaskCommand(taskId, USER_ID, null));
    body = JaxbSerializationProvider.convertJaxbObjectToString(commandMessage);
    restRequest.body(MediaType.APPLICATION_XML, commandMessage);

    // Get response
    logger.debug(">> [completeTask] " + urlString);
    responseObj = restRequest.post();
    checkResponse(responseObj);
  }