Search in sources :

Example 1 with IdResponse

use of com.linkedin.restli.common.IdResponse in project rest.li by linkedin.

the class MockResponseBuilder method build.

/**
   * Builds a {@link Response} that has been constructed using the setters in this class.
   *
   * @return the constructed {@link Response}
   */
public Response<V> build() {
    Map<String, String> headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
    if (_headers != null) {
        headers.putAll(_headers);
    }
    ProtocolVersion protocolVersion = (_protocolVersion == null) ? AllProtocolVersions.BASELINE_PROTOCOL_VERSION : _protocolVersion;
    headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
    int status = (_status == 0) ? DEFAULT_HTTP_STATUS : _status;
    if (_entity instanceof CreateResponse || _entity instanceof IdResponse) {
        final K id;
        if (_entity instanceof CreateResponse) {
            @SuppressWarnings("unchecked") final CreateResponse<K> createResponse = (CreateResponse<K>) _entity;
            id = createResponse.getId();
        } else {
            @SuppressWarnings("unchecked") final IdResponse<K> idResponse = (IdResponse<K>) _entity;
            id = idResponse.getId();
        }
        headers.put(HeaderUtil.getIdHeaderName(protocolVersion), URIParamUtils.encodeKeyForBody(id, false, protocolVersion));
    }
    List<HttpCookie> cookies = _cookies == null ? Collections.<HttpCookie>emptyList() : _cookies;
    final ResponseImpl<V> response = new ResponseImpl<V>(status, headers, cookies, _entity, _restLiResponseException);
    response.setAttachmentReader(_restLiAttachmentReader);
    return response;
}
Also used : IdResponse(com.linkedin.restli.common.IdResponse) CreateResponse(com.linkedin.restli.client.response.CreateResponse) TreeMap(java.util.TreeMap) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) ResponseImpl(com.linkedin.restli.internal.client.ResponseImpl) HttpCookie(java.net.HttpCookie)

Example 2 with IdResponse

use of com.linkedin.restli.common.IdResponse in project rest.li by linkedin.

the class TestGroupsClient method testCollectionCreateGetUpdateDeleteId.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testCollectionCreateGetUpdateDeleteId(RestliRequestOptions requestOptions) throws RemoteInvocationException {
    // find with optional params
    Group group = new Group();
    String name = "test";
    int memberID = 1;
    group.setName(name);
    group.setOwner(buildGroupMembership(memberID, "a@a.a", "f", "l"));
    GroupMembershipParam param = new GroupMembershipParam();
    param.setIntParameter(1);
    param.setStringParameter("String");
    final GroupsRequestBuilders groupBuilders = new GroupsRequestBuilders(requestOptions);
    final GroupMembershipsRequestBuilders membershipBuilders = new GroupMembershipsRequestBuilders(requestOptions);
    // Create
    Response<IdResponse<Integer>> response = getClient().sendRequest(groupBuilders.create().input(group).build()).getResponse();
    Assert.assertEquals(response.getStatus(), 201);
    Integer createdId = response.getEntity().getId();
    Assert.assertNotNull(createdId);
    @SuppressWarnings("deprecation") String stringId = response.getId();
    Assert.assertEquals(createdId.intValue(), Integer.parseInt(stringId));
    // Get newly created group and verify name
    Assert.assertEquals(getClient().sendRequest(groupBuilders.get().id(createdId).build()).getResponse().getEntity().getName(), name);
    // Partial update - change name
    String newName = "new name";
    group.setName(newName);
    PatchRequest<Group> patch = PatchGenerator.diffEmpty(group);
    ResponseFuture<EmptyRecord> responseFuture = getClient().sendRequest(groupBuilders.partialUpdate().id(createdId).input(patch).build());
    Assert.assertEquals(204, responseFuture.getResponse().getStatus());
    // Get updated group and verify name
    Assert.assertEquals(getClient().sendRequest(groupBuilders.get().id(createdId).build()).getResponse().getEntity().getName(), newName);
    // Delete
    responseFuture = getClient().sendRequest(groupBuilders.delete().id(createdId).build());
    Assert.assertEquals(204, responseFuture.getResponse().getStatus());
    // Verify deleted
    try {
        getClient().sendRequest(groupBuilders.get().id(createdId).build()).getResponse();
        Assert.fail("Expected RestLiResponseException");
    } catch (RestLiResponseException e) {
        Assert.assertEquals(e.getStatus(), 404);
    }
    // Cleanup - delete the owner's membership that was created along with the group
    responseFuture = getClient().sendRequest(membershipBuilders.delete().id(buildCompoundKey(memberID, createdId)).build());
    Assert.assertEquals(204, responseFuture.getResponse().getStatus());
}
Also used : Group(com.linkedin.restli.examples.groups.api.Group) EmptyRecord(com.linkedin.restli.common.EmptyRecord) IdResponse(com.linkedin.restli.common.IdResponse) GroupMembershipsRequestBuilders(com.linkedin.restli.examples.groups.client.GroupMembershipsRequestBuilders) GroupMembershipParam(com.linkedin.restli.examples.groups.api.GroupMembershipParam) GroupsRequestBuilders(com.linkedin.restli.examples.groups.client.GroupsRequestBuilders) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) Test(org.testng.annotations.Test)

