Search in sources :

Example 31 with EntityResponse

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

the class TestMockBatchKVResponseFactory method customPrimitiveTyperefKeyData.

@DataProvider(name = "customPrimitiveTyperefKey")
public Object[][] customPrimitiveTyperefKeyData() {
    MyCustomString m1 = new MyCustomString("1");
    MyCustomString m2 = new MyCustomString("2");
    MyCustomString m3 = new MyCustomString("3");
    Map<MyCustomString, Greeting> recordTemplates = new HashMap<>();
    Map<MyCustomString, ErrorResponse> errorResponses = new HashMap<>();
    recordTemplates.put(m1, buildGreeting(1L));
    recordTemplates.put(m2, buildGreeting(2L));
    errorResponses.put(m3, new ErrorResponse().setMessage("3"));
    Map<MyCustomString, HttpStatus> statuses = new HashMap<>();
    statuses.put(m1, HttpStatus.S_200_OK);
    statuses.put(m2, HttpStatus.S_200_OK);
    statuses.put(m3, HttpStatus.S_500_INTERNAL_SERVER_ERROR);
    Map<MyCustomString, EntityResponse<Greeting>> expectedResults = new HashMap<>();
    expectedResults.put(m1, buildEntityResponse(recordTemplates.get(m1), HttpStatus.S_200_OK, null));
    expectedResults.put(m2, buildEntityResponse(recordTemplates.get(m2), HttpStatus.S_200_OK, null));
    expectedResults.put(m3, buildEntityResponse(null, HttpStatus.S_500_INTERNAL_SERVER_ERROR, errorResponses.get(m3)));
    return new Object[][] { { recordTemplates, statuses, errorResponses, expectedResults } };
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) MyCustomString(com.linkedin.restli.common.MyCustomString) HashMap(java.util.HashMap) HttpStatus(com.linkedin.restli.common.HttpStatus) EntityResponse(com.linkedin.restli.common.EntityResponse) ErrorResponse(com.linkedin.restli.common.ErrorResponse) DataProvider(org.testng.annotations.DataProvider)

Example 32 with EntityResponse

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

the class MockBatchEntityResponseFactory method createWithComplexKey.

/**
 * Create a {@link BatchKVResponse} where the key is a {@link ComplexResourceKey}
 *
 * @param valueClass the value class
 * @param keyKeyClass the class of the key part of the {@link ComplexResourceKey}
 * @param keyParamsClass the class of the params part of the {@link ComplexResourceKey}
 * @param recordTemplates the data that will be returned for a call to {@link com.linkedin.restli.client.response.BatchKVResponse#getResults()}
 *                        NOTE: the params part of the {@link ComplexResourceKey} is removed in this map. A new
 *                        instance of the params class is created with no data in it.
 * @param statuses The HTTP status codes that will be returned as part of {@link EntityResponse}s
 *                 returned in {@link com.linkedin.restli.client.response.BatchKVResponse#getResults()}
 * @param errorResponses the data that will be returned for a call to {@link com.linkedin.restli.client.response.BatchKVResponse#getErrors()}
 *                       NOTE: the params part of the {@link ComplexResourceKey} is removed in this map. A new
 *                       instance of the params class is created with no data in it.
 * @param <V>
 */
