Search in sources :

Example 16 with FieldDef

use of com.linkedin.data.template.FieldDef in project rest.li by linkedin.

the class TestAllPartitionsRequestBuilder method testSendAllPartitionsRequests.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "restliRequestOptions")
public void testSendAllPartitionsRequests(RestliRequestOptions options, RingFactory<URI> ringFactory) throws ServiceUnavailableException, URISyntaxException, RestException, InterruptedException {
    final int PARTITION_NUM = 5;
    List<URI> expectedUris = new ArrayList<URI>();
    ConsistentHashKeyMapper mapper = getKeyToHostMapper(PARTITION_NUM, expectedUris, ringFactory);
    AllPartitionsRequestBuilder<Greeting> searchRB = new AllPartitionsRequestBuilder<Greeting>(mapper);
    ActionRequestBuilder<Long, Greeting> builder = new ActionRequestBuilder<Long, Greeting>(TEST_URI, Greeting.class, _COLL_SPEC, options);
    ActionRequest<Greeting> request = builder.name("updateTone").id(1L).setParam(new FieldDef<Tone>("newTone", Tone.class, DataTemplateUtil.getSchema(Tone.class)), Tone.FRIENDLY).build();
    final Map<String, Greeting> results = new ConcurrentHashMap<String, Greeting>();
    final CountDownLatch latch = new CountDownLatch(PARTITION_NUM);
    final List<Throwable> errors = new ArrayList<Throwable>();
    final List<Greeting> responses = new ArrayList<Greeting>();
    Callback<Response<Greeting>> cb = new Callback<Response<Greeting>>() {

        @Override
        public void onError(Throwable e) {
            synchronized (errors) {
                errors.add(e);
            }
            latch.countDown();
        }

        @Override
        public void onSuccess(Response<Greeting> response) {
            results.put(response.getEntity().toString(), response.getEntity());
            synchronized (responses) {
                responses.add(response.getEntity());
            }
            latch.countDown();
        }
    };
    HostSet hostsResult = searchRB.sendRequests(getClient(), request, new RequestContext(), cb);
    List<URI> uris = hostsResult.getAllHosts();
    Assert.assertTrue(uris.containsAll(expectedUris));
    Assert.assertTrue(expectedUris.containsAll(uris));
    latch.await();
    if (!errors.isEmpty()) {
        Assert.fail("I knew it: " + errors.toString());
    }
    Assert.assertEquals(PARTITION_NUM, responses.size());
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) ArrayList(java.util.ArrayList) URI(java.net.URI) HostSet(com.linkedin.d2.balancer.util.HostSet) AllPartitionsRequestBuilder(com.linkedin.restli.client.AllPartitionsRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CountDownLatch(java.util.concurrent.CountDownLatch) Response(com.linkedin.restli.client.Response) FieldDef(com.linkedin.data.template.FieldDef) ActionRequestBuilder(com.linkedin.restli.client.ActionRequestBuilder) Callback(com.linkedin.common.callback.Callback) Tone(com.linkedin.restli.examples.greetings.api.Tone) ConsistentHashKeyMapper(com.linkedin.d2.balancer.util.hashing.ConsistentHashKeyMapper) ConsistentHashKeyMapperTest(com.linkedin.d2.balancer.util.hashing.ConsistentHashKeyMapperTest) Test(org.testng.annotations.Test)

Example 17 with FieldDef

use of com.linkedin.data.template.FieldDef in project rest.li by linkedin.

the class ResourceSchemaToResourceSpecTranslator method toActionMetadata.

// this is dynamic, don't have concrete classes for the FieldDef
@SuppressWarnings({ "unchecked", "rawtypes" })
private ActionMetadata toActionMetadata(ActionSchema action) {
    ArrayList<FieldDef<?>> fieldDefs = new ArrayList<FieldDef<?>>();
    if (action.hasParameters()) {
        for (ParameterSchema parameterSchema : action.getParameters()) {
            DataSchema dataSchema = RestSpecCodec.textToSchema(parameterSchema.getType(), _schemaResolver);
            Class<?> paramClass = toType(dataSchema);
            FieldDef<?> fieldDef = new FieldDef(parameterSchema.getName(), paramClass, dataSchema);
            fieldDefs.add(fieldDef);
        }
    }
    Collection<FieldDef<?>> response;
    if (action.hasReturns()) {
        DataSchema returnType = RestSpecCodec.textToSchema(action.getReturns(), _schemaResolver);
        Class<?> returnClass = toType(returnType);
        response = Collections.<FieldDef<?>>singletonList(new FieldDef("value", returnClass, returnType));
    } else {
        response = Collections.emptyList();
    }
    return new ActionMetadata(action.getName(), new DynamicRecordMetadata(action.getName(), fieldDefs), new DynamicRecordMetadata(action.getName(), response));
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) FieldDef(com.linkedin.data.template.FieldDef) DynamicRecordMetadata(com.linkedin.data.template.DynamicRecordMetadata) ArrayList(java.util.ArrayList) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema)

Example 18 with FieldDef

use of com.linkedin.data.template.FieldDef in project rest.li by linkedin.

the class ExampleRequestResponseGenerator method addParams.

private void addParams(ActionRequestBuilder<?, ?> request, DynamicRecordMetadata requestMetadata, ParameterSchemaArray parameters) {
    if (parameters != null) {
        for (ParameterSchema parameter : parameters) {
            FieldDef<?> fieldDef = requestMetadata.getFieldDef(parameter.getName());
            Object value = generateFieldDefValue(fieldDef);
            // to the dereferenced type so the example values can be set on the request without coercing.
            if (fieldDef.getDataSchema().getType() == Type.TYPEREF) {
                FieldDef<?> deRefFieldDef = new FieldDef<>(fieldDef.getName(), fieldDef.getDataClass(), fieldDef.getDataSchema().getDereferencedDataSchema());
                deRefFieldDef.getField().setRecord(fieldDef.getField().getRecord());
                fieldDef = deRefFieldDef;
            }
            request.setParam(fieldDef, value);
        }
    }
}
Also used : FieldDef(com.linkedin.data.template.FieldDef) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema)

Aggregations

FieldDef (com.linkedin.data.template.FieldDef)18 HashMap (java.util.HashMap)7 Test (org.testng.annotations.Test)7 ByteString (com.linkedin.data.ByteString)6 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)6 DynamicRecordMetadata (com.linkedin.data.template.DynamicRecordMetadata)6 DynamicRecordTemplate (com.linkedin.data.template.DynamicRecordTemplate)5 DataMap (com.linkedin.data.DataMap)4 TestRecord (com.linkedin.restli.client.test.TestRecord)4 ResourceSpec (com.linkedin.restli.common.ResourceSpec)4 ResourceSpecImpl (com.linkedin.restli.common.ResourceSpecImpl)4 ArrayList (java.util.ArrayList)4 ActionResponse (com.linkedin.restli.common.ActionResponse)3 CompoundKey (com.linkedin.restli.common.CompoundKey)3 DataSchema (com.linkedin.data.schema.DataSchema)2 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)2 RequestContext (com.linkedin.r2.message.RequestContext)2 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)2 ParameterSchema (com.linkedin.restli.restspec.ParameterSchema)2 Callback (com.linkedin.common.callback.Callback)1