Example 3 with IdResponse

use of com.linkedin.restli.common.IdResponse in project rest.li by linkedin.

the class TestGroupsClient method testCollectionCreateGetUpdateDelete.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testCollectionCreateGetUpdateDelete(RestliRequestOptions requestOptions) throws RemoteInvocationException {
    // find with optional params
    Group group = new Group();
    String name = "test";
    int memberID = 1;
    group.setName(name);
    group.setOwner(buildGroupMembership(memberID, "a@a.a", "f", "l"));
    GroupMembershipParam param = new GroupMembershipParam();
    param.setIntParameter(1);
    param.setStringParameter("String");
    final GroupsRequestBuilders groupBuilders = new GroupsRequestBuilders(requestOptions);
    final GroupMembershipsRequestBuilders membershipBuilders = new GroupMembershipsRequestBuilders(requestOptions);
    // Create
    Response<IdResponse<Integer>> response = getClient().sendRequest(groupBuilders.create().input(group).build()).getResponse();
    Assert.assertEquals(response.getStatus(), 201);
    @SuppressWarnings("unchecked") IdResponse<Integer> createResponse = response.getEntity();
    Assert.assertNotNull(createResponse.getId());
    @SuppressWarnings("deprecation") String stringId = response.getId();
    Assert.assertEquals(createResponse.getId().intValue(), Integer.parseInt(stringId));
    // Get newly created group and verify name
    Integer createdId = createResponse.getId();
    Assert.assertEquals(getClient().sendRequest(groupBuilders.get().id(createResponse.getId()).build()).getResponse().getEntity().getName(), name);
    // Partial update - change name
    String newName = "new name";
    group.setName(newName);
    PatchRequest<Group> patch = PatchGenerator.diffEmpty(group);
    ResponseFuture<EmptyRecord> responseFuture = getClient().sendRequest(groupBuilders.partialUpdate().id(createdId).input(patch).build());
    Assert.assertEquals(204, responseFuture.getResponse().getStatus());
    // Get updated group and verify name
    Assert.assertEquals(getClient().sendRequest(groupBuilders.get().id(createdId).build()).getResponse().getEntity().getName(), newName);
    // Delete
    responseFuture = getClient().sendRequest(groupBuilders.delete().id(createdId).build());
    Assert.assertEquals(204, responseFuture.getResponse().getStatus());
    // Verify deleted
    try {
        getClient().sendRequest(groupBuilders.get().id(createdId).build()).getResponse();
        Assert.fail("Expected RestLiResponseException");
    } catch (RestLiResponseException e) {
        Assert.assertEquals(e.getStatus(), 404);
    }
    // Cleanup - delete the owner's membership that was created along with the group
    responseFuture = getClient().sendRequest(membershipBuilders.delete().id(buildCompoundKey(memberID, createdId)).build());
    Assert.assertEquals(204, responseFuture.getResponse().getStatus());
}
Also used : Group(com.linkedin.restli.examples.groups.api.Group) EmptyRecord(com.linkedin.restli.common.EmptyRecord) IdResponse(com.linkedin.restli.common.IdResponse) GroupMembershipsRequestBuilders(com.linkedin.restli.examples.groups.client.GroupMembershipsRequestBuilders) GroupMembershipParam(com.linkedin.restli.examples.groups.api.GroupMembershipParam) GroupsRequestBuilders(com.linkedin.restli.examples.groups.client.GroupsRequestBuilders) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) Test(org.testng.annotations.Test)

