Search in sources :

Example 36 with ComplexResourceKey

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

the class TestMockBatchCreateIdResponseFactory method provideKeys.

@DataProvider
public Object[][] provideKeys() {
    Greeting g1 = buildGreeting(1L);
    Greeting g2 = buildGreeting(2L);
    Greeting g3 = buildGreeting(3L);
    return new Object[][] { new Object[] { new Long[] { 1L, 2L, 3L } }, new Object[] { new MyCustomString[] { new MyCustomString("1"), new MyCustomString("2"), new MyCustomString("3") } }, new Object[] { new CompoundKey[] { buildCompoundKey("c1", 1), buildCompoundKey("c2", 2), buildCompoundKey("c3", 3) } }, new Object[] { new ComplexResourceKey<?, ?>[] { new ComplexResourceKey<Greeting, Greeting>(g1, g1), new ComplexResourceKey<Greeting, Greeting>(g2, g2), new ComplexResourceKey<Greeting, Greeting>(g3, g3) } } };
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) MyCustomString(com.linkedin.restli.common.MyCustomString) CompoundKey(com.linkedin.restli.common.CompoundKey) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) DataProvider(org.testng.annotations.DataProvider)

Example 37 with ComplexResourceKey

use of com.linkedin.restli.common.ComplexResourceKey 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<ComplexResourceKey<Greeting, Greeting>, Greeting>();
    Map<ComplexResourceKey<Greeting, Greeting>, ErrorResponse> errorResponses = new HashMap<ComplexResourceKey<Greeting, Greeting>, ErrorResponse>();
    Greeting g1 = buildGreeting(1L);
    Greeting g2 = buildGreeting(2L);
    Greeting g3 = buildGreeting(3L);
    recordTemplates.put(new ComplexResourceKey<Greeting, Greeting>(g1, g1), g1);
    recordTemplates.put(new ComplexResourceKey<Greeting, Greeting>(g2, g2), g2);
    errorResponses.put(new ComplexResourceKey<Greeting, Greeting>(g3, g3), new ErrorResponse().setMessage("3"));
    Map<ComplexResourceKey<Greeting, Greeting>, HttpStatus> statuses = new HashMap<ComplexResourceKey<Greeting, Greeting>, HttpStatus>();
    statuses.put(new ComplexResourceKey<Greeting, Greeting>(g1, g1), HttpStatus.S_200_OK);
    statuses.put(new ComplexResourceKey<Greeting, Greeting>(g2, g2), HttpStatus.S_200_OK);
    statuses.put(new ComplexResourceKey<Greeting, Greeting>(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<ComplexResourceKey<Greeting, Greeting>, Greeting>();
    expectedRecordTemplates.put(new ComplexResourceKey<Greeting, Greeting>(g1, new Greeting()), recordTemplates.get(new ComplexResourceKey<Greeting, Greeting>(g1, g1)));
    expectedRecordTemplates.put(new ComplexResourceKey<Greeting, Greeting>(g2, new Greeting()), recordTemplates.get(new ComplexResourceKey<Greeting, Greeting>(g2, g2)));
    Map<ComplexResourceKey<Greeting, Greeting>, EntityResponse<Greeting>> expectedResults = new HashMap<ComplexResourceKey<Greeting, Greeting>, EntityResponse<Greeting>>();
    expectedResults.put(new ComplexResourceKey<Greeting, Greeting>(g1, new Greeting()), buildEntityResponse(recordTemplates.get(new ComplexResourceKey<Greeting, Greeting>(g1, g1)), HttpStatus.S_200_OK, null));
    expectedResults.put(new ComplexResourceKey<Greeting, Greeting>(g2, new Greeting()), buildEntityResponse(recordTemplates.get(new ComplexResourceKey<Greeting, Greeting>(g2, g2)), HttpStatus.S_200_OK, null));
    expectedResults.put(new ComplexResourceKey<Greeting, Greeting>(g3, new Greeting()), buildEntityResponse(null, HttpStatus.S_500_INTERNAL_SERVER_ERROR, errorResponses.get(new ComplexResourceKey<Greeting, Greeting>(g3, g3))));
    Map<ComplexResourceKey<Greeting, Greeting>, ErrorResponse> expectedErrors = new HashMap<ComplexResourceKey<Greeting, Greeting>, ErrorResponse>();
    expectedErrors.put(new ComplexResourceKey<Greeting, Greeting>(g3, new Greeting()), errorResponses.get(new ComplexResourceKey<Greeting, Greeting>(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 38 with ComplexResourceKey

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

the class QueryParamsDataMap method fixUpComplexKeySingletonArray.

/**
   * Because of backwards compatibility concerns, array fields of the key component of a
   * {@link ComplexResourceKey}s in a get request will be represented in the request url in the old
   * style.  That is, if an array field has the name "a", and contains [1,2] the part of the url
   * representing the serialized array will look like  "a=1&a=2".  However, if the array is a
   * singleton it will just be represented by "a=1". Therefore it is not possible to distinguish
   * between a single value itself and an array containing a single value.
   *
   * The purpose of this function is to fix up the singleton array problem by checking to see whether the given
   * ComplexKey's key part has an array component, and, if so and the data for that field is NOT a dataList,
   * placing the data into a dataList.
   *
   * @param complexResourceKey The complex key to be fixed.
   */
public static ComplexResourceKey<?, ?> fixUpComplexKeySingletonArray(ComplexResourceKey<?, ?> complexResourceKey) {
    RecordTemplate key = complexResourceKey.getKey();
    DataMap dataMap = key.data();
    List<RecordDataSchema.Field> fields = key.schema() == null ? Collections.<RecordDataSchema.Field>emptyList() : key.schema().getFields();
    for (RecordDataSchema.Field f : fields) {
        DataSchema.Type type = f.getType().getType();
        String fieldName = f.getName();
        if (type == DataSchema.Type.ARRAY && dataMap.containsKey(fieldName)) {
            Object arrayFieldValue = dataMap.get(fieldName);
            if (!(arrayFieldValue instanceof DataList)) {
                DataList list = new DataList();
                list.add(arrayFieldValue);
                ValidateDataAgainstSchema.validate(list, f.getType(), new ValidationOptions(RequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT, CoercionMode.STRING_TO_PRIMITIVE));
                dataMap.put(fieldName, list);
            }
        }
    }
    RecordTemplate wrappedKey = DataTemplateUtil.wrap(dataMap, key.getClass());
    @SuppressWarnings("unchecked") ComplexResourceKey<?, ?> newKey = new ComplexResourceKey<RecordTemplate, RecordTemplate>(wrappedKey, complexResourceKey.getParams());
    return newKey;
}
Also used : ByteString(com.linkedin.data.ByteString) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) DataMap(com.linkedin.data.DataMap) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataList(com.linkedin.data.DataList) RecordTemplate(com.linkedin.data.template.RecordTemplate) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey)

Example 39 with ComplexResourceKey

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

the class TestClientBuilders method testActionRequestInputIsReadOnly.

@Test
@SuppressWarnings("unchecked")
public void testActionRequestInputIsReadOnly() {
    FieldDef<TestRecord> pParam = new FieldDef<TestRecord>("p", TestRecord.class, DataTemplateUtil.getSchema(TestRecord.class));
    Map<String, DynamicRecordMetadata> requestMetadataMap = new HashMap<String, DynamicRecordMetadata>();
    DynamicRecordMetadata requestMetadata = new DynamicRecordMetadata("action", Collections.<FieldDef<?>>singleton(pParam));
    requestMetadataMap.put("action", requestMetadata);
    DynamicRecordMetadata responseMetadata = new DynamicRecordMetadata("action", Collections.<FieldDef<?>>emptyList());
    Map<String, DynamicRecordMetadata> responseMetadataMap = new HashMap<String, DynamicRecordMetadata>();
    responseMetadataMap.put("action", responseMetadata);
    ResourceSpec resourceSpec = new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), requestMetadataMap, responseMetadataMap, ComplexResourceKey.class, TestRecord.class, TestRecord.class, TestRecord.class, Collections.<String, CompoundKey.TypeInfo>emptyMap());
    ActionRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord> builder = new ActionRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord>(TEST_URI, TestRecord.class, resourceSpec, RestliRequestOptions.DEFAULT_OPTIONS);
    TestRecord testRecord1 = new TestRecord();
    TestRecord testRecord2 = new TestRecord();
    ComplexResourceKey<TestRecord, TestRecord> key = new ComplexResourceKey<TestRecord, TestRecord>(testRecord1, testRecord2);
    ActionRequest<TestRecord> request = builder.name("action").setParam(pParam, testRecord1).id(key).build();
    DynamicRecordTemplate inputParams = (DynamicRecordTemplate) request.getInputRecord();
    Assert.assertNotSame(inputParams.getValue(pParam).data(), testRecord1.data());
    Assert.assertTrue(inputParams.data().isReadOnly());
    Assert.assertTrue(inputParams.getValue(pParam).data().isMadeReadOnly());
    Assert.assertNotSame(request.getId(), key);
    Assert.assertTrue(((ComplexResourceKey<TestRecord, TestRecord>) request.getId()).isReadOnly());
    testRecord1.data().makeReadOnly();
    testRecord2.data().makeReadOnly();
    request = builder.build();
    inputParams = (DynamicRecordTemplate) request.getInputRecord();
    Assert.assertSame(inputParams.getValue(pParam).data(), testRecord1.data());
    Assert.assertTrue(inputParams.data().isReadOnly());
    Assert.assertSame(request.getId(), key);
}
Also used : HashMap(java.util.HashMap) CompoundKey(com.linkedin.restli.common.CompoundKey) ResourceSpec(com.linkedin.restli.common.ResourceSpec) ByteString(com.linkedin.data.ByteString) FieldDef(com.linkedin.data.template.FieldDef) DynamicRecordMetadata(com.linkedin.data.template.DynamicRecordMetadata) DynamicRecordTemplate(com.linkedin.data.template.DynamicRecordTemplate) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) TestRecord(com.linkedin.restli.client.test.TestRecord) ResourceSpecImpl(com.linkedin.restli.common.ResourceSpecImpl) Test(org.testng.annotations.Test)

