Esempio n. 1
0
  /**
   * a more concrete example of custom action<br>
   * resource level determines the granularity of the action<br>
   * mismatching the resource level in the request throws exception and will respond HTTP 400
   *
   * @param resource Instance of the resource class. This is not part of the action method and is
   *     needed because this implementation is not an actual resource.
   */
  @Action(name = "updateTone", resourceLevel = ResourceLevel.ENTITY)
  // The base resource parameter gets special handling in the generator. It is set to the actual
  // resource class instance, and is not part of the generated REST method.
  public Greeting updateTone(
      BaseResource resource,
      @ActionParam("newTone") @Optional Tone newTone,
      @ActionParam("delOld") @Optional("false") Boolean delOld) {
    // the way to get entity key in action
    Long key = resource.getContext().getPathKeys().get(_resourceName + "Id");
    Greeting g = _db.get(key);
    if (g == null) {
      // HTTP 404
      return g;
    }

    // delete existing Greeting and assign new key
    if (delOld) {
      _db.remove(key);
      key = _idSeq.incrementAndGet();
      g.setId(key);
    }

    Tone t;
    // newTone is an optional parameter
    // omitting it in request results a null value
    if (newTone == null) {
      t = DEFAULT_TONE;
    } else {
      t = newTone;
    }
    g.setTone(t);
    _db.put(key, g);

    return g;
  }
  @Test(
      dataProvider =
          com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX
              + "buildersClientDataDataProvider")
  public void testCreateId(RestClient restClient, RestliRequestOptions requestOptions)
      throws RemoteInvocationException {
    Greeting greeting = new Greeting();
    greeting.setMessage("Hello there!");
    greeting.setTone(Tone.FRIENDLY);

    final GreetingsRequestBuilders builders = new GreetingsRequestBuilders(requestOptions);

    CreateIdRequest<Long, Greeting> createRequest = builders.create().input(greeting).build();
    Response<IdResponse<Long>> response = restClient.sendRequest(createRequest).getResponse();
    Assert.assertNull(response.getHeader(RestConstants.HEADER_CONTENT_TYPE));
    @SuppressWarnings("unchecked")
    long id = response.getEntity().getId();
    @SuppressWarnings("deprecation")
    String stringId = response.getId();
    Assert.assertEquals(id, Long.parseLong(stringId));

    Request<Greeting> getRequest = builders.get().id(id).build();
    Response<Greeting> getResponse = restClient.sendRequest(getRequest).getResponse();
    Greeting responseGreeting = getResponse.getEntity();

    Assert.assertEquals(responseGreeting.getMessage(), greeting.getMessage());
    Assert.assertEquals(responseGreeting.getTone(), greeting.getTone());
  }
  @Test(
      dataProvider =
          com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX
              + "requestBuilderDataProvider")
  public void testPostsWithCharset(RootBuilderWrapper<Long, Greeting> builders)
      throws RemoteInvocationException {
    RestClient restClient = new RestClient(CLIENT, URI_PREFIX);

    Request<Greeting> request =
        builders
            .<Greeting>action("SomeAction")
            .id(1L)
            .setActionParam("A", 1)
            .setActionParam("B", "")
            .setActionParam("C", new TransferOwnershipRequest())
            .setActionParam("D", new TransferOwnershipRequest())
            .setActionParam("E", 3)
            .setHeader("Content-Type", "application/json; charset=UTF-8")
            .build();

    Response<Greeting> response = restClient.sendRequest(request).getResponse();

    Greeting actionGreeting = response.getEntity();
    Assert.assertEquals(actionGreeting.getMessage(), "This is a newly created greeting");

    Greeting createGreeting = new Greeting();
    createGreeting.setMessage("Hello there!");
    createGreeting.setTone(Tone.FRIENDLY);

    Request<EmptyRecord> createRequest =
        builders
            .create()
            .input(createGreeting)
            .setHeader("Content-Type", "application/json; charset=UTF-8")
            .build();
    Response<EmptyRecord> emptyRecordResponse = restClient.sendRequest(createRequest).getResponse();
    Assert.assertNull(emptyRecordResponse.getHeader(RestConstants.HEADER_CONTENT_TYPE));
  }