@SuppressWarnings("rawtypes")
public static <KK extends RecordTemplate, KP extends RecordTemplate, V extends RecordTemplate> BatchKVResponse<ComplexResourceKey<KK, KP>, EntityResponse<V>> createWithComplexKey(Class<V> valueClass, Class<KK> keyKeyClass, Class<KP> keyParamsClass, Map<ComplexResourceKey<KK, KP>, V> recordTemplates, Map<ComplexResourceKey<KK, KP>, HttpStatus> statuses, Map<ComplexResourceKey<KK, KP>, ErrorResponse> errorResponses) {
    ProtocolVersion version = AllProtocolVersions.BASELINE_PROTOCOL_VERSION;
    DataMap batchResponseDataMap = buildDataMap(recordTemplates, statuses, errorResponses, version);
    @SuppressWarnings("unchecked") BatchKVResponse<ComplexResourceKey<KK, KP>, EntityResponse<V>> response = (BatchKVResponse<ComplexResourceKey<KK, KP>, EntityResponse<V>>) (Object) new BatchEntityResponse<>(batchResponseDataMap, new TypeSpec<>(ComplexResourceKey.class), TypeSpec.forClassMaybeNull(valueClass), null, ComplexKeySpec.forClassesMaybeNull(keyKeyClass, keyParamsClass), version);
    return response;
}
Also used : BatchEntityResponse(com.linkedin.restli.internal.client.response.BatchEntityResponse) BatchEntityResponse(com.linkedin.restli.internal.client.response.BatchEntityResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) DataMap(com.linkedin.data.DataMap)

Example 33 with EntityResponse

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

the class TestMockBatchKVResponseFactory method complexKeyData.

@DataProvider(name = "complexKey")
public Object[][] complexKeyData() {
    Map<ComplexResourceKey<Greeting, Greeting>, Greeting> recordTemplates = new HashMap<>();
    Map<ComplexResourceKey<Greeting, Greeting>, ErrorResponse> errorResponses = new HashMap<>();
    Greeting g1 = buildGreeting(1L);
    Greeting g2 = buildGreeting(2L);
    Greeting g3 = buildGreeting(3L);
    recordTemplates.put(new ComplexResourceKey<>(g1, g1), g1);
    recordTemplates.put(new ComplexResourceKey<>(g2, g2), g2);
    errorResponses.put(new ComplexResourceKey<>(g3, g3), new ErrorResponse().setMessage("3"));
    Map<ComplexResourceKey<Greeting, Greeting>, HttpStatus> statuses = new HashMap<>();
    statuses.put(new ComplexResourceKey<>(g1, g1), HttpStatus.S_200_OK);
    statuses.put(new ComplexResourceKey<>(g2, g2), HttpStatus.S_200_OK);
    statuses.put(new ComplexResourceKey<>(g3, g3), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
    // Strip the parameters from complex keys in expected results and expected errors.
    Map<ComplexResourceKey<Greeting, Greeting>, Greeting> expectedRecordTemplates = new HashMap<>();
    expectedRecordTemplates.put(new ComplexResourceKey<>(g1, new Greeting()), recordTemplates.get(new ComplexResourceKey<>(g1, g1)));
    expectedRecordTemplates.put(new ComplexResourceKey<>(g2, new Greeting()), recordTemplates.get(new ComplexResourceKey<>(g2, g2)));
    Map<ComplexResourceKey<Greeting, Greeting>, EntityResponse<Greeting>> expectedResults = new HashMap<>();
    expectedResults.put(new ComplexResourceKey<>(g1, new Greeting()), buildEntityResponse(recordTemplates.get(new ComplexResourceKey<>(g1, g1)), HttpStatus.S_200_OK, null));
    expectedResults.put(new ComplexResourceKey<>(g2, new Greeting()), buildEntityResponse(recordTemplates.get(new ComplexResourceKey<>(g2, g2)), HttpStatus.S_200_OK, null));
    expectedResults.put(new ComplexResourceKey<>(g3, new Greeting()), buildEntityResponse(null, HttpStatus.S_500_INTERNAL_SERVER_ERROR, errorResponses.get(new ComplexResourceKey<>(g3, g3))));
    Map<ComplexResourceKey<Greeting, Greeting>, ErrorResponse> expectedErrors = new HashMap<>();
    expectedErrors.put(new ComplexResourceKey<>(g3, new Greeting()), errorResponses.get(new ComplexResourceKey<>(g3, g3)));
    return new Object[][] { { recordTemplates, statuses, errorResponses, expectedRecordTemplates, expectedResults, expectedErrors } };
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) HashMap(java.util.HashMap) HttpStatus(com.linkedin.restli.common.HttpStatus) EntityResponse(com.linkedin.restli.common.EntityResponse) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) ErrorResponse(com.linkedin.restli.common.ErrorResponse) DataProvider(org.testng.annotations.DataProvider)

