Search in sources :

Example 1 with CreateResponse

use of com.linkedin.restli.client.response.CreateResponse 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.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<>(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 CreateResponse

use of com.linkedin.restli.client.response.CreateResponse in project rest.li by linkedin.

the class TestDebugRequestHandlers method createNewGreetingOnTheServer.

private Long createNewGreetingOnTheServer(RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
    Greeting newGreeting = new Greeting().setMessage("New Greeting!").setTone(Tone.FRIENDLY);
    RootBuilderWrapper.MethodBuilderWrapper<Long, Greeting, EmptyRecord> createBuilderWrapper = builders.create();
    Long createdId;
    if (createBuilderWrapper.isRestLi2Builder()) {
        Object objBuilder = createBuilderWrapper.getBuilder();
        @SuppressWarnings("unchecked") CreateIdRequestBuilder<Long, Greeting> createIdRequestBuilder = (CreateIdRequestBuilder<Long, Greeting>) objBuilder;
        CreateIdRequest<Long, Greeting> request = createIdRequestBuilder.input(newGreeting).build();
        Response<IdResponse<Long>> response = getClient().sendRequest(request).getResponse();
        createdId = response.getEntity().getId();
    } else {
        Request<EmptyRecord> request = createBuilderWrapper.input(newGreeting).build();
        Response<EmptyRecord> response = getClient().sendRequest(request).getResponse();
        @SuppressWarnings("unchecked") CreateResponse<Long> createResponse = (CreateResponse<Long>) response.getEntity();
        createdId = createResponse.getId();
    }
    return createdId;
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) EmptyRecord(com.linkedin.restli.common.EmptyRecord) IdResponse(com.linkedin.restli.common.IdResponse) CreateResponse(com.linkedin.restli.client.response.CreateResponse) RootBuilderWrapper(com.linkedin.restli.test.util.RootBuilderWrapper) CreateIdRequestBuilder(com.linkedin.restli.client.CreateIdRequestBuilder)

Example 3 with CreateResponse

use of com.linkedin.restli.client.response.CreateResponse in project rest.li by linkedin.

the class TestSimpleResourceHierarchy method testSubCollectionCreate.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testSubCollectionCreate(RestliRequestOptions requestOptions) throws RemoteInvocationException {
    Greeting greeting = new Greeting();
    greeting.setMessage("Hello there!");
    greeting.setTone(Tone.FRIENDLY);
    final SubgreetingsBuilders builders = new SubgreetingsBuilders(requestOptions);
    // POST
    Request<EmptyRecord> createRequest = builders.create().input(greeting).build();
    Response<EmptyRecord> response = getClient().sendRequest(createRequest).getResponse();
    Assert.assertNull(response.getHeader(RestConstants.HEADER_CONTENT_TYPE));
    @SuppressWarnings("unchecked") CreateResponse<Long> createResponse = (CreateResponse<Long>) response.getEntity();
    long id = createResponse.getId();
    @SuppressWarnings("deprecation") String stringId = response.getId();
    Assert.assertEquals(id, Long.parseLong(stringId));
    // GET again to verify that the create has worked.
    Request<Greeting> getRequest = builders.get().id(id).build();
    Response<Greeting> getResponse = getClient().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) EmptyRecord(com.linkedin.restli.common.EmptyRecord) CreateResponse(com.linkedin.restli.client.response.CreateResponse) SubgreetingsBuilders(com.linkedin.restli.examples.greetings.client.SubgreetingsBuilders) Test(org.testng.annotations.Test)

Example 4 with CreateResponse

use of com.linkedin.restli.client.response.CreateResponse 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 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 CreateResponse

use of com.linkedin.restli.client.response.CreateResponse in project rest.li by linkedin.

the class TestComplexKeysResource method testCreateMainOldBuilders.

private void testCreateMainOldBuilders(CreateRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> createRequestBuilder, GetRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> getRequestBuilder) throws RemoteInvocationException {
    final String messageText = "newMessage";
    Message message = new Message();
    message.setMessage(messageText);
    Request<EmptyRecord> request = createRequestBuilder.input(message).build();
    ResponseFuture<EmptyRecord> future = getClient().sendRequest(request);
    Response<EmptyRecord> response = future.getResponse();
    Assert.assertEquals(response.getStatus(), 201);
    ComplexResourceKey<TwoPartKey, TwoPartKey> expectedComplexKey = getComplexKey(messageText, messageText);
    try {
        @SuppressWarnings("deprecation") String stringId = response.getId();
        Assert.fail("getId() should throw an exception for complex resource keys!");
    } catch (UnsupportedOperationException e) {
    // expected
    }
    @SuppressWarnings("unchecked") CreateResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>> createResponse = (CreateResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>>) response.getEntity();
    Assert.assertEquals(createResponse.getId(), expectedComplexKey);
    // attempt to get the record you just created
    @SuppressWarnings("unchecked") Request<Message> getRequest = getRequestBuilder.id(expectedComplexKey).build();
    ResponseFuture<Message> getFuture = getClient().sendRequest(getRequest);
    Response<Message> getResponse = getFuture.getResponse();
    Assert.assertEquals(getResponse.getEntity().getMessage(), messageText);
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) EmptyRecord(com.linkedin.restli.common.EmptyRecord) Message(com.linkedin.restli.examples.greetings.api.Message) CreateResponse(com.linkedin.restli.client.response.CreateResponse) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey)

Aggregations

CreateResponse (com.linkedin.restli.client.response.CreateResponse)9 EmptyRecord (com.linkedin.restli.common.EmptyRecord)7 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)6 IdResponse (com.linkedin.restli.common.IdResponse)4 Test (org.testng.annotations.Test)4 CreateIdRequestBuilder (com.linkedin.restli.client.CreateIdRequestBuilder)2 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)2 RootBuilderWrapper (com.linkedin.restli.test.util.RootBuilderWrapper)2 BatchCreateIdResponse (com.linkedin.restli.common.BatchCreateIdResponse)1 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)1 CustomLong (com.linkedin.restli.examples.custom.types.CustomLong)1 Message (com.linkedin.restli.examples.greetings.api.Message)1 TwoPartKey (com.linkedin.restli.examples.greetings.api.TwoPartKey)1 CustomTypes2Builders (com.linkedin.restli.examples.greetings.client.CustomTypes2Builders)1 GreetingsBuilders (com.linkedin.restli.examples.greetings.client.GreetingsBuilders)1 SubgreetingsBuilders (com.linkedin.restli.examples.greetings.client.SubgreetingsBuilders)1 ResponseImpl (com.linkedin.restli.internal.client.ResponseImpl)1 HttpCookie (java.net.HttpCookie)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1