Example 40 with ComplexResourceKey

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

the class TestClientBuilders method testComplexKeyUpdateRequestBuilder.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "complexKey")
public void testComplexKeyUpdateRequestBuilder(URIDetails expectedURIDetails) {
    UpdateRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord> builder = new UpdateRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord>(TEST_URI, TestRecord.class, _COMPLEX_KEY_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
    ComplexResourceKey<TestRecord, TestRecord> key = buildComplexKey(1L, "keyMessage", 2L, "paramMessage");
    UpdateRequest<TestRecord> request = builder.id(key).input(new TestRecord()).build();
    testBaseUriGeneration(request, expectedURIDetails.getProtocolVersion());
    Assert.assertEquals(request.isSafe(), false);
    Assert.assertEquals(request.isIdempotent(), true);
    checkBasicRequest(request, expectedURIDetails, ResourceMethod.UPDATE, new TestRecord(), Collections.<String, String>emptyMap(), null);
}
Also used : ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

Aggregations

ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)71 Test (org.testng.annotations.Test)36 TestRecord (com.linkedin.restli.client.test.TestRecord)18 DataMap (com.linkedin.data.DataMap)15 CompoundKey (com.linkedin.restli.common.CompoundKey)15 TwoPartKey (com.linkedin.restli.examples.greetings.api.TwoPartKey)15 HashMap (java.util.HashMap)15 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)12 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)11 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)11 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)11 DiscoveredItemKey (com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItemKey)11 DiscoveredItemKeyParams (com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItemKeyParams)11 AfterTest (org.testng.annotations.AfterTest)11 BeforeTest (org.testng.annotations.BeforeTest)11 Message (com.linkedin.restli.examples.greetings.api.Message)10 ArrayList (java.util.ArrayList)10 RecordTemplate (com.linkedin.data.template.RecordTemplate)9 Key (com.linkedin.restli.server.Key)9 DiscoveredItem (com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItem)9