Example 34 with EntityResponse

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

the class TestComplexKeysResource method testBatchCreateIdMain.

private void testBatchCreateIdMain(BatchCreateIdRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchCreateRequestBuilder, BatchGetEntityRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchGetRequestBuilder) throws RemoteInvocationException {
    final String messageText1 = "firstMessage";
    Message message1 = new Message();
    message1.setMessage(messageText1);
    final String messageText2 = "secondMessage";
    Message message2 = new Message();
    message2.setMessage(messageText2);
    List<Message> messages = new ArrayList<>(2);
    messages.add(message1);
    messages.add(message2);
    ComplexResourceKey<TwoPartKey, TwoPartKey> expectedComplexKey1 = getComplexKey(messageText1, messageText1);
    ComplexResourceKey<TwoPartKey, TwoPartKey> expectedComplexKey2 = getComplexKey(messageText2, messageText2);
    // test build
    BatchCreateIdRequest<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> request = batchCreateRequestBuilder.inputs(messages).build();
    Response<BatchCreateIdResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>>> response = getClient().sendRequest(request).getResponse();
    Assert.assertEquals(response.getStatus(), 200);
    Set<ComplexResourceKey<TwoPartKey, TwoPartKey>> expectedComplexKeys = new HashSet<>(2);
    expectedComplexKeys.add(expectedComplexKey1);
    expectedComplexKeys.add(expectedComplexKey2);
    for (CreateIdStatus<ComplexResourceKey<TwoPartKey, TwoPartKey>> status : response.getEntity().getElements()) {
        Assert.assertEquals(status.getStatus(), Integer.valueOf(201));
        Assert.assertTrue(expectedComplexKeys.contains(status.getKey()));
        try {
            @SuppressWarnings("deprecation") String id = status.getId();
            Assert.fail("buildReadOnlyId should throw an exception for ComplexKeys");
        } catch (UnsupportedOperationException e) {
        // expected
        }
        expectedComplexKeys.remove(status.getKey());
    }
    Assert.assertTrue(expectedComplexKeys.isEmpty());
    // attempt to batch get created records
    List<ComplexResourceKey<TwoPartKey, TwoPartKey>> createdKeys = new ArrayList<>(2);
    createdKeys.add(expectedComplexKey1);
    createdKeys.add(expectedComplexKey2);
    Request<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>>> getRequest = batchGetRequestBuilder.ids(createdKeys).build();
    ResponseFuture<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>>> getFuture = getClient().sendRequest(getRequest);
    Response<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>>> getResponse = getFuture.getResponse();
    Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>> getResults = getResponse.getEntity().getResults();
    Assert.assertEquals(getResults.get(expectedComplexKey1).getEntity(), message1);
    Assert.assertEquals(getResults.get(expectedComplexKey2).getEntity(), message2);
    Assert.assertEquals(getResults.size(), 2);
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) Message(com.linkedin.restli.examples.greetings.api.Message) ArrayList(java.util.ArrayList) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) BatchCreateIdResponse(com.linkedin.restli.common.BatchCreateIdResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) HashSet(java.util.HashSet)

Example 35 with EntityResponse

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

the class TestComplexKeysResource method testBatchUpdateMain.