Example 4 with IdResponse

use of com.linkedin.restli.common.IdResponse in project rest.li by linkedin.

the class ResponseImpl method getId.

/**
   * Specific getter for the 'X-LinkedIn-Id' header
   *
   * @throws UnsupportedOperationException if the entity returned is a {@link CreateResponse} or {@link IdResponse}
   * and the key is a {@link ComplexResourceKey} or {@link CompoundKey}.
   *
   * @deprecated
   * @see {@link com.linkedin.restli.client.Response#getId()}
   */
@Override
@Deprecated
public String getId() {
    if (_entity instanceof CreateResponse<?> || _entity instanceof IdResponse<?> || _entity instanceof IdEntityResponse<?, ?>) {
        final Object id = checkAndReturnId();
        final ProtocolVersion protocolVersion = ProtocolVersionUtil.extractProtocolVersion(_headers);
        return URIParamUtils.encodeKeyForHeader(id, protocolVersion);
    } else {
        return null;
    }
}
Also used : IdResponse(com.linkedin.restli.common.IdResponse) CreateResponse(com.linkedin.restli.client.response.CreateResponse) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion)

Example 5 with IdResponse

use of com.linkedin.restli.common.IdResponse in project rest.li by linkedin.

the class TestGreetingClientContentTypes method testCreateId.

@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());
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) IdResponse(com.linkedin.restli.common.IdResponse) GreetingsRequestBuilders(com.linkedin.restli.examples.greetings.client.GreetingsRequestBuilders) Test(org.testng.annotations.Test)

Aggregations

IdResponse (com.linkedin.restli.common.IdResponse)16 Test (org.testng.annotations.Test)10 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)9 CreateResponse (com.linkedin.restli.client.response.CreateResponse)4 BatchCreateIdResponse (com.linkedin.restli.common.BatchCreateIdResponse)4 EmptyRecord (com.linkedin.restli.common.EmptyRecord)4 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)3 CreateIdRequestBuilder (com.linkedin.restli.client.CreateIdRequestBuilder)2 RestLiResponseException (com.linkedin.restli.client.RestLiResponseException)2 Group (com.linkedin.restli.examples.groups.api.Group)2 GroupMembershipParam (com.linkedin.restli.examples.groups.api.GroupMembershipParam)2 GroupMembershipsRequestBuilders (com.linkedin.restli.examples.groups.client.GroupMembershipsRequestBuilders)2 GroupsRequestBuilders (com.linkedin.restli.examples.groups.client.GroupsRequestBuilders)2 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)2 CreateResponse (com.linkedin.restli.server.CreateResponse)2 ResourceContext (com.linkedin.restli.server.ResourceContext)2 RootBuilderWrapper (com.linkedin.restli.test.util.RootBuilderWrapper)2 DataMap (com.linkedin.data.DataMap)1 UriBuilder (com.linkedin.jersey.api.uri.UriBuilder)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1