private void testBatchUpdateMain(RootBuilderWrapper.MethodBuilderWrapper<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message, BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus>> requestBuilder, BatchGetEntityRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchGetRequestBuilder) throws RemoteInvocationException {
    final String messageText = StringTestKeys.SIMPLEKEY + " " + StringTestKeys.SIMPLEKEY2;
    final Message message = new Message();
    message.setId(StringTestKeys.SIMPLEKEY + " " + StringTestKeys.SIMPLEKEY2);
    message.setMessage(messageText);
    message.setTone(Tone.INSULTING);
    final String messageText2 = StringTestKeys.URL + " " + StringTestKeys.URL2;
    final Message message2 = new Message();
    message2.setId(StringTestKeys.URL + " " + StringTestKeys.URL2);
    message2.setMessage(messageText2);
    message2.setTone(Tone.INSULTING);
    final Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> inputs = new HashMap<>();
    ComplexResourceKey<TwoPartKey, TwoPartKey> key1 = getComplexKey(StringTestKeys.SIMPLEKEY, StringTestKeys.SIMPLEKEY2);
    ComplexResourceKey<TwoPartKey, TwoPartKey> key2 = getComplexKey(StringTestKeys.URL, StringTestKeys.URL2);
    ComplexResourceKey<TwoPartKey, TwoPartKey> key3 = getComplexKey(StringTestKeys.ERROR, StringTestKeys.ERROR);
    inputs.put(key1, message);
    inputs.put(key2, message2);
    // key3 should error anyway, so message is irrelevant.
    inputs.put(key3, message);
    final Request<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus>> request = requestBuilder.inputs(inputs).build();
    final ResponseFuture<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus>> future = getClient().sendRequest(request);
    final BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus> response = future.getResponse().getEntity();
    Assert.assertEquals(response.getResults().size(), inputs.size());
    for (Map.Entry<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus> resp : response.getResults().entrySet()) {
        Assert.assertTrue(inputs.containsKey(resp.getKey()));
        final UpdateStatus status = resp.getValue();
        if (status.hasError()) {
            Assert.assertTrue(status.getStatus() == status.getError().getStatus(), "Update status should match the status of the error, if there is any.");
        } else {
            Assert.assertEquals((int) status.getStatus(), 200);
        }
    }
    Assert.assertNotNull(response.getResults().get(key1));
    Assert.assertNotNull(response.getResults().get(key2));
    Assert.assertNotNull(response.getErrors().get(key3));
    ArrayList<ComplexResourceKey<TwoPartKey, TwoPartKey>> ids = new ArrayList<>();
    ids.add(key1);
    ids.add(key2);
    BatchGetEntityRequest<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchGetRequest = batchGetRequestBuilder.ids(ids).build();
    ResponseFuture<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>>> batchGetFuture = getClient().sendRequest(batchGetRequest);
    BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>> batchGetResponse = batchGetFuture.getResponse().getEntity();
    Assert.assertEquals(batchGetResponse.getResults().get(key1).getEntity().getTone(), Tone.INSULTING);
    Assert.assertEquals(batchGetResponse.getResults().get(key2).getEntity().getTone(), Tone.INSULTING);
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) UpdateStatus(com.linkedin.restli.common.UpdateStatus) Message(com.linkedin.restli.examples.greetings.api.Message) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Map(java.util.Map) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap)

Aggregations

EntityResponse (com.linkedin.restli.common.EntityResponse)37 Test (org.testng.annotations.Test)21 HashMap (java.util.HashMap)18 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)17 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)14 Map (java.util.Map)12 DataMap (com.linkedin.data.DataMap)11 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)11 Message (com.linkedin.restli.examples.greetings.api.Message)10 IdEntityResponse (com.linkedin.restli.common.IdEntityResponse)9 ErrorResponse (com.linkedin.restli.common.ErrorResponse)7 TwoPartKey (com.linkedin.restli.examples.greetings.api.TwoPartKey)7 ArrayList (java.util.ArrayList)7 CompoundKey (com.linkedin.restli.common.CompoundKey)6 UpdateStatus (com.linkedin.restli.common.UpdateStatus)6 HashSet (java.util.HashSet)6 HttpStatus (com.linkedin.restli.common.HttpStatus)4 PatchRequest (com.linkedin.restli.common.PatchRequest)4 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)4 ComplexKeys (com.linkedin.restli.examples.greetings.client.ComplexKeys)4